Skip to content

Commit

Permalink
drivers: nvmem: add nvmem-huk driver
Browse files Browse the repository at this point in the history
This driver is meant to read the otp unique hardware key from a
nvmem controller. It uses the nvmem framework to read the nvmem
cells from the device-tree.

Signed-off-by: Thomas Perrot <[email protected]>
  • Loading branch information
tprrt committed Nov 28, 2023
1 parent e50efbd commit 9d56b30
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
74 changes: 74 additions & 0 deletions core/drivers/nvmem/nvmem_huk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2023, Microchip
*/

#include <drivers/nvmem.h>
#include <io.h>
#include <kernel/dt.h>
#include <kernel/huk_subkey.h>
#include <kernel/tee_common_otp.h>
#include <malloc.h>
#include <matrix.h>
#include <sama5d2.h>
#include <tee_api_defines.h>
#include <tee_api_types.h>
#include <types_ext.h>
#include <trace.h>

static uint8_t *huk;

TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey)
{
if (!huk)
return TEE_ERROR_NO_DATA;

memcpy(hwkey->data, huk, HW_UNIQUE_KEY_LENGTH);

return TEE_SUCCESS;
}

static TEE_Result nvmem_huk_read(const void *fdt, int node)
{
TEE_Result res = TEE_ERROR_GENERIC;
struct nvmem_cell *cell = NULL;
uint8_t *data = NULL;
size_t len = 0;

res = nvmem_get_cell_by_name(fdt, node, "hw_unique_key", &cell);
if (res)
return res;

res = nvmem_cell_malloc_and_read(cell, &data);
if (res)
goto out_free_cell;

if (len != HW_UNIQUE_KEY_LENGTH) {
res = TEE_ERROR_GENERIC;
goto out_free_cell;
}
huk = data;

out_free_cell:
nvmem_put_cell(cell);

return res;
}

static TEE_Result nvmem_huk_probe(const void *fdt, int node,
const void *compat_data __unused)
{
return nvmem_huk_read(fdt, node);
}

static const struct dt_device_match nvmem_huk_match_table[] = {
{ .compatible = "optee,nvmem-huk" },
{ }
};

DEFINE_DT_DRIVER(nvmem_huk_dt_driver) = {
.name = "nvmem_huk",
.type = DT_DRIVER_NVMEM,
.match_table = nvmem_huk_match_table,
.probe = nvmem_huk_probe,
};
1 change: 1 addition & 0 deletions core/drivers/nvmem/sub.mk
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
srcs-y += nvmem.c
srcs-$(CFG_ATMEL_SFC) += atmel_sfc.c
srcs-$(CFG_NVMEM_DIE_ID) += nvmem_die_id.c
srcs-$(CFG_NVMEM_HUK) += nvmem_huk.c

0 comments on commit 9d56b30

Please sign in to comment.