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 __HI_BURN_FILE_H__ 17 #define __HI_BURN_FILE_H__ 18 19 #include <hi_types.h> 20 21 /** 22 * 23 * Initial vector length.CNcomment:初始向量长度。CNend 24 */ 25 #define IV_BYTE_LEN 16 26 27 /** 28 * 29 * RSA2048 parameter length.CNcomment:RSA2048参数长度。CNend 30 */ 31 #define RSA_2048_LEN 256 32 33 /** 34 * 35 * RSA2048 parameter E length.CNcomment:RSA2048参数E长度。CNend 36 */ 37 #define RSA_EXP_E_LEN 4 38 39 /** 40 * 41 * ECC parameter length.CNcomment:ECC参数长度。CNend 42 */ 43 #define ECC_32_BYTES 32 44 45 /** 46 * 47 * SHA256 hash length.CNcomment:SHA256哈希值长度。CNend 48 */ 49 #define SHA_256_LEN 32 50 51 /** 52 * 53 * Kernel upgrade file.CNcomment:Kernel升级文件。CNend 54 */ 55 #define HI_UPG_FILE_KERNEL 0xF0 /* Kernel upgrade file. */ 56 57 /** 58 * 59 * FlashBoot upgrade file.CNcomment:FlashBoot升级文件。CNend 60 */ 61 #define HI_UPG_FILE_BOOT 0xE1 /* FlashBoot upgrade file. */ 62 63 /** 64 * 65 * Parameter of sign algorithm.CNcomment:签名算法参数。CNend 66 */ 67 typedef struct { 68 hi_u32 hash_alg : 16; /**< HASH algorithm:0:SHA256.CNcomment:HASH算法。CNend */ 69 hi_u32 sign_alg : 6; /**< Sign algorithm:0:RSA-PKCS;1:RSA-PSS;0x10:ECDSA256;0x3F:SHA256.CNcomment:签名算法。CNend */ 70 hi_u32 sign_param : 10; /**< Sign parameter, default '0'.CNcomment:HASH算法。CNend */ 71 } sign_alg_param; 72 73 typedef struct { 74 hi_u8 mod_n[RSA_2048_LEN]; /**< Mod N.CNcomment:RSA参数N。CNend */ 75 hi_u8 exp_e[RSA_EXP_E_LEN]; /**< Exp E.CNcomment:RSA参数E。CNend */ 76 hi_u8 padding[28]; /**< Padding field:28.CNcomment:填充字段。CNend */ 77 } upg_rsa_key; 78 79 typedef struct { 80 hi_u8 sign[RSA_2048_LEN]; /**< RSA signature.CNcomment:RSA签名。CNend */ 81 } upg_rsa_sign; 82 83 typedef struct { 84 hi_u8 px[ECC_32_BYTES]; /**< Pubkey x.CNcomment:ECC密钥x。CNend */ 85 hi_u8 py[ECC_32_BYTES]; /**< Pubkey y.CNcomment:ECC密钥y。CNend */ 86 hi_u8 padding[224]; /**< Padding field:224. CNcomment:填充字段。CNend */ 87 } upg_ecc_key; 88 89 typedef struct { 90 hi_u8 r[ECC_32_BYTES]; /**< Signature of ECC.CNcomment:ECC签名信息r。CNend */ 91 hi_u8 s[ECC_32_BYTES]; /**< Signature of ECC.CNcomment:ECC签名信息s。CNend */ 92 hi_u8 padding[192]; /**< Padding field:192.CNcomment:填充字段。CNend */ 93 } upg_ecc_sign; 94 95 typedef struct { 96 hi_u8 padding[288]; /**< Padding field:288.CNcomment:填充字段。CNend */ 97 } upg_sha256_key; 98 99 typedef struct { 100 hi_u8 check_sum[SHA_256_LEN]; /**< Hash value of SHA256.CNcomment:SHA256算法哈希值。CNend */ 101 hi_u8 padding[224]; /**< Padding field:224.CNcomment:填充字段。CNend */ 102 } upg_sha256_sign; 103 104 typedef struct { 105 hi_u8 reserved[32]; /**< 32:Reserved for user.CNcomment:用户字段定义字段。CNend */ 106 }hi_upg_user_info; 107 108 typedef struct { 109 hi_u32 image_id; /**< Identity of upgrade file Key Area.CNcomment:升级文件魔术字。CNend */ 110 hi_u32 struct_version; /**< The structure of upgrade file version.CNcomment:升级文件结构体版本号。CNend */ 111 hi_u32 section_offset; /**< Offset of upgrade Section.CNcomment:Section段偏移。CNend */ 112 hi_u32 section_len; /**< Length of upgrade Section.CNcomment:Section段长度。CNend */ 113 hi_upg_user_info user_info; /**< Reserved for user.CNcomment:用户自定义字段。CNend */ 114 hi_u8 file_type; /**< Upgrade file type:0xF0: kernel file; 0xE1: boot file.CNcomment:升级文件类型。CNend */ 115 hi_u8 file_version; /**< File Version, for anti-rollback. [0, 16] for boot file and [0, 48] for kernel file. 116 CNcomment:升级文件结构体版本号。CNend */ 117 hi_u8 encrypt_flag; /**< 0x42: Section Area is not encrypted; other: Section Area is encrypted. 118 CNcomment:Section段加密标志。CNend */ 119 hi_u8 file_attr; /**< File Attributes.CNcomment:文件属性。CNend */ 120 hi_u32 file_len; /**< Entire file length.CNcomment:升级文件长度。CNend */ 121 hi_u32 key_len; /**< Length of Key(288Bytes).True length:RSA2048: 272 Bytes, ECDSA: 64Bytes. 122 CNcomment:密钥长度。CNend */ 123 sign_alg_param param; /**< Parma of the signature algorithm.CNcomment:签名算法参数。CNend */ 124 hi_u8 aes_key[IV_BYTE_LEN]; /**< Part of key factor.CNcomment:AES密钥。CNend */ 125 hi_u8 aes_iv[IV_BYTE_LEN]; /**< The IV (AES-256 CBC-mode) to encrypt Section.CNcomment:AES初始向量。CNend */ 126 } hi_upg_common_head; 127 128 typedef struct { 129 hi_u32 image_id; /**< Identity of upgrade file Key Area.CNcomment:升级文件魔术字。CNend */ 130 hi_u32 struct_version; /**< The structure of upgrade file Section Area.CNcomment:升级文件结构体版本号。CNend */ 131 sign_alg_param param; /**< The signature algorithm.CNcomment:签名算法参数。CNend */ 132 hi_u8 section_count; /**< The number of sections.CNcomment:Section段个数。CNend */ 133 hi_u8 reserved[27]; /**< 27 bytes reserved.CNcomment:保留字段。CNend */ 134 hi_u8 section0_compress; /**< Whether section 0 is compressed.CNcomment:Section0是否压缩。CNend */ 135 hi_u8 pad0[3]; /**< 3 bytes padding.CNcomment:填充字段。CNend */ 136 hi_u32 section0_offset; /**< Offset of Section0.CNcomment:Section0段偏移。CNend */ 137 hi_u32 section0_len; /**< Length of Section0, aligned to 16 bytes.CNcomment:Section0段长度。CNend */ 138 hi_u8 section1_compress; /**< Whether section 1 is compressed.CNcomment:Section1是否压缩Nend */ 139 hi_u8 pad1[3]; /**< 3 bytes padding.CNcomment:填充字段。CNend */ 140 hi_u32 section1_offset; /**< Offset of Section1.CNcomment:Section1段偏移。CNend */ 141 hi_u32 section1_len; /**< Length of Section1, aligned to 16 bytes.CNcomment:Section1段长度。CNend */ 142 } hi_upg_section_head; 143 144 typedef struct { 145 upg_rsa_key key; /**< Key of rsa.CNcomment:RSA算法密钥。CNend */ 146 upg_rsa_sign sign; /**< Sign of rsa.CNcomment:RSA算法签名。CNend */ 147 } hi_upg_rsa_alg; 148 149 typedef struct { 150 upg_ecc_key key; /**< Key of ecc.CNcomment:ECC算法密钥。CNend */ 151 upg_ecc_sign sign; /**< Sign of ecc.CNcomment:ECC算法签名。CNend */ 152 } hi_upg_ecc_alg; 153 154 typedef struct { 155 upg_sha256_key key; /**< Padding field.CNcomment:填充字段。CNend */ 156 upg_sha256_sign sign; /**< Hash of sha256.CNcomment:SHA256算法哈希值。CNend */ 157 } hi_upg_sha256_alg; 158 159 typedef struct { 160 union { 161 upg_rsa_key rsa; /**< Key of rsa.CNcomment:rsa密钥。CNend */ 162 upg_ecc_key ecc; /**< Key of ecc.CNcomment:ecc密钥。CNend */ 163 upg_sha256_key sha; /**< Padding field.CNcomment:填充字段。CNend */ 164 } key; 165 }hi_upg_key; 166 167 typedef struct { 168 union { 169 upg_rsa_sign rsa; /**< Sign of rsa.CNcomment:rsa签名。CNend */ 170 upg_ecc_sign ecc; /**< Sign of ecc.CNcomment:ecc签名。CNend */ 171 upg_sha256_sign sha; /**< Hash of sha256.CNcomment:SHA256哈希。CNend */ 172 } sign; 173 }hi_upg_sign; 174 175 typedef struct { 176 hi_upg_common_head common; /**< Common head of upg file.CNcomment:公共文件头。CNend */ 177 union { 178 hi_upg_rsa_alg rsa; /**< Key and sign of RSA.CNcomment:RSA密钥及签名信息。CNend */ 179 hi_upg_ecc_alg ecc; /**< Key and sign of ECC.CNcomment:ECC密钥及签名信息。CNend */ 180 hi_upg_sha256_alg sha;/**< Key and sign of SHA256.CNcomment:SHA256密钥及签名信息。CNend */ 181 } sign_alg; 182 } hi_upg_head; 183 184 185 hi_void loaderboot_get_start_addr_offset(hi_u32 addr, hi_u32 *offset); 186 187 #endif 188