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