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 uint8_t *die_id;
17 static size_t die_id_len;
18
tee_otp_get_die_id(uint8_t * buffer,size_t len)19 int tee_otp_get_die_id(uint8_t *buffer, size_t len)
20 {
21 if (!die_id) {
22 if (huk_subkey_derive(HUK_SUBKEY_DIE_ID, NULL, 0, buffer, len))
23 return -1;
24 return 0;
25 }
26
27 memcpy(buffer, die_id, MIN(die_id_len, len));
28
29 return 0;
30 }
31
nvmem_die_id_probe(const void * fdt,int node,const void * compat_data __unused)32 static TEE_Result nvmem_die_id_probe(const void *fdt, int node,
33 const void *compat_data __unused)
34 {
35 TEE_Result res = TEE_ERROR_GENERIC;
36 struct nvmem_cell *cell = NULL;
37 uint8_t *data = NULL;
38
39 res = nvmem_get_cell_by_name(fdt, node, "die_id", &cell);
40 if (res)
41 return res;
42
43 res = nvmem_cell_malloc_and_read(cell, &data);
44 if (!res) {
45 die_id = data;
46 die_id_len = cell->len;
47 }
48
49 nvmem_put_cell(cell);
50
51 return res;
52 }
53
54 static const struct dt_device_match nvmem_die_id_match_table[] = {
55 { .compatible = "optee,nvmem-die-id" },
56 { }
57 };
58
59 DEFINE_DT_DRIVER(nvmem_die_id_dt_driver) = {
60 .name = "nvmem_die_id_key",
61 .type = DT_DRIVER_NVMEM,
62 .match_table = nvmem_die_id_match_table,
63 .probe = nvmem_die_id_probe,
64 };
65