• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __LOAD_CRYPTO_H__
17 #define __LOAD_CRYPTO_H__
18 #ifdef CONFIG_FLASH_ENCRYPT_SUPPORT
19 #include <hi_boot_rom.h>
20 
21 #define crypto_mem_free(sz)               \
22     do {                                  \
23         if ((sz) != HI_NULL) {            \
24             boot_free(sz);                \
25         }                                 \
26         (sz) = HI_NULL;                   \
27     } while (0)
28 
29 #define IV_BYTE_LENGTH          16
30 #define ROOTKEY_IV_BYTE_LENGTH  32
31 
32 #define DIE_ID_BYTE_LENGTH      24
33 
34 #define KEY_BYTE_LENGTH         32
35 
36 #define SHA_256_LENGTH          32
37 
38 #define ROOT_SALT_LENGTH        32
39 
40 #define CRYPTO_CNT_NUM          6
41 
42 #define CRYPTO_KERNEL_LENGTH  4096
43 
44 #define KERNEL_RAM_ADDR       0xD8400
45 
46 #define KDF_ITERATION_CNT       1024
47 
48 #define MIN_CRYPTO_BLOCK_SIZE   16
49 
50 #define HI_NV_FTM_STARTUP_CFG_ID          0x3
51 #define HI_NV_FTM_KERNELA_WORK_ID         0x4
52 #define HI_NV_FTM_BACKUP_KERNELA_WORK_ID  0x5
53 
54 typedef enum {
55     CRYPTO_WORKKEY_KERNEL_A = 0x1,
56     CRYPTO_WORKKEY_KERNEL_A_BACKUP = 0x2,
57     CRYPTO_WORKKEY_KERNEL_A_BOTH = 0x3,
58 } crypto_workkey_partition;
59 
60 typedef struct {
61     uintptr_t addr_start; /* boot start address */
62     hi_u16 mode;          /* upgrade mode */
63     hi_u8 file_type;      /* file type:boot or code+nv */
64     hi_u8 refresh_nv;     /* refresh nv when the flag bit 0x55 is read */
65     hi_u8 reset_cnt;      /* number of restarts in upgrade mode */
66     hi_u8 cnt_max;        /* the maximum number of restarts (default value : 3) */
67     hi_u16 reserved1;
68     uintptr_t addr_write; /* write kernel upgrade file address */
69     hi_u32 reserved2; /* 2: reserved bytes */
70 } hi_nv_ftm_startup_cfg;
71 
72 typedef struct {
73     hi_u8 root_salt[ROOT_SALT_LENGTH];
74     hi_u8 iv_nv[IV_BYTE_LENGTH];
75     hi_u8 iv_content[IV_BYTE_LENGTH];
76     hi_u8 work_text[KEY_BYTE_LENGTH];
77     hi_u8 content_sh256[SHA_256_LENGTH];
78 } hi_flash_crypto_content;
79 
80 hi_u32 crypto_check_encrypt(hi_void);
81 hi_u32 crypto_encrypt_factory_image(uintptr_t file_addr);
82 
83 #endif
84 #endif
85