]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b6396403 SG |
2 | /* |
3 | * (C) Copyright 2000-2009 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
b6396403 SG |
5 | */ |
6 | ||
ea51a628 | 7 | #ifndef USE_HOSTCC |
b6396403 | 8 | #include <common.h> |
ea51a628 | 9 | #include <bootstage.h> |
51bb3384 | 10 | #include <cli.h> |
be595146 | 11 | #include <command.h> |
1eb69ae4 | 12 | #include <cpu_func.h> |
c7694dd4 | 13 | #include <env.h> |
90268b87 | 14 | #include <errno.h> |
b6396403 | 15 | #include <fdt_support.h> |
36bf446b | 16 | #include <irq_func.h> |
b6396403 | 17 | #include <lmb.h> |
f7ae49fc | 18 | #include <log.h> |
b6396403 | 19 | #include <malloc.h> |
0eb25b61 | 20 | #include <mapmem.h> |
90526e9f SG |
21 | #include <net.h> |
22 | #include <asm/cache.h> | |
401d1c4f | 23 | #include <asm/global_data.h> |
b6396403 | 24 | #include <asm/io.h> |
b6386f38 | 25 | #include <linux/sizes.h> |
dec166d6 | 26 | #include <tpm-v2.h> |
b6396403 SG |
27 | #if defined(CONFIG_CMD_USB) |
28 | #include <usb.h> | |
29 | #endif | |
ea51a628 SG |
30 | #else |
31 | #include "mkimage.h" | |
32 | #endif | |
b6396403 | 33 | |
ea51a628 SG |
34 | #include <bootm.h> |
35 | #include <image.h> | |
b6396403 | 36 | |
b6386f38 SG |
37 | #define MAX_CMDLINE_SIZE SZ_4K |
38 | ||
b6396403 SG |
39 | #define IH_INITRD_ARCH IH_ARCH_DEFAULT |
40 | ||
ea51a628 SG |
41 | #ifndef USE_HOSTCC |
42 | ||
43 | DECLARE_GLOBAL_DATA_PTR; | |
44 | ||
d9d7c20b | 45 | struct bootm_headers images; /* pointers to os/initrd/fdt images */ |
5db28905 | 46 | |
7f3b1ee3 SG |
47 | __weak void board_quiesce_devices(void) |
48 | { | |
49 | } | |
50 | ||
51 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) | |
52 | /** | |
53 | * image_get_kernel - verify legacy format kernel image | |
54 | * @img_addr: in RAM address of the legacy format image to be verified | |
55 | * @verify: data CRC verification flag | |
56 | * | |
57 | * image_get_kernel() verifies legacy image integrity and returns pointer to | |
58 | * legacy image header if image verification was completed successfully. | |
59 | * | |
60 | * returns: | |
61 | * pointer to a legacy image header if valid image was found | |
62 | * otherwise return NULL | |
63 | */ | |
64 | static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify) | |
65 | { | |
66 | struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr; | |
67 | ||
68 | if (!image_check_magic(hdr)) { | |
69 | puts("Bad Magic Number\n"); | |
70 | bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); | |
71 | return NULL; | |
72 | } | |
73 | bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); | |
74 | ||
75 | if (!image_check_hcrc(hdr)) { | |
76 | puts("Bad Header Checksum\n"); | |
77 | bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); | |
78 | return NULL; | |
79 | } | |
80 | ||
81 | bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); | |
82 | image_print_contents(hdr); | |
83 | ||
84 | if (verify) { | |
85 | puts(" Verifying Checksum ... "); | |
86 | if (!image_check_dcrc(hdr)) { | |
87 | printf("Bad Data CRC\n"); | |
88 | bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); | |
89 | return NULL; | |
90 | } | |
91 | puts("OK\n"); | |
92 | } | |
93 | bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); | |
94 | ||
95 | if (!image_check_target_arch(hdr)) { | |
96 | printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr)); | |
97 | bootstage_error(BOOTSTAGE_ID_CHECK_ARCH); | |
98 | return NULL; | |
99 | } | |
100 | return hdr; | |
101 | } | |
102 | #endif | |
103 | ||
104 | /** | |
820110c4 SG |
105 | * boot_get_kernel() - find kernel image |
106 | * | |
820110c4 | 107 | * @addr_fit: first argument to bootm: address, fit configuration, etc. |
7f3b1ee3 SG |
108 | * @os_data: pointer to a ulong variable, will hold os data start address |
109 | * @os_len: pointer to a ulong variable, will hold os data length | |
820110c4 SG |
110 | * address and length, otherwise NULL |
111 | * pointer to image header if valid image was found, plus kernel start | |
7721e71f | 112 | * @kernp: image header if valid image was found, otherwise NULL |
7f3b1ee3 SG |
113 | * |
114 | * boot_get_kernel() tries to find a kernel image, verifies its integrity | |
115 | * and locates kernel data. | |
116 | * | |
b13e9488 SG |
117 | * Return: 0 on success, -ve on error. -EPROTOTYPE means that the image is in |
118 | * a wrong or unsupported format | |
7f3b1ee3 | 119 | */ |
b13e9488 SG |
120 | static int boot_get_kernel(const char *addr_fit, struct bootm_headers *images, |
121 | ulong *os_data, ulong *os_len, const void **kernp) | |
329da485 | 122 | { |
7f3b1ee3 SG |
123 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
124 | struct legacy_img_hdr *hdr; | |
125 | #endif | |
126 | ulong img_addr; | |
127 | const void *buf; | |
530cc479 | 128 | const char *fit_uname_config = NULL, *fit_uname_kernel = NULL; |
7f3b1ee3 SG |
129 | #if CONFIG_IS_ENABLED(FIT) |
130 | int os_noffset; | |
131 | #endif | |
132 | ||
133 | #ifdef CONFIG_ANDROID_BOOT_IMAGE | |
134 | const void *boot_img; | |
135 | const void *vendor_boot_img; | |
136 | #endif | |
820110c4 | 137 | img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config, |
7f3b1ee3 SG |
138 | &fit_uname_kernel); |
139 | ||
140 | if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD)) | |
141 | img_addr += image_load_offset; | |
142 | ||
143 | bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC); | |
144 | ||
145 | /* check image type, for FIT images get FIT kernel node */ | |
146 | *os_data = *os_len = 0; | |
147 | buf = map_sysmem(img_addr, 0); | |
148 | switch (genimg_get_format(buf)) { | |
149 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) | |
150 | case IMAGE_FORMAT_LEGACY: | |
151 | printf("## Booting kernel from Legacy Image at %08lx ...\n", | |
152 | img_addr); | |
153 | hdr = image_get_kernel(img_addr, images->verify); | |
154 | if (!hdr) | |
7721e71f | 155 | return -EINVAL; |
7f3b1ee3 SG |
156 | bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
157 | ||
158 | /* get os_data and os_len */ | |
159 | switch (image_get_type(hdr)) { | |
160 | case IH_TYPE_KERNEL: | |
161 | case IH_TYPE_KERNEL_NOLOAD: | |
162 | *os_data = image_get_data(hdr); | |
163 | *os_len = image_get_data_size(hdr); | |
164 | break; | |
165 | case IH_TYPE_MULTI: | |
166 | image_multi_getimg(hdr, 0, os_data, os_len); | |
167 | break; | |
168 | case IH_TYPE_STANDALONE: | |
169 | *os_data = image_get_data(hdr); | |
170 | *os_len = image_get_data_size(hdr); | |
171 | break; | |
172 | default: | |
7f3b1ee3 | 173 | bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
7721e71f | 174 | return -EPROTOTYPE; |
7f3b1ee3 SG |
175 | } |
176 | ||
177 | /* | |
178 | * copy image header to allow for image overwrites during | |
179 | * kernel decompression. | |
180 | */ | |
181 | memmove(&images->legacy_hdr_os_copy, hdr, | |
182 | sizeof(struct legacy_img_hdr)); | |
183 | ||
184 | /* save pointer to image header */ | |
185 | images->legacy_hdr_os = hdr; | |
186 | ||
187 | images->legacy_hdr_valid = 1; | |
188 | bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE); | |
189 | break; | |
190 | #endif | |
191 | #if CONFIG_IS_ENABLED(FIT) | |
192 | case IMAGE_FORMAT_FIT: | |
193 | os_noffset = fit_image_load(images, img_addr, | |
194 | &fit_uname_kernel, &fit_uname_config, | |
195 | IH_ARCH_DEFAULT, IH_TYPE_KERNEL, | |
196 | BOOTSTAGE_ID_FIT_KERNEL_START, | |
197 | FIT_LOAD_IGNORED, os_data, os_len); | |
198 | if (os_noffset < 0) | |
7721e71f | 199 | return -ENOENT; |
7f3b1ee3 SG |
200 | |
201 | images->fit_hdr_os = map_sysmem(img_addr, 0); | |
202 | images->fit_uname_os = fit_uname_kernel; | |
203 | images->fit_uname_cfg = fit_uname_config; | |
204 | images->fit_noffset_os = os_noffset; | |
205 | break; | |
206 | #endif | |
207 | #ifdef CONFIG_ANDROID_BOOT_IMAGE | |
7721e71f SG |
208 | case IMAGE_FORMAT_ANDROID: { |
209 | int ret; | |
210 | ||
7f3b1ee3 SG |
211 | boot_img = buf; |
212 | vendor_boot_img = NULL; | |
213 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { | |
214 | boot_img = map_sysmem(get_abootimg_addr(), 0); | |
215 | vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); | |
216 | } | |
217 | printf("## Booting Android Image at 0x%08lx ...\n", img_addr); | |
7721e71f SG |
218 | ret = android_image_get_kernel(boot_img, vendor_boot_img, |
219 | images->verify, os_data, os_len); | |
7f3b1ee3 SG |
220 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
221 | unmap_sysmem(vendor_boot_img); | |
222 | unmap_sysmem(boot_img); | |
223 | } | |
4f77169c SG |
224 | if (ret) |
225 | return ret; | |
7f3b1ee3 | 226 | break; |
7721e71f | 227 | } |
7f3b1ee3 SG |
228 | #endif |
229 | default: | |
bdfa1b67 | 230 | bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
b13e9488 | 231 | return -EPROTOTYPE; |
7f3b1ee3 SG |
232 | } |
233 | ||
234 | debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n", | |
235 | *os_data, *os_len, *os_len); | |
7721e71f | 236 | *kernp = buf; |
7f3b1ee3 | 237 | |
7721e71f | 238 | return 0; |
329da485 SG |
239 | } |
240 | ||
b6396403 | 241 | #ifdef CONFIG_LMB |
d9d7c20b | 242 | static void boot_start_lmb(struct bootm_headers *images) |
b6396403 SG |
243 | { |
244 | ulong mem_start; | |
245 | phys_size_t mem_size; | |
246 | ||
723806cc SG |
247 | mem_start = env_get_bootm_low(); |
248 | mem_size = env_get_bootm_size(); | |
b6396403 | 249 | |
9cc2323f SG |
250 | lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start, |
251 | mem_size, NULL); | |
b6396403 SG |
252 | } |
253 | #else | |
254 | #define lmb_reserve(lmb, base, size) | |
d9d7c20b | 255 | static inline void boot_start_lmb(struct bootm_headers *images) { } |
b6396403 SG |
256 | #endif |
257 | ||
a50e886a | 258 | static int bootm_start(void) |
b6396403 SG |
259 | { |
260 | memset((void *)&images, 0, sizeof(images)); | |
bfebc8c9 | 261 | images.verify = env_get_yesno("verify"); |
b6396403 SG |
262 | |
263 | boot_start_lmb(&images); | |
264 | ||
265 | bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start"); | |
266 | images.state = BOOTM_STATE_START; | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
921070bc | 271 | static ulong bootm_data_addr(const char *addr_str) |
9d46e63d PR |
272 | { |
273 | ulong addr; | |
274 | ||
921070bc SG |
275 | if (addr_str) |
276 | addr = hextoul(addr_str, NULL); | |
9d46e63d PR |
277 | else |
278 | addr = image_load_addr; | |
279 | ||
280 | return addr; | |
281 | } | |
282 | ||
921070bc SG |
283 | /** |
284 | * bootm_pre_load() - Handle the pre-load processing | |
285 | * | |
286 | * This can be used to do a full signature check of the image, for example. | |
287 | * It calls image_pre_load() with the data address of the image to check. | |
288 | * | |
289 | * @addr_str: String containing load address in hex, or NULL to use | |
290 | * image_load_addr | |
291 | * Return: 0 if OK, CMD_RET_FAILURE on failure | |
292 | */ | |
293 | static int bootm_pre_load(const char *addr_str) | |
9d46e63d | 294 | { |
921070bc | 295 | ulong data_addr = bootm_data_addr(addr_str); |
9d46e63d PR |
296 | int ret = 0; |
297 | ||
494bcf1a | 298 | if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD)) |
9d46e63d PR |
299 | ret = image_pre_load(data_addr); |
300 | ||
301 | if (ret) | |
302 | ret = CMD_RET_FAILURE; | |
303 | ||
304 | return ret; | |
305 | } | |
306 | ||
3e3bd5bd SG |
307 | /** |
308 | * bootm_find_os(): Find the OS to boot | |
309 | * | |
310 | * @cmd_name: Command name that started this boot, e.g. "bootm" | |
311 | * @addr_fit: Address and/or FIT specifier (first arg of bootm command) | |
312 | * Return: 0 on success, -ve on error | |
313 | */ | |
314 | static int bootm_find_os(const char *cmd_name, const char *addr_fit) | |
b6396403 SG |
315 | { |
316 | const void *os_hdr; | |
636da203 SO |
317 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
318 | const void *vendor_boot_img; | |
319 | const void *boot_img; | |
320 | #endif | |
b6396403 | 321 | bool ep_found = false; |
90268b87 | 322 | int ret; |
b6396403 SG |
323 | |
324 | /* get kernel image header, start address and length */ | |
3e3bd5bd | 325 | ret = boot_get_kernel(addr_fit, &images, &images.os.image_start, |
b13e9488 | 326 | &images.os.image_len, &os_hdr); |
4c76f5e4 | 327 | if (ret) { |
b13e9488 | 328 | if (ret == -EPROTOTYPE) |
3e3bd5bd | 329 | printf("Wrong Image Type for %s command\n", cmd_name); |
b13e9488 | 330 | |
4c76f5e4 | 331 | printf("ERROR %dE: can't get kernel image!\n", ret); |
b6396403 SG |
332 | return 1; |
333 | } | |
334 | ||
335 | /* get image parameters */ | |
336 | switch (genimg_get_format(os_hdr)) { | |
c76c93a3 | 337 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
b6396403 SG |
338 | case IMAGE_FORMAT_LEGACY: |
339 | images.os.type = image_get_type(os_hdr); | |
340 | images.os.comp = image_get_comp(os_hdr); | |
341 | images.os.os = image_get_os(os_hdr); | |
342 | ||
343 | images.os.end = image_get_image_end(os_hdr); | |
344 | images.os.load = image_get_load(os_hdr); | |
90268b87 | 345 | images.os.arch = image_get_arch(os_hdr); |
b6396403 SG |
346 | break; |
347 | #endif | |
bf371b4c | 348 | #if CONFIG_IS_ENABLED(FIT) |
b6396403 SG |
349 | case IMAGE_FORMAT_FIT: |
350 | if (fit_image_get_type(images.fit_hdr_os, | |
351 | images.fit_noffset_os, | |
352 | &images.os.type)) { | |
353 | puts("Can't get image type!\n"); | |
354 | bootstage_error(BOOTSTAGE_ID_FIT_TYPE); | |
355 | return 1; | |
356 | } | |
357 | ||
358 | if (fit_image_get_comp(images.fit_hdr_os, | |
359 | images.fit_noffset_os, | |
360 | &images.os.comp)) { | |
361 | puts("Can't get image compression!\n"); | |
362 | bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION); | |
363 | return 1; | |
364 | } | |
365 | ||
366 | if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os, | |
367 | &images.os.os)) { | |
368 | puts("Can't get image OS!\n"); | |
369 | bootstage_error(BOOTSTAGE_ID_FIT_OS); | |
370 | return 1; | |
371 | } | |
372 | ||
90268b87 SG |
373 | if (fit_image_get_arch(images.fit_hdr_os, |
374 | images.fit_noffset_os, | |
375 | &images.os.arch)) { | |
376 | puts("Can't get image ARCH!\n"); | |
377 | return 1; | |
378 | } | |
379 | ||
b6396403 SG |
380 | images.os.end = fit_get_end(images.fit_hdr_os); |
381 | ||
382 | if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os, | |
383 | &images.os.load)) { | |
384 | puts("Can't get image load address!\n"); | |
385 | bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR); | |
386 | return 1; | |
387 | } | |
388 | break; | |
389 | #endif | |
390 | #ifdef CONFIG_ANDROID_BOOT_IMAGE | |
391 | case IMAGE_FORMAT_ANDROID: | |
636da203 SO |
392 | boot_img = os_hdr; |
393 | vendor_boot_img = NULL; | |
394 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { | |
395 | boot_img = map_sysmem(get_abootimg_addr(), 0); | |
396 | vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); | |
397 | } | |
b6396403 | 398 | images.os.type = IH_TYPE_KERNEL; |
636da203 | 399 | images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img); |
b6396403 | 400 | images.os.os = IH_OS_LINUX; |
636da203 SO |
401 | images.os.end = android_image_get_end(boot_img, vendor_boot_img); |
402 | images.os.load = android_image_get_kload(boot_img, vendor_boot_img); | |
86f4695b AD |
403 | images.ep = images.os.load; |
404 | ep_found = true; | |
636da203 SO |
405 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
406 | unmap_sysmem(vendor_boot_img); | |
407 | unmap_sysmem(boot_img); | |
408 | } | |
b6396403 SG |
409 | break; |
410 | #endif | |
411 | default: | |
412 | puts("ERROR: unknown image format type!\n"); | |
413 | return 1; | |
414 | } | |
415 | ||
90268b87 | 416 | /* If we have a valid setup.bin, we will use that for entry (x86) */ |
5bda35cf SG |
417 | if (images.os.arch == IH_ARCH_I386 || |
418 | images.os.arch == IH_ARCH_X86_64) { | |
90268b87 SG |
419 | ulong len; |
420 | ||
421 | ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len); | |
422 | if (ret < 0 && ret != -ENOENT) { | |
423 | puts("Could not find a valid setup.bin for x86\n"); | |
424 | return 1; | |
425 | } | |
426 | /* Kernel entry point is the setup.bin */ | |
427 | } else if (images.legacy_hdr_valid) { | |
b6396403 | 428 | images.ep = image_get_ep(&images.legacy_hdr_os_copy); |
bf371b4c | 429 | #if CONFIG_IS_ENABLED(FIT) |
b6396403 SG |
430 | } else if (images.fit_uname_os) { |
431 | int ret; | |
432 | ||
433 | ret = fit_image_get_entry(images.fit_hdr_os, | |
434 | images.fit_noffset_os, &images.ep); | |
435 | if (ret) { | |
436 | puts("Can't get entry point property!\n"); | |
437 | return 1; | |
438 | } | |
439 | #endif | |
440 | } else if (!ep_found) { | |
441 | puts("Could not find kernel entry point!\n"); | |
442 | return 1; | |
443 | } | |
444 | ||
445 | if (images.os.type == IH_TYPE_KERNEL_NOLOAD) { | |
ef65aa35 | 446 | if (IS_ENABLED(CONFIG_CMD_BOOTI) && |
4533b3d0 HS |
447 | images.os.arch == IH_ARCH_ARM64 && |
448 | images.os.os == IH_OS_LINUX) { | |
487b5fa6 MV |
449 | ulong image_addr; |
450 | ulong image_size; | |
451 | ||
452 | ret = booti_setup(images.os.image_start, &image_addr, | |
453 | &image_size, true); | |
454 | if (ret != 0) | |
455 | return 1; | |
456 | ||
457 | images.os.type = IH_TYPE_KERNEL; | |
458 | images.os.load = image_addr; | |
459 | images.ep = image_addr; | |
460 | } else { | |
461 | images.os.load = images.os.image_start; | |
462 | images.ep += images.os.image_start; | |
463 | } | |
b6396403 SG |
464 | } |
465 | ||
7a80de46 | 466 | images.os.start = map_to_sysmem(os_hdr); |
b6396403 SG |
467 | |
468 | return 0; | |
469 | } | |
470 | ||
d52e8575 KA |
471 | /** |
472 | * bootm_find_images - wrapper to find and locate various images | |
473 | * @flag: Ignored Argument | |
474 | * @argc: command argument count | |
475 | * @argv: command argument list | |
fbde7589 TK |
476 | * @start: OS image start address |
477 | * @size: OS image size | |
d52e8575 KA |
478 | * |
479 | * boot_find_images() will attempt to load an available ramdisk, | |
480 | * flattened device tree, as well as specifically marked | |
481 | * "loadable" images (loadables are FIT only) | |
482 | * | |
483 | * Note: bootm_find_images will skip an image if it is not found | |
484 | * | |
485 | * @return: | |
486 | * 0, if all existing images were loaded correctly | |
487 | * 1, if an image is found but corrupted, or invalid | |
488 | */ | |
fbde7589 TK |
489 | int bootm_find_images(int flag, int argc, char *const argv[], ulong start, |
490 | ulong size) | |
b6396403 | 491 | { |
8eda15bc | 492 | const char *select = NULL; |
b6396403 SG |
493 | int ret; |
494 | ||
8eda15bc SG |
495 | if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) { |
496 | char *buf; | |
497 | ||
498 | /* Look for an Android boot image */ | |
499 | buf = map_sysmem(images.os.start, 0); | |
500 | if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) | |
501 | select = argc ? argv[0] : env_get("loadaddr"); | |
502 | } | |
503 | ||
504 | if (argc >= 2) | |
505 | select = argv[1]; | |
506 | ||
b6396403 | 507 | /* find ramdisk */ |
8eda15bc | 508 | ret = boot_get_ramdisk(select, &images, IH_INITRD_ARCH, |
b6396403 SG |
509 | &images.rd_start, &images.rd_end); |
510 | if (ret) { | |
511 | puts("Ramdisk image is corrupt or invalid\n"); | |
512 | return 1; | |
513 | } | |
514 | ||
fbde7589 TK |
515 | /* check if ramdisk overlaps OS image */ |
516 | if (images.rd_start && (((ulong)images.rd_start >= start && | |
ef4f4f1f JC |
517 | (ulong)images.rd_start < start + size) || |
518 | ((ulong)images.rd_end > start && | |
519 | (ulong)images.rd_end <= start + size) || | |
520 | ((ulong)images.rd_start < start && | |
521 | (ulong)images.rd_end >= start + size))) { | |
fbde7589 TK |
522 | printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n", |
523 | start, start + size); | |
524 | return 1; | |
525 | } | |
526 | ||
0c303f9a | 527 | #if CONFIG_IS_ENABLED(OF_LIBFDT) |
b6396403 SG |
528 | /* find flattened device tree */ |
529 | ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images, | |
530 | &images.ft_addr, &images.ft_len); | |
531 | if (ret) { | |
532 | puts("Could not find a valid device tree\n"); | |
533 | return 1; | |
534 | } | |
fbde7589 TK |
535 | |
536 | /* check if FDT overlaps OS image */ | |
537 | if (images.ft_addr && | |
538 | (((ulong)images.ft_addr >= start && | |
5acfdfbd | 539 | (ulong)images.ft_addr < start + size) || |
fbde7589 | 540 | ((ulong)images.ft_addr + images.ft_len >= start && |
5acfdfbd | 541 | (ulong)images.ft_addr + images.ft_len < start + size))) { |
fbde7589 TK |
542 | printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n", |
543 | start, start + size); | |
544 | return 1; | |
545 | } | |
546 | ||
3a09f38d | 547 | if (IS_ENABLED(CONFIG_CMD_FDT)) |
596be5f3 | 548 | set_working_fdt_addr(map_to_sysmem(images.ft_addr)); |
b6396403 SG |
549 | #endif |
550 | ||
bf371b4c | 551 | #if CONFIG_IS_ENABLED(FIT) |
4ed37abc SG |
552 | if (IS_ENABLED(CONFIG_FPGA)) { |
553 | /* find bitstreams */ | |
554 | ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT, | |
555 | NULL, NULL); | |
556 | if (ret) { | |
557 | printf("FPGA image is corrupted or invalid\n"); | |
558 | return 1; | |
559 | } | |
62afc601 | 560 | } |
62afc601 | 561 | |
84a07dbf KA |
562 | /* find all of the loadables */ |
563 | ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT, | |
564 | NULL, NULL); | |
565 | if (ret) { | |
566 | printf("Loadable(s) is corrupt or invalid\n"); | |
567 | return 1; | |
568 | } | |
84a07dbf KA |
569 | #endif |
570 | ||
b6396403 SG |
571 | return 0; |
572 | } | |
573 | ||
09140113 SG |
574 | static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc, |
575 | char *const argv[]) | |
b6396403 SG |
576 | { |
577 | if (((images.os.type == IH_TYPE_KERNEL) || | |
578 | (images.os.type == IH_TYPE_KERNEL_NOLOAD) || | |
579 | (images.os.type == IH_TYPE_MULTI)) && | |
580 | (images.os.os == IH_OS_LINUX || | |
581 | images.os.os == IH_OS_VXWORKS)) | |
fbde7589 | 582 | return bootm_find_images(flag, argc, argv, 0, 0); |
b6396403 SG |
583 | |
584 | return 0; | |
585 | } | |
40e5975f SG |
586 | #endif /* USE_HOSTC */ |
587 | ||
2090854c | 588 | #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE) |
3086c055 SG |
589 | /** |
590 | * handle_decomp_error() - display a decompression error | |
591 | * | |
592 | * This function tries to produce a useful message. In the case where the | |
593 | * uncompressed size is the same as the available space, we can assume that | |
594 | * the image is too large for the buffer. | |
595 | * | |
596 | * @comp_type: Compression type being used (IH_COMP_...) | |
597 | * @uncomp_size: Number of bytes uncompressed | |
c45568cc | 598 | * @buf_size: Number of bytes the decompresion buffer was |
2090854c | 599 | * @ret: errno error code received from compression library |
185f812c | 600 | * Return: Appropriate BOOTM_ERR_ error code |
3086c055 | 601 | */ |
c45568cc TR |
602 | static int handle_decomp_error(int comp_type, size_t uncomp_size, |
603 | size_t buf_size, int ret) | |
40e5975f | 604 | { |
3086c055 SG |
605 | const char *name = genimg_get_comp_name(comp_type); |
606 | ||
2090854c JW |
607 | /* ENOSYS means unimplemented compression type, don't reset. */ |
608 | if (ret == -ENOSYS) | |
609 | return BOOTM_ERR_UNIMPLEMENTED; | |
610 | ||
c45568cc | 611 | if (uncomp_size >= buf_size) |
3086c055 | 612 | printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n"); |
40e5975f | 613 | else |
3086c055 SG |
614 | printf("%s: uncompress error %d\n", name, ret); |
615 | ||
616 | /* | |
617 | * The decompression routines are now safe, so will not write beyond | |
618 | * their bounds. Probably it is not necessary to reset, but maintain | |
619 | * the current behaviour for now. | |
620 | */ | |
621 | printf("Must RESET board to recover\n"); | |
40e5975f SG |
622 | #ifndef USE_HOSTCC |
623 | bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); | |
624 | #endif | |
625 | ||
626 | return BOOTM_ERR_RESET; | |
627 | } | |
2090854c | 628 | #endif |
2b164f1c | 629 | |
ce1400f6 | 630 | #ifndef USE_HOSTCC |
d9d7c20b | 631 | static int bootm_load_os(struct bootm_headers *images, int boot_progress) |
2b164f1c | 632 | { |
da79b2f2 | 633 | struct image_info os = images->os; |
2b164f1c | 634 | ulong load = os.load; |
cc955358 | 635 | ulong load_end; |
2b164f1c SG |
636 | ulong blob_start = os.start; |
637 | ulong blob_end = os.end; | |
638 | ulong image_start = os.image_start; | |
639 | ulong image_len = os.image_len; | |
bbac9222 | 640 | ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN); |
2b164f1c SG |
641 | bool no_overlap; |
642 | void *load_buf, *image_buf; | |
643 | int err; | |
644 | ||
645 | load_buf = map_sysmem(load, 0); | |
646 | image_buf = map_sysmem(os.image_start, image_len); | |
2090854c JW |
647 | err = image_decomp(os.comp, load, os.image_start, os.type, |
648 | load_buf, image_buf, image_len, | |
649 | CONFIG_SYS_BOOTM_LEN, &load_end); | |
2b164f1c | 650 | if (err) { |
c45568cc TR |
651 | err = handle_decomp_error(os.comp, load_end - load, |
652 | CONFIG_SYS_BOOTM_LEN, err); | |
2b164f1c SG |
653 | bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); |
654 | return err; | |
655 | } | |
21d39468 HS |
656 | /* We need the decompressed image size in the next steps */ |
657 | images->os.image_len = load_end - load; | |
bbac9222 | 658 | |
b4353b37 | 659 | flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start); |
b6396403 | 660 | |
cc955358 | 661 | debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end); |
b6396403 SG |
662 | bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED); |
663 | ||
2b164f1c SG |
664 | no_overlap = (os.comp == IH_COMP_NONE && load == image_start); |
665 | ||
cc955358 | 666 | if (!no_overlap && load < blob_end && load_end > blob_start) { |
b6396403 SG |
667 | debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n", |
668 | blob_start, blob_end); | |
669 | debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load, | |
cc955358 | 670 | load_end); |
b6396403 SG |
671 | |
672 | /* Check what type of image this is. */ | |
673 | if (images->legacy_hdr_valid) { | |
674 | if (image_get_type(&images->legacy_hdr_os_copy) | |
675 | == IH_TYPE_MULTI) | |
676 | puts("WARNING: legacy format multi component image overwritten\n"); | |
677 | return BOOTM_ERR_OVERLAP; | |
678 | } else { | |
679 | puts("ERROR: new format image overwritten - must RESET the board to recover\n"); | |
680 | bootstage_error(BOOTSTAGE_ID_OVERWRITTEN); | |
681 | return BOOTM_ERR_RESET; | |
682 | } | |
683 | } | |
684 | ||
cc955358 TR |
685 | lmb_reserve(&images->lmb, images->os.load, (load_end - |
686 | images->os.load)); | |
b6396403 SG |
687 | return 0; |
688 | } | |
689 | ||
690 | /** | |
691 | * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot | |
692 | * | |
185f812c | 693 | * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were |
b6396403 SG |
694 | * enabled) |
695 | */ | |
696 | ulong bootm_disable_interrupts(void) | |
697 | { | |
698 | ulong iflag; | |
699 | ||
700 | /* | |
701 | * We have reached the point of no return: we are going to | |
702 | * overwrite all exception vector code, so we cannot easily | |
703 | * recover from any failures any more... | |
704 | */ | |
705 | iflag = disable_interrupts(); | |
706 | #ifdef CONFIG_NETCONSOLE | |
707 | /* Stop the ethernet stack if NetConsole could have left it up */ | |
708 | eth_halt(); | |
b6396403 SG |
709 | #endif |
710 | ||
711 | #if defined(CONFIG_CMD_USB) | |
712 | /* | |
713 | * turn off USB to prevent the host controller from writing to the | |
714 | * SDRAM while Linux is booting. This could happen (at least for OHCI | |
715 | * controller), because the HCCA (Host Controller Communication Area) | |
716 | * lies within the SDRAM and the host controller writes continously to | |
717 | * this area (as busmaster!). The HccaFrameNumber is for example | |
718 | * updated every 1 ms within the HCCA structure in SDRAM! For more | |
719 | * details see the OpenHCI specification. | |
720 | */ | |
721 | usb_stop(); | |
722 | #endif | |
723 | return iflag; | |
724 | } | |
725 | ||
6cd92b1a | 726 | #define CONSOLE_ARG "console=" |
ba9aa40b SA |
727 | #define NULL_CONSOLE (CONSOLE_ARG "ttynull") |
728 | #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE) | |
b6396403 | 729 | |
b6386f38 SG |
730 | /** |
731 | * fixup_silent_linux() - Handle silencing the linux boot if required | |
732 | * | |
733 | * This uses the silent_linux envvar to control whether to add/set a "console=" | |
734 | * parameter to the command line | |
735 | * | |
736 | * @buf: Buffer containing the string to process | |
737 | * @maxlen: Maximum length of buffer | |
185f812c | 738 | * Return: 0 if OK, -ENOSPC if @maxlen is too small |
b6386f38 SG |
739 | */ |
740 | static int fixup_silent_linux(char *buf, int maxlen) | |
b6396403 | 741 | { |
b6396403 | 742 | int want_silent; |
b6386f38 SG |
743 | char *cmdline; |
744 | int size; | |
b6396403 | 745 | |
b6386f38 SG |
746 | /* |
747 | * Move the input string to the end of buffer. The output string will be | |
748 | * built up at the start. | |
749 | */ | |
750 | size = strlen(buf) + 1; | |
751 | if (size * 2 > maxlen) | |
752 | return -ENOSPC; | |
753 | cmdline = buf + maxlen - size; | |
754 | memmove(cmdline, buf, size); | |
b6396403 SG |
755 | /* |
756 | * Only fix cmdline when requested. The environment variable can be: | |
757 | * | |
758 | * no - we never fixup | |
759 | * yes - we always fixup | |
760 | * unset - we rely on the console silent flag | |
761 | */ | |
bfebc8c9 | 762 | want_silent = env_get_yesno("silent_linux"); |
b6396403 | 763 | if (want_silent == 0) |
4ae42643 | 764 | return 0; |
b6396403 | 765 | else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT)) |
4ae42643 | 766 | return 0; |
b6396403 SG |
767 | |
768 | debug("before silent fix-up: %s\n", cmdline); | |
b6386f38 | 769 | if (*cmdline) { |
b6396403 SG |
770 | char *start = strstr(cmdline, CONSOLE_ARG); |
771 | ||
b6386f38 SG |
772 | /* Check space for maximum possible new command line */ |
773 | if (size + CONSOLE_ARG_SIZE > maxlen) | |
4ae42643 | 774 | return -ENOSPC; |
b6396403 SG |
775 | |
776 | if (start) { | |
777 | char *end = strchr(start, ' '); | |
6cd92b1a | 778 | int start_bytes; |
b6396403 | 779 | |
ba9aa40b | 780 | start_bytes = start - cmdline; |
6cd92b1a | 781 | strncpy(buf, cmdline, start_bytes); |
ba9aa40b | 782 | strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE); |
b6396403 | 783 | if (end) |
ba9aa40b | 784 | strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end); |
b6396403 | 785 | else |
ba9aa40b | 786 | buf[start_bytes + CONSOLE_ARG_SIZE] = '\0'; |
b6396403 | 787 | } else { |
ba9aa40b | 788 | sprintf(buf, "%s %s", cmdline, NULL_CONSOLE); |
b6396403 | 789 | } |
b6386f38 SG |
790 | if (buf + strlen(buf) >= cmdline) |
791 | return -ENOSPC; | |
b6396403 | 792 | } else { |
ba9aa40b | 793 | if (maxlen < CONSOLE_ARG_SIZE) |
b6386f38 | 794 | return -ENOSPC; |
ba9aa40b | 795 | strcpy(buf, NULL_CONSOLE); |
b6396403 | 796 | } |
b6386f38 SG |
797 | debug("after silent fix-up: %s\n", buf); |
798 | ||
799 | return 0; | |
800 | } | |
801 | ||
51bb3384 SG |
802 | /** |
803 | * process_subst() - Handle substitution of ${...} fields in the environment | |
804 | * | |
805 | * Handle variable substitution in the provided buffer | |
806 | * | |
807 | * @buf: Buffer containing the string to process | |
808 | * @maxlen: Maximum length of buffer | |
185f812c | 809 | * Return: 0 if OK, -ENOSPC if @maxlen is too small |
51bb3384 SG |
810 | */ |
811 | static int process_subst(char *buf, int maxlen) | |
812 | { | |
813 | char *cmdline; | |
814 | int size; | |
815 | int ret; | |
816 | ||
817 | /* Move to end of buffer */ | |
818 | size = strlen(buf) + 1; | |
819 | cmdline = buf + maxlen - size; | |
820 | if (buf + size > cmdline) | |
821 | return -ENOSPC; | |
822 | memmove(cmdline, buf, size); | |
823 | ||
824 | ret = cli_simple_process_macros(cmdline, buf, cmdline - buf); | |
825 | ||
826 | return ret; | |
827 | } | |
828 | ||
4448fe8e SG |
829 | int bootm_process_cmdline(char *buf, int maxlen, int flags) |
830 | { | |
831 | int ret; | |
832 | ||
833 | /* Check config first to enable compiler to eliminate code */ | |
834 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && | |
835 | !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && | |
836 | (flags & BOOTM_CL_SILENT)) { | |
837 | ret = fixup_silent_linux(buf, maxlen); | |
838 | if (ret) | |
839 | return log_msg_ret("silent", ret); | |
840 | } | |
a8d69627 SG |
841 | if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) && |
842 | (flags & BOOTM_CL_SUBST)) { | |
51bb3384 SG |
843 | ret = process_subst(buf, maxlen); |
844 | if (ret) | |
185756ec | 845 | return log_msg_ret("subst", ret); |
51bb3384 | 846 | } |
4448fe8e SG |
847 | |
848 | return 0; | |
849 | } | |
850 | ||
b3c01678 | 851 | int bootm_process_cmdline_env(int flags) |
b6386f38 SG |
852 | { |
853 | const int maxlen = MAX_CMDLINE_SIZE; | |
b3c01678 | 854 | bool do_silent; |
b6386f38 SG |
855 | const char *env; |
856 | char *buf; | |
857 | int ret; | |
b6396403 | 858 | |
b6386f38 SG |
859 | /* First check if any action is needed */ |
860 | do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) && | |
b3c01678 | 861 | !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT); |
51bb3384 | 862 | if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST)) |
b6386f38 SG |
863 | return 0; |
864 | ||
865 | env = env_get("bootargs"); | |
866 | if (env && strlen(env) >= maxlen) | |
867 | return -E2BIG; | |
868 | buf = malloc(maxlen); | |
869 | if (!buf) | |
870 | return -ENOMEM; | |
871 | if (env) | |
872 | strcpy(buf, env); | |
873 | else | |
874 | *buf = '\0'; | |
4448fe8e | 875 | ret = bootm_process_cmdline(buf, maxlen, flags); |
b6386f38 SG |
876 | if (!ret) { |
877 | ret = env_set("bootargs", buf); | |
878 | ||
879 | /* | |
880 | * If buf is "" and bootargs does not exist, this will produce | |
881 | * an error trying to delete bootargs. Ignore it | |
882 | */ | |
883 | if (ret == -ENOENT) | |
884 | ret = 0; | |
885 | } | |
b6396403 | 886 | free(buf); |
b6386f38 SG |
887 | if (ret) |
888 | return log_msg_ret("env", ret); | |
4ae42643 SG |
889 | |
890 | return 0; | |
b6396403 | 891 | } |
b6396403 | 892 | |
dec166d6 EJ |
893 | int bootm_measure(struct bootm_headers *images) |
894 | { | |
895 | int ret = 0; | |
896 | ||
897 | /* Skip measurement if EFI is going to do it */ | |
898 | if (images->os.os == IH_OS_EFI && | |
899 | IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) && | |
900 | IS_ENABLED(CONFIG_BOOTM_EFI)) | |
901 | return ret; | |
902 | ||
903 | if (IS_ENABLED(CONFIG_MEASURED_BOOT)) { | |
904 | struct tcg2_event_log elog; | |
905 | struct udevice *dev; | |
906 | void *initrd_buf; | |
907 | void *image_buf; | |
908 | const char *s; | |
909 | u32 rd_len; | |
910 | bool ign; | |
911 | ||
912 | elog.log_size = 0; | |
913 | ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG); | |
914 | ret = tcg2_measurement_init(&dev, &elog, ign); | |
915 | if (ret) | |
916 | return ret; | |
917 | ||
918 | image_buf = map_sysmem(images->os.image_start, | |
919 | images->os.image_len); | |
920 | ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len, | |
921 | image_buf, EV_COMPACT_HASH, | |
922 | strlen("linux") + 1, (u8 *)"linux"); | |
923 | if (ret) | |
924 | goto unmap_image; | |
925 | ||
926 | rd_len = images->rd_end - images->rd_start; | |
927 | initrd_buf = map_sysmem(images->rd_start, rd_len); | |
928 | ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf, | |
929 | EV_COMPACT_HASH, strlen("initrd") + 1, | |
930 | (u8 *)"initrd"); | |
931 | if (ret) | |
932 | goto unmap_initrd; | |
933 | ||
934 | if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) { | |
935 | ret = tcg2_measure_data(dev, &elog, 0, images->ft_len, | |
936 | (u8 *)images->ft_addr, | |
937 | EV_TABLE_OF_DEVICES, | |
938 | strlen("dts") + 1, | |
939 | (u8 *)"dts"); | |
940 | if (ret) | |
941 | goto unmap_initrd; | |
942 | } | |
943 | ||
944 | s = env_get("bootargs"); | |
945 | if (!s) | |
946 | s = ""; | |
947 | ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s, | |
948 | EV_PLATFORM_CONFIG_FLAGS, | |
949 | strlen(s) + 1, (u8 *)s); | |
950 | ||
951 | unmap_initrd: | |
952 | unmap_sysmem(initrd_buf); | |
953 | ||
954 | unmap_image: | |
955 | unmap_sysmem(image_buf); | |
956 | tcg2_measurement_term(dev, &elog, ret != 0); | |
957 | } | |
958 | ||
959 | return ret; | |
960 | } | |
961 | ||
b6396403 SG |
962 | /** |
963 | * Execute selected states of the bootm command. | |
964 | * | |
965 | * Note the arguments to this state must be the first argument, Any 'bootm' | |
966 | * or sub-command arguments must have already been taken. | |
967 | * | |
968 | * Note that if states contains more than one flag it MUST contain | |
969 | * BOOTM_STATE_START, since this handles and consumes the command line args. | |
970 | * | |
971 | * Also note that aside from boot_os_fn functions and bootm_load_os no other | |
972 | * functions we store the return value of in 'ret' may use a negative return | |
973 | * value, without special handling. | |
974 | * | |
975 | * @param cmdtp Pointer to bootm command table entry | |
976 | * @param flag Command flags (CMD_FLAG_...) | |
977 | * @param argc Number of subcommand arguments (0 = no arguments) | |
978 | * @param argv Arguments | |
979 | * @param states Mask containing states to run (BOOTM_STATE_...) | |
980 | * @param images Image header information | |
981 | * @param boot_progress 1 to show boot progress, 0 to not do this | |
185f812c | 982 | * Return: 0 if ok, something else on error. Some errors will cause this |
b6396403 SG |
983 | * function to perform a reboot! If states contains BOOTM_STATE_OS_GO |
984 | * then the intent is to boot an OS, so this function will not return | |
985 | * unless the image type is standalone. | |
986 | */ | |
09140113 | 987 | int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, |
d9d7c20b | 988 | char *const argv[], int states, struct bootm_headers *images, |
09140113 | 989 | int boot_progress) |
b6396403 SG |
990 | { |
991 | boot_os_fn *boot_fn; | |
992 | ulong iflag = 0; | |
993 | int ret = 0, need_boot_fn; | |
994 | ||
995 | images->state |= states; | |
996 | ||
997 | /* | |
998 | * Work through the states and see how far we get. We stop on | |
999 | * any error. | |
1000 | */ | |
1001 | if (states & BOOTM_STATE_START) | |
a50e886a | 1002 | ret = bootm_start(); |
b6396403 | 1003 | |
9d46e63d | 1004 | if (!ret && (states & BOOTM_STATE_PRE_LOAD)) |
921070bc | 1005 | ret = bootm_pre_load(argv[0]); |
9d46e63d | 1006 | |
b6396403 | 1007 | if (!ret && (states & BOOTM_STATE_FINDOS)) |
3e3bd5bd | 1008 | ret = bootm_find_os(cmdtp->name, argv[0]); |
b6396403 | 1009 | |
ba079840 | 1010 | if (!ret && (states & BOOTM_STATE_FINDOTHER)) |
b6396403 | 1011 | ret = bootm_find_other(cmdtp, flag, argc, argv); |
b6396403 | 1012 | |
dec166d6 EJ |
1013 | if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret && |
1014 | (states & BOOTM_STATE_MEASURE)) | |
1015 | bootm_measure(images); | |
1016 | ||
b6396403 SG |
1017 | /* Load the OS */ |
1018 | if (!ret && (states & BOOTM_STATE_LOADOS)) { | |
b6396403 | 1019 | iflag = bootm_disable_interrupts(); |
cc955358 TR |
1020 | ret = bootm_load_os(images, 0); |
1021 | if (ret && ret != BOOTM_ERR_OVERLAP) | |
b6396403 SG |
1022 | goto err; |
1023 | else if (ret == BOOTM_ERR_OVERLAP) | |
1024 | ret = 0; | |
b6396403 SG |
1025 | } |
1026 | ||
1027 | /* Relocate the ramdisk */ | |
1028 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH | |
1029 | if (!ret && (states & BOOTM_STATE_RAMDISK)) { | |
1030 | ulong rd_len = images->rd_end - images->rd_start; | |
1031 | ||
1032 | ret = boot_ramdisk_high(&images->lmb, images->rd_start, | |
1033 | rd_len, &images->initrd_start, &images->initrd_end); | |
1034 | if (!ret) { | |
018f5303 SG |
1035 | env_set_hex("initrd_start", images->initrd_start); |
1036 | env_set_hex("initrd_end", images->initrd_end); | |
b6396403 SG |
1037 | } |
1038 | } | |
1039 | #endif | |
0c303f9a | 1040 | #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB) |
b6396403 SG |
1041 | if (!ret && (states & BOOTM_STATE_FDT)) { |
1042 | boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr); | |
1043 | ret = boot_relocate_fdt(&images->lmb, &images->ft_addr, | |
1044 | &images->ft_len); | |
1045 | } | |
1046 | #endif | |
1047 | ||
1048 | /* From now on, we need the OS boot function */ | |
1049 | if (ret) | |
1050 | return ret; | |
1051 | boot_fn = bootm_os_get_boot_func(images->os.os); | |
1052 | need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE | | |
1053 | BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP | | |
1054 | BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO); | |
1055 | if (boot_fn == NULL && need_boot_fn) { | |
1056 | if (iflag) | |
1057 | enable_interrupts(); | |
1058 | printf("ERROR: booting os '%s' (%d) is not supported\n", | |
1059 | genimg_get_os_name(images->os.os), images->os.os); | |
1060 | bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS); | |
1061 | return 1; | |
1062 | } | |
1063 | ||
19e8649e | 1064 | |
b6396403 SG |
1065 | /* Call various other states that are not generally used */ |
1066 | if (!ret && (states & BOOTM_STATE_OS_CMDLINE)) | |
1067 | ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images); | |
1068 | if (!ret && (states & BOOTM_STATE_OS_BD_T)) | |
1069 | ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images); | |
19e8649e | 1070 | if (!ret && (states & BOOTM_STATE_OS_PREP)) { |
51bb3384 | 1071 | ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX); |
d9477a0a SG |
1072 | if (ret) { |
1073 | printf("Cmdline setup failed (err=%d)\n", ret); | |
1074 | ret = CMD_RET_FAILURE; | |
1075 | goto err; | |
4ae42643 | 1076 | } |
b6396403 | 1077 | ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); |
19e8649e | 1078 | } |
b6396403 SG |
1079 | |
1080 | #ifdef CONFIG_TRACE | |
1081 | /* Pretend to run the OS, then run a user command */ | |
1082 | if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) { | |
00caae6d | 1083 | char *cmd_list = env_get("fakegocmd"); |
b6396403 SG |
1084 | |
1085 | ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO, | |
1086 | images, boot_fn); | |
1087 | if (!ret && cmd_list) | |
1088 | ret = run_command_list(cmd_list, -1, flag); | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | /* Check for unsupported subcommand. */ | |
1093 | if (ret) { | |
13819f07 | 1094 | printf("subcommand failed (err=%d)\n", ret); |
b6396403 SG |
1095 | return ret; |
1096 | } | |
1097 | ||
1098 | /* Now run the OS! We hope this doesn't return */ | |
1099 | if (!ret && (states & BOOTM_STATE_OS_GO)) | |
1100 | ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO, | |
1101 | images, boot_fn); | |
1102 | ||
1103 | /* Deal with any fallout */ | |
1104 | err: | |
1105 | if (iflag) | |
1106 | enable_interrupts(); | |
1107 | ||
1108 | if (ret == BOOTM_ERR_UNIMPLEMENTED) | |
1109 | bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL); | |
1110 | else if (ret == BOOTM_ERR_RESET) | |
1111 | do_reset(cmdtp, flag, argc, argv); | |
1112 | ||
1113 | return ret; | |
1114 | } | |
1115 | ||
daffb0be SG |
1116 | int bootm_boot_start(ulong addr, const char *cmdline) |
1117 | { | |
1118 | static struct cmd_tbl cmd = {"bootm"}; | |
1119 | char addr_str[30]; | |
1120 | char *argv[] = {addr_str, NULL}; | |
1121 | int states; | |
1122 | int ret; | |
1123 | ||
1124 | /* | |
1125 | * TODO([email protected]): This uses the command-line interface, but | |
1126 | * should not. To clean this up, the various bootm states need to be | |
1127 | * passed an info structure instead of cmdline flags. Then this can | |
1128 | * set up the required info and move through the states without needing | |
1129 | * the command line. | |
1130 | */ | |
1131 | states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD | | |
1132 | BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS | | |
1133 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | | |
1134 | BOOTM_STATE_OS_GO; | |
1135 | if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH)) | |
1136 | states |= BOOTM_STATE_RAMDISK; | |
1137 | if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS)) | |
1138 | states |= BOOTM_STATE_OS_CMDLINE; | |
1139 | images.state |= states; | |
1140 | ||
1141 | snprintf(addr_str, sizeof(addr_str), "%lx", addr); | |
1142 | ||
1143 | ret = env_set("bootargs", cmdline); | |
1144 | if (ret) { | |
1145 | printf("Failed to set cmdline\n"); | |
1146 | return ret; | |
1147 | } | |
1148 | ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1); | |
1149 | ||
1150 | return ret; | |
1151 | } | |
1152 | ||
f6c6df7e HS |
1153 | /** |
1154 | * switch_to_non_secure_mode() - switch to non-secure mode | |
1155 | * | |
1156 | * This routine is overridden by architectures requiring this feature. | |
1157 | */ | |
1158 | void __weak switch_to_non_secure_mode(void) | |
1159 | { | |
1160 | } | |
1161 | ||
ce1400f6 SG |
1162 | #else /* USE_HOSTCC */ |
1163 | ||
93e07880 | 1164 | #if defined(CONFIG_FIT_SIGNATURE) |
8a9d0373 SG |
1165 | static int bootm_host_load_image(const void *fit, int req_image_type, |
1166 | int cfg_noffset) | |
ce1400f6 SG |
1167 | { |
1168 | const char *fit_uname_config = NULL; | |
1169 | ulong data, len; | |
d9d7c20b | 1170 | struct bootm_headers images; |
ce1400f6 | 1171 | int noffset; |
c45568cc | 1172 | ulong load_end, buf_size; |
ce1400f6 | 1173 | uint8_t image_type; |
0cd57f29 | 1174 | uint8_t image_comp; |
ce1400f6 SG |
1175 | void *load_buf; |
1176 | int ret; | |
1177 | ||
8a9d0373 | 1178 | fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL); |
ce1400f6 SG |
1179 | memset(&images, '\0', sizeof(images)); |
1180 | images.verify = 1; | |
1181 | noffset = fit_image_load(&images, (ulong)fit, | |
1182 | NULL, &fit_uname_config, | |
1183 | IH_ARCH_DEFAULT, req_image_type, -1, | |
1184 | FIT_LOAD_IGNORED, &data, &len); | |
1185 | if (noffset < 0) | |
1186 | return noffset; | |
1187 | if (fit_image_get_type(fit, noffset, &image_type)) { | |
1188 | puts("Can't get image type!\n"); | |
1189 | return -EINVAL; | |
1190 | } | |
1191 | ||
88de6c51 DG |
1192 | if (fit_image_get_comp(fit, noffset, &image_comp)) |
1193 | image_comp = IH_COMP_NONE; | |
ce1400f6 SG |
1194 | |
1195 | /* Allow the image to expand by a factor of 4, should be safe */ | |
c45568cc TR |
1196 | buf_size = (1 << 20) + len * 4; |
1197 | load_buf = malloc(buf_size); | |
0cd57f29 | 1198 | ret = image_decomp(image_comp, 0, data, image_type, load_buf, |
c45568cc | 1199 | (void *)data, len, buf_size, &load_end); |
ce1400f6 | 1200 | free(load_buf); |
081cc197 | 1201 | |
2090854c | 1202 | if (ret) { |
0cd57f29 | 1203 | ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret); |
2090854c JW |
1204 | if (ret != BOOTM_ERR_UNIMPLEMENTED) |
1205 | return ret; | |
1206 | } | |
ce1400f6 SG |
1207 | |
1208 | return 0; | |
1209 | } | |
1210 | ||
1211 | int bootm_host_load_images(const void *fit, int cfg_noffset) | |
1212 | { | |
1213 | static uint8_t image_types[] = { | |
1214 | IH_TYPE_KERNEL, | |
1215 | IH_TYPE_FLATDT, | |
1216 | IH_TYPE_RAMDISK, | |
1217 | }; | |
1218 | int err = 0; | |
1219 | int i; | |
1220 | ||
1221 | for (i = 0; i < ARRAY_SIZE(image_types); i++) { | |
1222 | int ret; | |
1223 | ||
8a9d0373 | 1224 | ret = bootm_host_load_image(fit, image_types[i], cfg_noffset); |
ce1400f6 SG |
1225 | if (!err && ret && ret != -ENOENT) |
1226 | err = ret; | |
1227 | } | |
1228 | ||
1229 | /* Return the first error we found */ | |
1230 | return err; | |
1231 | } | |
93e07880 | 1232 | #endif |
ea51a628 SG |
1233 | |
1234 | #endif /* ndef USE_HOSTCC */ |