blob: f588a5f52160b12ec7fd5c61820cdbc41c5df86b [file] [log] [blame]
Peng Fan36986792019-09-16 03:09:31 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <common.h>
7#include <errno.h>
8#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Peng Fan36986792019-09-16 03:09:31 +000010#include <linux/libfdt.h>
11#include <spl.h>
12
13#include <asm/arch/sys_proto.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17static int is_boot_from_stream_device(u32 boot)
18{
19 u32 interface;
20
21 interface = boot >> 16;
22 if (interface >= BT_DEV_TYPE_USB)
23 return 1;
24
25 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
26 return 1;
27
28 return 0;
29}
30
31static ulong spl_romapi_read_seekable(struct spl_load_info *load,
32 ulong sector, ulong count,
33 void *buf)
34{
35 u32 pagesize = *(u32 *)load->priv;
36 volatile gd_t *pgd = gd;
37 ulong byte = count * pagesize;
38 int ret;
39 u32 offset;
40
41 offset = sector * pagesize;
42
43 debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte);
44
45 ret = g_rom_api->download_image(buf, offset, byte,
46 ((uintptr_t)buf) ^ offset ^ byte);
47 gd = pgd;
48
49 if (ret == ROM_API_OKAY)
50 return count;
51
52 printf("ROM API Failure when load 0x%x\n", offset);
53
54 return 0;
55}
56
57static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev,
59 u32 rom_bt_dev)
60{
61 volatile gd_t *pgd = gd;
62 int ret;
63 u32 offset;
64 u32 pagesize, size;
65 struct image_header *header;
66 u32 image_offset;
67
68 ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
69 ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
70 ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
71 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
72 ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
73 ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
74
75 gd = pgd;
76
77 if (ret != ROM_API_OKAY) {
78 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
79 return -1;
80 }
81
82 header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
83
84 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
85 image_offset, pagesize, offset);
86
87 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR)
88 offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512;
89 else
90 offset = image_offset +
91 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000;
92
93 size = ALIGN(sizeof(struct image_header), pagesize);
94 ret = g_rom_api->download_image((u8 *)header, offset, size,
95 ((uintptr_t)header) ^ offset ^ size);
96 gd = pgd;
97
98 if (ret != ROM_API_OKAY) {
99 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
100 offset, size);
101 return -1;
102 }
103
104 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
105 image_get_magic(header) == FDT_MAGIC) {
106 struct spl_load_info load;
107
108 memset(&load, 0, sizeof(load));
109 load.bl_len = pagesize;
110 load.read = spl_romapi_read_seekable;
111 load.priv = &pagesize;
112 return spl_load_simple_fit(spl_image, &load,
113 offset / pagesize, header);
114 } else {
115 /* TODO */
116 puts("Can't support legacy image\n");
117 return -1;
118 }
119
120 return 0;
121}
122
123static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
124 ulong count, void *buf)
125{
126 memcpy(buf, (void *)(sector), count);
127
128 if (load->priv) {
129 ulong *p = (ulong *)load->priv;
130 ulong total = sector + count;
131
132 if (total > *p)
133 *p = total;
134 }
135
136 return count;
137}
138
139static ulong get_fit_image_size(void *fit)
140{
141 struct spl_image_info spl_image;
142 struct spl_load_info spl_load_info;
143 ulong last = (ulong)fit;
144
145 memset(&spl_load_info, 0, sizeof(spl_load_info));
146 spl_load_info.bl_len = 1;
147 spl_load_info.read = spl_ram_load_read;
148 spl_load_info.priv = &last;
149
150 spl_load_simple_fit(&spl_image, &spl_load_info,
151 (uintptr_t)fit, fit);
152
153 return last - (ulong)fit;
154}
155
156u8 *search_fit_header(u8 *p, int size)
157{
158 int i;
159
160 for (i = 0; i < size; i += 4)
161 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
162 return p + i;
163
164 return NULL;
165}
166
167static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
168 struct spl_boot_device *bootdev)
169{
170 struct spl_load_info load;
171 volatile gd_t *pgd = gd;
172 u32 pagesize, pg;
173 int ret;
174 int i = 0;
175 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
176 u8 *pfit = NULL;
177 int imagesize;
178 int total;
179
180 ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
181 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
182 gd = pgd;
183
184 if (ret != ROM_API_OKAY)
185 puts("failure at query_boot_info\n");
186
187 pg = pagesize;
188 if (pg < 1024)
189 pg = 1024;
190
191 for (i = 0; i < 640; i++) {
192 ret = g_rom_api->download_image(p, 0, pg,
193 ((uintptr_t)p) ^ pg);
194 gd = pgd;
195
196 if (ret != ROM_API_OKAY) {
197 puts("Steam(USB) download failure\n");
198 return -1;
199 }
200
201 pfit = search_fit_header(p, pg);
202 p += pg;
203
204 if (pfit)
205 break;
206 }
207
208 if (!pfit) {
209 puts("Can't found uboot FIT image in 640K range \n");
210 return -1;
211 }
212
213 if (p - pfit < sizeof(struct fdt_header)) {
214 ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
215 gd = pgd;
216
217 if (ret != ROM_API_OKAY) {
218 puts("Steam(USB) download failure\n");
219 return -1;
220 }
221
222 p += pg;
223 }
224
225 imagesize = fit_get_size(pfit);
226 printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize);
227
228 if (p - pfit < imagesize) {
229 imagesize -= p - pfit;
230 /*need pagesize hear after ROM fix USB problme*/
231 imagesize += pg - 1;
232 imagesize /= pg;
233 imagesize *= pg;
234
235 printf("Need continue download %d\n", imagesize);
236
237 ret = g_rom_api->download_image(p, 0, imagesize,
238 ((uintptr_t)p) ^ imagesize);
239 gd = pgd;
240
241 p += imagesize;
242
243 if (ret != ROM_API_OKAY) {
244 printf("Failure download %d\n", imagesize);
245 return -1;
246 }
247 }
248
249 total = get_fit_image_size(pfit);
250 total += 3;
251 total &= ~0x3;
252
253 imagesize = total - (p - pfit);
254
255 imagesize += pagesize - 1;
256 imagesize /= pagesize;
257 imagesize *= pagesize;
258
259 printf("Download %d, total fit %d\n", imagesize, total);
260
261 ret = g_rom_api->download_image(p, 0, imagesize,
262 ((uintptr_t)p) ^ imagesize);
263 if (ret != ROM_API_OKAY)
264 printf("ROM download failure %d\n", imagesize);
265
266 memset(&load, 0, sizeof(load));
267 load.bl_len = 1;
268 load.read = spl_ram_load_read;
269
270 return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit);
271}
272
273int board_return_to_bootrom(struct spl_image_info *spl_image,
274 struct spl_boot_device *bootdev)
275{
276 volatile gd_t *pgd = gd;
277 int ret;
278 u32 boot;
279
280 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
281 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
282 gd = pgd;
283
284 if (ret != ROM_API_OKAY) {
285 puts("ROMAPI: failure at query_boot_info\n");
286 return -1;
287 }
288
289 if (is_boot_from_stream_device(boot))
290 return spl_romapi_load_image_stream(spl_image, bootdev);
291
292 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
293}