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