• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * @file hi_upg_file.h
3  *
4  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /**
19  * @defgroup upg Upgrade
20  * @ingroup system
21 */
22 
23 #ifndef __HI_UPG_FILE_H__
24 #define __HI_UPG_FILE_H__
25 
26 #include <hi_types.h>
27 
28 /**
29  *
30  * Initial vector length.CNcomment:初始向量长度。CNend
31  */
32 #define IV_BYTE_LEN   16
33 
34 /**
35  *
36  * RSA2048 parameter length.CNcomment:RSA2048参数长度。CNend
37  */
38 #define RSA_2048_LEN  256
39 
40 /**
41  *
42  * RSA2048 parameter E length.CNcomment:RSA2048参数E长度。CNend
43  */
44 #define RSA_EXP_E_LEN 4
45 
46 /**
47  *
48  * ECC parameter length.CNcomment:ECC参数长度。CNend
49  */
50 #define ECC_32_BYTES  32
51 
52 /**
53  *
54  * SHA256 hash length.CNcomment:SHA256哈希值长度。CNend
55  */
56 #define SHA_256_LEN   32
57 
58 /**
59  *
60  * Kernel upgrade file.CNcomment:Kernel升级文件。CNend
61  */
62 #define HI_UPG_FILE_KERNEL 0xF0 /* Kernel upgrade file. */
63 
64 /**
65  *
66  * FlashBoot upgrade file.CNcomment:FlashBoot升级文件。CNend
67  */
68 #define HI_UPG_FILE_BOOT   0xE1 /* FlashBoot upgrade file. */
69 
70 /**
71  *
72  * Parameter of sign algorithm.CNcomment:签名算法参数。CNend
73  */
74 typedef struct {
75     hi_u32 hash_alg : 16; /**< HASH algorithm:0:SHA256.CNcomment:HASH算法。CNend */
76     hi_u32 sign_alg : 6; /**< Sign algorithm:0:RSA-PKCS;1:RSA-PSS;0x10:ECDSA256;0x3F:SHA256.CNcomment:签名算法。CNend */
77     hi_u32 sign_param : 10; /**< Sign parameter, default '0'.CNcomment:HASH算法。CNend */
78 } sign_alg_param;
79 
80 typedef struct {
81     hi_u8 mod_n[RSA_2048_LEN];  /**< Mod N.CNcomment:RSA参数N。CNend */
82     hi_u8 exp_e[RSA_EXP_E_LEN]; /**< Exp E.CNcomment:RSA参数E。CNend */
83     hi_u8 padding[28];          /**< Padding field:28.CNcomment:填充字段。CNend */
84 } upg_rsa_key;
85 
86 typedef struct {
87     hi_u8 sign[RSA_2048_LEN]; /**< RSA signature.CNcomment:RSA签名。CNend */
88 } upg_rsa_sign;
89 
90 typedef struct {
91     hi_u8 px[ECC_32_BYTES]; /**< Pubkey x.CNcomment:ECC密钥x。CNend */
92     hi_u8 py[ECC_32_BYTES]; /**< Pubkey y.CNcomment:ECC密钥y。CNend */
93     hi_u8 padding[224];     /**< Padding field:224. CNcomment:填充字段。CNend */
94 } upg_ecc_key;
95 
96 typedef struct {
97     hi_u8 r[ECC_32_BYTES]; /**< Signature of ECC.CNcomment:ECC签名信息r。CNend */
98     hi_u8 s[ECC_32_BYTES]; /**< Signature of ECC.CNcomment:ECC签名信息s。CNend */
99     hi_u8 padding[192];    /**< Padding field:192.CNcomment:填充字段。CNend */
100 } upg_ecc_sign;
101 
102 typedef struct {
103     hi_u8 padding[288]; /**< Padding field:288.CNcomment:填充字段。CNend */
104 } upg_sha256_key;
105 
106 typedef struct {
107     hi_u8 check_sum[SHA_256_LEN]; /**< Hash value of SHA256.CNcomment:SHA256算法哈希值。CNend */
108     hi_u8 padding[224];           /**< Padding field:224.CNcomment:填充字段。CNend */
109 } upg_sha256_sign;
110 
111 /**
112  * @ingroup upg
113  *
114  * Parameters of user-defined fields.CNcomment:用户自定义字段。CNend
115  */
116 typedef struct {
117     hi_u8 reserved[32];    /**< 32:Reserved for user.CNcomment:用户自定义字段。CNend */
118 }hi_upg_user_info;
119 
120 typedef struct {
121     hi_u32 image_id;       /**< Identity of upgrade file Key Area.CNcomment:升级文件魔术字。CNend */
122     hi_u32 struct_version; /**< The structure of upgrade file version.CNcomment:升级文件结构体版本号。CNend */
123     hi_u32 section_offset; /**< Offset of upgrade Section.CNcomment:Section段偏移。CNend */
124     hi_u32 section_len;    /**< Length of upgrade Section.CNcomment:Section段长度。CNend */
125     hi_upg_user_info user_info; /**< Reserved for user.CNcomment:用户自定义字段。CNend */
126     hi_u8 file_type;       /**< Upgrade file type:0xF0: kernel file; 0xE1: boot file.CNcomment:升级文件类型。CNend */
127     hi_u8 file_version;    /**< File Version, for anti-rollback. [0, 16] for boot file and [0, 48] for kernel file.
128                                 CNcomment:升级文件结构体版本号。CNend */
129     hi_u8 encrypt_flag;    /**< 0x42: Section Area is not encrypted; other: Section Area is encrypted.
130                                 CNcomment:Section段加密标志。CNend */
131     hi_u8 file_attr;            /**< File Attributes.CNcomment:文件属性。CNend */
132     hi_u32 file_len;            /**< Entire file length.CNcomment:升级文件长度。CNend */
133     hi_u32 key_len;             /**< Length of Key(288Bytes).True length:RSA2048: 272 Bytes, ECDSA: 64Bytes.
134                                      CNcomment:密钥长度。CNend */
135     sign_alg_param param;       /**< Parma of the signature algorithm.CNcomment:签名算法参数。CNend */
136     hi_u8 aes_key[IV_BYTE_LEN]; /**< Part of key factor.CNcomment:AES密钥。CNend */
137     hi_u8 aes_iv[IV_BYTE_LEN];  /**< The IV (AES-256 CBC-mode) to encrypt Section.CNcomment:AES初始向量。CNend */
138 } hi_upg_common_head;
139 
140 typedef struct {
141     hi_u32 image_id;           /**< Identity of upgrade file Key Area.CNcomment:升级文件魔术字。CNend */
142     hi_u32 struct_version;     /**< The structure of upgrade file Section Area.CNcomment:升级文件结构体版本号。CNend */
143     sign_alg_param param;      /**< The signature algorithm.CNcomment:签名算法参数。CNend */
144     hi_u8 section_count;       /**< The number of sections.CNcomment:Section段个数。CNend */
145     hi_u8 reserved[27];        /**< 27 bytes reserved.CNcomment:保留字段。CNend */
146     hi_u8 section0_compress;   /**< Whether section 0 is compressed.CNcomment:Section0是否压缩。CNend */
147     hi_u8 pad0[3];             /**< 3 bytes padding.CNcomment:填充字段。CNend */
148     hi_u32 section0_offset;    /**< Offset of Section0.CNcomment:Section0段偏移。CNend */
149     hi_u32 section0_len;       /**< Length of Section0, aligned to 16 bytes.CNcomment:Section0段长度。CNend */
150     hi_u8 section1_compress;   /**< Whether section 1 is compressed.CNcomment:Section1是否压缩Nend */
151     hi_u8 pad1[3];             /**< 3 bytes padding.CNcomment:填充字段。CNend */
152     hi_u32 section1_offset;    /**< Offset of Section1.CNcomment:Section1段偏移。CNend */
153     hi_u32 section1_len;       /**< Length of Section1, aligned to 16 bytes.CNcomment:Section1段长度。CNend */
154 } hi_upg_section_head;
155 
156 typedef struct {
157     upg_rsa_key key;    /**< Key of rsa.CNcomment:RSA算法密钥。CNend */
158     upg_rsa_sign sign;  /**< Sign of rsa.CNcomment:RSA算法签名。CNend */
159 } hi_upg_rsa_alg;
160 
161 typedef struct {
162     upg_ecc_key key;    /**< Key of ecc.CNcomment:ECC算法密钥。CNend */
163     upg_ecc_sign sign;  /**< Sign of ecc.CNcomment:ECC算法签名。CNend */
164 } hi_upg_ecc_alg;
165 
166 typedef struct {
167     upg_sha256_key key;    /**< Padding field.CNcomment:填充字段。CNend */
168     upg_sha256_sign sign;  /**< Hash of sha256.CNcomment:SHA256算法哈希值。CNend */
169 } hi_upg_sha256_alg;
170 
171 typedef struct {
172     union {
173         upg_rsa_key rsa;    /**< Key of rsa.CNcomment:rsa密钥。CNend */
174         upg_ecc_key ecc;    /**< Key of ecc.CNcomment:ecc密钥。CNend */
175         upg_sha256_key sha; /**< Padding field.CNcomment:填充字段。CNend */
176     } key;
177 }hi_upg_key;
178 
179 typedef struct {
180     union {
181         upg_rsa_sign rsa; /**< Sign of rsa.CNcomment:rsa签名。CNend */
182         upg_ecc_sign ecc; /**< Sign of ecc.CNcomment:ecc签名。CNend */
183         upg_sha256_sign sha; /**< Hash of sha256.CNcomment:SHA256哈希。CNend */
184     } sign;
185 }hi_upg_sign;
186 
187 typedef struct {
188     hi_upg_common_head common; /**< Common head of upg file.CNcomment:公共文件头。CNend */
189     union {
190         hi_upg_rsa_alg rsa; /**< Key and sign of RSA.CNcomment:RSA密钥及签名信息。CNend */
191         hi_upg_ecc_alg ecc; /**< Key and sign of ECC.CNcomment:ECC密钥及签名信息。CNend */
192         hi_upg_sha256_alg sha;/**< Key and sign of SHA256.CNcomment:SHA256密钥及签名信息。CNend */
193     } sign_alg;
194 } hi_upg_head;
195 
196 typedef struct {
197     hi_upg_common_head common; /**< Common head of upg file.CNcomment:公共文件头。CNend */
198     union {
199         hi_upg_rsa_alg rsa; /**< Key and sign of RSA.CNcomment:RSA密钥及签名信息。CNend */
200         hi_upg_ecc_alg ecc; /**< Key and sign of ECC.CNcomment:ECC密钥及签名信息。CNend */
201         hi_upg_sha256_alg sha; /**< Key and sign of SHA256.CNcomment:SHA256密钥及签名信息。CNend */
202     } sign_alg;
203     hi_upg_section_head section; /**< Head of code section.CNcomment:Code段头信息。CNend */
204     hi_upg_sign section_sign;  /**< Sign of code section.CNcomment:Code段签名信息。CNend */
205 } hi_upg_file_head;
206 
207 #endif
208 
209