]> Git Repo - u-boot.git/blob - boot/image-board.c
Merge patch series "Add DFU and usb boot for TI am62x SK and beagleplay"
[u-boot.git] / boot / image-board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Image code used by boards (and not host tools)
4  *
5  * (C) Copyright 2008 Semihalf
6  *
7  * (C) Copyright 2000-2006
8  * Wolfgang Denk, DENX Software Engineering, [email protected].
9  */
10
11 #include <config.h>
12 #include <bootstage.h>
13 #include <cpu_func.h>
14 #include <display_options.h>
15 #include <env.h>
16 #include <fpga.h>
17 #include <image.h>
18 #include <init.h>
19 #include <log.h>
20 #include <mapmem.h>
21 #include <rtc.h>
22 #include <watchdog.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 /**
29  * image_get_ramdisk - get and verify ramdisk image
30  * @rd_addr: ramdisk image start address
31  * @arch: expected ramdisk architecture
32  * @verify: checksum verification flag
33  *
34  * image_get_ramdisk() returns a pointer to the verified ramdisk image
35  * header. Routine receives image start address and expected architecture
36  * flag. Verification done covers data and header integrity and os/type/arch
37  * fields checking.
38  *
39  * returns:
40  *     pointer to a ramdisk image header, if image was found and valid
41  *     otherwise, return NULL
42  */
43 static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44                                                       int verify)
45 {
46         const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
47
48         if (!image_check_magic(rd_hdr)) {
49                 puts("Bad Magic Number\n");
50                 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51                 return NULL;
52         }
53
54         if (!image_check_hcrc(rd_hdr)) {
55                 puts("Bad Header Checksum\n");
56                 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57                 return NULL;
58         }
59
60         bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61         image_print_contents(rd_hdr);
62
63         if (verify) {
64                 puts("   Verifying Checksum ... ");
65                 if (!image_check_dcrc(rd_hdr)) {
66                         puts("Bad Data CRC\n");
67                         bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68                         return NULL;
69                 }
70                 puts("OK\n");
71         }
72
73         bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75         if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76             !image_check_arch(rd_hdr, arch) ||
77             !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78                 printf("No Linux %s Ramdisk Image\n",
79                        genimg_get_arch_name(arch));
80                 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81                 return NULL;
82         }
83
84         return rd_hdr;
85 }
86
87 /*****************************************************************************/
88 /* Shared dual-format routines */
89 /*****************************************************************************/
90 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;   /* Default Load Address */
91 ulong image_save_addr;                  /* Default Save Address */
92 ulong image_save_size;                  /* Default Save Size (in bytes) */
93
94 static int on_loadaddr(const char *name, const char *value, enum env_op op,
95                        int flags)
96 {
97         switch (op) {
98         case env_op_create:
99         case env_op_overwrite:
100                 image_load_addr = hextoul(value, NULL);
101                 break;
102         default:
103                 break;
104         }
105
106         return 0;
107 }
108 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110 phys_addr_t env_get_bootm_low(void)
111 {
112         char *s = env_get("bootm_low");
113
114         if (s)
115                 return simple_strtoull(s, NULL, 16);
116
117 #if defined(CFG_SYS_SDRAM_BASE)
118         return CFG_SYS_SDRAM_BASE;
119 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
120         return gd->bd->bi_dram[0].start;
121 #else
122         return 0;
123 #endif
124 }
125
126 phys_size_t env_get_bootm_size(void)
127 {
128         phys_addr_t start, low;
129         phys_size_t size;
130         char *s = env_get("bootm_size");
131
132         if (s)
133                 return simple_strtoull(s, NULL, 16);
134
135         start = gd->ram_base;
136         size = gd->ram_size;
137
138         if (start + size > gd->ram_top)
139                 size = gd->ram_top - start;
140
141         s = env_get("bootm_low");
142         if (s)
143                 low = simple_strtoull(s, NULL, 16);
144         else
145                 low = start;
146
147         return size - (low - start);
148 }
149
150 phys_size_t env_get_bootm_mapsize(void)
151 {
152         char *s = env_get("bootm_mapsize");
153
154         if (s)
155                 return simple_strtoull(s, NULL, 16);
156
157 #if defined(CFG_SYS_BOOTMAPSZ)
158         return CFG_SYS_BOOTMAPSZ;
159 #else
160         return env_get_bootm_size();
161 #endif
162 }
163
164 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
165 {
166         if (to == from)
167                 return;
168
169         if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
170                 if (to > from) {
171                         from += len;
172                         to += len;
173                 }
174                 while (len > 0) {
175                         size_t tail = (len > chunksz) ? chunksz : len;
176
177                         schedule();
178                         if (to > from) {
179                                 to -= tail;
180                                 from -= tail;
181                         }
182                         memmove(to, from, tail);
183                         if (to < from) {
184                                 to += tail;
185                                 from += tail;
186                         }
187                         len -= tail;
188                 }
189         } else {
190                 memmove(to, from, len);
191         }
192 }
193
194 ulong genimg_get_kernel_addr_fit(const char *const img_addr,
195                                  const char **fit_uname_config,
196                                  const char **fit_uname_kernel)
197 {
198         ulong kernel_addr;
199
200         /* find out kernel image address */
201         if (!img_addr) {
202                 kernel_addr = image_load_addr;
203                 debug("*  kernel: default image load address = 0x%08lx\n",
204                       image_load_addr);
205         } else if (CONFIG_IS_ENABLED(FIT) &&
206                    fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
207                                   fit_uname_config)) {
208                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
209                       *fit_uname_config, kernel_addr);
210         } else if (CONFIG_IS_ENABLED(FIT) &&
211                    fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
212                                       fit_uname_kernel)) {
213                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
214                       *fit_uname_kernel, kernel_addr);
215         } else {
216                 kernel_addr = hextoul(img_addr, NULL);
217                 debug("*  kernel: cmdline image address = 0x%08lx\n",
218                       kernel_addr);
219         }
220
221         return kernel_addr;
222 }
223
224 /**
225  * genimg_get_kernel_addr() is the simple version of
226  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
227  */
228 ulong genimg_get_kernel_addr(char * const img_addr)
229 {
230         const char *fit_uname_config = NULL;
231         const char *fit_uname_kernel = NULL;
232
233         return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
234                                           &fit_uname_kernel);
235 }
236
237 /**
238  * genimg_get_format - get image format type
239  * @img_addr: image start address
240  *
241  * genimg_get_format() checks whether provided address points to a valid
242  * legacy or FIT image.
243  *
244  * New uImage format and FDT blob are based on a libfdt. FDT blob
245  * may be passed directly or embedded in a FIT image. In both situations
246  * genimg_get_format() must be able to dectect libfdt header.
247  *
248  * returns:
249  *     image format type or IMAGE_FORMAT_INVALID if no image is present
250  */
251 int genimg_get_format(const void *img_addr)
252 {
253         if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
254                 const struct legacy_img_hdr *hdr;
255
256                 hdr = (const struct legacy_img_hdr *)img_addr;
257                 if (image_check_magic(hdr))
258                         return IMAGE_FORMAT_LEGACY;
259         }
260         if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
261                 if (!fdt_check_header(img_addr))
262                         return IMAGE_FORMAT_FIT;
263         }
264         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
265             is_android_boot_image_header(img_addr))
266                 return IMAGE_FORMAT_ANDROID;
267
268         return IMAGE_FORMAT_INVALID;
269 }
270
271 /**
272  * fit_has_config - check if there is a valid FIT configuration
273  * @images: pointer to the bootm command headers structure
274  *
275  * fit_has_config() checks if there is a FIT configuration in use
276  * (if FTI support is present).
277  *
278  * returns:
279  *     0, no FIT support or no configuration found
280  *     1, configuration found
281  */
282 int genimg_has_config(struct bootm_headers *images)
283 {
284         if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
285                 return 1;
286
287         return 0;
288 }
289
290 /**
291  * select_ramdisk() - Select and locate the ramdisk to use
292  *
293  * @images: pointer to the bootm images structure
294  * @select: name of ramdisk to select, or hex address, NULL for any
295  * @arch: expected ramdisk architecture
296  * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
297  * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
298  * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
299  *      other -ve value on other error
300  */
301 static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
302                           ulong *rd_datap, ulong *rd_lenp)
303 {
304         const char *fit_uname_config;
305         const char *fit_uname_ramdisk;
306         bool done_select = !select;
307         bool done = false;
308         int rd_noffset;
309         ulong rd_addr = 0;
310         char *buf;
311
312         if (CONFIG_IS_ENABLED(FIT)) {
313                 fit_uname_config = images->fit_uname_cfg;
314                 fit_uname_ramdisk = NULL;
315
316                 if (select) {
317                         ulong default_addr;
318                         /*
319                          * If the init ramdisk comes from the FIT image and
320                          * the FIT image address is omitted in the command
321                          * line argument, try to use os FIT image address or
322                          * default load address.
323                          */
324                         if (images->fit_uname_os)
325                                 default_addr = (ulong)images->fit_hdr_os;
326                         else
327                                 default_addr = image_load_addr;
328
329                         if (fit_parse_conf(select, default_addr, &rd_addr,
330                                            &fit_uname_config)) {
331                                 debug("*  ramdisk: config '%s' from image at 0x%08lx\n",
332                                       fit_uname_config, rd_addr);
333                                 done_select = true;
334                         } else if (fit_parse_subimage(select, default_addr,
335                                                       &rd_addr,
336                                                       &fit_uname_ramdisk)) {
337                                 debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
338                                       fit_uname_ramdisk, rd_addr);
339                                 done_select = true;
340                         }
341                 }
342         }
343         if (!done_select) {
344                 rd_addr = hextoul(select, NULL);
345                 debug("*  ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
346         }
347         if (CONFIG_IS_ENABLED(FIT) && !select) {
348                 /* use FIT configuration provided in first bootm
349                  * command argument. If the property is not defined,
350                  * quit silently (with -ENOPKG)
351                  */
352                 rd_addr = map_to_sysmem(images->fit_hdr_os);
353                 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
354                                                       rd_addr);
355                 if (rd_noffset == -ENOENT)
356                         return -ENOPKG;
357                 else if (rd_noffset < 0)
358                         return rd_noffset;
359         }
360
361         /*
362          * Check if there is an initrd image at the
363          * address provided in the second bootm argument
364          * check image type, for FIT images get FIT node.
365          */
366         buf = map_sysmem(rd_addr, 0);
367         switch (genimg_get_format(buf)) {
368         case IMAGE_FORMAT_LEGACY:
369                 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
370                         const struct legacy_img_hdr *rd_hdr;
371
372                         printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
373                                rd_addr);
374
375                         bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
376                         rd_hdr = image_get_ramdisk(rd_addr, arch,
377                                                    images->verify);
378
379                         if (!rd_hdr)
380                                 return -ENOENT;
381
382                         *rd_datap = image_get_data(rd_hdr);
383                         *rd_lenp = image_get_data_size(rd_hdr);
384                         done = true;
385                 }
386                 break;
387         case IMAGE_FORMAT_FIT:
388                 if (CONFIG_IS_ENABLED(FIT)) {
389                         rd_noffset = fit_image_load(images, rd_addr,
390                                                     &fit_uname_ramdisk,
391                                                     &fit_uname_config,
392                                                     arch, IH_TYPE_RAMDISK,
393                                                     BOOTSTAGE_ID_FIT_RD_START,
394                                                     FIT_LOAD_OPTIONAL_NON_ZERO,
395                                                     rd_datap, rd_lenp);
396                         if (rd_noffset < 0)
397                                 return rd_noffset;
398
399                         images->fit_hdr_rd = map_sysmem(rd_addr, 0);
400                         images->fit_uname_rd = fit_uname_ramdisk;
401                         images->fit_noffset_rd = rd_noffset;
402                         done = true;
403                 }
404                 break;
405         case IMAGE_FORMAT_ANDROID:
406                 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
407                         int ret;
408                         if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
409                                 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
410                                 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
411
412                                 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
413                                                                 rd_datap, rd_lenp);
414                                 unmap_sysmem(vendor_boot_img);
415                                 unmap_sysmem(boot_img);
416                         } else {
417                                 void *ptr = map_sysmem(images->os.start, 0);
418
419                                 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
420                                 unmap_sysmem(ptr);
421                         }
422
423                         if (ret)
424                                 return ret;
425                         done = true;
426                 }
427                 break;
428         }
429
430         if (!done) {
431                 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
432                         char *end = NULL;
433
434                         if (select)
435                                 end = strchr(select, ':');
436                         if (end) {
437                                 *rd_lenp = hextoul(++end, NULL);
438                                 *rd_datap = rd_addr;
439                                 done = true;
440                         }
441                 }
442
443                 if (!done) {
444                         puts("Wrong Ramdisk Image Format\n");
445                         return -EINVAL;
446                 }
447         }
448
449         return 0;
450 }
451
452 int boot_get_ramdisk(char const *select, struct bootm_headers *images,
453                      uint arch, ulong *rd_start, ulong *rd_end)
454 {
455         ulong rd_data, rd_len;
456
457         *rd_start = 0;
458         *rd_end = 0;
459
460         /*
461          * Look for a '-' which indicates to ignore the
462          * ramdisk argument
463          */
464         if (select && strcmp(select, "-") ==  0) {
465                 debug("## Skipping init Ramdisk\n");
466                 rd_len = 0;
467                 rd_data = 0;
468         } else if (select || genimg_has_config(images)) {
469                 int ret;
470
471                 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
472                 if (ret == -ENOPKG)
473                         return 0;
474                 else if (ret)
475                         return ret;
476         } else if (images->legacy_hdr_valid &&
477                         image_check_type(&images->legacy_hdr_os_copy,
478                                          IH_TYPE_MULTI)) {
479                 /*
480                  * Now check if we have a legacy mult-component image,
481                  * get second entry data start address and len.
482                  */
483                 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
484                 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
485                        (ulong)images->legacy_hdr_os);
486
487                 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
488         } else {
489                 /*
490                  * no initrd image
491                  */
492                 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
493                 rd_len = 0;
494                 rd_data = 0;
495         }
496
497         if (!rd_data) {
498                 debug("## No init Ramdisk\n");
499         } else {
500                 *rd_start = rd_data;
501                 *rd_end = rd_data + rd_len;
502         }
503         debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
504               *rd_start, *rd_end);
505
506         return 0;
507 }
508
509 /**
510  * boot_ramdisk_high - relocate init ramdisk
511  * @lmb: pointer to lmb handle, will be used for memory mgmt
512  * @rd_data: ramdisk data start address
513  * @rd_len: ramdisk data length
514  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
515  *      start address (after possible relocation)
516  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
517  *      end address (after possible relocation)
518  *
519  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
520  * variable and if requested ramdisk data is moved to a specified location.
521  *
522  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
523  * start/end addresses if ramdisk image start and len were provided,
524  * otherwise set initrd_start and initrd_end set to zeros.
525  *
526  * returns:
527  *      0 - success
528  *     -1 - failure
529  */
530 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
531                       ulong *initrd_start, ulong *initrd_end)
532 {
533         char    *s;
534         phys_addr_t initrd_high;
535         int     initrd_copy_to_ram = 1;
536
537         s = env_get("initrd_high");
538         if (s) {
539                 /* a value of "no" or a similar string will act like 0,
540                  * turning the "load high" feature off. This is intentional.
541                  */
542                 initrd_high = hextoul(s, NULL);
543                 if (initrd_high == ~0)
544                         initrd_copy_to_ram = 0;
545         } else {
546                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
547         }
548
549         debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
550               (u64)initrd_high, initrd_copy_to_ram);
551
552         if (rd_data) {
553                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
554                         debug("   in-place initrd\n");
555                         *initrd_start = rd_data;
556                         *initrd_end = rd_data + rd_len;
557                         lmb_reserve(lmb, rd_data, rd_len);
558                 } else {
559                         if (initrd_high)
560                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
561                                                 rd_len, 0x1000, initrd_high);
562                         else
563                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
564                                                                  0x1000);
565
566                         if (*initrd_start == 0) {
567                                 puts("ramdisk - allocation error\n");
568                                 goto error;
569                         }
570                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
571
572                         *initrd_end = *initrd_start + rd_len;
573                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
574                                *initrd_start, *initrd_end);
575
576                         memmove_wd((void *)*initrd_start,
577                                    (void *)rd_data, rd_len, CHUNKSZ);
578
579                         /*
580                          * Ensure the image is flushed to memory to handle
581                          * AMP boot scenarios in which we might not be
582                          * HW cache coherent
583                          */
584                         if (IS_ENABLED(CONFIG_MP)) {
585                                 flush_cache((unsigned long)*initrd_start,
586                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
587                         }
588                         puts("OK\n");
589                 }
590         } else {
591                 *initrd_start = 0;
592                 *initrd_end = 0;
593         }
594         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
595               *initrd_start, *initrd_end);
596
597         return 0;
598
599 error:
600         return -1;
601 }
602
603 int boot_get_setup(struct bootm_headers *images, u8 arch,
604                    ulong *setup_start, ulong *setup_len)
605 {
606         if (!CONFIG_IS_ENABLED(FIT))
607                 return -ENOENT;
608
609         return boot_get_setup_fit(images, arch, setup_start, setup_len);
610 }
611
612 int boot_get_fpga(struct bootm_headers *images)
613 {
614         ulong tmp_img_addr, img_data, img_len;
615         void *buf;
616         int conf_noffset;
617         int fit_img_result;
618         const char *uname, *name;
619         int err;
620         int devnum = 0; /* TODO support multi fpga platforms */
621
622         if (!IS_ENABLED(CONFIG_FPGA))
623                 return -ENOSYS;
624
625         /* Check to see if the images struct has a FIT configuration */
626         if (!genimg_has_config(images)) {
627                 debug("## FIT configuration was not specified\n");
628                 return 0;
629         }
630
631         /*
632          * Obtain the os FIT header from the images struct
633          */
634         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
635         buf = map_sysmem(tmp_img_addr, 0);
636         /*
637          * Check image type. For FIT images get FIT node
638          * and attempt to locate a generic binary.
639          */
640         switch (genimg_get_format(buf)) {
641         case IMAGE_FORMAT_FIT:
642                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
643
644                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
645                                            NULL);
646                 if (!uname) {
647                         debug("## FPGA image is not specified\n");
648                         return 0;
649                 }
650                 fit_img_result = fit_image_load(images,
651                                                 tmp_img_addr,
652                                                 (const char **)&uname,
653                                                 &images->fit_uname_cfg,
654                                                 IH_ARCH_DEFAULT,
655                                                 IH_TYPE_FPGA,
656                                                 BOOTSTAGE_ID_FPGA_INIT,
657                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
658                                                 &img_data, &img_len);
659
660                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
661                       uname, img_data, img_len);
662
663                 if (fit_img_result < 0) {
664                         /* Something went wrong! */
665                         return fit_img_result;
666                 }
667
668                 if (!fpga_is_partial_data(devnum, img_len)) {
669                         name = "full";
670                         err = fpga_loadbitstream(devnum, (char *)img_data,
671                                                  img_len, BIT_FULL);
672                         if (err)
673                                 err = fpga_load(devnum, (const void *)img_data,
674                                                 img_len, BIT_FULL, 0);
675                 } else {
676                         name = "partial";
677                         err = fpga_loadbitstream(devnum, (char *)img_data,
678                                                  img_len, BIT_PARTIAL);
679                         if (err)
680                                 err = fpga_load(devnum, (const void *)img_data,
681                                                 img_len, BIT_PARTIAL, 0);
682                 }
683
684                 if (err)
685                         return err;
686
687                 printf("   Programming %s bitstream... OK\n", name);
688                 break;
689         default:
690                 printf("The given image format is not supported (corrupt?)\n");
691                 return 1;
692         }
693
694         return 0;
695 }
696
697 static void fit_loadable_process(u8 img_type,
698                                  ulong img_data,
699                                  ulong img_len)
700 {
701         int i;
702         const unsigned int count =
703                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
704         struct fit_loadable_tbl *fit_loadable_handler =
705                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
706         /* For each loadable handler */
707         for (i = 0; i < count; i++, fit_loadable_handler++)
708                 /* matching this type */
709                 if (fit_loadable_handler->type == img_type)
710                         /* call that handler with this image data */
711                         fit_loadable_handler->handler(img_data, img_len);
712 }
713
714 int boot_get_loadable(struct bootm_headers *images)
715 {
716         /*
717          * These variables are used to hold the current image location
718          * in system memory.
719          */
720         ulong tmp_img_addr;
721         /*
722          * These two variables are requirements for fit_image_load, but
723          * their values are not used
724          */
725         ulong img_data, img_len;
726         void *buf;
727         int loadables_index;
728         int conf_noffset;
729         int fit_img_result;
730         const char *uname;
731         u8 img_type;
732
733         /* Check to see if the images struct has a FIT configuration */
734         if (!genimg_has_config(images)) {
735                 debug("## FIT configuration was not specified\n");
736                 return 0;
737         }
738
739         /*
740          * Obtain the os FIT header from the images struct
741          */
742         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
743         buf = map_sysmem(tmp_img_addr, 0);
744         /*
745          * Check image type. For FIT images get FIT node
746          * and attempt to locate a generic binary.
747          */
748         switch (genimg_get_format(buf)) {
749         case IMAGE_FORMAT_FIT:
750                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
751
752                 for (loadables_index = 0;
753                      uname = fdt_stringlist_get(buf, conf_noffset,
754                                                 FIT_LOADABLE_PROP,
755                                                 loadables_index, NULL), uname;
756                      loadables_index++) {
757                         fit_img_result = fit_image_load(images, tmp_img_addr,
758                                                         &uname,
759                                                         &images->fit_uname_cfg,
760                                                         IH_ARCH_DEFAULT,
761                                                         IH_TYPE_LOADABLE,
762                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
763                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
764                                                         &img_data, &img_len);
765                         if (fit_img_result < 0) {
766                                 /* Something went wrong! */
767                                 return fit_img_result;
768                         }
769
770                         fit_img_result = fit_image_get_node(buf, uname);
771                         if (fit_img_result < 0) {
772                                 /* Something went wrong! */
773                                 return fit_img_result;
774                         }
775                         fit_img_result = fit_image_get_type(buf,
776                                                             fit_img_result,
777                                                             &img_type);
778                         if (fit_img_result < 0) {
779                                 /* Something went wrong! */
780                                 return fit_img_result;
781                         }
782
783                         fit_loadable_process(img_type, img_data, img_len);
784                 }
785                 break;
786         default:
787                 printf("The given image format is not supported (corrupt?)\n");
788                 return 1;
789         }
790
791         return 0;
792 }
793
794 /**
795  * boot_get_cmdline - allocate and initialize kernel cmdline
796  * @lmb: pointer to lmb handle, will be used for memory mgmt
797  * @cmd_start: pointer to a ulong variable, will hold cmdline start
798  * @cmd_end: pointer to a ulong variable, will hold cmdline end
799  *
800  * This allocates space for kernel command line below
801  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
802  * variable is present its contents is copied to allocated kernel
803  * command line.
804  *
805  * returns:
806  *      0 - success
807  *     -1 - failure
808  */
809 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
810 {
811         int barg;
812         char *cmdline;
813         char *s;
814
815         /*
816          * Help the compiler detect that this function is only called when
817          * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
818          */
819         if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
820                 return 0;
821
822         barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
823         cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
824                                 env_get_bootm_mapsize() + env_get_bootm_low());
825         if (!cmdline)
826                 return -1;
827
828         s = env_get("bootargs");
829         if (!s)
830                 s = "";
831
832         strcpy(cmdline, s);
833
834         *cmd_start = (ulong)cmdline;
835         *cmd_end = *cmd_start + strlen(cmdline);
836
837         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
838
839         return 0;
840 }
841
842 /**
843  * boot_get_kbd - allocate and initialize kernel copy of board info
844  * @lmb: pointer to lmb handle, will be used for memory mgmt
845  * @kbd: double pointer to board info data
846  *
847  * boot_get_kbd() allocates space for kernel copy of board info data below
848  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
849  * with the current u-boot board info data.
850  *
851  * returns:
852  *      0 - success
853  *     -1 - failure
854  */
855 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
856 {
857         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
858                                                        sizeof(struct bd_info),
859                                                        0xf,
860                                                        env_get_bootm_mapsize() +
861                                                        env_get_bootm_low());
862         if (!*kbd)
863                 return -1;
864
865         **kbd = *gd->bd;
866
867         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
868
869         if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
870                 do_bdinfo(NULL, 0, 0, NULL);
871
872         return 0;
873 }
874
875 int image_setup_linux(struct bootm_headers *images)
876 {
877         ulong of_size = images->ft_len;
878         char **of_flat_tree = &images->ft_addr;
879         struct lmb *lmb = images_lmb(images);
880         int ret;
881
882         /* This function cannot be called without lmb support */
883         if (!IS_ENABLED(CONFIG_LMB))
884                 return -EFAULT;
885         if (CONFIG_IS_ENABLED(OF_LIBFDT))
886                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
887
888         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
889                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
890                                        &images->cmdline_end);
891                 if (ret) {
892                         puts("ERROR with allocation of cmdline\n");
893                         return ret;
894                 }
895         }
896
897         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
898                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
899                 if (ret)
900                         return ret;
901         }
902
903         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
904                 ret = image_setup_libfdt(images, *of_flat_tree, lmb);
905                 if (ret)
906                         return ret;
907         }
908
909         return 0;
910 }
911
912 void genimg_print_size(uint32_t size)
913 {
914         printf("%d Bytes = ", size);
915         print_size(size, "\n");
916 }
917
918 void genimg_print_time(time_t timestamp)
919 {
920         struct rtc_time tm;
921
922         rtc_to_tm(timestamp, &tm);
923         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
924                tm.tm_year, tm.tm_mon, tm.tm_mday,
925                tm.tm_hour, tm.tm_min, tm.tm_sec);
926 }
927
928 /**
929  * get_default_image() - Return default property from /images
930  *
931  * Return: Pointer to value of default property (or NULL)
932  */
933 static const char *get_default_image(const void *fit)
934 {
935         int images_noffset;
936
937         images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
938         if (images_noffset < 0)
939                 return NULL;
940
941         return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
942 }
943
944 int image_locate_script(void *buf, int size, const char *fit_uname,
945                         const char *confname, char **datap, uint *lenp)
946 {
947         const struct legacy_img_hdr *hdr;
948         const void *fit_data;
949         const void *fit_hdr;
950         size_t fit_len;
951         int noffset;
952         int verify;
953         ulong len;
954         u32 *data;
955
956         verify = env_get_yesno("verify");
957
958         switch (genimg_get_format(buf)) {
959         case IMAGE_FORMAT_LEGACY:
960                 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
961                         goto exit_image_format;
962                 } else {
963                         hdr = buf;
964
965                         if (!image_check_magic(hdr)) {
966                                 puts("Bad magic number\n");
967                                 return 1;
968                         }
969
970                         if (!image_check_hcrc(hdr)) {
971                                 puts("Bad header crc\n");
972                                 return 1;
973                         }
974
975                         if (verify) {
976                                 if (!image_check_dcrc(hdr)) {
977                                         puts("Bad data crc\n");
978                                         return 1;
979                                 }
980                         }
981
982                         if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
983                                 puts("Bad image type\n");
984                                 return 1;
985                         }
986
987                         /* get length of script */
988                         data = (u32 *)image_get_data(hdr);
989
990                         len = uimage_to_cpu(*data);
991                         if (!len) {
992                                 puts("Empty Script\n");
993                                 return 1;
994                         }
995
996                         /*
997                          * scripts are just multi-image files with one
998                          * component, so seek past the zero-terminated sequence
999                          * of image lengths to get to the actual image data
1000                          */
1001                         while (*data++);
1002                 }
1003                 break;
1004         case IMAGE_FORMAT_FIT:
1005                 if (!IS_ENABLED(CONFIG_FIT)) {
1006                         goto exit_image_format;
1007                 } else {
1008                         fit_hdr = buf;
1009                         if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1010                                 puts("Bad FIT image format\n");
1011                                 return 1;
1012                         }
1013
1014                         if (!fit_uname) {
1015                                 /* If confname is empty, use the default */
1016                                 if (confname && *confname)
1017                                         noffset = fit_conf_get_node(fit_hdr, confname);
1018                                 else
1019                                         noffset = fit_conf_get_node(fit_hdr, NULL);
1020                                 if (noffset < 0) {
1021                                         if (!confname)
1022                                                 goto fallback;
1023                                         printf("Could not find config %s\n", confname);
1024                                         return 1;
1025                                 }
1026
1027                                 if (verify && fit_config_verify(fit_hdr, noffset))
1028                                         return 1;
1029
1030                                 noffset = fit_conf_get_prop_node(fit_hdr,
1031                                                                  noffset,
1032                                                                  FIT_SCRIPT_PROP,
1033                                                                  IH_PHASE_NONE);
1034                                 if (noffset < 0) {
1035                                         if (!confname)
1036                                                 goto fallback;
1037                                         printf("Could not find script in %s\n", confname);
1038                                         return 1;
1039                                 }
1040                         } else {
1041 fallback:
1042                                 if (!fit_uname || !*fit_uname)
1043                                         fit_uname = get_default_image(fit_hdr);
1044                                 if (!fit_uname) {
1045                                         puts("No FIT subimage unit name\n");
1046                                         return 1;
1047                                 }
1048
1049                                 /* get script component image node offset */
1050                                 noffset = fit_image_get_node(fit_hdr, fit_uname);
1051                                 if (noffset < 0) {
1052                                         printf("Can't find '%s' FIT subimage\n",
1053                                                fit_uname);
1054                                         return 1;
1055                                 }
1056                         }
1057
1058                         if (!fit_image_check_type(fit_hdr, noffset,
1059                                                   IH_TYPE_SCRIPT)) {
1060                                 puts("Not a image image\n");
1061                                 return 1;
1062                         }
1063
1064                         /* verify integrity */
1065                         if (verify && !fit_image_verify(fit_hdr, noffset)) {
1066                                 puts("Bad Data Hash\n");
1067                                 return 1;
1068                         }
1069
1070                         /* get script subimage data address and length */
1071                         if (fit_image_get_data_and_size(fit_hdr, noffset,
1072                                                         &fit_data, &fit_len)) {
1073                                 puts("Could not find script subimage data\n");
1074                                 return 1;
1075                         }
1076
1077                         data = (u32 *)fit_data;
1078                         len = (ulong)fit_len;
1079                 }
1080                 break;
1081         default:
1082                 goto exit_image_format;
1083         }
1084
1085         *datap = (char *)data;
1086         *lenp = len;
1087
1088         return 0;
1089
1090 exit_image_format:
1091         puts("Wrong image format for \"source\" command\n");
1092         return -EPERM;
1093 }
This page took 0.091276 seconds and 4 git commands to generate.