blob: e2388e3fef86116da2cb990a8a55abdf7610f081 [file] [log] [blame]
Peng Fan0aef2f22019-09-23 10:18:44 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
Tom Rini5ab32a02024-04-30 07:35:49 -06006#include <config.h>
Peng Fan0aef2f22019-09-23 10:18:44 +08007#include <errno.h>
Sean Anderson952ed672023-10-14 16:47:44 -04008#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Peng Fan0aef2f22019-09-23 10:18:44 +080011#include <asm/io.h>
12#include <mmc.h>
13#include <spi_flash.h>
Tom Rinia3a142c2023-03-09 11:22:08 -050014#include <spl.h>
Peng Fan0aef2f22019-09-23 10:18:44 +080015#include <nand.h>
Peng Fan0aef2f22019-09-23 10:18:44 +080016#include <asm/arch/sys_proto.h>
17#include <asm/mach-imx/boot_mode.h>
18
19#define MMC_DEV 0
20#define QSPI_DEV 1
21#define NAND_DEV 2
22#define QSPI_NOR_DEV 3
Ye Li7a71c612021-08-07 16:00:39 +080023#define ROM_API_DEV 4
Peng Fan0aef2f22019-09-23 10:18:44 +080024
Peng Fan7e7d36f2023-06-15 18:09:17 +080025/* The unit of second image offset number which provision by the fuse bits */
26#define SND_IMG_OFF_UNIT (0x100000UL)
27
28/*
29 * If num = 0, off = (2 ^ 2) * 1MB
30 * else If num = 2, off = (2 ^ 0) * 1MB
31 * else off = (2 ^ num) * 1MB
32 */
33#define SND_IMG_NUM_TO_OFF(num) \
34 ((1UL << ((0 == (num)) ? 2 : (2 == (num)) ? 0 : (num))) * SND_IMG_OFF_UNIT)
35
36#define GET_SND_IMG_NUM(fuse) (((fuse) >> 24) & 0x1F)
37
38#if defined(CONFIG_IMX8QM)
39#define FUSE_IMG_SET_OFF_WORD 464
40#elif defined(CONFIG_IMX8QXP)
41#define FUSE_IMG_SET_OFF_WORD 720
42#endif
43
Ye Li7a71c612021-08-07 16:00:39 +080044int get_container_size(ulong addr, u16 *header_length)
Peng Fan0aef2f22019-09-23 10:18:44 +080045{
46 struct container_hdr *phdr;
47 struct boot_img_t *img_entry;
48 struct signature_block_hdr *sign_hdr;
49 u8 i = 0;
50 u32 max_offset = 0, img_end;
51
52 phdr = (struct container_hdr *)addr;
Sean Andersonc5126682023-10-14 16:47:43 -040053 if (!valid_container_hdr(phdr)) {
Peng Fan0aef2f22019-09-23 10:18:44 +080054 debug("Wrong container header\n");
55 return -EFAULT;
56 }
57
Ye Li7a71c612021-08-07 16:00:39 +080058 max_offset = phdr->length_lsb + (phdr->length_msb << 8);
59 if (header_length)
60 *header_length = max_offset;
Peng Fan0aef2f22019-09-23 10:18:44 +080061
62 img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr));
63 for (i = 0; i < phdr->num_images; i++) {
64 img_end = img_entry->offset + img_entry->size;
65 if (img_end > max_offset)
66 max_offset = img_end;
67
68 debug("img[%u], end = 0x%x\n", i, img_end);
69
70 img_entry++;
71 }
72
73 if (phdr->sig_blk_offset != 0) {
74 sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset);
75 u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8);
76
77 if (phdr->sig_blk_offset + len > max_offset)
78 max_offset = phdr->sig_blk_offset + len;
79
80 debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len);
81 }
82
83 return max_offset;
84}
85
Ye Li7a71c612021-08-07 16:00:39 +080086static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length)
Peng Fan0aef2f22019-09-23 10:18:44 +080087{
88 u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT);
89 int ret = 0;
90
91 if (!buf) {
92 printf("Malloc buffer failed\n");
93 return -ENOMEM;
94 }
95
Simon Glassb58bfe02021-08-08 12:20:09 -060096#ifdef CONFIG_SPL_MMC
Peng Fan0aef2f22019-09-23 10:18:44 +080097 if (dev_type == MMC_DEV) {
98 unsigned long count = 0;
99 struct mmc *mmc = (struct mmc *)dev;
100
101 count = blk_dread(mmc_get_blk_desc(mmc),
102 offset / mmc->read_bl_len,
103 CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len,
104 buf);
105 if (count == 0) {
106 printf("Read container image from MMC/SD failed\n");
107 return -EIO;
108 }
109 }
110#endif
111
112#ifdef CONFIG_SPL_SPI_LOAD
113 if (dev_type == QSPI_DEV) {
114 struct spi_flash *flash = (struct spi_flash *)dev;
115
116 ret = spi_flash_read(flash, offset,
117 CONTAINER_HDR_ALIGNMENT, buf);
118 if (ret != 0) {
119 printf("Read container image from QSPI failed\n");
120 return -EIO;
121 }
122 }
123#endif
124
125#ifdef CONFIG_SPL_NAND_SUPPORT
126 if (dev_type == NAND_DEV) {
127 ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT,
128 buf);
129 if (ret != 0) {
130 printf("Read container image from NAND failed\n");
131 return -EIO;
132 }
133 }
134#endif
135
136#ifdef CONFIG_SPL_NOR_SUPPORT
137 if (dev_type == QSPI_NOR_DEV)
138 memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT);
139#endif
140
Ye Li7a71c612021-08-07 16:00:39 +0800141#ifdef CONFIG_SPL_BOOTROM_SUPPORT
142 if (dev_type == ROM_API_DEV) {
143 ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf);
144 if (!ret) {
145 printf("Read container image from ROM API failed\n");
146 return -EIO;
147 }
148 }
149#endif
150
151 ret = get_container_size((ulong)buf, header_length);
Peng Fan0aef2f22019-09-23 10:18:44 +0800152
153 free(buf);
154
155 return ret;
156}
157
Peng Fan7e7d36f2023-06-15 18:09:17 +0800158static bool check_secondary_cnt_set(unsigned long *set_off)
159{
160#if IS_ENABLED(CONFIG_ARCH_IMX8)
161 int ret;
162 u8 set_id = 1;
163 u32 fuse_val = 0;
164
165 if (!(is_imx8qxp() && is_soc_rev(CHIP_REV_B))) {
166 ret = sc_misc_get_boot_container(-1, &set_id);
167 if (ret)
168 return false;
169 /* Secondary boot */
170 if (set_id == 2) {
171 ret = sc_misc_otp_fuse_read(-1, FUSE_IMG_SET_OFF_WORD, &fuse_val);
172 if (!ret) {
173 if (set_off)
174 *set_off = SND_IMG_NUM_TO_OFF(GET_SND_IMG_NUM(fuse_val));
175 return true;
176 }
177 }
178 }
179#endif
180
181 return false;
182}
183
Peng Fan0aef2f22019-09-23 10:18:44 +0800184static unsigned long get_boot_device_offset(void *dev, int dev_type)
185{
Peng Fan7e7d36f2023-06-15 18:09:17 +0800186 unsigned long offset = 0, sec_set_off = 0;
187 bool sec_boot = false;
188
189 if (dev_type == ROM_API_DEV) {
190 offset = (unsigned long)dev;
191 return offset;
192 }
193
194 sec_boot = check_secondary_cnt_set(&sec_set_off);
195 if (sec_boot)
196 printf("Secondary set selected\n");
197 else
198 printf("Primary set selected\n");
Peng Fan0aef2f22019-09-23 10:18:44 +0800199
200 if (dev_type == MMC_DEV) {
201 struct mmc *mmc = (struct mmc *)dev;
202
203 if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) {
Peng Fan7e7d36f2023-06-15 18:09:17 +0800204 offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET;
Peng Fan0aef2f22019-09-23 10:18:44 +0800205 } else {
206 u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
207
208 if (part == 1 || part == 2) {
209 if (is_imx8qxp() && is_soc_rev(CHIP_REV_B))
210 offset = CONTAINER_HDR_MMCSD_OFFSET;
211 else
212 offset = CONTAINER_HDR_EMMC_OFFSET;
213 } else {
Peng Fan7e7d36f2023-06-15 18:09:17 +0800214 offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET;
Peng Fan0aef2f22019-09-23 10:18:44 +0800215 }
216 }
217 } else if (dev_type == QSPI_DEV) {
Peng Fan7e7d36f2023-06-15 18:09:17 +0800218 offset = sec_boot ? (sec_set_off + CONTAINER_HDR_QSPI_OFFSET) :
219 CONTAINER_HDR_QSPI_OFFSET;
Peng Fan0aef2f22019-09-23 10:18:44 +0800220 } else if (dev_type == NAND_DEV) {
Peng Fan7e7d36f2023-06-15 18:09:17 +0800221 offset = sec_boot ? (sec_set_off + CONTAINER_HDR_NAND_OFFSET) :
222 CONTAINER_HDR_NAND_OFFSET;
Peng Fan0aef2f22019-09-23 10:18:44 +0800223 } else if (dev_type == QSPI_NOR_DEV) {
224 offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000;
Peng Fan7e7d36f2023-06-15 18:09:17 +0800225 } else {
226 printf("Not supported dev_type: %d\n", dev_type);
Peng Fan0aef2f22019-09-23 10:18:44 +0800227 }
228
Peng Fan7e7d36f2023-06-15 18:09:17 +0800229 debug("container set offset 0x%lx\n", offset);
230
Peng Fan0aef2f22019-09-23 10:18:44 +0800231 return offset;
232}
233
234static int get_imageset_end(void *dev, int dev_type)
235{
236 unsigned long offset1 = 0, offset2 = 0;
237 int value_container[2];
Ye Li7a71c612021-08-07 16:00:39 +0800238 u16 hdr_length;
Peng Fan0aef2f22019-09-23 10:18:44 +0800239
240 offset1 = get_boot_device_offset(dev, dev_type);
241 offset2 = CONTAINER_HDR_ALIGNMENT + offset1;
242
Ye Li7a71c612021-08-07 16:00:39 +0800243 value_container[0] = get_dev_container_size(dev, dev_type, offset1, &hdr_length);
Peng Fan0aef2f22019-09-23 10:18:44 +0800244 if (value_container[0] < 0) {
245 printf("Parse seco container failed %d\n", value_container[0]);
246 return value_container[0];
247 }
248
249 debug("seco container size 0x%x\n", value_container[0]);
250
Ye Li7a71c612021-08-07 16:00:39 +0800251 value_container[1] = get_dev_container_size(dev, dev_type, offset2, &hdr_length);
Peng Fan0aef2f22019-09-23 10:18:44 +0800252 if (value_container[1] < 0) {
253 debug("Parse scu container failed %d, only seco container\n",
254 value_container[1]);
255 /* return seco container total size */
256 return value_container[0] + offset1;
257 }
258
259 debug("scu container size 0x%x\n", value_container[1]);
260
261 return value_container[1] + offset2;
262}
263
264#ifdef CONFIG_SPL_SPI_LOAD
Ye Lie8f97df2024-03-28 18:50:55 +0800265unsigned int spl_spi_get_uboot_offs(struct spi_flash *flash)
Peng Fan0aef2f22019-09-23 10:18:44 +0800266{
267 int end;
268
269 end = get_imageset_end(flash, QSPI_DEV);
270 end = ROUND(end, SZ_1K);
271
272 printf("Load image from QSPI 0x%x\n", end);
273
274 return end;
275}
276#endif
277
Simon Glassb58bfe02021-08-08 12:20:09 -0600278#ifdef CONFIG_SPL_MMC
Marek Vasutf9a921e2023-10-16 18:16:12 +0200279unsigned long arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
280 unsigned long raw_sect)
Peng Fan0aef2f22019-09-23 10:18:44 +0800281{
282 int end;
283
284 end = get_imageset_end(mmc, MMC_DEV);
285 end = ROUND(end, SZ_1K);
286
287 printf("Load image from MMC/SD 0x%x\n", end);
288
289 return end / mmc->read_bl_len;
290}
Peng Fan7e7d36f2023-06-15 18:09:17 +0800291
292int spl_mmc_emmc_boot_partition(struct mmc *mmc)
293{
294 int part;
295
296 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
297 if (part == 1 || part == 2) {
298 unsigned long sec_set_off = 0;
299 bool sec_boot = false;
300
301 sec_boot = check_secondary_cnt_set(&sec_set_off);
302 if (sec_boot)
303 part = (part == 1) ? 2 : 1;
304 } else if (part == 7) {
305 part = 0;
306 }
307
308 return part;
309}
Peng Fan0aef2f22019-09-23 10:18:44 +0800310#endif
311
312#ifdef CONFIG_SPL_NAND_SUPPORT
313uint32_t spl_nand_get_uboot_raw_page(void)
314{
315 int end;
316
317 end = get_imageset_end((void *)NULL, NAND_DEV);
318 end = ROUND(end, SZ_16K);
319
320 printf("Load image from NAND 0x%x\n", end);
321
322 return end;
323}
324#endif
325
326#ifdef CONFIG_SPL_NOR_SUPPORT
327unsigned long spl_nor_get_uboot_base(void)
328{
329 int end;
330
331 /* Calculate the image set end,
Tom Rini6a5dccc2022-11-16 13:10:41 -0500332 * if it is less than CFG_SYS_UBOOT_BASE(0x8281000),
333 * we use CFG_SYS_UBOOT_BASE
Peng Fan0aef2f22019-09-23 10:18:44 +0800334 * Otherwise, use the calculated address
335 */
336 end = get_imageset_end((void *)NULL, QSPI_NOR_DEV);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500337 if (end <= CFG_SYS_UBOOT_BASE)
338 end = CFG_SYS_UBOOT_BASE;
Peng Fan0aef2f22019-09-23 10:18:44 +0800339 else
340 end = ROUND(end, SZ_1K);
341
342 printf("Load image from NOR 0x%x\n", end);
343
344 return end;
345}
346#endif
Ye Li7a71c612021-08-07 16:00:39 +0800347
348#ifdef CONFIG_SPL_BOOTROM_SUPPORT
Ye Li479fd4a2021-08-07 16:01:08 +0800349u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
350{
351 return image_offset;
352}
353
Ye Li7a71c612021-08-07 16:00:39 +0800354ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
355{
356 ulong end;
357
Ye Li479fd4a2021-08-07 16:01:08 +0800358 image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev);
359
Ye Li7a71c612021-08-07 16:00:39 +0800360 end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV);
361 end = ROUND(end, SZ_1K);
362
363 printf("Load image from 0x%lx by ROM_API\n", end);
364
365 return end;
366}
367#endif