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