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