lin
2025-01-10 9ec4e21f2f615ef95b70a249569906799e36bace
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
/*
 * (C) Copyright 2018-2020
 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
 * wangwei <wangwei@allwinnertech.com>
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
#include <common.h>
#include <sys_config.h>
#include <securestorage.h>
#include <private_uboot.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
extern int do_burn_from_boot(cmd_tbl_t *cmdtp, int flag, int argc,
                char *const argv[]);
 
//#define  SUNXI_SECURESTORAGE_TEST_ERASE
 
int sunxi_keydata_burn_by_usb(void)
{
   char buffer[512];
   __maybe_unused int data_len;
   __maybe_unused int ret = 0;
   __maybe_unused uint burn_private_start;
   __maybe_unused uint burn_private_len;
 
   int workmode     = uboot_spare_head.boot_data.work_mode;
   int if_need_burn_key = 0;
 
   ret = script_parser_fetch("/soc/target", "burn_key", &if_need_burn_key,
                 1);
 
   if (if_need_burn_key != 1) {
       pr_err("out of usb burn from boot: not need burn key\n");
       return 0;
   }
 
   if (workmode != WORK_MODE_BOOT) {
       pr_err("out of usb burn from boot: not boot mode\n");
       return 0;
   }
   memset(buffer, 0, 512);
#ifdef CONFIG_SUNXI_SECURE_STORAGE
   if (sunxi_secure_storage_init()) {
       pr_err("sunxi secure storage is not supported\n");
   } else {
#ifndef SUNXI_SECURESTORAGE_TEST_ERASE
       ret = sunxi_secure_object_read("key_burned_flag", buffer, 512,
                          &data_len);
       if (ret) {
           pr_msg("sunxi secure storage has no flag\n");
       } else {
           if (!strcmp(buffer, "key_burned")) {
               pr_msg("find key burned flag\n");
               return 0;
           }
           pr_msg("do not find key burned flag\n");
       }
#else
       if (!sunxi_secure_storage_erase("key_burned_flag"))
           sunxi_secure_storage_exit();
 
       return 0;
#endif
   }
#endif
   return do_burn_from_boot(NULL, 0, 0, NULL);
}