blob: 5b059a64292ec9062d01566b6290cd324d40a2b5 [file] [log] [blame]
Peng Fand1c07782019-09-23 10:18:44 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <common.h>
7#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Peng Fand1c07782019-09-23 10:18:44 +080010#include <asm/io.h>
11#include <mmc.h>
12#include <spi_flash.h>
Tom Rini16d82d72023-03-09 11:22:08 -050013#include <spl.h>
Peng Fand1c07782019-09-23 10:18:44 +080014#include <nand.h>
Ye Li31f00852021-08-07 16:00:37 +080015#include <asm/mach-imx/image.h>
Peng Fand1c07782019-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 Li6f3858d2021-08-07 16:00:39 +080023#define ROM_API_DEV 4
Peng Fand1c07782019-09-23 10:18:44 +080024
Ye Li6f3858d2021-08-07 16:00:39 +080025int get_container_size(ulong addr, u16 *header_length)
Peng Fand1c07782019-09-23 10:18:44 +080026{
27 struct container_hdr *phdr;
28 struct boot_img_t *img_entry;
29 struct signature_block_hdr *sign_hdr;
30 u8 i = 0;
31 u32 max_offset = 0, img_end;
32
33 phdr = (struct container_hdr *)addr;
34 if (phdr->tag != 0x87 && phdr->version != 0x0) {
35 debug("Wrong container header\n");
36 return -EFAULT;
37 }
38
Ye Li6f3858d2021-08-07 16:00:39 +080039 max_offset = phdr->length_lsb + (phdr->length_msb << 8);
40 if (header_length)
41 *header_length = max_offset;
Peng Fand1c07782019-09-23 10:18:44 +080042
43 img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr));
44 for (i = 0; i < phdr->num_images; i++) {
45 img_end = img_entry->offset + img_entry->size;
46 if (img_end > max_offset)
47 max_offset = img_end;
48
49 debug("img[%u], end = 0x%x\n", i, img_end);
50
51 img_entry++;
52 }
53
54 if (phdr->sig_blk_offset != 0) {
55 sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset);
56 u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8);
57
58 if (phdr->sig_blk_offset + len > max_offset)
59 max_offset = phdr->sig_blk_offset + len;
60
61 debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len);
62 }
63
64 return max_offset;
65}
66
Ye Li6f3858d2021-08-07 16:00:39 +080067static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length)
Peng Fand1c07782019-09-23 10:18:44 +080068{
69 u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT);
70 int ret = 0;
71
72 if (!buf) {
73 printf("Malloc buffer failed\n");
74 return -ENOMEM;
75 }
76
Simon Glass103c5f12021-08-08 12:20:09 -060077#ifdef CONFIG_SPL_MMC
Peng Fand1c07782019-09-23 10:18:44 +080078 if (dev_type == MMC_DEV) {
79 unsigned long count = 0;
80 struct mmc *mmc = (struct mmc *)dev;
81
82 count = blk_dread(mmc_get_blk_desc(mmc),
83 offset / mmc->read_bl_len,
84 CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len,
85 buf);
86 if (count == 0) {
87 printf("Read container image from MMC/SD failed\n");
88 return -EIO;
89 }
90 }
91#endif
92
93#ifdef CONFIG_SPL_SPI_LOAD
94 if (dev_type == QSPI_DEV) {
95 struct spi_flash *flash = (struct spi_flash *)dev;
96
97 ret = spi_flash_read(flash, offset,
98 CONTAINER_HDR_ALIGNMENT, buf);
99 if (ret != 0) {
100 printf("Read container image from QSPI failed\n");
101 return -EIO;
102 }
103 }
104#endif
105
106#ifdef CONFIG_SPL_NAND_SUPPORT
107 if (dev_type == NAND_DEV) {
108 ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT,
109 buf);
110 if (ret != 0) {
111 printf("Read container image from NAND failed\n");
112 return -EIO;
113 }
114 }
115#endif
116
117#ifdef CONFIG_SPL_NOR_SUPPORT
118 if (dev_type == QSPI_NOR_DEV)
119 memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT);
120#endif
121
Ye Li6f3858d2021-08-07 16:00:39 +0800122#ifdef CONFIG_SPL_BOOTROM_SUPPORT
123 if (dev_type == ROM_API_DEV) {
124 ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf);
125 if (!ret) {
126 printf("Read container image from ROM API failed\n");
127 return -EIO;
128 }
129 }
130#endif
131
132 ret = get_container_size((ulong)buf, header_length);
Peng Fand1c07782019-09-23 10:18:44 +0800133
134 free(buf);
135
136 return ret;
137}
138
139static unsigned long get_boot_device_offset(void *dev, int dev_type)
140{
141 unsigned long offset = 0;
142
143 if (dev_type == MMC_DEV) {
144 struct mmc *mmc = (struct mmc *)dev;
145
146 if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) {
147 offset = CONTAINER_HDR_MMCSD_OFFSET;
148 } else {
149 u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
150
151 if (part == 1 || part == 2) {
152 if (is_imx8qxp() && is_soc_rev(CHIP_REV_B))
153 offset = CONTAINER_HDR_MMCSD_OFFSET;
154 else
155 offset = CONTAINER_HDR_EMMC_OFFSET;
156 } else {
157 offset = CONTAINER_HDR_MMCSD_OFFSET;
158 }
159 }
160 } else if (dev_type == QSPI_DEV) {
161 offset = CONTAINER_HDR_QSPI_OFFSET;
162 } else if (dev_type == NAND_DEV) {
163 offset = CONTAINER_HDR_NAND_OFFSET;
164 } else if (dev_type == QSPI_NOR_DEV) {
165 offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000;
Ye Li6f3858d2021-08-07 16:00:39 +0800166 } else if (dev_type == ROM_API_DEV) {
167 offset = (unsigned long)dev;
Peng Fand1c07782019-09-23 10:18:44 +0800168 }
169
170 return offset;
171}
172
173static int get_imageset_end(void *dev, int dev_type)
174{
175 unsigned long offset1 = 0, offset2 = 0;
176 int value_container[2];
Ye Li6f3858d2021-08-07 16:00:39 +0800177 u16 hdr_length;
Peng Fand1c07782019-09-23 10:18:44 +0800178
179 offset1 = get_boot_device_offset(dev, dev_type);
180 offset2 = CONTAINER_HDR_ALIGNMENT + offset1;
181
Ye Li6f3858d2021-08-07 16:00:39 +0800182 value_container[0] = get_dev_container_size(dev, dev_type, offset1, &hdr_length);
Peng Fand1c07782019-09-23 10:18:44 +0800183 if (value_container[0] < 0) {
184 printf("Parse seco container failed %d\n", value_container[0]);
185 return value_container[0];
186 }
187
188 debug("seco container size 0x%x\n", value_container[0]);
189
Ye Li6f3858d2021-08-07 16:00:39 +0800190 value_container[1] = get_dev_container_size(dev, dev_type, offset2, &hdr_length);
Peng Fand1c07782019-09-23 10:18:44 +0800191 if (value_container[1] < 0) {
192 debug("Parse scu container failed %d, only seco container\n",
193 value_container[1]);
194 /* return seco container total size */
195 return value_container[0] + offset1;
196 }
197
198 debug("scu container size 0x%x\n", value_container[1]);
199
200 return value_container[1] + offset2;
201}
202
203#ifdef CONFIG_SPL_SPI_LOAD
204unsigned long spl_spi_get_uboot_offs(struct spi_flash *flash)
205{
206 int end;
207
208 end = get_imageset_end(flash, QSPI_DEV);
209 end = ROUND(end, SZ_1K);
210
211 printf("Load image from QSPI 0x%x\n", end);
212
213 return end;
214}
215#endif
216
Simon Glass103c5f12021-08-08 12:20:09 -0600217#ifdef CONFIG_SPL_MMC
Faiz Abbascf008252020-02-26 13:44:35 +0530218unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
219 unsigned long raw_sect)
Peng Fand1c07782019-09-23 10:18:44 +0800220{
221 int end;
222
223 end = get_imageset_end(mmc, MMC_DEV);
224 end = ROUND(end, SZ_1K);
225
226 printf("Load image from MMC/SD 0x%x\n", end);
227
228 return end / mmc->read_bl_len;
229}
230#endif
231
232#ifdef CONFIG_SPL_NAND_SUPPORT
233uint32_t spl_nand_get_uboot_raw_page(void)
234{
235 int end;
236
237 end = get_imageset_end((void *)NULL, NAND_DEV);
238 end = ROUND(end, SZ_16K);
239
240 printf("Load image from NAND 0x%x\n", end);
241
242 return end;
243}
244#endif
245
246#ifdef CONFIG_SPL_NOR_SUPPORT
247unsigned long spl_nor_get_uboot_base(void)
248{
249 int end;
250
251 /* Calculate the image set end,
Tom Rini65cc0e22022-11-16 13:10:41 -0500252 * if it is less than CFG_SYS_UBOOT_BASE(0x8281000),
253 * we use CFG_SYS_UBOOT_BASE
Peng Fand1c07782019-09-23 10:18:44 +0800254 * Otherwise, use the calculated address
255 */
256 end = get_imageset_end((void *)NULL, QSPI_NOR_DEV);
Tom Rini65cc0e22022-11-16 13:10:41 -0500257 if (end <= CFG_SYS_UBOOT_BASE)
258 end = CFG_SYS_UBOOT_BASE;
Peng Fand1c07782019-09-23 10:18:44 +0800259 else
260 end = ROUND(end, SZ_1K);
261
262 printf("Load image from NOR 0x%x\n", end);
263
264 return end;
265}
266#endif
Ye Li6f3858d2021-08-07 16:00:39 +0800267
268#ifdef CONFIG_SPL_BOOTROM_SUPPORT
Ye Lie8b68042021-08-07 16:01:08 +0800269u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
270{
271 return image_offset;
272}
273
Ye Li6f3858d2021-08-07 16:00:39 +0800274ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
275{
276 ulong end;
277
Ye Lie8b68042021-08-07 16:01:08 +0800278 image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev);
279
Ye Li6f3858d2021-08-07 16:00:39 +0800280 end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV);
281 end = ROUND(end, SZ_1K);
282
283 printf("Load image from 0x%lx by ROM_API\n", end);
284
285 return end;
286}
287#endif