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