• 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 _UPG_COMMON_H_
17 #define _UPG_COMMON_H_
18 
19 #include <hi_upg_api.h>
20 #include <hi_upg_file.h>
21 #include <hi_sal.h>
22 #include <hi_ft_nv.h>
23 #include <hi_nvm.h>
24 #include <hi_nv.h>
25 #include <hi_partition_table.h>
26 #include <hi_mem.h>
27 #include <hi_flash.h>
28 #include <hi_efuse.h>
29 #include <hi_sem.h>
30 #include <hi_stdlib.h>
31 #include <hi_task.h>
32 
33 #ifdef UPG_DEBUG
34 #include <hi_early_debug.h>
35 #define upg_print(ftm...) \
36     do {                  \
37         printf(ftm);      \
38     } while (0);
39 #else
40 #define upg_print(ftm...)
41 #endif
42 
43 #define HI_UPG_MODE_NORMAL  0      /* normal mode */
44 #define HI_UPG_MODE_UPGRADE 1      /* upgrade mode */
45 #define HI_UPG_MODE_UPGRADE_WAIT 2 /* asynchronous upgrade mode */
46 
47 #define UPG_FLASH_RETRY_TIMES 3
48 #define UPG_MAX_BACKUP_CNT    3
49 
50 #define UPG_FLASH_BLOCK_SIZE 0x1000
51 #define UPG_NV_KERNEL_OFFSET 0x1000
52 
53 #define PRODUCT_FLASH_MAX_SIZE 0x200000
54 
55 #define BIT_U8   8
56 #define BIT_U16  16
57 #define MASK_U16 0xFFFF
58 
59 typedef enum {
60     HI_MODE_PKCS_V15 = 0x00,
61     HI_MODE_PKCS_PSS = 0x01,
62     HI_MODE_ECC = 0x10,
63 } hi_padding_mode;
64 
65 typedef struct {
66     hi_bool is_init;
67     hi_bool is_upg_process;
68     hi_bool check_head_flag;
69     hi_bool transmit_finish_flag;
70     hi_u32 cache_size; /* cached file size */
71     hi_u32 file_addr;  /* flash addr of cached file */
72     hi_u32 upg_sem;
73     hi_u32 rsa_key_addr; /* flash addr of rsa key */
74     hi_u32 ecc_key_addr; /* flash addr of ecc key */
75     hi_u32 cur_kernel_size; /* current kernel size */
76     hi_upg_common_head common_head;
77 } upg_ctx;
78 
79 typedef hi_u8 srv_bit; /* value 0 or 1 */
80 #define SRV_BIT_LOW  0
81 #define SRV_BIT_HIGH 1
82 
83 /*
84     i must 0~7 b must 0 or 1 x must a character
85     high ---------------->  low
86     **************************
87      7  6  5  4  3  2   1   0
88     **************************
89  */
90 #define bit_set(x, i, b) ((x) = (hi_u8)((b) ? ((1 << (i)) | (x)) : ((~(hi_u8)(1 << (i))) & (x))))
91 #define bit_get(x, i)    (((1 << (i)) & (x)) ? 1 : 0)
92 
93 #define upg_mem_free(sz)                  \
94     do {                                  \
95         if ((sz) != HI_NULL) {            \
96             hi_free(HI_MOD_ID_UPG, (sz)); \
97         }                                 \
98         (sz) = HI_NULL;                   \
99     } while (0)
100 
101 #define upg_align_128bit(x) ((((x) + (BIT_U16) - 1) / (BIT_U16)) * (BIT_U16))
102 #define upg_align_pre(val, a)    ((val) & (~((a) - 1)))
103 #define upg_align_next(val, a)   ((((val) + ((a) - 1)) & (~((a) - 1))))
104 
105 hi_u32 upg_get_efuse_code_ver(hi_u8 *ver);
106 hi_u32 upg_get_efuse_boot_ver(hi_u8 *ver);
107 hi_u32 upg_get_rsa_key_from_boot(hi_u8 *key, hi_u32 key_len);
108 hi_u32 upg_get_ecc_key_from_boot(hi_u8 *key, hi_u32 key_len);
109 hi_u32 upg_get_max_file_len(hi_u8 file_type, hi_u32 *file_len);
110 hi_bool upg_tool_bit_test(const hi_u8 *data, hi_u16 pos, hi_u16 data_len);
111 hi_void upg_tool_bit_set(hi_u8 *data, hi_u16 pos, hi_u8 val);
112 hi_u32 upg_get_start_up_cfg(hi_nv_ftm_startup_cfg *cfg);
113 hi_void upg_clear_contset(hi_u8 *content, hi_u32 content_len);
114 
115 #endif /* _UPG_COMMON_H_ */
116