blob: 4614eb280d297c83a521fa819f13bd5da8e0ec48 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * (C) Copyright 2016 ZXIC Inc.
3 */
4#include <common.h>
5#include <asm/io.h>
6#include <asm/string.h>
7
8#include <image.h>
9#include <linux/byteorder/generic.h>
10#include <secure_verify.h>
11
12#include "flash.h"
13#include <bbt.h>
xf.li6c8fc1e2023-08-12 00:11:09 -070014#include "pub_flags.h"
lh9ed821d2023-04-07 01:36:19 -070015
16/*
17 ******************************************************************************
18 * Function:
19 * Description:
20 * Parameters:
21 * Input:
22 * Output:
23 * Returns:
24 * Others:
25 *******************************************************************************
26 */
27uint32_t page_align(uint32_t offset)
28{
29 uint32_t page_size = flash.page_size;
30 if(offset & (page_size - 1))
31 {
32 offset &= (~(page_size - 1));
33 offset += page_size;
34 }
35 return offset;
36}
37
38
39/*
40 ******************************************************************************
41 * Function:
42 * Description:
43 * Parameters:
44 * Input:
45 * Output:
46 * Returns:
47 * Others:
48 *******************************************************************************
49 */
50uint32_t find_partition_para(partition_entry_t *entry,
51 uint32_t entrys,
52 uint8_t *name)
53{
54 partition_entry_t *p_entry = entry;
55 uint32_t entry_nums = entrys;
56 while(entry_nums--)
57 {
58 if(memcmp( p_entry->part_name, name, strlen(name)) == 0)
59 return p_entry->part_offset;
60 p_entry++;
61 }
62 return -1;
63}
64
65/*
66 ******************************************************************************
67 * Function:
68 * Description:
69 * Parameters:
70 * Input:
71 * Output:
72 * Returns:
73 * Others:
74 *******************************************************************************
75 */
76int read_image_part_offset(uint8_t *name, uint32_t *offset)
77{
78 int ret = 0;
79 uint32_t part_size = 0;
80
81 if(flash.flash_type == NOR_BOOT)
82 {
83 part_size = 8*flash.page_size;
84 }
85 else
86 {
87 part_size = flash.page_size;
88 }
89
90 ret = flash.read(0x2000, part_size, CFG_TEMP_ADDR);
91 if(ret != 0)
92 {
93 return -1;
94 }
95
96 partition_table_t *part = (partition_table_t *)CFG_TEMP_ADDR;
97 partition_entry_t *entry = &part->table[0];
98 uint32_t entrys = part->entrys;
99
100 if(part->magic != CFG_PARTITION_MAGIC)
101 {
102 return -1;
103 }
104
105 *offset = find_partition_para(entry, entrys, name);
106 if(*offset == -1 )
107 {
108 return -1;
109 }
110
111 return 0;
112}
113
114//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700115#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700116int read_flags_image(uint8_t *name)
117{
xf.li6c8fc1e2023-08-12 00:11:09 -0700118 T_FLAGS_INFO *fotaFlag;
lh9ed821d2023-04-07 01:36:19 -0700119 uint32_t uiPageSize = 0;
120 uint32_t flags_offset = 0;
121 uint32_t off = 0;
122 uint32_t flags_size = 0x4E0000;/*flags·ÖÇø*/
123 uint32_t blockNum = 0;
124 int32_t flag_one = 0;
125 uint32_t work_area_offset = 0;
126 uint32_t backup_area_offset = 0;
127 int32_t ret = 0;
xf.li6c8fc1e2023-08-12 00:11:09 -0700128 int32_t fota_size = sizeof(T_FLAGS_INFO);
lh9ed821d2023-04-07 01:36:19 -0700129 int32_t block_size = flash.block_size;
130
131 fota_size = page_align(fota_size);
132
133 ret = read_image_part_offset(name, &flags_offset);
134 if(ret != 0)
135 {
136 printf("flags partition offset fail.\n");
137 return -1;
138 }
139
140 /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
141 for (off = flags_offset; off < flags_offset+flags_size; off += flash.block_size)
142 {
143 if (!(nand_block_isbad(off)))
144 {
145 blockNum += 1;
146 }
147
148 if((blockNum == 1) && (flag_one == 0))
149 {
150 work_area_offset = off;
151 flag_one = 1;
152 }
153
154 else if(blockNum == 2)
155 {
156 backup_area_offset = off;
157 break;
158 }
159 }
160
161 if(blockNum < 2)
162 {
163 printf("flags partition have not enough space!\n");
164
165 return -1;
166 }
167
168 ret = flash.read(work_area_offset, fota_size, CFG_TEMP_ADDR);
169 if(ret != 0)
170 {
171 printf("read flags work partition err.\n");
172
173 return -1;
174 }
175
xf.li6c8fc1e2023-08-12 00:11:09 -0700176 fotaFlag = (T_FLAGS_INFO *)(CFG_TEMP_ADDR);
lh9ed821d2023-04-07 01:36:19 -0700177
xf.li6c8fc1e2023-08-12 00:11:09 -0700178 if(fotaFlag->magic_start != FLAGS_MAGIC || fotaFlag->magic_end != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -0700179 {
180 ret = flash.read(backup_area_offset, fota_size, CFG_TEMP_ADDR);
181 if(ret != 0)
182 {
183 printf("read flags backup partition err.\n");
184
185 return -1;
186 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700187 fotaFlag = (T_FLAGS_INFO *)(CFG_TEMP_ADDR);
188 if(fotaFlag->magic_start != FLAGS_MAGIC || fotaFlag->magic_end != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -0700189 {
190 printf("flags magic err.\n");
191 return -1;
192 }
193 }
194
195 return 0;
196
197}
198#endif
199/*
200 ****************************************************************************
201 * Function:
202 * Description:
203 * Parameters:
204 * Input:
205 * Output:
206 * Returns:
207 * Others:
208 *****************************************************************************
209 */
210int read_uboot_image(uint8_t *name, uint32_t *uboot_entry_point)
211{
212 /*
213 * +----------------------------------------------------------------+
214 * | sImageHeader | image_header_t | u-boot.bin |
215 * +----------------------------------------------------------------+
216 * | 384 Bytes | 64 Bytes | xxx Bytes |
217 *
218 */
219
220 image_header_t *psImgHdrOld = NULL;
221
222 uint32_t uiPageSize = 0;
223
224 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
xf.liaa4d92f2023-09-13 00:18:58 -0700225 uint32_t uiImgHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700226
227 uint32_t uiUBootSize = 0;
228 uint32_t uiUBootLoadAddr = 0;
229 uint32_t uiUBootIsReadSize = 0;
230
231 int32_t ret = 0;
232 uint32_t uboot_offset = 0;
233
234
235
236 if(flash.flash_type == NOR_BOOT)
237 {
238 uiPageSize = 8*flash.page_size;
239 }
240 else
241 {
242 uiPageSize = flash.page_size;
243 }
244
245 ret = read_image_part_offset(name, &uboot_offset);
246 if(ret != 0)
247 {
248 printf("offset fail.\n");
249 return -1;
250 }
251
252 ret = flash.read(uboot_offset, uiPageSize, CFG_TEMP_ADDR);
253 if(ret != 0)
254 {
255 printf("image header err.\n");
256 return -1;
257 }
258
259 psImgHdrOld = (image_header_t *)(CFG_TEMP_ADDR + uiImgHdrSizeNew);
260 uiUBootSize = ___htonl(psImgHdrOld->ih_size);
261 uiUBootLoadAddr = ___htonl(psImgHdrOld->ih_load);
262 *uboot_entry_point = ___htonl(psImgHdrOld->ih_ep);
263
264 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
265 {
266 printf("magic err.\n");
267 return -1;
268 }
269
270 uiUBootIsReadSize = uiPageSize - uiImgHdrSizeOld - uiImgHdrSizeNew;
271 uiUBootSize -= uiUBootIsReadSize;
272 uiUBootSize = page_align(uiUBootSize);
273
274 memcpy(uiUBootLoadAddr - uiImgHdrSizeOld,
275 CFG_TEMP_ADDR + uiImgHdrSizeNew,
276 uiUBootIsReadSize + uiImgHdrSizeOld);
277
278 ret = flash.read(uboot_offset + uiPageSize,
279 uiUBootSize,
280 uiUBootLoadAddr + uiUBootIsReadSize);
281 if(ret != 0)
282 {
283 printf("image err.\n");
284 return -1;
285 }
286
287 ret = SecureVerify(CFG_TEMP_ADDR);
288 if(ret != 0)
289 {
290 printf("Secure verify fail!\n");
291 return -1;
292 }
293
294 return 0;
295}
296
297/*
298 ******************************************************************************
299 * Function:
300 * Description:
301 * Parameters:
302 * Input:
303 * Output:
304 * Returns:
305 * Others:
306 *******************************************************************************
307 */
308 int nand_read_m0(uint32_t *m0_entry_point)
309{
310 int remap = 0;
311 image_header_t *header = NULL;
312 uint32_t image_header_size = sizeof(image_header_t);
313 uint32_t m0_size = 0;
314 uint32_t m0_load_addr = 0;
315 /*
316 * +---------------------------------------------------------+
317 * | image_header_t | m0 |
318 * +---------------------------------------------------------+
319 m0.bin
320 */
321
322 /*1¡¢µÈ´ýBOOT°áÔËM0°æ±¾µ½CONFIG_SYS_SDRAM_TEMP_BASEÖÐ*/
323 writel(0, M0_IMAGE_READY_FLAG_ADDR);
324 while(!readl(M0_IMAGE_READY_FLAG_ADDR));
325
326 /*2¡¢M0µÄ0µØÖ·ÖØÓ³Éä*/
327 remap = readl(0x140000);
328 remap |= 0x800000;
329 writel(remap,0x140000);
330
331 /*3¡¢½âÎöM0µÄ°æ±¾Í·£¬»ñÈ¡ÔËÐеØÖ·ÒÔ¼°´óС*/
xf.liaa4d92f2023-09-13 00:18:58 -0700332 header = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + sizeof(sImageNewHeader));
lh9ed821d2023-04-07 01:36:19 -0700333 m0_size = ___htonl(header->ih_size); /* m0.bin */
334 m0_load_addr = ___htonl(header->ih_load);
335 *m0_entry_point = ___htonl(header->ih_ep);
336
337 if(___htonl(header->ih_magic) != IH_MAGIC)
338 {
339 return -1;
340 }
341
342 /*4¡¢½«M0µÄ°æ±¾´ÓIRAM0¿½±´µ½load_addr*/
xf.liaa4d92f2023-09-13 00:18:58 -0700343 memcpy(m0_load_addr, CONFIG_SYS_SDRAM_TEMP_BASE + sizeof(sImageNewHeader)+ image_header_size, m0_size);
lh9ed821d2023-04-07 01:36:19 -0700344
345 /*5¡¢ÐÞ¸ÄIRAM±ê¼Ç£¬Ê¹BOOT¼ÌÐøÍùÏÂÖ´ÐÐ*/
346 writel(0, M0_IMAGE_READY_FLAG_ADDR);
347
348 return 0;
349}
350
351