blob: 111b57da6fe9df47940b3f3156528e0eb9cf46d2 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 MediaTek Inc.
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/moduleparam.h>
10#include <linux/slab.h>
11#include <linux/unistd.h>
12#include <linux/sched.h>
13#include <linux/fs.h>
14#include <linux/uaccess.h>
15#include <linux/version.h>
16#include <linux/spinlock.h>
17#include <linux/semaphore.h>
18#include <linux/delay.h>
19#include <linux/kthread.h>
20#include <linux/errno.h>
21#include <linux/cdev.h>
22#include <linux/device.h>
23#include <linux/mutex.h>
24#include <linux/string.h>
25#include <linux/random.h>
26#include <linux/memory.h>
27#include <linux/io.h>
28#include <linux/proc_fs.h>
29#include <crypto/hash.h>
30
31#include <linux/rpmb.h>
32
33#include <linux/scatterlist.h>
34#include <linux/mmc/card.h>
35#include <linux/mmc/host.h>
36#include <linux/mmc/mmc.h>
37#include <linux/mmc/sd.h>
38#include "queue.h"
39#include <linux/of.h>
40
41/* #define __RPMB_MTK_DEBUG_MSG */
42/* #define __RPMB_MTK_DEBUG_HMAC_VERIFY */
43/* #define __RPMB_IOCTL_SUPPORT */
44
45/* TEE usage */
46#if IS_ENABLED(CONFIG_TRUSTONIC_TEE_SUPPORT)
47#include "mobicore_driver_api.h"
48#include "drrpmb_gp_Api.h"
49#include "drrpmb_Api.h"
50
51static struct mc_uuid_t rpmb_gp_uuid = RPMB_GP_UUID;
52static struct mc_session_handle rpmb_gp_session = {0};
53static u32 rpmb_gp_devid = MC_DEVICE_ID_DEFAULT;
54static struct dciMessage_t *rpmb_gp_dci;
55#endif
56
57/*
58 * Dummy definition for MAX_RPMB_TRANSFER_BLK.
59 *
60 * For UFS RPMB driver, MAX_RPMB_TRANSFER_BLK will be always
61 * used however it will NOT be defined in projects w/o Security
62 * OS. Thus we add a dummy definition here to avoid build errors.
63 *
64 * For eMMC RPMB driver, MAX_RPMB_TRANSFER_BLK will be used
65 * only if RPMB_MULTI_BLOCK_ACCESS is defined. thus
66 * build error will not happen on projects w/o Security OS.
67 *
68 * NOTE: This dummy definition shall be located after
69 * #include "drrpmb_Api.h" and
70 * #include "rpmb-mtk.h"
71 * since MAX_RPMB_TRANSFER_BLK will be defined in those
72 * header files if security OS is enabled.
73 */
74#ifndef MAX_RPMB_TRANSFER_BLK
75#define MAX_RPMB_TRANSFER_BLK (1U)
76#endif
77
78#define RPMB_NAME "rpmb"
79
80//extern struct msdc_host *mtk_msdc_host[];
81
82#define RPMB_IOCTL_PROGRAM_KEY 1
83#define RPMB_IOCTL_WRITE_DATA 3
84#define RPMB_IOCTL_READ_DATA 4
85
86struct rpmb_ioc_param {
87 unsigned char *keybytes;
88 unsigned char *databytes;
89 unsigned int data_len;
90 unsigned short addr;
91 unsigned char *hmac;
92 unsigned int hmac_len;
93};
94
95#define RPMB_SZ_MAC 32U
96#define RPMB_SZ_DATA 256U
97#define RPMB_SZ_STUFF 196U
98#define RPMB_SZ_NONCE 16U
99
100struct s_rpmb {
101 unsigned char stuff[RPMB_SZ_STUFF];
102 unsigned char mac[RPMB_SZ_MAC];
103 unsigned char data[RPMB_SZ_DATA];
104 unsigned char nonce[RPMB_SZ_NONCE];
105 unsigned int write_counter;
106 unsigned short address;
107 unsigned short block_count;
108 unsigned short result;
109 unsigned short request;
110};
111
112enum {
113 RPMB_SUCCESS = 0,
114 RPMB_HMAC_ERROR,
115 RPMB_RESULT_ERROR,
116 RPMB_WC_ERROR,
117 RPMB_NONCE_ERROR,
118 RPMB_ALLOC_ERROR,
119 RPMB_TRANSFER_NOT_COMPLETE,
120};
121
122#define RPMB_REQ 1 /* RPMB request mark */
123#define RPMB_RESP (1 << 1)/* RPMB response mark */
124#define RPMB_AVAILABLE_SECTORS 8 /* 4K page size */
125
126#define RPMB_TYPE_BEG 510
127#define RPMB_RES_BEG 508
128#define RPMB_BLKS_BEG 506
129#define RPMB_ADDR_BEG 504
130#define RPMB_WCOUNTER_BEG 500
131
132#define RPMB_NONCE_BEG 484
133#define RPMB_DATA_BEG 228
134#define RPMB_MAC_BEG 196
135
136struct emmc_rpmb_req {
137 __u16 type; /* RPMB request type */
138 __u16 *result; /* response or request result */
139 __u16 blk_cnt; /* Number of blocks(half sector 256B) */
140 __u16 addr; /* data address */
141 __u32 *wc; /* write counter */
142 __u8 *nonce; /* Ramdom number */
143 __u8 *data; /* Buffer of the user data */
144 __u8 *mac; /* Message Authentication Code */
145 __u8 *data_frame;
146};
147
148#define DEFAULT_HANDLES_NUM (64)
149#define MAX_OPEN_SESSIONS (0xffffffffU - 1)
150
151/* Debug message event */
152#define DBG_EVT_NONE (0) /* No event */
153#define DBG_EVT_CMD (1U << 0)/* SEC CMD related event */
154#define DBG_EVT_FUNC (1U << 1U)/* SEC function event */
155#define DBG_EVT_INFO (1U << 2U)/* SEC information event */
156#define DBG_EVT_WRN (1U << 30U) /* Warning event */
157#define DBG_EVT_ERR (0x80000000U) /* Error event, 1 << 31 */
158#ifdef __RPMB_MTK_DEBUG_MSG
159#define DBG_EVT_DBG_INFO (DBG_EVT_ERR) /* Error event */
160#else
161#define DBG_EVT_DBG_INFO (1U << 2U) /* Information event */
162#endif
163#define DBG_EVT_ALL (0xffffffffU)
164
165static u32 dbg_evt = DBG_EVT_ALL; //Chun-Hung
166#define DBG_EVT_MASK (dbg_evt)
167
168#define MSG(evt, fmt, args...) \
169do {\
170 if (((DBG_EVT_##evt) & DBG_EVT_MASK) != 0U) { \
171 (void)(pr_notice("[%s] "fmt, RPMB_NAME, ##args)); \
172 } \
173} while (false)
174
175static struct task_struct *open_th;
176static struct task_struct *rpmb_gp_Dci_th;
177
178
179static struct cdev rpmb_cdev;
180#ifdef __RPMB_IOCTL_SUPPORT
181static struct class *mtk_rpmb_class;
182#endif
183
184static int hmac_sha256(const char *keybytes, u32 klen, const char *str,
185 size_t len, u8 *hmac)
186{
187 struct shash_desc *shash;
188 struct crypto_shash *hmacsha256 = crypto_alloc_shash("hmac(sha256)",
189 0, 0);
190 size_t size = 0;
191 int err = 0;
192 int nbytes = (int)len;
193
194 if (IS_ERR(hmacsha256))
195 return -1;
196
197 size = sizeof(struct shash_desc) + crypto_shash_descsize(hmacsha256);
198
199 shash = kmalloc(size, GFP_KERNEL);
200 if (shash == NULL) {
201 err = -1;
202 goto malloc_err;
203 }
204 shash->tfm = hmacsha256;
205 shash->flags = 0x0;
206
207 err = crypto_shash_setkey(hmacsha256, keybytes, klen);
208 if (err != 0) {
209 err = -1;
210 goto hash_err;
211 }
212
213 err = crypto_shash_init(shash);
214 if (err != 0) {
215 err = -1;
216 goto hash_err;
217 }
218
219 if (nbytes != crypto_shash_update(shash, str, (unsigned int)nbytes)) {
220 err = -1;
221 goto hash_err;
222 }
223 err = crypto_shash_final(shash, hmac);
224
225hash_err:
226 kfree(shash);
227malloc_err:
228 crypto_free_shash(hmacsha256);
229
230 return err;
231}
232
233#ifdef __RPMB_MTK_DEBUG_HMAC_VERIFY
234unsigned char rpmb_key[32] = {
235 0x64, 0x76, 0xEE, 0xF0, 0xF1, 0x6B, 0x30, 0x47,
236 0xE9, 0x79, 0x31, 0x58, 0xF6, 0x42, 0xDA, 0x46,
237 0xF7, 0x3B, 0x53, 0xFD, 0xC5, 0xF8, 0x84, 0xCE,
238 0x03, 0x73, 0x15, 0xBC, 0x54, 0x47, 0xD4, 0x6A
239};
240
241static int rpmb_cal_hmac(struct rpmb_frame *frame, int blk_cnt,
242 u8 *key, u8 *key_mac)
243{
244 int i;
245 u8 *buf, *buf_start;
246
247 buf = buf_start = kzalloc(284 * blk_cnt, 0);
248
249 for (i = 0; i < blk_cnt; i++) {
250 memcpy(buf, frame[i].data, 284);
251 buf += 284;
252 }
253
254 if (hmac_sha256(key, 32, buf_start, 284UL * blk_cnt, key_mac) != 0)
255 MSG(ERR, "hmac_sha256() return error!\n");
256
257 kfree(buf_start);
258
259 return 0;
260}
261#endif
262
263static void rpmb_dump_frame(u8 *data_frame)
264{
265 MSG(DBG_INFO, "mac, frame[196] = 0x%x\n", data_frame[196]);
266 MSG(DBG_INFO, "mac, frame[197] = 0x%x\n", data_frame[197]);
267 MSG(DBG_INFO, "mac, frame[198] = 0x%x\n", data_frame[198]);
268 MSG(DBG_INFO, "data,frame[228] = 0x%x\n", data_frame[228]);
269 MSG(DBG_INFO, "data,frame[229] = 0x%x\n", data_frame[229]);
270 MSG(DBG_INFO, "nonce, frame[484] = 0x%x\n", data_frame[484]);
271 MSG(DBG_INFO, "nonce, frame[485] = 0x%x\n", data_frame[485]);
272 MSG(DBG_INFO, "nonce, frame[486] = 0x%x\n", data_frame[486]);
273 MSG(DBG_INFO, "nonce, frame[487] = 0x%x\n", data_frame[487]);
274 MSG(DBG_INFO, "wc, frame[500] = 0x%x\n", data_frame[500]);
275 MSG(DBG_INFO, "wc, frame[501] = 0x%x\n", data_frame[501]);
276 MSG(DBG_INFO, "wc, frame[502] = 0x%x\n", data_frame[502]);
277 MSG(DBG_INFO, "wc, frame[503] = 0x%x\n", data_frame[503]);
278 MSG(DBG_INFO, "addr, frame[504] = 0x%x\n", data_frame[504]);
279 MSG(DBG_INFO, "addr, frame[505] = 0x%x\n", data_frame[505]);
280 MSG(DBG_INFO, "blkcnt,frame[506] = 0x%x\n", data_frame[506]);
281 MSG(DBG_INFO, "blkcnt,frame[507] = 0x%x\n", data_frame[507]);
282 MSG(DBG_INFO, "result, frame[508] = 0x%x\n", data_frame[508]);
283 MSG(DBG_INFO, "result, frame[509] = 0x%x\n", data_frame[509]);
284 MSG(DBG_INFO, "type, frame[510] = 0x%x\n", data_frame[510]);
285 MSG(DBG_INFO, "type, frame[511] = 0x%x\n", data_frame[511]);
286}
287
288static struct rpmb_frame *rpmb_alloc_frames(unsigned int cnt)
289{
290 return kzalloc(sizeof(struct rpmb_frame) * cnt, 0);
291}
292
293static int rpmb_req_get_wc(u8 *keybytes, u32 *wc, u8 *frame)
294{
295 struct rpmb_data rpmbdata;
296 struct rpmb_dev *mmc_rpmb;
297 u8 nonce[RPMB_SZ_NONCE] = {0};
298 u8 hmac[RPMB_SZ_MAC];
299 int ret, i;
300
301 MSG(INFO, "%s start!!!\n", __func__);
302
303 mmc_rpmb = rpmb_dev_get_by_type(RPMB_TYPE_EMMC);
304
305 do {
306 /*
307 * Initial frame buffers
308 */
309
310 if (frame != NULL) {
311
312 /*
313 * Use external frame if possible.
314 * External frame shall have below field ready,
315 *
316 * nonce
317 * req_resp
318 */
319 rpmbdata.icmd.frames = (struct rpmb_frame *)frame;
320 rpmbdata.ocmd.frames = (struct rpmb_frame *)frame;
321
322 } else {
323
324 rpmbdata.icmd.frames = rpmb_alloc_frames(1);
325
326 if (rpmbdata.icmd.frames == NULL)
327 return RPMB_ALLOC_ERROR;
328
329 rpmbdata.ocmd.frames = rpmb_alloc_frames(1);
330
331 if (rpmbdata.ocmd.frames == NULL)
332 return RPMB_ALLOC_ERROR;
333 }
334
335 /*
336 * Prepare frame contents.
337 *
338 * Input frame (in view of device) only needs nonce
339 */
340
341 rpmbdata.req_type = RPMB_GET_WRITE_COUNTER;
342 rpmbdata.icmd.nframes = 1;
343
344 /* Fill-in essential field in self-prepared frame */
345
346 if (frame == NULL) {
347 get_random_bytes(nonce, (int)RPMB_SZ_NONCE);
348 rpmbdata.icmd.frames->req_resp =
349 cpu_to_be16(RPMB_GET_WRITE_COUNTER);
350 memcpy(rpmbdata.icmd.frames->nonce, nonce,
351 RPMB_SZ_NONCE);
352 }
353
354 /* Output frame (in view of device) */
355
356 rpmbdata.ocmd.nframes = 1;
357
358 ret = rpmb_cmd_req(mmc_rpmb, &rpmbdata);
359
360 if (ret != 0) {
361 MSG(ERR, "%s, rpmb_cmd_req IO error!!!(0x%x)\n",
362 __func__, ret);
363 break;
364 }
365
366 /* Verify HMAC only if key is available */
367
368 if (keybytes != NULL) {
369 if (strlen(keybytes) != 32UL) {
370 MSG(ERR, "%s, error rpmb key len = 0x%x\n",
371 __func__, (unsigned int)strlen(keybytes));
372 ret = RPMB_WC_ERROR;
373 break;
374 }
375
376 /*
377 * Authenticate response write counter frame.
378 */
379 if (hmac_sha256(keybytes, 32,
380 rpmbdata.ocmd.frames->data,
381 284UL, hmac) != 0)
382 MSG(ERR, "hmac_sha256() return error!\n");
383
384 if (memcmp(hmac, rpmbdata.ocmd.frames->key_mac,
385 RPMB_SZ_MAC) != 0) {
386 MSG(ERR, "%s, hmac compare error!!!\n",
387 __func__);
388 ret = RPMB_HMAC_ERROR;
389 }
390
391 /*
392 * DEVICE ISSUE:
393 * We found some devices will return hmac vale with
394 * all zeros.
395 * For this kind of device, bypass hmac comparison.
396 */
397 if (ret == RPMB_HMAC_ERROR) {
398 for (i = 0; i < 32; i++) {
399 if (rpmbdata.ocmd.frames->key_mac[i] !=
400 0U) {
401 MSG(ERR,
402 "%s, dev hmac not NULL!\n",
403 __func__);
404 break;
405 }
406 }
407
408 MSG(ERR,
409 "%s, device hmac has all zero, bypassed!\n",
410 __func__);
411 ret = RPMB_SUCCESS;
412 }
413 }
414
415 /*
416 * Verify nonce and result only in self-prepared frame
417 * External frame shall be verified by frame provider,
418 * for example, TEE.
419 */
420 if (frame == NULL) {
421 if (memcmp(nonce, rpmbdata.ocmd.frames->nonce,
422 RPMB_SZ_NONCE) != 0) {
423 MSG(ERR, "%s, nonce compare error!!!\n",
424 __func__);
425 rpmb_dump_frame((u8 *)rpmbdata.ocmd.frames);
426 ret = RPMB_NONCE_ERROR;
427 break;
428 }
429
430 if (rpmbdata.ocmd.frames->result != 0U) {
431 MSG(ERR, "%s, result error!!! (0x%x)\n",
432 __func__,
433 cpu_to_be16(rpmbdata.ocmd.frames->result));
434 ret = RPMB_RESULT_ERROR;
435 break;
436 }
437 }
438
439 if (wc != NULL) {
440 *wc = cpu_to_be32(rpmbdata.ocmd.frames->write_counter);
441 MSG(DBG_INFO, "%s: wc = %d (0x%x)\n",
442 __func__, *wc, *wc);
443 }
444 } while (false);
445
446 MSG(DBG_INFO, "%s: end\n", __func__);
447
448 if (frame == NULL) {
449 kfree(rpmbdata.icmd.frames);
450 kfree(rpmbdata.ocmd.frames);
451 }
452
453 return ret;
454}
455
456static int rpmb_req_read_data(u8 *frame, u32 blk_cnt)
457{
458 struct rpmb_data rpmbdata;
459 struct rpmb_dev *mmc_rpmb;
460 int ret;
461
462 mmc_rpmb = rpmb_dev_get_by_type(RPMB_TYPE_EMMC);
463
464 MSG(DBG_INFO, "%s: blk_cnt: %d\n", __func__, blk_cnt);
465
466 rpmbdata.req_type = RPMB_READ_DATA;
467 rpmbdata.icmd.nframes = 1;
468 rpmbdata.icmd.frames = (struct rpmb_frame *)frame;
469
470 /*
471 * We need to fill-in block_count by ourselves for UFS case.
472 * TEE does not fill-in this field because eMMC spec specifiy it as 0.
473 */
474 rpmbdata.icmd.frames->block_count = cpu_to_be16((u16)blk_cnt);
475
476 rpmbdata.ocmd.nframes = blk_cnt;
477 rpmbdata.ocmd.frames = (struct rpmb_frame *)frame;
478
479 ret = rpmb_cmd_req(mmc_rpmb, &rpmbdata);
480
481 if (ret != 0)
482 MSG(ERR, "%s: rpmb_cmd_req IO error, ret %d (0x%x)\n",
483 __func__, ret, ret);
484
485 MSG(DBG_INFO, "%s: result 0x%x\n", __func__,
486 rpmbdata.ocmd.frames->result);
487
488 MSG(DBG_INFO, "%s: ret 0x%x\n", __func__, ret);
489
490 return ret;
491}
492
493static int rpmb_req_write_data(u8 *frame, u32 blk_cnt)
494{
495 struct rpmb_data rpmbdata;
496 struct rpmb_dev *mmc_rpmb;
497 int ret;
498#ifdef __RPMB_MTK_DEBUG_HMAC_VERIFY
499 u8 *key_mac;
500#endif
501
502 mmc_rpmb = rpmb_dev_get_by_type(RPMB_TYPE_EMMC);
503
504 MSG(DBG_INFO, "%s: blk_cnt: %d\n", __func__, blk_cnt);
505
506 /*
507 * Alloc output frame to avoid overwriting input frame buffer
508 * provided by TEE
509 */
510 rpmbdata.ocmd.frames = rpmb_alloc_frames(1);
511
512 if (rpmbdata.ocmd.frames == NULL)
513 return RPMB_ALLOC_ERROR;
514
515 rpmbdata.ocmd.nframes = 1;
516
517 rpmbdata.req_type = RPMB_WRITE_DATA;
518 rpmbdata.icmd.nframes = blk_cnt;
519 rpmbdata.icmd.frames = (struct rpmb_frame *)frame;
520
521#ifdef __RPMB_MTK_DEBUG_HMAC_VERIFY
522 key_mac = kzalloc(32, 0);
523
524 rpmb_cal_hmac((struct rpmb_frame *)frame, blk_cnt, rpmb_key, key_mac);
525
526 if (memcmp(key_mac, ((struct rpmb_frame *)frame)[blk_cnt - 1].key_mac,
527 32)) {
528 MSG(ERR, "%s, Key Mac is NOT matched!\n", __func__);
529 kfree(key_mac);
530 ret = 1;
531 goto out;
532 } else
533 MSG(ERR, "%s, Key Mac check passed.\n", __func__);
534
535 kfree(key_mac);
536#endif
537
538 ret = rpmb_cmd_req(mmc_rpmb, &rpmbdata);
539
540 if (ret != 0)
541 MSG(ERR, "%s: rpmb_cmd_req IO error, ret %d (0x%x)\n",
542 __func__, ret, ret);
543
544 /*
545 * Microtrust TEE will check write counter in the first frame,
546 * thus we copy response frame to the first frame.
547 */
548 memcpy(frame, rpmbdata.ocmd.frames, 512);
549
550 MSG(DBG_INFO, "%s: result 0x%x\n", __func__,
551 rpmbdata.ocmd.frames->result);
552
553 kfree(rpmbdata.ocmd.frames);
554
555 MSG(DBG_INFO, "%s: ret 0x%x\n", __func__, ret);
556
557#ifdef __RPMB_MTK_DEBUG_HMAC_VERIFY
558out:
559#endif
560
561 return ret;
562}
563
564
565static int rpmb_req_ioctl_write_data(struct rpmb_ioc_param *param)
566{
567 struct rpmb_data rpmbdata;
568 struct rpmb_dev *mmc_rpmb;
569 u32 i, tran_size, left_size = param->data_len;
570 u32 wc = 0xFFFFFFFFU;
571 u16 iCnt, total_blkcnt, tran_blkcnt, left_blkcnt;
572 u16 blkaddr;
573 u8 hmac[RPMB_SZ_MAC];
574 u8 *dataBuf, *dataBuf_start, *data_for_hmac;
575 size_t size_for_hmac;
576 int ret = 0;
577 u8 user_param_data;
578
579 MSG(DBG_INFO, "%s start!!!\n", __func__);
580
581 ret = get_user(user_param_data, param->databytes);
582 if (ret != 0)
583 return -EFAULT;
584
585 ret = get_user(user_param_data, param->keybytes);
586 if (ret != 0)
587 return -EFAULT;
588
589 mmc_rpmb = rpmb_dev_get_by_type(RPMB_TYPE_EMMC);
590
591 i = 0;
592 tran_blkcnt = 0;
593 dataBuf = NULL;
594 dataBuf_start = NULL;
595
596 total_blkcnt = (u16)(((param->data_len % RPMB_SZ_DATA) != 0U) ?
597 (param->data_len / RPMB_SZ_DATA + 1U) :
598 (param->data_len / RPMB_SZ_DATA));
599 left_blkcnt = total_blkcnt;
600
601 /*
602 * For RPMB write data, the elements we need in the input data frame is
603 * 1. address.
604 * 2. write counter.
605 * 3. data.
606 * 4. block count.
607 * 5. MAC
608 */
609
610 blkaddr = param->addr;
611
612 while (left_blkcnt > 0U) {
613
614 if (left_blkcnt >= MAX_RPMB_TRANSFER_BLK)
615 tran_blkcnt = MAX_RPMB_TRANSFER_BLK;
616 else
617 tran_blkcnt = left_blkcnt;
618
619 MSG(DBG_INFO, "%s, total_blkcnt = 0x%x, tran_blkcnt = 0x%x\n",
620 __func__, left_blkcnt, tran_blkcnt);
621
622 ret = rpmb_req_get_wc(param->keybytes, &wc, NULL);
623 if (ret != 0) {
624 MSG(ERR, "%s, rpmb_req_get_wc error!!!(0x%x)\n",
625 __func__, ret);
626 return ret;
627 }
628
629 /*
630 * Initial frame buffers
631 */
632
633 rpmbdata.icmd.frames = rpmb_alloc_frames(tran_blkcnt);
634
635 if (rpmbdata.icmd.frames == NULL)
636 return RPMB_ALLOC_ERROR;
637
638 rpmbdata.ocmd.frames = rpmb_alloc_frames(1);
639
640 if (rpmbdata.ocmd.frames == NULL)
641 return RPMB_ALLOC_ERROR;
642
643 /*
644 * Initial data buffer for HMAC computation.
645 * Since HAMC computation tool which we use needs
646 * consecutive data buffer.
647 * Pre-alloced it.
648 */
649
650 dataBuf_start = dataBuf = kzalloc(284UL * tran_blkcnt, 0);
651
652 /*
653 * Prepare frame contents
654 */
655
656 rpmbdata.req_type = RPMB_WRITE_DATA;
657
658
659 /* Output frames (in view of device) */
660
661 rpmbdata.ocmd.nframes = 1;
662
663 /*
664 * All input frames (in view of device) need below stuff,
665 * 1. address.
666 * 2. write counter.
667 * 3. data.
668 * 4. block count.
669 * 5. MAC
670 */
671
672 rpmbdata.icmd.nframes = tran_blkcnt;
673
674 /* size for hmac calculation: 512 - 228 = 284 */
675 size_for_hmac = sizeof(struct rpmb_frame) -
676 offsetof(struct rpmb_frame, data);
677
678 for (iCnt = 0; iCnt < tran_blkcnt; iCnt++) {
679 /*
680 * Prepare write data frame. need addr, wc, blkcnt,
681 * data and mac.
682 */
683 rpmbdata.icmd.frames[iCnt].req_resp =
684 cpu_to_be16(RPMB_WRITE_DATA);
685 rpmbdata.icmd.frames[iCnt].addr = cpu_to_be16(blkaddr);
686 rpmbdata.icmd.frames[iCnt].block_count =
687 cpu_to_be16(tran_blkcnt);
688 rpmbdata.icmd.frames[iCnt].write_counter =
689 cpu_to_be32(wc);
690
691 if (left_size >= RPMB_SZ_DATA)
692 tran_size = RPMB_SZ_DATA;
693 else
694 tran_size = left_size;
695
696 memcpy(rpmbdata.icmd.frames[iCnt].data,
697 param->databytes +
698 i * MAX_RPMB_TRANSFER_BLK * RPMB_SZ_DATA +
699 (iCnt * RPMB_SZ_DATA),
700 tran_size);
701 left_size -= tran_size;
702
703 data_for_hmac = rpmbdata.icmd.frames[iCnt].data;
704
705 /* copy data part */
706 memcpy(dataBuf, data_for_hmac, RPMB_SZ_DATA);
707
708 /* copy left part */
709 memcpy(dataBuf + RPMB_SZ_DATA,
710 data_for_hmac + RPMB_SZ_DATA,
711 size_for_hmac - RPMB_SZ_DATA);
712
713 dataBuf = dataBuf + size_for_hmac;
714
715 }
716
717 iCnt--;
718
719 if (hmac_sha256(param->keybytes, 32, dataBuf_start,
720 284UL * tran_blkcnt,
721 rpmbdata.icmd.frames[iCnt].key_mac) != 0)
722 MSG(ERR, "hmac_sha256() return error!\n");
723
724 /*
725 * Send write data request.
726 */
727
728 ret = rpmb_cmd_req(mmc_rpmb, &rpmbdata);
729
730 if (ret != 0) {
731 MSG(ERR, "%s, rpmb_cmd_req IO error!!!(0x%x)\n",
732 __func__, ret);
733 break;
734 }
735
736 /*
737 * Authenticate write result response.
738 * 1. authenticate hmac.
739 * 2. check result.
740 * 3. compare write counter is increamented.
741 */
742 if (hmac_sha256(param->keybytes, 32,
743 rpmbdata.ocmd.frames->data,
744 284UL, hmac) != 0)
745 MSG(ERR, "hmac_sha256() return error!\n");
746
747 if (memcmp(hmac, rpmbdata.ocmd.frames->key_mac,
748 RPMB_SZ_MAC) != 0) {
749 MSG(ERR, "%s, hmac compare error!!!\n", __func__);
750 ret = RPMB_HMAC_ERROR;
751 break;
752 }
753
754 if (rpmbdata.ocmd.frames->result != 0U) {
755 MSG(ERR, "%s, result error!!! (0x%x)\n", __func__,
756 cpu_to_be16(rpmbdata.ocmd.frames->result));
757 ret = RPMB_RESULT_ERROR;
758 break;
759 }
760
761 if (cpu_to_be32(rpmbdata.ocmd.frames->write_counter) !=
762 wc + 1U) {
763 MSG(ERR, "%s, write counter error!!! (0x%x)\n",
764 __func__,
765 cpu_to_be32(rpmbdata.ocmd.frames->write_counter));
766 ret = RPMB_WC_ERROR;
767 break;
768 }
769
770 blkaddr += tran_blkcnt;
771 left_blkcnt -= tran_blkcnt;
772 i++;
773
774 kfree(rpmbdata.icmd.frames);
775 kfree(rpmbdata.ocmd.frames);
776 kfree(dataBuf_start);
777 };
778
779 if (ret != 0) {
780 kfree(rpmbdata.icmd.frames);
781 kfree(rpmbdata.ocmd.frames);
782 kfree(dataBuf_start);
783 }
784
785 if (left_blkcnt != 0U || left_size != 0U) {
786 MSG(ERR, "left_blkcnt or left_size is not empty!!!!!!\n");
787 return RPMB_TRANSFER_NOT_COMPLETE;
788 }
789
790 MSG(DBG_INFO, "%s end!!!\n", __func__);
791
792 return ret;
793}
794
795static int rpmb_req_ioctl_read_data(struct rpmb_ioc_param *param)
796{
797 struct rpmb_data rpmbdata;
798 struct rpmb_dev *mmc_rpmb;
799 u32 i, tran_size, left_size = param->data_len;
800 u16 iCnt, total_blkcnt, tran_blkcnt, left_blkcnt;
801 u16 blkaddr;
802 u8 nonce[RPMB_SZ_NONCE] = {0};
803 u8 hmac[RPMB_SZ_MAC];
804 u8 *dataBuf, *dataBuf_start, *data_for_hmac;
805 size_t size_for_hmac;
806 int ret = 0;
807 u8 user_param_data;
808
809 MSG(DBG_INFO, "%s start!!!\n", __func__);
810
811 ret = get_user(user_param_data, param->databytes);
812 if (ret != 0)
813 return -EFAULT;
814
815 ret = get_user(user_param_data, param->keybytes);
816 if (ret != 0)
817 return -EFAULT;
818
819 mmc_rpmb = rpmb_dev_get_by_type(RPMB_TYPE_EMMC);
820
821 i = 0;
822 tran_blkcnt = 0;
823 dataBuf = NULL;
824 dataBuf_start = NULL;
825
826 total_blkcnt = (u16)(((param->data_len % RPMB_SZ_DATA) != 0U) ?
827 (param->data_len / RPMB_SZ_DATA + 1U) :
828 (param->data_len / RPMB_SZ_DATA));
829 left_blkcnt = total_blkcnt;
830
831 blkaddr = param->addr;
832
833 while (left_blkcnt > 0U) {
834
835 if (left_blkcnt >= MAX_RPMB_TRANSFER_BLK)
836 tran_blkcnt = MAX_RPMB_TRANSFER_BLK;
837 else
838 tran_blkcnt = left_blkcnt;
839
840 MSG(DBG_INFO, "%s, left_blkcnt = 0x%x, tran_blkcnt = 0x%x\n",
841 __func__, left_blkcnt, tran_blkcnt);
842
843 /*
844 * initial frame buffers
845 */
846
847 rpmbdata.icmd.frames = rpmb_alloc_frames(1);
848
849 if (rpmbdata.icmd.frames == NULL)
850 return RPMB_ALLOC_ERROR;
851
852 rpmbdata.ocmd.frames = rpmb_alloc_frames(tran_blkcnt);
853
854 if (rpmbdata.ocmd.frames == NULL)
855 return RPMB_ALLOC_ERROR;
856
857 /*
858 * Initial data buffer for HMAC computation.
859 * Since HAMC computation tool which we use needs
860 * consecutive data buffer.
861 * Pre-alloced it.
862 */
863
864 dataBuf_start = dataBuf = kzalloc(284UL * tran_blkcnt, 0);
865
866 get_random_bytes(nonce, (int)RPMB_SZ_NONCE);
867
868 /*
869 * Prepare request read data frame.
870 *
871 * Input frame (in view of device) only needs addr and nonce.
872 */
873
874 rpmbdata.req_type = RPMB_READ_DATA;
875 rpmbdata.icmd.nframes = 1;
876 rpmbdata.icmd.frames->req_resp = cpu_to_be16(RPMB_READ_DATA);
877 rpmbdata.icmd.frames->addr = cpu_to_be16(blkaddr);
878 rpmbdata.icmd.frames->block_count = cpu_to_be16(tran_blkcnt);
879 memcpy(rpmbdata.icmd.frames->nonce, nonce, RPMB_SZ_NONCE);
880
881 /* output frames (in view of device) */
882
883 rpmbdata.ocmd.nframes = tran_blkcnt;
884
885 ret = rpmb_cmd_req(mmc_rpmb, &rpmbdata);
886
887 if (ret != 0) {
888 MSG(ERR, "%s, rpmb_cmd_req IO error!!!(0x%x)\n",
889 __func__, ret);
890 break;
891 }
892
893 /*
894 * Retrieve every data frame one by one.
895 */
896
897 /* size for hmac calculation: 512 - 228 = 284 */
898 size_for_hmac = sizeof(struct rpmb_frame) -
899 offsetof(struct rpmb_frame, data);
900
901 for (iCnt = 0; iCnt < tran_blkcnt; iCnt++) {
902
903 if (left_size >= RPMB_SZ_DATA)
904 tran_size = RPMB_SZ_DATA;
905 else
906 tran_size = left_size;
907
908 /*
909 * dataBuf used for hmac calculation. we need to
910 * aggregate each block's data till to type field.
911 * each block has 284 bytes (size_for_hmac) need
912 * aggregation.
913 */
914 data_for_hmac = rpmbdata.ocmd.frames[iCnt].data;
915
916 /* copy data part */
917 memcpy(dataBuf, data_for_hmac, RPMB_SZ_DATA);
918
919 /* copy left part */
920 memcpy(dataBuf + RPMB_SZ_DATA,
921 data_for_hmac + RPMB_SZ_DATA,
922 size_for_hmac - RPMB_SZ_DATA);
923
924 dataBuf = dataBuf + size_for_hmac;
925
926 /*
927 * Sorry, I shouldn't copy read data to user's
928 * buffer now, it should be later
929 * after checking no problem,
930 * but for convenience...you know...
931 */
932 memcpy(param->databytes +
933 i * MAX_RPMB_TRANSFER_BLK * RPMB_SZ_DATA +
934 (iCnt * RPMB_SZ_DATA),
935 rpmbdata.ocmd.frames[iCnt].data,
936 tran_size);
937 left_size -= tran_size;
938 }
939
940 iCnt--;
941
942 /*
943 * Authenticate response read data frame.
944 */
945 if (hmac_sha256(param->keybytes, 32,
946 dataBuf_start, size_for_hmac * tran_blkcnt,
947 hmac) != 0)
948 MSG(ERR, "hmac_sha256() return error!\n");
949
950 if (memcmp(hmac, rpmbdata.ocmd.frames[iCnt].key_mac,
951 RPMB_SZ_MAC) != 0) {
952 MSG(ERR, "%s, hmac compare error!!!\n", __func__);
953 ret = RPMB_HMAC_ERROR;
954 break;
955 }
956
957 if (memcmp(nonce, rpmbdata.ocmd.frames[iCnt].nonce,
958 RPMB_SZ_NONCE) != 0) {
959 MSG(ERR, "%s, nonce compare error!!!\n", __func__);
960 ret = RPMB_NONCE_ERROR;
961 break;
962 }
963
964 if (rpmbdata.ocmd.frames[iCnt].result != 0U) {
965 MSG(ERR, "%s, result error!!! (0x%x)\n",
966 __func__,
967 cpu_to_be16p(&rpmbdata.ocmd.frames[iCnt].result));
968 ret = RPMB_RESULT_ERROR;
969 break;
970 }
971
972 blkaddr += tran_blkcnt;
973 left_blkcnt -= tran_blkcnt;
974 i++;
975
976 kfree(rpmbdata.icmd.frames);
977 kfree(rpmbdata.ocmd.frames);
978 kfree(dataBuf_start);
979 };
980
981 if (ret != 0) {
982 kfree(rpmbdata.icmd.frames);
983 kfree(rpmbdata.ocmd.frames);
984 kfree(dataBuf_start);
985 }
986
987 if (left_blkcnt != 0U || left_size != 0U) {
988 MSG(ERR, "left_blkcnt or left_size is not empty!!!!!!\n");
989 return RPMB_TRANSFER_NOT_COMPLETE;
990 }
991
992 MSG(DBG_INFO, "%s end!!!\n", __func__);
993
994 return ret;
995}
996
997/*
998 * End of above.
999 *
1000 ******************************************************************************/
1001
1002
1003#if IS_ENABLED(CONFIG_TRUSTONIC_TEE_SUPPORT)
1004
1005static enum mc_result rpmb_gp_execute(u32 cmdId)
1006{
1007 int ret;
1008
1009 switch (cmdId) {
1010
1011 case DCI_RPMB_CMD_READ_DATA:
1012
1013 MSG(DBG_INFO, "%s: DCI_RPMB_CMD_READ_DATA\n", __func__);
1014
1015 ret = rpmb_req_read_data(rpmb_gp_dci->request.frame,
1016 rpmb_gp_dci->request.blks);
1017
1018 break;
1019
1020 case DCI_RPMB_CMD_GET_WCNT:
1021
1022 MSG(DBG_INFO, "%s: DCI_RPMB_CMD_GET_WCNT\n", __func__);
1023
1024 ret = rpmb_req_get_wc(NULL, NULL, rpmb_gp_dci->request.frame);
1025
1026 break;
1027
1028 case DCI_RPMB_CMD_WRITE_DATA:
1029
1030 MSG(DBG_INFO, "%s: DCI_RPMB_CMD_WRITE_DATA\n", __func__);
1031
1032 ret = rpmb_req_write_data(rpmb_gp_dci->request.frame,
1033 rpmb_gp_dci->request.blks);
1034
1035 break;
1036
1037 default:
1038 MSG(ERR, "%s: receive an unknown command id(%d).\n",
1039 __func__, cmdId);
1040 break;
1041
1042 }
1043
1044 return MC_DRV_OK;
1045}
1046
1047static int rpmb_gp_listenDci(void *arg)
1048{
1049 enum mc_result mc_ret;
1050 u32 cmdId;
1051
1052 MSG(INFO, "%s: DCI listener.\n", __func__);
1053
1054 for (;;) {
1055
1056 MSG(INFO, "%s: Waiting for notification\n", __func__);
1057
1058 /* Wait for notification from SWd */
1059 mc_ret = mc_wait_notification(&rpmb_gp_session,
1060 MC_INFINITE_TIMEOUT);
1061 if (mc_ret != MC_DRV_OK) {
1062 MSG(ERR, "%s: mcWaitNotification failed, mc_ret=%d\n",
1063 __func__, mc_ret);
1064 break;
1065 }
1066
1067 cmdId = rpmb_gp_dci->command.header.commandId;
1068
1069 MSG(INFO, "%s: wait notification done!! cmdId = %x\n",
1070 __func__, cmdId);
1071
1072 /* Received exception. */
1073 mc_ret = rpmb_gp_execute(cmdId);
1074
1075 /* Notify the STH*/
1076 mc_ret = mc_notify(&rpmb_gp_session);
1077 if (mc_ret != MC_DRV_OK) {
1078 MSG(ERR, "%s: mcNotify returned: %d\n",
1079 __func__, mc_ret);
1080 break;
1081 }
1082 }
1083
1084 return 0;
1085}
1086
1087static enum mc_result rpmb_gp_open_session(void)
1088{
1089 int cnt = 0;
1090 enum mc_result mc_ret;
1091
1092 MSG(INFO, "%s start\n", __func__);
1093
1094 do {
1095 msleep(6000);
1096
1097 /* open device */
1098 mc_ret = mc_open_device(rpmb_gp_devid);
1099 if (mc_ret != MC_DRV_OK) {
1100 MSG(ERR, "%s, mc_open_device failed: %d\n",
1101 __func__, mc_ret);
1102 cnt++;
1103 continue;
1104 }
1105
1106 MSG(INFO, "%s, mc_open_device success.\n", __func__);
1107
1108
1109 /* allocating WSM for DCI */
1110 mc_ret = mc_malloc_wsm(rpmb_gp_devid, 0,
1111 (u32)sizeof(struct dciMessage_t),
1112 (uint8_t **)&rpmb_gp_dci, 0);
1113 if (mc_ret != MC_DRV_OK) {
1114 MSG(ERR, "%s, mc_malloc_wsm failed: %d\n",
1115 __func__, mc_ret);
1116 mc_ret = mc_close_device(rpmb_gp_devid);
1117 cnt++;
1118 continue;
1119 }
1120
1121 MSG(INFO, "%s, mc_malloc_wsm success.\n", __func__);
1122 MSG(INFO, "uuid[0]=%d, uuid[1]=%d, uuid[2]=%d, uuid[3]=%d\n",
1123 rpmb_gp_uuid.value[0],
1124 rpmb_gp_uuid.value[1],
1125 rpmb_gp_uuid.value[2],
1126 rpmb_gp_uuid.value[3]
1127 );
1128
1129 rpmb_gp_session.device_id = rpmb_gp_devid;
1130
1131 /* open session */
1132 mc_ret = mc_open_session(&rpmb_gp_session,
1133 &rpmb_gp_uuid,
1134 (uint8_t *) rpmb_gp_dci,
1135 (u32)sizeof(struct dciMessage_t));
1136
1137 if (mc_ret != MC_DRV_OK) {
1138 MSG(ERR,
1139 "%s, mc_open_session failed, result(%d), cnt(%d)\n",
1140 __func__, mc_ret, cnt);
1141
1142 mc_ret = mc_free_wsm(rpmb_gp_devid,
1143 (uint8_t *)rpmb_gp_dci);
1144 MSG(ERR, "%s, free wsm result (%d)\n",
1145 __func__, mc_ret);
1146
1147 mc_ret = mc_close_device(rpmb_gp_devid);
1148 MSG(ERR, "%s, try free wsm and close device\n",
1149 __func__);
1150 cnt++;
1151 continue;
1152 }
1153 MSG(INFO, "%s, mc_open_session success.\n", __func__);
1154
1155 /* create a thread for listening DCI signals */
1156 rpmb_gp_Dci_th = kthread_run(rpmb_gp_listenDci,
1157 NULL, "rpmb_gp_Dci");
1158 if (IS_ERR(rpmb_gp_Dci_th))
1159 MSG(ERR, "%s, init kthread_run failed!\n", __func__);
1160 else
1161 break;
1162
1163 } while (cnt < 60);
1164
1165 if (cnt >= 60)
1166 MSG(ERR, "%s, open session failed!!!\n", __func__);
1167
1168
1169 MSG(ERR, "%s end, mc_ret = %x\n", __func__, mc_ret);
1170
1171 return mc_ret;
1172}
1173
1174
1175static int rpmb_thread(void *context)
1176{
1177 enum mc_result ret;
1178
1179 MSG(INFO, "%s start\n", __func__);
1180
1181 ret = rpmb_gp_open_session();
1182 MSG(INFO, "%s rpmb_gp_open_session, ret = %x\n", __func__, ret);
1183
1184 return 0;
1185}
1186#endif
1187
1188static int rpmb_open(struct inode *pinode, struct file *pfile)
1189{
1190 return 0;
1191}
1192
1193static long rpmb_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
1194{
1195 int err = 0;
1196 unsigned long n;
1197 struct rpmb_ioc_param param;
1198
1199 n = copy_from_user(&param, (void *)arg, sizeof(param));
1200
1201 if (n != 0UL) {
1202 MSG(ERR, "%s, copy from user failed: %x\n", __func__, err);
1203 return -EFAULT;
1204 }
1205
1206 switch (cmd) {
1207
1208 case RPMB_IOCTL_PROGRAM_KEY:
1209
1210 MSG(DBG_INFO, "%s, cmd = RPMB_IOCTL_PROGRAM_KEY not support\n",
1211 __func__);
1212
1213 break;
1214
1215 case RPMB_IOCTL_READ_DATA:
1216
1217 MSG(DBG_INFO, "%s, cmd = RPMB_IOCTL_READ_DATA!!!!\n", __func__);
1218
1219 err = rpmb_req_ioctl_read_data(&param);
1220
1221 if (err != 0) {
1222 MSG(ERR, "%s, rpmb_req_ioctl_read_data() error!(%x)\n",
1223 __func__, err);
1224 break;
1225 }
1226
1227 n = copy_to_user((void *)arg, &param, sizeof(param));
1228
1229 if (n != 0UL) {
1230 MSG(ERR, "%s, copy to user user failed: %x\n",
1231 __func__, err);
1232 err = -EFAULT;
1233 break;
1234 }
1235
1236 break;
1237
1238 case RPMB_IOCTL_WRITE_DATA:
1239
1240 MSG(DBG_INFO, "%s, cmd = RPMB_IOCTL_WRITE_DATA!!!\n", __func__);
1241
1242 err = rpmb_req_ioctl_write_data(&param);
1243
1244 if (err != 0)
1245 MSG(ERR, "%s, rpmb_req_ioctl_write_data() error!(%x)\n",
1246 __func__, err);
1247
1248 break;
1249
1250 default:
1251 MSG(ERR, "%s, wrong ioctl code (%d)!!!\n", __func__, cmd);
1252 err = -ENOTTY;
1253 break;
1254 }
1255
1256 return err;
1257}
1258
1259static int rpmb_close(struct inode *pinode, struct file *pfile)
1260{
1261 int ret = 0;
1262
1263 MSG(INFO, "%s, !!!!!!!!!!!!\n", __func__);
1264
1265 return ret;
1266}
1267
1268static const struct file_operations rpmb_fops = {
1269 .owner = THIS_MODULE,
1270 .open = rpmb_open,
1271 .release = rpmb_close,
1272 .unlocked_ioctl = rpmb_ioctl,
1273 .write = NULL,
1274 .read = NULL,
1275};
1276
1277static int __init rpmb_init(void)
1278{
1279 int alloc_ret;
1280 int cdev_ret = -1;
1281 unsigned int major;
1282 dev_t dev;
1283 struct device_node *np;
1284 struct device *device = NULL;
1285
1286 np = of_find_compatible_node(NULL, NULL, "mediatek,rpmb-kinibi");
1287 if (np == NULL)
1288 goto out_err;
1289
1290 MSG(INFO, "%s start\n", __func__);
1291
1292 alloc_ret = alloc_chrdev_region(&dev, 0, 1, RPMB_NAME);
1293
1294 if (alloc_ret != 0) {
1295 MSG(ERR, "%s, init alloc_chrdev_region failed!\n", __func__);
1296 goto error;
1297 }
1298
1299 major = MAJOR(dev);
1300
1301 cdev_init(&rpmb_cdev, &rpmb_fops);
1302 rpmb_cdev.owner = THIS_MODULE;
1303
1304 cdev_ret = cdev_add(&rpmb_cdev, MKDEV(major, 0U), 1);
1305 if (cdev_ret != 0) {
1306 MSG(ERR, "%s, init cdev_add failed!\n", __func__);
1307 goto error;
1308 }
1309
1310#ifdef __RPMB_IOCTL_SUPPORT
1311 mtk_rpmb_class = class_create(THIS_MODULE, RPMB_NAME);
1312
1313 if (IS_ERR(mtk_rpmb_class)) {
1314 MSG(ERR, "%s, init class_create failed!\n", __func__);
1315 goto error;
1316 }
1317
1318 device = device_create(mtk_rpmb_class, NULL, MKDEV(major, 0), NULL,
1319 RPMB_NAME "%d", 0);
1320#endif
1321 if (IS_ERR(device)) {
1322 MSG(ERR, "%s, init device_create failed!\n", __func__);
1323 goto error;
1324 }
1325
1326#if IS_ENABLED(CONFIG_TRUSTONIC_TEE_SUPPORT)
1327 open_th = kthread_run(rpmb_thread, NULL, "rpmb_open");
1328 if (IS_ERR(open_th))
1329 MSG(ERR, "%s, init kthread_run failed!\n", __func__);
1330#endif
1331
1332 MSG(INFO, "%s end!!!!\n", __func__);
1333
1334 return 0;
1335
1336error:
1337#ifdef __RPMB_IOCTL_SUPPORT
1338 if (mtk_rpmb_class)
1339 class_destroy(mtk_rpmb_class);
1340#endif
1341 if (cdev_ret == 0)
1342 cdev_del(&rpmb_cdev);
1343
1344 if (alloc_ret == 0)
1345 unregister_chrdev_region(dev, 1);
1346
1347out_err:
1348 return -1;
1349}
1350
1351late_initcall(rpmb_init);
1352
1353MODULE_DESCRIPTION("RPMB class");
1354MODULE_LICENSE("GPL v2");