xref: /optee_os/core/drivers/nvmem/nvmem_huk.c (revision af3fb62410645ac9636d27c3d1db72c0c9fca913)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2023, Microchip
4  */
5 
6 #include <drivers/nvmem.h>
7 #include <io.h>
8 #include <kernel/dt.h>
9 #include <kernel/huk_subkey.h>
10 #include <kernel/tee_common_otp.h>
11 #include <malloc.h>
12 #include <tee_api_defines.h>
13 #include <tee_api_types.h>
14 #include <types_ext.h>
15 
16 static struct nvmem_cell *huk_cell;
17 
18 TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey)
19 {
20 	TEE_Result res = TEE_ERROR_GENERIC;
21 	uint8_t *huk = NULL;
22 	size_t len = 0;
23 
24 	res = nvmem_cell_malloc_and_read(huk_cell, &huk);
25 	if (res)
26 		goto out_free_cell;
27 
28 	if (len != HW_UNIQUE_KEY_LENGTH) {
29 		res = TEE_ERROR_GENERIC;
30 		goto out_free_cell;
31 	}
32 
33 	memcpy(hwkey->data, huk, HW_UNIQUE_KEY_LENGTH);
34 
35 out_free_cell:
36 	nvmem_put_cell(huk_cell);
37 
38 	return res;
39 }
40 
41 static TEE_Result nvmem_huk_get_cell(const void *fdt, int node)
42 {
43 	return nvmem_get_cell_by_name(fdt, node, "hw_unique_key", &huk_cell);
44 }
45 
46 static TEE_Result nvmem_huk_probe(const void *fdt, int node,
47 				  const void *compat_data __unused)
48 {
49 	return nvmem_huk_get_cell(fdt, node);
50 }
51 
52 static const struct dt_device_match nvmem_huk_match_table[] = {
53 	{ .compatible = "optee,nvmem-huk" },
54 	{ }
55 };
56 
57 DEFINE_DT_DRIVER(nvmem_huk_dt_driver) = {
58 	.name = "nvmem_huk",
59 	.type = DT_DRIVER_NVMEM,
60 	.match_table = nvmem_huk_match_table,
61 	.probe = nvmem_huk_probe,
62 };
63