1 /*
2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8 #include <common.h>
9 #include <boot_rkimg.h>
10 #include <keylad.h>
11 #include <crypto.h>
12 #include <errno.h>
13 #include <fdt_support.h>
14 #include <image.h>
15 #include <malloc.h>
16 #include <mtd_blk.h>
17 #include <mp_boot.h>
18 #include <spl.h>
19 #include <spl_ab.h>
20 #include <linux/libfdt.h>
21
22 #ifndef CONFIG_SYS_BOOTM_LEN
23 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
24 #endif
25
26 /**
27 * spl_fit_get_image_name(): By using the matching configuration subnode,
28 * retrieve the name of an image, specified by a property name and an index
29 * into that.
30 * @fit: Pointer to the FDT blob.
31 * @images: Offset of the /images subnode.
32 * @type: Name of the property within the configuration subnode.
33 * @index: Index into the list of strings in this property.
34 * @outname: Name of the image
35 *
36 * Return: 0 on success, or a negative error number
37 */
spl_fit_get_image_name(const void * fit,int images,const char * type,int index,char ** outname)38 static int spl_fit_get_image_name(const void *fit, int images,
39 const char *type, int index,
40 char **outname)
41 {
42 const char *name, *str;
43 __maybe_unused int node;
44 int conf_node;
45 int len, i;
46
47 conf_node = fit_find_config_node(fit);
48 if (conf_node < 0) {
49 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
50 printf("No matching DT out of these options:\n");
51 for (node = fdt_first_subnode(fit, conf_node);
52 node >= 0;
53 node = fdt_next_subnode(fit, node)) {
54 name = fdt_getprop(fit, node, "description", &len);
55 printf(" %s\n", name);
56 }
57 #endif
58 return conf_node;
59 }
60
61 name = fdt_getprop(fit, conf_node, type, &len);
62 if (!name) {
63 debug("cannot find property '%s': %d\n", type, len);
64 return -EINVAL;
65 }
66
67 str = name;
68 for (i = 0; i < index; i++) {
69 str = strchr(str, '\0') + 1;
70 if (!str || (str - name >= len)) {
71 debug("no string for index %d\n", index);
72 return -E2BIG;
73 }
74 }
75
76 *outname = (char *)str;
77 return 0;
78 }
79
80 /**
81 * spl_fit_get_image_node(): By using the matching configuration subnode,
82 * retrieve the name of an image, specified by a property name and an index
83 * into that.
84 * @fit: Pointer to the FDT blob.
85 * @images: Offset of the /images subnode.
86 * @type: Name of the property within the configuration subnode.
87 * @index: Index into the list of strings in this property.
88 *
89 * Return: the node offset of the respective image node or a negative
90 * error number.
91 */
spl_fit_get_image_node(const void * fit,int images,const char * type,int index)92 static int spl_fit_get_image_node(const void *fit, int images,
93 const char *type, int index)
94 {
95 char *str;
96 int err;
97 int node;
98
99 err = spl_fit_get_image_name(fit, images, type, index, &str);
100 if (err)
101 return err;
102
103 debug("%s: '%s'\n", type, str);
104
105 node = fdt_subnode_offset(fit, images, str);
106 if (node < 0) {
107 debug("cannot find image node '%s': %d\n", str, node);
108 return -EINVAL;
109 }
110
111 return node;
112 }
113
get_aligned_image_offset(struct spl_load_info * info,int offset)114 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
115 {
116 /*
117 * If it is a FS read, get the first address before offset which is
118 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
119 * block number to which offset belongs.
120 */
121 if (info->filename)
122 return offset & ~(ARCH_DMA_MINALIGN - 1);
123
124 return offset / info->bl_len;
125 }
126
get_aligned_image_overhead(struct spl_load_info * info,int offset)127 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
128 {
129 /*
130 * If it is a FS read, get the difference between the offset and
131 * the first address before offset which is aligned to
132 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
133 * block.
134 */
135 if (info->filename)
136 return offset & (ARCH_DMA_MINALIGN - 1);
137
138 return offset % info->bl_len;
139 }
140
get_aligned_image_size(struct spl_load_info * info,int data_size,int offset)141 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
142 int offset)
143 {
144 data_size = data_size + get_aligned_image_overhead(info, offset);
145
146 if (info->filename)
147 return data_size;
148
149 return (data_size + info->bl_len - 1) / info->bl_len;
150 }
151
152 #ifdef CONFIG_SPL_FIT_CIPHER
spl_fit_image_uncipher(const void * fit,int noffset,ulong cipher_addr,size_t cipher_sz,ulong uncipher_addr)153 static int spl_fit_image_uncipher(const void *fit, int noffset,
154 ulong cipher_addr, size_t cipher_sz,
155 ulong uncipher_addr)
156 {
157 struct udevice *dev;
158 cipher_fw_context ctx;
159 int cipher_noffset;
160 const char *node_name;
161 const void *iv;
162 char *algo_name;
163 int key_len = 16;
164 int iv_len;
165 int ret;
166
167 node_name = fdt_get_name(fit, noffset, NULL);
168 if (!node_name) {
169 printf("Can't get node name.\n");
170 return -1;
171 }
172
173 cipher_noffset = fdt_subnode_offset(fit, noffset, FIT_CIPHER_NODENAME);
174 if (cipher_noffset < 0) {
175 printf("Can't get cipher node offset for image '%s'\n",
176 node_name);
177 return -1;
178 }
179
180 if (fit_image_cipher_get_algo(fit, cipher_noffset, &algo_name)) {
181 printf("Can't get cipher algo for image '%s'\n",
182 node_name);
183 return -1;
184 }
185
186 if (strcmp(algo_name, "aes128")) {
187 printf("Invalid cipher algo '%s'\n", algo_name);
188 return -1;
189 }
190
191 iv = fdt_getprop(fit, cipher_noffset, "iv", &iv_len);
192 if (!iv) {
193 printf("Can't get IV for image '%s'\n", node_name);
194 return -1;
195 }
196
197 if (iv_len != key_len) {
198 printf("Len iv(%d) != key(%d) for image '%s'\n",
199 iv_len, key_len, node_name);
200 return -1;
201 }
202
203 memset(&ctx, 0x00, sizeof(ctx));
204
205 ctx.algo = CRYPTO_AES;
206 ctx.mode = RK_MODE_CTR;
207 ctx.key_len = key_len;
208 ctx.iv = iv;
209 ctx.iv_len = iv_len;
210 ctx.fw_keyid = RK_FW_KEY0;
211
212 dev = crypto_get_device(CRYPTO_AES);
213 if (!dev) {
214 printf("No crypto device for expected AES\n");
215 return -ENODEV;
216 }
217
218 /* uncipher */
219 ret = crypto_fw_cipher(dev, &ctx, (void *)cipher_addr,
220 (void *)uncipher_addr, cipher_sz, true);
221
222 if (ret) {
223 printf("Uncipher data failed for image '%s', ret=%d\n",
224 node_name, ret);
225 return ret;
226 }
227
228 return 0;
229 }
230 #endif
231
232 /**
233 * spl_load_fit_image(): load the image described in a certain FIT node
234 * @info: points to information about the device to load data from
235 * @sector: the start sector of the FIT image on the device
236 * @fit: points to the flattened device tree blob describing the FIT
237 * image
238 * @base_offset: the beginning of the data area containing the actual
239 * image data, relative to the beginning of the FIT
240 * @node: offset of the DT node describing the image to load (relative
241 * to @fit)
242 * @image_info: will be filled with information about the loaded image
243 * If the FIT node does not contain a "load" (address) property,
244 * the image gets loaded to the address pointed to by the
245 * load_addr member in this struct.
246 *
247 * Return: 0 on success or a negative error number.
248 */
spl_load_fit_image(struct spl_load_info * info,ulong sector,void * fit,ulong base_offset,int node,struct spl_image_info * image_info)249 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
250 void *fit, ulong base_offset, int node,
251 struct spl_image_info *image_info)
252 {
253 int offset;
254 size_t length;
255 int len;
256 ulong size;
257 ulong comp_addr, load_addr, load_ptr;
258 void *src;
259 ulong overhead;
260 int nr_sectors;
261 int align_len = ARCH_DMA_MINALIGN - 1;
262 uint8_t image_comp = -1, type = -1;
263 const void *data;
264 bool external_data = false;
265
266 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
267 if (fit_image_get_comp(fit, node, &image_comp))
268 puts("Cannot get image compression format.\n");
269 else
270 debug("%s ", genimg_get_comp_name(image_comp));
271
272 if (fit_image_get_type(fit, node, &type))
273 puts("Cannot get image type.\n");
274 else
275 debug("%s ", genimg_get_type_name(type));
276 } else {
277 fit_image_get_comp(fit, node, &image_comp);
278 }
279
280 if (fit_image_get_load(fit, node, &load_addr))
281 load_addr = image_info->load_addr;
282
283 if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) {
284 /* Empirically, 2MB is enough for U-Boot, tee and atf */
285 if (fit_image_get_comp_addr(fit, node, &comp_addr))
286 comp_addr = load_addr + FIT_MAX_SPL_IMAGE_SZ;
287 } else {
288 comp_addr = load_addr;
289 }
290
291 #ifdef CONFIG_SPL_FIT_CIPHER
292 ulong cipher_addr;
293
294 if (fit_image_get_cipher_addr(fit, node, &cipher_addr))
295 cipher_addr = comp_addr + FIT_MAX_SPL_IMAGE_SZ;
296 #endif
297
298 if (!fit_image_get_data_position(fit, node, &offset)) {
299 external_data = true;
300 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
301 offset += base_offset;
302 external_data = true;
303 }
304
305 if (external_data) {
306 /* External data */
307 if (fit_image_get_data_size(fit, node, &len))
308 return -ENOENT;
309
310 load_ptr = (comp_addr + align_len) & ~align_len;
311 #if defined(CONFIG_ARCH_ROCKCHIP)
312 if ((load_ptr < CONFIG_SYS_SDRAM_BASE) ||
313 (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE))
314 load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
315 #endif
316 length = len;
317
318 overhead = get_aligned_image_overhead(info, offset);
319 nr_sectors = get_aligned_image_size(info, length, offset);
320
321 if (info->read(info,
322 sector + get_aligned_image_offset(info, offset),
323 nr_sectors, (void *)load_ptr) != nr_sectors)
324 return -EIO;
325
326 debug("External data: dst=%lx, offset=%x, size=%lx\n",
327 load_ptr, offset, (unsigned long)length);
328 src = (void *)load_ptr + overhead;
329 } else {
330 /* Embedded data */
331 if (fit_image_get_data(fit, node, &data, &length)) {
332 puts("Cannot get image data/size\n");
333 return -ENOENT;
334 }
335 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
336 (unsigned long)length);
337 src = (void *)data;
338 }
339
340 /* Check hashes and signature */
341 if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE)
342 printf("## Checking %s 0x%08lx (%s @0x%08lx) ... ",
343 fit_get_name(fit, node, NULL), load_addr,
344 (char *)fdt_getprop(fit, node, FIT_COMP_PROP, NULL),
345 (long)src);
346 else
347 printf("## Checking %s 0x%08lx ... ",
348 fit_get_name(fit, node, NULL), load_addr);
349
350 #ifdef CONFIG_FIT_SPL_PRINT
351 printf("\n");
352 fit_image_print(fit, node, "");
353 #endif
354 if (!fit_image_verify_with_data(fit, node,
355 src, length))
356 return -EPERM;
357
358 #ifdef CONFIG_SPL_FIT_CIPHER
359 if (fdt_subnode_offset(fit, node, FIT_CIPHER_NODENAME) > 0) {
360 printf(" Decrypting Data ...");
361 memcpy((void *)cipher_addr, src, length);
362 if (spl_fit_image_uncipher(fit, node, cipher_addr, length, (ulong)src))
363 return -EACCES;
364 printf(" OK ");
365 }
366 #endif
367
368 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
369 if (board_fit_image_post_process(fit, node, (ulong *)&load_addr,
370 (ulong **)&src, &length, info))
371 return -EINVAL;
372 #endif
373 puts("OK\n");
374
375 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) &&
376 IS_ENABLED(CONFIG_SPL_GZIP) &&
377 image_comp == IH_COMP_GZIP &&
378 type == IH_TYPE_KERNEL) {
379 size = length;
380 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
381 src, &size)) {
382 puts("Uncompressing error\n");
383 return -EIO;
384 }
385 length = size;
386 } else {
387 memcpy((void *)load_addr, src, length);
388 }
389
390 if (image_info) {
391 image_info->load_addr = load_addr;
392 image_info->size = length;
393 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
394 }
395
396 return 0;
397 }
398
spl_fit_append_fdt(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit,int images,ulong base_offset)399 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
400 struct spl_load_info *info, ulong sector,
401 void *fit, int images, ulong base_offset)
402 {
403 struct spl_image_info image_info;
404 int node, ret;
405
406 /* Figure out which device tree the board wants to use */
407 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
408 if (node < 0) {
409 debug("%s: cannot find FDT node\n", __func__);
410 return node;
411 }
412
413 /*
414 * Read the device tree and place it after the image.
415 * Align the destination address to ARCH_DMA_MINALIGN.
416 */
417 image_info.load_addr = spl_image->load_addr + spl_image->size;
418 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
419 &image_info);
420
421 if (ret < 0)
422 return ret;
423
424 /* Make the load-address of the FDT available for the SPL framework */
425 spl_image->fdt_addr = (void *)image_info.load_addr;
426 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
427 /* Try to make space, so we can inject details on the loadables */
428 fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
429 #endif
430
431 /*
432 * If need, load kernel FDT right after U-Boot FDT.
433 *
434 * kernel FDT is for U-Boot if there is not valid one
435 * from images, ie: resource.img, boot.img or recovery.img.
436 */
437 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 1);
438 if (node < 0) {
439 debug("%s: cannot find kernel FDT node\n", __func__);
440 /* attention: here return ret but not node */
441 return ret;
442 }
443
444 image_info.load_addr =
445 (ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr);
446 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
447 &image_info);
448
449 return ret;
450 }
451
spl_fit_record_loadable(const void * fit,int images,int index,void * blob,struct spl_image_info * image)452 static int spl_fit_record_loadable(const void *fit, int images, int index,
453 void *blob, struct spl_image_info *image)
454 {
455 int ret = 0;
456 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
457 char *name;
458 int node;
459
460 ret = spl_fit_get_image_name(fit, images, "loadables",
461 index, &name);
462 if (ret < 0)
463 return ret;
464
465 node = spl_fit_get_image_node(fit, images, "loadables", index);
466
467 ret = fdt_record_loadable(blob, index, name, image->load_addr,
468 image->size, image->entry_point,
469 fdt_getprop(fit, node, "type", NULL),
470 fdt_getprop(fit, node, "os", NULL));
471 #endif
472 return ret;
473 }
474
spl_fit_image_get_os(const void * fit,int noffset,uint8_t * os)475 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
476 {
477 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
478 return -ENOTSUPP;
479 #else
480 return fit_image_get_os(fit, noffset, os);
481 #endif
482 }
483
spl_fit_standalone_release(char * id,uintptr_t entry_point)484 __weak int spl_fit_standalone_release(char *id, uintptr_t entry_point)
485 {
486 return 0;
487 }
488
spl_fit_load_blob(struct spl_load_info * info,ulong sector,void * fit_header,int * base_offset)489 static void *spl_fit_load_blob(struct spl_load_info *info,
490 ulong sector, void *fit_header,
491 int *base_offset)
492 {
493 int align_len = ARCH_DMA_MINALIGN - 1;
494 ulong count;
495 ulong size;
496 int sectors;
497 void *fit;
498
499 /*
500 * For FIT with external data, figure out where the external images
501 * start. This is the base for the data-offset properties in each
502 * image.
503 */
504 size = fdt_totalsize(fit_header);
505 size = FIT_ALIGN(size);
506 *base_offset = FIT_ALIGN(size);
507
508 /*
509 * So far we only have one block of data from the FIT. Read the entire
510 * thing, including that first block, placing it so it finishes before
511 * where we will load the image.
512 *
513 * Note that we will load the image such that its first byte will be
514 * at the load address. Since that byte may be part-way through a
515 * block, we may load the image up to one block before the load
516 * address. So take account of that here by subtracting an addition
517 * block length from the FIT start position.
518 *
519 * In fact the FIT has its own load address, but we assume it cannot
520 * be before CONFIG_SYS_TEXT_BASE.
521 *
522 * For FIT with data embedded, data is loaded as part of FIT image.
523 * For FIT with external data, data is not loaded in this step.
524 */
525 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
526 align_len) & ~align_len);
527 sectors = get_aligned_image_size(info, size, 0);
528 count = info->read(info, sector, sectors, fit);
529 #if defined(CONFIG_SPL_MTD_SUPPORT) && !defined(CONFIG_FPGA_RAM)
530 mtd_blk_map_fit(info->dev, sector, fit);
531 #endif
532 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
533 sector, sectors, fit, count);
534 if (count == 0)
535 return NULL;
536
537 return fit;
538 }
539
540 #ifdef CONFIG_SPL_KERNEL_BOOT
541 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
spl_kernel_partition(struct spl_image_info * spl,struct spl_load_info * info)542 __weak const char *spl_kernel_partition(struct spl_image_info *spl,
543 struct spl_load_info *info)
544 {
545 return PART_BOOT;
546 }
547 #endif
548
spl_fit_get_kernel_dtb(const void * fit,int images_noffset)549 static int spl_fit_get_kernel_dtb(const void *fit, int images_noffset)
550 {
551 const char *name = NULL;
552 int node, index = 0;
553
554 for (; ; index++) {
555 node = spl_fit_get_image_node(fit, images_noffset,
556 FIT_FDT_PROP, index);
557 if (node < 0)
558 break;
559 name = fdt_get_name(fit, node, NULL);
560 if(!strcmp(name, "fdt"))
561 return node;
562 #if defined(CONFIG_SPL_ROCKCHIP_HWID_DTB)
563 if (spl_find_hwid_dtb(name)) {
564 printf("HWID DTB: %s\n", name);
565 break;
566 }
567 #endif
568 }
569
570 return node;
571 }
572
spl_load_kernel_fit(struct spl_image_info * spl_image,struct spl_load_info * info)573 static int spl_load_kernel_fit(struct spl_image_info *spl_image,
574 struct spl_load_info *info)
575 {
576 /*
577 * Never change the image order.
578 *
579 * Considering thunder-boot feature, there maybe asynchronous
580 * loading operation of these images and ramdisk is usually to
581 * be the last one.
582 *
583 * The .its content rule of kernel fit image follows U-Boot proper.
584 */
585 const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
586 struct spl_image_info image_info;
587 char fit_header[info->bl_len];
588 int images_noffset;
589 int base_offset;
590 int sector;
591 int node, ret, i;
592 void *fit;
593
594 if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
595 return 0;
596
597 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
598 const char *part_name = PART_BOOT;
599 disk_partition_t part_info;
600
601 part_name = spl_kernel_partition(spl_image, info);
602 if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
603 printf("%s: no partition\n", __func__);
604 return -EINVAL;
605 }
606 sector = part_info.start;
607 #else
608 sector = CONFIG_SPL_KERNEL_BOOT_SECTOR;
609 #endif
610 printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name);
611
612 if (info->read(info, sector, 1, &fit_header) != 1) {
613 debug("%s: Failed to read header\n", __func__);
614 return -EIO;
615 }
616
617 if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
618 printf("%s: Not fit magic\n", __func__);
619 return -EINVAL;
620 }
621
622 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
623 if (!fit) {
624 debug("%s: Cannot load blob\n", __func__);
625 return -ENODEV;
626 }
627
628 /* verify the configure node by keys, if required */
629 #ifdef CONFIG_SPL_FIT_SIGNATURE
630 int conf_noffset;
631
632 conf_noffset = fit_conf_get_node(fit, NULL);
633 if (conf_noffset <= 0) {
634 printf("No default config node\n");
635 return -EINVAL;
636 }
637
638 ret = fit_config_verify(fit, conf_noffset);
639 if (ret) {
640 printf("fit verify configure failed, ret=%d\n", ret);
641 return ret;
642 }
643 printf("\n");
644 #endif
645 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
646 if (images_noffset < 0) {
647 debug("%s: Cannot find /images node: %d\n",
648 __func__, images_noffset);
649 return images_noffset;
650 }
651
652 for (i = 0; i < ARRAY_SIZE(images); i++) {
653 if (!strcmp(images[i], FIT_FDT_PROP))
654 node = spl_fit_get_kernel_dtb(fit, images_noffset);
655 else
656 node = spl_fit_get_image_node(fit, images_noffset,
657 images[i], 0);
658 if (node < 0) {
659 debug("No image: %s\n", images[i]);
660 continue;
661 }
662
663 ret = spl_load_fit_image(info, sector, fit, base_offset,
664 node, &image_info);
665 if (ret)
666 return ret;
667
668 /* initial addr or entry point */
669 if (!strcmp(images[i], FIT_FDT_PROP)) {
670 spl_image->fdt_addr = (void *)image_info.load_addr;
671 if (spl_fdt_chosen_bootargs(info, (void *)image_info.load_addr)) {
672 printf("ERROR: Append bootargs failed\n");
673 return -EINVAL;
674 }
675 } else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
676 #if CONFIG_IS_ENABLED(OPTEE)
677 spl_image->entry_point_os = image_info.load_addr;
678 #endif
679 #if CONFIG_IS_ENABLED(ATF)
680 spl_image->entry_point_bl33 = image_info.load_addr;
681 #endif
682 } else if (!strcmp(images[i], FIT_RAMDISK_PROP)) {
683 fdt_initrd(spl_image->fdt_addr, image_info.load_addr,
684 image_info.load_addr + image_info.size);
685 }
686 }
687
688 debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
689 (ulong)spl_image->fdt_addr,
690 spl_image->entry_point,
691 #if CONFIG_IS_ENABLED(OPTEE)
692 spl_image->entry_point_os);
693 #endif
694 #if CONFIG_IS_ENABLED(ATF)
695 spl_image->entry_point_bl33);
696 #endif
697
698 return 0;
699 }
700 #endif
701
spl_internal_load_simple_fit(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit_header)702 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
703 struct spl_load_info *info,
704 ulong sector, void *fit_header)
705 {
706 struct spl_image_info image_info;
707 char *desc;
708 #if CONFIG_IS_ENABLED(ATF)
709 uint8_t ih_arch;
710 #endif
711 int base_offset;
712 int images, ret;
713 int index = 0;
714 int node = -1;
715 void *fit;
716
717 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
718 if (!fit) {
719 debug("%s: Cannot load blob\n", __func__);
720 return -1;
721 }
722
723 /* find the node holding the images information */
724 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
725 if (images < 0) {
726 debug("%s: Cannot find /images node: %d\n", __func__, images);
727 return -1;
728 }
729
730 /* if board sigs verify required, check self */
731 if (fit_board_verify_required_sigs() &&
732 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
733 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
734 hang();
735 }
736
737 /* verify the configure node by keys, if required */
738 #ifdef CONFIG_SPL_FIT_SIGNATURE
739 int conf_noffset;
740
741 conf_noffset = fit_conf_get_node(fit, NULL);
742 if (conf_noffset <= 0) {
743 printf("No default config node\n");
744 return -EINVAL;
745 }
746
747 ret = fit_config_verify(fit, conf_noffset);
748 if (ret) {
749 printf("fit verify configure failed, ret=%d\n", ret);
750 return ret;
751 }
752 printf("\n");
753
754 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
755 uint32_t this_index, min_index;
756
757 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
758 &this_index, &min_index);
759 if (ret) {
760 printf("fit failed to get rollback index, ret=%d\n", ret);
761 return ret;
762 } else if (this_index < min_index) {
763 printf("fit reject rollback: %d < %d(min)\n",
764 this_index, min_index);
765 return -EINVAL;
766 }
767
768 printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
769 #endif
770 #endif
771
772 /*
773 * If required to start the other core before load "loadables"
774 * firmwares, use the config "standalone" to load the other core's
775 * firmware, then start it.
776 * Normally, different cores' firmware is attach to the config
777 * "loadables" and load them together.
778 */
779 for (; ; index++) {
780 node = spl_fit_get_image_node(fit, images,
781 FIT_STANDALONE_PROP, index);
782 if (node < 0)
783 break;
784
785 ret = spl_load_fit_image(info, sector, fit, base_offset,
786 node, &image_info);
787 if (ret)
788 return ret;
789
790 ret = fit_get_desc(fit, node, &desc);
791 if (ret)
792 return ret;
793
794 if (image_info.entry_point == FDT_ERROR)
795 image_info.entry_point = image_info.load_addr;
796
797 flush_dcache_range(image_info.load_addr,
798 image_info.load_addr + image_info.size);
799 ret = spl_fit_standalone_release(desc, image_info.entry_point);
800 if (ret)
801 printf("%s: start standalone fail, ret=%d\n", desc, ret);
802 }
803
804 /* standalone is special one, continue to find others */
805 node = -1;
806 index = 0;
807
808 /*
809 * Find the U-Boot image using the following search order:
810 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
811 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
812 * - fall back to using the first 'loadables' entry
813 */
814 if (node < 0)
815 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
816 0);
817 #ifdef CONFIG_SPL_OS_BOOT
818 if (node < 0)
819 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
820 #endif
821 if (node < 0) {
822 debug("could not find firmware image, trying loadables...\n");
823 node = spl_fit_get_image_node(fit, images, "loadables", 0);
824 /*
825 * If we pick the U-Boot image from "loadables", start at
826 * the second image when later loading additional images.
827 */
828 index = 1;
829 }
830 if (node < 0) {
831 debug("%s: Cannot find u-boot image node: %d\n",
832 __func__, node);
833 return -1;
834 }
835
836 /* Load the image and set up the spl_image structure */
837 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
838 spl_image);
839 if (ret)
840 return ret;
841
842 /*
843 * For backward compatibility, we treat the first node that is
844 * as a U-Boot image, if no OS-type has been declared.
845 */
846 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
847 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
848 #if !defined(CONFIG_SPL_OS_BOOT)
849 else
850 spl_image->os = IH_OS_U_BOOT;
851 #endif
852
853 /* Booting a next-stage U-Boot may require us to append the FDT. */
854 if (spl_image->os == IH_OS_U_BOOT) {
855 ret = spl_fit_append_fdt(spl_image, info, sector, fit,
856 images, base_offset);
857 if (ret < 0)
858 return ret;
859 }
860
861 /* Now check if there are more images for us to load */
862 for (; ; index++) {
863 uint8_t os_type = IH_OS_INVALID;
864
865 node = spl_fit_get_image_node(fit, images, "loadables", index);
866 if (node < 0)
867 break;
868
869 if (!spl_fit_image_get_os(fit, node, &os_type))
870 debug("Loadable is %s\n", genimg_get_os_name(os_type));
871
872 /* skip U-Boot ? */
873 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
874 os_type == IH_OS_U_BOOT)
875 continue;
876
877 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
878 &image_info);
879 if (ret < 0)
880 return ret;
881
882 if (os_type == IH_OS_U_BOOT) {
883 #if CONFIG_IS_ENABLED(ATF)
884 fit_image_get_arch(fit, node, &ih_arch);
885 debug("Image ARCH is %s\n", genimg_get_arch_name(ih_arch));
886 if (ih_arch == IH_ARCH_ARM)
887 spl_image->flags |= SPL_ATF_AARCH32_BL33;
888 spl_image->entry_point_bl33 = image_info.load_addr;
889 #elif CONFIG_IS_ENABLED(OPTEE)
890 spl_image->entry_point_os = image_info.load_addr;
891 #endif
892 ret = spl_fit_append_fdt(&image_info, info, sector,
893 fit, images, base_offset);
894 if (ret < 0)
895 return ret;
896 spl_image->fdt_addr = image_info.fdt_addr;
897 }
898
899 /*
900 * If the "firmware" image did not provide an entry point,
901 * use the first valid entry point from the loadables.
902 */
903 if (spl_image->entry_point == FDT_ERROR &&
904 image_info.entry_point != FDT_ERROR)
905 spl_image->entry_point = image_info.entry_point;
906
907 /* Record our loadables into the FDT */
908 if (spl_image->fdt_addr && spl_image->next_stage == SPL_NEXT_STAGE_UBOOT)
909 spl_fit_record_loadable(fit, images, index,
910 spl_image->fdt_addr,
911 &image_info);
912 #if CONFIG_IS_ENABLED(ATF)
913 else if (os_type == IH_OS_OP_TEE)
914 spl_image->entry_point_bl32 = image_info.load_addr;
915 #endif
916 }
917
918 /*
919 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
920 * Makefile will set it to 0 and it will end up as the entry point
921 * here. What it actually means is: use the load address.
922 */
923 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
924 spl_image->entry_point = spl_image->load_addr;
925
926 return 0;
927 }
928
spl_load_simple_fit(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit)929 int spl_load_simple_fit(struct spl_image_info *spl_image,
930 struct spl_load_info *info, ulong sector, void *fit)
931 {
932 ulong sector_offs = sector;
933 int ret = -EINVAL;
934 int i;
935
936 #ifdef CONFIG_MP_BOOT
937 mpb_init_1(*info);
938 #endif
939
940 printf("Trying fit image at 0x%lx sector\n", sector_offs);
941 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
942 if (i > 0) {
943 sector_offs +=
944 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
945 printf("Trying fit image at 0x%lx sector\n", sector_offs);
946 if (info->read(info, sector_offs, 1, fit) != 1) {
947 printf("IO error\n");
948 continue;
949 }
950 }
951
952 if (image_get_magic(fit) != FDT_MAGIC) {
953 printf("Not fit magic\n");
954 continue;
955 }
956
957 ret = spl_internal_load_simple_fit(spl_image, info,
958 sector_offs, fit);
959 if (!ret) {
960 #ifdef CONFIG_SPL_KERNEL_BOOT
961 ret = spl_load_kernel_fit(spl_image, info);
962 #endif
963 break;
964 }
965 }
966 #ifdef CONFIG_SPL_AB
967 /* If boot fail in spl, spl must decrease 1 and do_reset. */
968 if (ret)
969 return spl_ab_decrease_reset(info->dev);
970 /*
971 * If boot successfully, it is no need to do decrease
972 * and U-boot will always decrease 1.
973 * If in thunderboot process, always need to decrease 1.
974 */
975 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
976 spl_ab_decrease_tries(info->dev);
977 #endif
978
979 return ret;
980 }
981
982