hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Rockchip Secure OTP Driver
 *
 * Copyright (c) 2023 Rockchip Electronics Co. Ltd.
 * Author: Hisping <hisping.lin@rock-chips.com>
 */
 
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/nvmem-provider.h>
#include <linux/reset.h>
#include <linux/rockchip/cpu.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/tee_drv.h>
#include <linux/uuid.h>
 
static DEFINE_MUTEX(nvmem_mutex);
 
struct rockchip_data;
 
struct rockchip_otp {
   struct device *dev;
   struct nvmem_config *config;
   const struct rockchip_data *data;
};
 
struct rockchip_data {
   int size;
   int (*reg_read)(unsigned int offset, void *val, size_t bytes);
   int (*reg_write)(unsigned int offset, void *val, size_t bytes);
   int (*init)(struct rockchip_otp *otp);
};
 
static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
{
   if (ver->impl_id == TEE_IMPL_ID_OPTEE)
       return 1;
   else
       return 0;
}
 
/*
 * func: read data from non-protected oem zone in secure otp
 */
#define STORAGE_CMD_READ_OEM_NS_OTP        13
int rockchip_read_oem_non_protected_otp(unsigned int byte_off,
               void *byte_buf, size_t byte_len)
{
   const uuid_t pta_uuid =
       UUID_INIT(0x2d26d8a8, 0x5134, 0x4dd8,
             0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71);
   struct tee_ioctl_open_session_arg sess_arg;
   struct tee_shm *device_shm = NULL;
   struct tee_context *ctx = NULL;
   u32 shm_size = 0;
   int rc;
   struct tee_ioctl_invoke_arg inv_arg;
   struct tee_param param[4];
   u8 *read_data = NULL;
 
   if (!byte_buf) {
       pr_err("buf is null\n");
       return -EINVAL;
   }
 
   memset(&sess_arg, 0, sizeof(sess_arg));
 
   mutex_lock(&nvmem_mutex);
 
   /* Open context with OP-TEE driver */
   ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
   if (IS_ERR(ctx)) {
       pr_err("tee_client_open_context failed\n");
       rc = -ENODEV;
       goto out_exit;
   }
 
   /* Open session */
   memcpy(sess_arg.uuid, pta_uuid.b, TEE_IOCTL_UUID_LEN);
   sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
   sess_arg.num_params = 0;
 
   rc = tee_client_open_session(ctx, &sess_arg, NULL);
   if ((rc < 0) || (sess_arg.ret != 0)) {
       pr_err("tee_client_open_session failed, err: %x\n",
           sess_arg.ret);
       rc = -EINVAL;
       goto out_ctx;
   }
 
   /* Alloc share memory */
   shm_size = byte_len;
   device_shm = tee_shm_alloc(ctx, shm_size,
                  TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
   if (IS_ERR(device_shm)) {
       pr_err("tee_shm_alloc failed\n");
       rc = PTR_ERR(device_shm);
       goto out_sess;
   }
 
   /* Invoke func */
   memset(&inv_arg, 0, sizeof(inv_arg));
   memset(&param, 0, sizeof(param));
 
   inv_arg.func = STORAGE_CMD_READ_OEM_NS_OTP;
   inv_arg.session =  sess_arg.session;
   inv_arg.num_params = 4;
 
   /* Fill invoke cmd params */
   param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
   param[0].u.value.a = byte_off;
 
   param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
   param[1].u.memref.shm = device_shm;
   param[1].u.memref.size = shm_size;
   param[1].u.memref.shm_offs = 0;
 
   rc = tee_client_invoke_func(ctx, &inv_arg, param);
   if ((rc < 0) || (inv_arg.ret != 0)) {
       pr_err("invoke function err: %x\n", inv_arg.ret);
       rc = -EINVAL;
       goto out_shm;
   }
 
   read_data = tee_shm_get_va(device_shm, 0);
   if (IS_ERR(read_data)) {
       pr_err("tee_shm_get_va failed\n");
       rc = -EINVAL;
       goto out_shm;
   }
   memcpy(byte_buf, read_data, byte_len);
 
out_shm:
   tee_shm_free(device_shm);
out_sess:
   tee_client_close_session(ctx, sess_arg.session);
out_ctx:
   tee_client_close_context(ctx);
out_exit:
   mutex_unlock(&nvmem_mutex);
   return rc;
}
EXPORT_SYMBOL_GPL(rockchip_read_oem_non_protected_otp);
 
/*
 * func: write data to non-protected oem zone in secure otp
 */
#define STORAGE_CMD_WRITE_OEM_NS_OTP        12
int rockchip_write_oem_non_protected_otp(unsigned int byte_off,
               void *byte_buf, size_t byte_len)
{
   const uuid_t pta_uuid =
       UUID_INIT(0x2d26d8a8, 0x5134, 0x4dd8,
             0xb3, 0x2f, 0xb3, 0x4b, 0xce, 0xeb, 0xc4, 0x71);
   struct tee_ioctl_open_session_arg sess_arg;
   struct tee_shm *device_shm = NULL;
   struct tee_context *ctx = NULL;
   u32 shm_size = 0;
   int rc;
   struct tee_ioctl_invoke_arg inv_arg;
   struct tee_param param[4];
   u8 *write_data = NULL;
 
   if (!byte_buf) {
       pr_err("buf is null\n");
       return -EINVAL;
   }
 
   memset(&sess_arg, 0, sizeof(sess_arg));
 
   mutex_lock(&nvmem_mutex);
 
   /* Open context with OP-TEE driver */
   ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
   if (IS_ERR(ctx)) {
       pr_err("tee_client_open_context failed\n");
       rc = -ENODEV;
       goto out_exit;
   }
 
   /* Open session */
   memcpy(sess_arg.uuid, pta_uuid.b, TEE_IOCTL_UUID_LEN);
   sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
   sess_arg.num_params = 0;
 
   rc = tee_client_open_session(ctx, &sess_arg, NULL);
   if ((rc < 0) || (sess_arg.ret != 0)) {
       pr_err("tee_client_open_session failed, err: %x\n",
           sess_arg.ret);
       rc = -EINVAL;
       goto out_ctx;
   }
 
   /* Alloc share memory */
   shm_size = byte_len;
   device_shm = tee_shm_alloc(ctx, shm_size,
                  TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
   if (IS_ERR(device_shm)) {
       pr_err("tee_shm_alloc failed\n");
       rc = PTR_ERR(device_shm);
       goto out_sess;
   }
 
   write_data = tee_shm_get_va(device_shm, 0);
   if (IS_ERR(write_data)) {
       pr_err("tee_shm_get_va failed\n");
       rc = -EINVAL;
       goto out_shm;
   }
   memcpy(write_data, byte_buf, byte_len);
 
   /* Invoke func */
   memset(&inv_arg, 0, sizeof(inv_arg));
   memset(&param, 0, sizeof(param));
 
   inv_arg.func = STORAGE_CMD_WRITE_OEM_NS_OTP;
   inv_arg.session =  sess_arg.session;
   inv_arg.num_params = 4;
 
   /* Fill invoke cmd params */
   param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
   param[0].u.value.a = byte_off;
 
   param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
   param[1].u.memref.shm = device_shm;
   param[1].u.memref.size = shm_size;
   param[1].u.memref.shm_offs = 0;
 
   rc = tee_client_invoke_func(ctx, &inv_arg, param);
   if ((rc < 0) || (inv_arg.ret != 0)) {
       pr_err("invoke function err: %x\n", inv_arg.ret);
       rc = -EINVAL;
       goto out_shm;
   }
 
out_shm:
   tee_shm_free(device_shm);
out_sess:
   tee_client_close_session(ctx, sess_arg.session);
out_ctx:
   tee_client_close_context(ctx);
out_exit:
   mutex_unlock(&nvmem_mutex);
   return rc;
}
EXPORT_SYMBOL_GPL(rockchip_write_oem_non_protected_otp);
 
static int rockchip_secure_otp_read(void *context, unsigned int offset,
               void *val, size_t bytes)
{
   struct rockchip_otp *otp = context;
   int ret = -EINVAL;
 
   if (otp->data && otp->data->reg_read)
       ret = otp->data->reg_read(offset, val, bytes);
 
   return ret;
}
 
static int rockchip_secure_otp_write(void *context, unsigned int offset,
               void *val, size_t bytes)
{
   struct rockchip_otp *otp = context;
   int ret = -EINVAL;
 
   if (otp->data && otp->data->reg_write)
       ret = otp->data->reg_write(offset, val, bytes);
 
   return ret;
}
 
static struct nvmem_config otp_config = {
   .name = "rockchip-secure-otp",
   .owner = THIS_MODULE,
   .read_only = false,
   .reg_read = rockchip_secure_otp_read,
   .reg_write = rockchip_secure_otp_write,
   .stride = 4,
   .word_size = 4,
};
 
static const struct rockchip_data secure_otp_data = {
   .reg_read = rockchip_read_oem_non_protected_otp,
   .reg_write = rockchip_write_oem_non_protected_otp,
};
 
static const struct of_device_id rockchip_secure_otp_match[] = {
   {
       .compatible = "rockchip,secure-otp",
       .data = (void *)&secure_otp_data,
   },
   { /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, rockchip_secure_otp_match);
 
static int rockchip_secure_otp_probe(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
   struct rockchip_otp *otp;
   const struct rockchip_data *data;
   struct nvmem_device *nvmem;
   u32 otp_size;
   int ret;
 
   data = device_get_match_data(dev);
   if (!data) {
       dev_err(dev, "failed to get match data\n");
       return -EINVAL;
   }
 
   ret = device_property_read_u32(dev, "rockchip,otp-size", &otp_size);
   if (ret) {
       dev_err(dev, "otp size parameter not specified\n");
       return -EINVAL;
   } else if (otp_size == 0) {
       dev_err(dev, "otp size must be > 0\n");
       return -EINVAL;
   }
 
   otp = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_otp),
              GFP_KERNEL);
   if (!otp)
       return -ENOMEM;
 
   otp->data = data;
   otp->dev = dev;
 
   otp->config = &otp_config;
   otp->config->size = otp_size;
   otp->config->priv = otp;
   otp->config->dev = dev;
 
   if (data->init) {
       ret = data->init(otp);
       if (ret)
           return ret;
   }
 
   nvmem = devm_nvmem_register(dev, otp->config);
 
   return PTR_ERR_OR_ZERO(nvmem);
}
 
static struct platform_driver rockchip_secure_otp_driver = {
   .probe = rockchip_secure_otp_probe,
   .driver = {
       .name = "rockchip-secure-otp",
       .of_match_table = rockchip_secure_otp_match,
   },
};
 
static int __init rockchip_secure_otp_init(void)
{
   int ret;
 
   ret = platform_driver_register(&rockchip_secure_otp_driver);
   if (ret) {
       pr_err("failed to register secure otp driver\n");
       return ret;
   }
 
   return 0;
}
 
static void __exit rockchip_secure_otp_exit(void)
{
   return platform_driver_unregister(&rockchip_secure_otp_driver);
}
 
subsys_initcall(rockchip_secure_otp_init);
module_exit(rockchip_secure_otp_exit);
 
MODULE_DESCRIPTION("Rockchip Secure OTP Driver");
MODULE_LICENSE("GPL");