blob: d3206000e92da3790488dca434da3e57224d3d1d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2018 MediaTek Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <debug.h>
25#include <errno.h>
26#include <fit.h>
27#include <kernel/vm.h>
28#include <lib/bio.h>
29#include <lib/mempool.h>
30#include <libfdt.h>
31#include <platform.h>
32#include <string.h>
33#include <sys/types.h>
34#include <trace.h>
35
36#include "avb.h"
37#include "blxboot_ab.h"
38#include "blxboot_plat.h"
39#include "imageinfo.h"
40
41#if ENABLE_ANDROID_BOOTIMG_SUPPORT
42#include <lib/android_bootimg.h>
43#include <lib/boot_info.h>
44#endif
45
46#define LOCAL_TRACE 0
47
48/* BL33 load and entry point address */
49#define CFG_BL33_LOAD_EP_ADDR (BL33_ADDR)
50
51/* ramdisk args to be appended to kernel command line */
52#define MAX_RAMDISK_ARG_LEN (45)
53#define RAMDISK_ARG_STR_FMT "initrd=0x%lx,0x%x"
54
55extern __WEAK paddr_t kvaddr_to_paddr(void *ptr);
56
57/* forward declaration */
58static int load_avb_image(const char *part_name,
59 struct imageinfo_t *img);
60static int load_builtin_bl33_image(const char *part_name,
61 struct imageinfo_t *img);
62static int load_fit_image(const char *part_name,
63 struct imageinfo_t *img);
64static int load_android_image(const char *part_name,
65 struct imageinfo_t *img);
66static int load_dtbo_image(const char *part_name,
67 struct imageinfo_t *img);
68static int load_scpsys_image(const char *part_name,
69 struct imageinfo_t *img);
70static int load_scpdata_image(const char *part_name,
71 struct imageinfo_t *img);
72static int load_scpcfg_image(const char *part_name,
73 struct imageinfo_t *img);
74#if ENABLE_MODEM_LOAD
75static int load_md_image(const char *part_name,
76 struct imageinfo_t *img);
77#endif
78#if ENABLE_HSM_OS_LOAD
79static int load_hsm_os_image(const char *part_name,
80 struct imageinfo_t *img);
81#endif
82
83struct image_load_data tzimg = {
84 .part_name = TZ_PART_NAME,
85 .recovery_part_name = RECOVERY_TZ_PART_NAME,
86 .membdev_name = "download:tz",
87 .has_slot = (TZ_HAS_SLOT != 0),
88};
89
90struct image_load_data bl33img = {
91 .part_name = BL33_PART_NAME,
92 .recovery_part_name = RECOVERY_BL33_PART_NAME,
93 .membdev_name = "download:bl33",
94 .has_slot = (BL33_HAS_SLOT != 0),
95};
96
97#if ENABLE_SLAVE_CPU_LOAD
98struct image_load_data scpuimg = {
99 .part_name = SLAVE_CPU_PART_NAME,
100 .recovery_part_name = RECOVERY_SLAVE_CPU_PART_NAME,
101 .membdev_name = "download:scpu",
102 .has_slot = (SLAVE_CPU_HAS_SLOT != 0),
103};
104#endif
105
106struct image_load_data bootimg = {
107 .part_name = BOOT_PART_NAME,
108 .recovery_part_name = RECOVERY_BOOT_PART_NAME,
109 .membdev_name = "download:boot",
110 .has_slot = (BOOT_HAS_SLOT != 0),
111};
112
113struct image_load_data dtboimg = {
114 .part_name = DTBO_PART_NAME,
115 .has_slot = (DTBO_HAS_SLOT != 0),
116};
117
118struct image_load_data vpdimg = {
119#if defined(VPD_PART_NAME)
120 .part_name = VPD_PART_NAME,
121#endif
122#if defined(RECOVERY_VPD_PART_NAME)
123 .recovery_part_name = RECOVERY_VPD_PART_NAME,
124#endif
125 .membdev_name = "download:vpd",
126 .has_slot = (VPD_HAS_SLOT != 0),
127};
128
129#if ENABLE_SCP_LOAD
130struct image_load_data scpsysimg = {
131 .part_name = SCPSYS_PART_NAME,
132 .recovery_part_name = RECOVERY_SCPSYS_PART_NAME,
133 .membdev_name = "download:scpsys",
134 .has_slot = (SCPSYS_HAS_SLOT != 0),
135};
136#endif
137
138#if ENABLE_SCP_AUX_LOAD
139struct image_load_data scpdataimg = {
140 .part_name = SCPDATA_PART_NAME,
141 .recovery_part_name = RECOVERY_SCPDATA_PART_NAME,
142 .membdev_name = "download:scpdata",
143 .has_slot = (SCPDATA_HAS_SLOT != 0),
144};
145
146struct image_load_data scpcfgimg = {
147 .part_name = SCPCFG_PART_NAME,
148 .recovery_part_name = RECOVERY_SCPCFG_PART_NAME,
149 .buf = (void *)SCP_CFG_VA,
150 .has_slot = (SCPCFG_HAS_SLOT != 0),
151};
152#endif
153
154#if ENABLE_SPM_FW_LOAD
155struct image_load_data spmfwimg = {
156 .part_name = SPM_PART_NAME,
157 .recovery_part_name = SPM_PART_NAME,
158 .membdev_name = "download:spmfw",
159 .has_slot = (SPMFW_HAS_SLOT != 0),
160};
161#endif
162
163#if ENABLE_MODEM_LOAD
164struct image_load_data mdimg = {
165 .part_name = MD_PART_NAME,
166 .membdev_name = "download:md",
167 .buf = (void *)MD_ADDR,
168 .has_slot = (MD_HAS_SLOT != 0),
169};
170#endif
171
172#if ENABLE_HSM_OS_LOAD
173struct image_load_data hsm_os_img = {
174 .part_name = HSM_OS_PART_NAME,
175 .membdev_name = "download:hsmos",
176 .buf = (void *)HSM_OS_ADDR,
177 .has_slot = (HSM_OS_HAS_SLOT != 0),
178};
179#endif
180
181struct imageinfo_t imagelist[] = {
182#if ENABLE_SCP_AUX_LOAD
183 {
184 .type = IMGTYPE_SCPDATA,
185 .load = load_scpdata_image,
186 .imgdata = &scpdataimg,
187 },
188 {
189 .type = IMGTYPE_SCPCFG,
190 .load = load_scpcfg_image,
191 .imgdata = &scpcfgimg,
192 },
193#endif
194
195#if ENABLE_SCP_LOAD
196 {
197 .type = IMGTYPE_SCPSYS,
198 .load = load_scpsys_image,
199 .imgdata = &scpsysimg,
200 },
201#endif
202
203#if ENABLE_TZ_LOAD
204 {
205 .type = IMGTYPE_TZ,
206 .load = load_fit_image,
207 .imgdata = &tzimg,
208 },
209#endif /* ENABLE_TZ_LOAD */
210
211#if ENABLE_BL33_LOAD
212 {
213#if ENABLE_BUILTIN_BL33
214 .type = IMGTYPE_BUILTIN_BL33,
215 .load = load_builtin_bl33_image,
216#else
217 .type = IMGTYPE_BL33,
218 .load = load_fit_image,
219#endif
220 .imgdata = &bl33img,
221 },
222#endif /* ENABLE_BL33_LOAD */
223
224#if ENABLE_SLAVE_CPU_LOAD
225 {
226 .type = IMGTYPE_SLAVE_CPU,
227 .load = load_fit_image,
228 .imgdata = &scpuimg,
229 },
230#endif /* ENABLE_DSP_LOAD */
231
232#if ENABLE_KERNEL_LOAD
233 {
234 .type = IMGTYPE_KERNEL,
235#if defined(AVB_VERIFY_KERNEL)
236 .load = load_avb_image,
237#else
238#if ENABLE_ANDROID_BOOTIMG_SUPPORT
239 .load = load_android_image,
240#else
241 .load = load_fit_image,
242#endif
243#endif
244 .imgdata = &bootimg,
245 },
246 {
247 .type = IMGTYPE_VPD,
248 .load = load_dtbo_image,
249 .imgdata = &vpdimg,
250 },
251 {
252 .type = IMGTYPE_DTBO,
253 .load = load_dtbo_image,
254 .imgdata = &dtboimg,
255 },
256#endif /* ENABLE_KERNEL_LOAD */
257
258#if ENABLE_SPM_FW_LOAD
259 {
260 .type = IMGTYPE_SPM,
261 .load = load_fit_image,
262 .imgdata = &spmfwimg,
263 },
264#endif /* ENABLE_SPM_FW_LOAD */
265
266#if ENABLE_MODEM_LOAD
267 {
268 .type = IMGTYPE_MODEM,
269 .load = load_md_image,
270 .imgdata = &mdimg,
271 },
272#endif
273
274#if ENABLE_HSM_OS_LOAD
275 {
276 .type = IMGTYPE_HSM_OS,
277 .load = load_hsm_os_image,
278 .imgdata = &hsm_os_img,
279 },
280#endif
281
282 { .type = IMGTYPE_NONE } /* imagelist end marker */
283};
284
285static void append_cmdline_ramdisk_arg(addr_t ramdisk_addr, uint32_t ramdisk_sz)
286{
287 char ramdisk_arg[MAX_RAMDISK_ARG_LEN];
288
289 memset(ramdisk_arg, 0x0, sizeof(ramdisk_arg));
290 snprintf(ramdisk_arg, MAX_RAMDISK_ARG_LEN, RAMDISK_ARG_STR_FMT,
291 ramdisk_addr, ramdisk_sz);
292 plat_fixup_append(ramdisk_arg);
293}
294
295static int fit_load_images(void *fit, struct image_load_data *fit_data,
296 bool need_verified)
297{
298 addr_t load;
299 size_t load_size;
300 int ret;
301
302 /* TODO: decide verify policy with config. */
303 if (need_verified) {
304 dprintf(ALWAYS, "verify fit conf sig: %s\n", fit_data->part_name);
305 ret = fit_conf_verify_sig(NULL, fit);
306 if (ret < 0)
307 return ret;
308 }
309
310 ret = fit_load_image(NULL, "kernel", fit, NULL, NULL,
311 (paddr_t *)&fit_data->kernel_entry, need_verified);
312 if (ret && (ret != -ENOENT)) {
313 dprintf(CRITICAL, "%s load kernel failed\n", fit_data->part_name);
314 return ret;
315 }
316
317 ret = fit_load_image(NULL, "ramdisk", fit, &load, &load_size, NULL,
318 need_verified);
319 if (ret) {
320 if (ret != -ENOENT) {
321 dprintf(CRITICAL, "%s load ramdisk failed\n", fit_data->part_name);
322 return ret;
323 }
324 } else {
325#if WITH_KERNEL_VM
326 load = kvaddr_to_paddr((void *)load);
327#endif
328 append_cmdline_ramdisk_arg(load, load_size);
329 }
330
331 ret = fit_load_image(NULL, "fdt", fit, (addr_t *)&fit_data->dtb_load, NULL,
332 NULL, need_verified);
333 if (ret && (ret != -ENOENT)) {
334 fit_data->dtb_load = ERR_ADDR; /* [TODO] remove or assign it to NULL */
335 dprintf(CRITICAL, "%s load fdt failed\n", fit_data->part_name);
336 return ret;
337 }
338
339 ret = fit_load_image(NULL, "tee", fit, NULL, NULL,
340 (paddr_t *)&fit_data->trustedos_entry, need_verified);
341 if (ret && (ret != -ENOENT)) {
342 dprintf(CRITICAL, "%s load trustedos failed\n", fit_data->part_name);
343 return ret;
344 }
345
346 return 0;
347}
348
349#if ENABLE_ANDROID_BOOTIMG_SUPPORT
350static int load_android_hdr_images(const void *part_data,
351 struct image_load_data *android_data)
352{
353 int rc;
354 uint32_t ramdisk_target_addr, kernel_target_addr;
355 uint32_t ramdisk_sz, zimage_sz, dtb_sz;
356 uint64_t dtb_target_addr;
357 vaddr_t ramdisk_addr, kernel_addr, dtb_addr;
358 char android_dtb_param[] = "androidboot.dtb_idx=0";
359
360 load_bootinfo_bootimg_hdr((struct bootimg_hdr *)part_data);
361 set_bootimg_loaded((vaddr_t)part_data);
362
363 ramdisk_target_addr = get_ramdisk_target_addr();
364 ramdisk_addr = get_ramdisk_addr();
365 ramdisk_sz = get_ramdisk_real_sz();
366
367 kernel_target_addr = get_kernel_target_addr();
368 kernel_addr = get_kernel_addr();
369 zimage_sz = get_kernel_real_sz();
370
371 dtb_target_addr = get_dtb_target_addr();
372 dtb_addr = get_dtb_addr();
373 dtb_sz = get_dtb_size();
374
375 assert(ramdisk_target_addr != 0);
376 assert(ramdisk_addr != 0);
377 assert(kernel_target_addr != 0);
378 assert(kernel_addr != 0);
379 assert(dtb_target_addr != 0);
380 assert(dtb_addr != 0);
381
382 if(get_header_version() < BOOT_HEADER_VERSION_TWO) {
383 LTRACEF_LEVEL(CRITICAL,
384 "Android Image Header version is lower than v2\n");
385 return -EINVAL;
386 }
387
388 rc = android_processing_data("ramdisk", ramdisk_addr,
389 ramdisk_target_addr, ramdisk_sz);
390 if (rc != 0) {
391 LTRACEF_LEVEL(CRITICAL, "load %s failed\n", "ramdisk");
392 return -EINVAL;
393 }
394 append_cmdline_ramdisk_arg((addr_t)ramdisk_target_addr, ramdisk_sz);
395
396 rc = android_processing_data("kernel", kernel_addr,
397 kernel_target_addr, zimage_sz);
398 if (rc != 0) {
399 LTRACEF_LEVEL(CRITICAL, "load %s failed\n", "kernel");
400 return -EINVAL;
401 }
402 android_data->kernel_entry = kernel_target_addr;
403
404 rc = android_processing_data("dtb", dtb_addr,
405 dtb_target_addr, dtb_sz);
406 if (rc != 0) {
407 LTRACEF_LEVEL(CRITICAL, "load %s failed\n", "dtb");
408 return -EINVAL;
409 }
410#if WITH_KERNEL_VM
411 dtb_target_addr = (vaddr_t)paddr_to_kvaddr(dtb_target_addr);
412#endif
413 android_data->dtb_load = dtb_target_addr;
414
415 plat_fixup_append(android_dtb_param);
416
417 return 0;
418}
419#endif
420
421static int load_avb_image(const char *part_name, struct imageinfo_t *img)
422{
423 int ret;
424 size_t part_name_len;
425 struct image_load_data *imgdata;
426 char *raw_part_name;
427 const char *suffix;
428 AvbSlotVerifyResult verify_result;
429 AvbSlotVerifyData *verify_data;
430
431 if ((img == NULL) || (img->imgdata == NULL))
432 return -1;
433
434 imgdata = img->imgdata;
435
436 raw_part_name = strdup(part_name);
437 if (raw_part_name == NULL)
438 return -ENOMEM;
439
440 if (imgdata->has_slot) {
441 /* prepare partition name without ab suffix */
442 part_name_len = strlen(part_name);
443 suffix = get_suffix();
444 if (suffix && (part_name_len > strlen(suffix))) {
445 part_name_len -= strlen(suffix);
446 *(raw_part_name + part_name_len) = '\0';
447 }
448 }
449
450 ret = 0;
451 verify_result = android_verified_boot_2_0(raw_part_name, &verify_data);
452 if ((verify_result != AVB_SLOT_VERIFY_RESULT_OK) && !is_device_unlocked()) {
453 ret = -10;
454 goto exit;
455 }
456
457 void *part_data = get_partition_data(raw_part_name, verify_data);
458 if (part_data == NULL) {
459 ret = -11;
460 goto exit;
461 }
462
463#if ENABLE_ANDROID_BOOTIMG_SUPPORT
464 if (img->type == IMGTYPE_KERNEL) {
465 ret = load_android_hdr_images(part_data, imgdata);
466 } else {
467 if (android_dtbo_check_header((const void *)part_data) == DTBO_RET_OK) {
468 /* android dtbo image without fit header */
469 imgdata->dtb_load = (ulong)part_data;
470 } else {
471 /* android dtbo image with fit header */
472 ret = fit_load_images(part_data, imgdata, false);
473 }
474 }
475#else
476 ret = fit_load_images(part_data, imgdata, false);
477#endif
478
479 if (ret) {
480 LTRACEF_LEVEL(CRITICAL, "load %s image failed\n", part_name);
481 ret = -21;
482 goto exit;
483 }
484
485 if (img->type == IMGTYPE_KERNEL)
486 plat_fixup_append(verify_data->cmdline);
487
488exit:
489 if (raw_part_name)
490 free(raw_part_name);
491
492 return ret;
493}
494
495static int load_builtin_bl33_image(const char *part_name,
496 struct imageinfo_t *img)
497{
498 uint32_t bl33[] = { 0xea000005, /* b BL33_32_ENTRY | ands x5, x0, x0 */
499 0x58000160, /* .word 0x58000160 | ldr x0, _X0 */
500 0x58000181, /* .word 0x58000181 | ldr x1, _X1 */
501 0x580001a2, /* .word 0x580001a2 | ldr x2, _X2 */
502 0x580001c3, /* .word 0x580001c3 | ldr x3, _X3 */
503 0x580001e4, /* .word 0x580001e4 | ldr x4, _X4 */
504 0xd61f0080, /* .word 0xd61f0080 | br x4 */
505 /* BL33_32_ENTRY: | */
506 0xe59f0030, /* ldr r0, _R0 | .word 0xe59f0030 */
507 0xe59f1030, /* ldr r1, _R1 | .word 0xe59f1030 */
508 0xe59f2004, /* ldr r2, _X0 | .word 0xe59f2004 */
509 0xe59ff020, /* ldr pc, _X4 | .word 0xe59ff020 */
510 0x00000000, /* .word 0x00000000 */
511 0x00000000, /* _X0: .word 0x00000000 */
512 0x00000000, /* .word 0x00000000 */
513 0x00000000, /* _X1: .word 0x00000000 */
514 0x00000000, /* .word 0x00000000 */
515 0x00000000, /* _X2: .word 0x00000000 */
516 0x00000000, /* .word 0x00000000 */
517 0x00000000, /* _X3: .word 0x00000000 */
518 0x00000000, /* .word 0x00000000 */
519 0x00000000, /* _X4: .word 0x00000000 */
520 0x00000000, /* _R0: .word 0x00000000 */
521 0x00000000, /* _R1: .word 0x00000000 */
522 0x00000000 /* .word 0x00000000 */
523 };
524
525 memcpy((void *)CFG_BL33_LOAD_EP_ADDR, bl33, sizeof(bl33));
526 img->imgdata->kernel_entry = kvaddr_to_paddr ?
527 kvaddr_to_paddr((void *)CFG_BL33_LOAD_EP_ADDR) : CFG_BL33_LOAD_EP_ADDR;
528
529 return 0;
530}
531
532static int load_fit_image(const char *part_name, struct imageinfo_t *img)
533{
534 int err;
535
536 if ((err = fit_get_image(part_name, &img->imgdata->buf)) == 0) {
537 err = fit_load_images(img->imgdata->buf, img->imgdata, true);
538 }
539
540 return err;
541}
542
543#if ENABLE_ANDROID_BOOTIMG_SUPPORT
544static int load_android_image(const char *part_name, struct imageinfo_t *img)
545{
546 int err;
547
548 if ((err = android_get_image(part_name, &img->imgdata->buf)) == 0) {
549 err = load_android_hdr_images(img->imgdata->buf, img->imgdata);
550 }
551
552 return err;
553}
554#endif
555
556static int load_android_dtbo_image(const char *part_name,
557 struct imageinfo_t *img, uint32_t size)
558{
559 int ret;
560
561 /* android dtbo format */
562 LTRACEF("part_name(%s) in pure android dtbo format.\n", part_name);
563#if defined(AVB_VERIFY_KERNEL)
564 ret = load_avb_image(part_name, img);
565#else
566 bdev_t *bdev;
567 void *buf;
568 ssize_t read_bytes;
569
570 bdev = bio_open_by_label(part_name) ? : bio_open(part_name);
571 if (!bdev)
572 return -ENODEV;
573
574 ret = 0;
575 buf = mempool_alloc(size, MEMPOOL_ANY);
576 if (!buf) {
577 ret = -ENOMEM;
578 goto _err;
579 }
580
581 read_bytes = bio_read(bdev, buf, 0, size);
582 if (read_bytes < (ssize_t)size) {
583 LTRACEF("Read android dtbo image fail: read(0x%x), got(0x%zx)\n",
584 size, read_bytes);
585 mempool_free(buf);
586 ret = -EIO;
587 goto _err;
588 }
589
590 img->imgdata->dtb_load = (ulong)buf;
591
592_err:
593 if (bdev)
594 bio_close(bdev);
595#endif
596
597 return ret;
598}
599
600static int load_fdt_dtbo_image(const char *part_name, struct imageinfo_t *img)
601{
602 int ret;
603 int noffset;
604
605 /* legacy fdt format dtbo or android dtbo format with FIT header */
606 ret = fit_get_image(part_name, &img->imgdata->buf);
607 if (ret != 0)
608 return ret;
609
610 /* check configuration node to know it's legacy dtbo or not */
611 noffset = fdt_path_offset(img->imgdata->buf, "/configurations");
612 if (noffset < 0) {
613 LTRACEF("part_name(%s) in legacy dtbo format\n", part_name);
614 img->imgdata->dtb_load = (ulong)img->imgdata->buf;
615 return 0;
616 }
617
618 LTRACEF("part_name(%s) in fit android dtbo format.\n", part_name);
619 /* we verifies android dtbo image in the same method with kernel */
620#if defined(AVB_VERIFY_KERNEL)
621 if (img->type == IMGTYPE_DTBO) {
622 mempool_free(img->imgdata->buf);
623 img->imgdata->buf = NULL;
624
625 /* [TODO] load_avb_image() will waste time to load dtbo image again */
626 ret = load_avb_image(part_name, img);
627 } else {
628 ret = fit_load_images(img->imgdata->buf, img->imgdata, true);
629 }
630#else
631 /* get android dtbo */
632 ret = fit_load_images(img->imgdata->buf, img->imgdata, true);
633#endif
634
635 return ret;
636}
637
638static int load_dtbo_image(const char *part_name, struct imageinfo_t *img)
639{
640 int ret;
641 uint32_t total_size;
642 ssize_t read_bytes;
643 void *buf;
644 bdev_t *bdev;
645
646 bdev = bio_open_by_label(part_name) ? : bio_open(part_name);
647 if (!bdev) {
648 LTRACEF("Partition [%s] is not exist.\n", part_name);
649 return -ENODEV;
650 }
651
652 ret = 0;
653 /* read 1 block to determine the dtbo image format */
654 buf = malloc(bdev->block_size);
655 if (!buf) {
656 ret = -ENOMEM;
657 goto _err;
658 }
659
660 memset(buf, 0, bdev->block_size);
661 read_bytes = bio_read(bdev, buf, 0, bdev->block_size);
662 if (read_bytes < (ssize_t)bdev->block_size) {
663 LTRACEF("bio_read offset: 0, size: %ld, got: %ld\n",
664 bdev->block_size, read_bytes);
665 ret = -EIO;
666 goto _err;
667 }
668
669 if (android_dtbo_check_header((const void *)buf) == DTBO_RET_OK) {
670 total_size = android_dtbo_total_size((const void *)buf);
671 if (load_android_dtbo_image(part_name, img, total_size) < 0)
672 ret = -EINVAL;
673 } else if (fdt_check_header((const void *)buf) == 0) {
674 if (load_fdt_dtbo_image(part_name, img) < 0)
675 ret = -EINVAL;
676 } else {
677 LTRACEF("Not valid dtbo image.\n");
678 ret = -ENOTSUP;
679 }
680
681_err:
682 if (buf)
683 free(buf);
684 bio_close(bdev);
685
686 return ret;
687}
688
689static int load_scpsys_image(const char *part_name, struct imageinfo_t *img)
690{
691 int err;
692 void *fit;
693
694 err = fit_get_image(part_name, &img->imgdata->buf);
695 if (err) {
696 LTRACEF("fit_get_image %s fail, err=%d\n", part_name, err);
697 return err;
698 }
699 fit = img->imgdata->buf;
700
701 dprintf(ALWAYS, "verify fit conf sig: %s\n", part_name);
702 err = fit_conf_verify_sig(NULL, fit);
703 if (err < 0)
704 return err;
705
706 err = fit_load_image(NULL, "kernel", fit, NULL, NULL, NULL, true);
707 if (err) {
708 LTRACEF("fit_load_image %s: sram fw fail, err=%d\n", part_name, err);
709 return err;
710 }
711
712 /* the dram part of scpsys image is an optional image, could be absent */
713 err = fit_load_image(NULL, "firmware", fit, NULL, NULL, NULL, true);
714 if (err && err != -ENOENT) {
715 LTRACEF("fit_load_image %s: dram fw fail, err=%d\n", part_name, err);
716 return err;
717 }
718
719 return 0;
720}
721
722static int load_scpdata_image(const char *part_name, struct imageinfo_t *img)
723{
724#define FIT_FASTLOGO_IMG_NODE_NAME "logo"
725#define FIT_GUIDELINE_IMG_NODE_NAME "guideline"
726#define FIT_WARNMSG_IMG_NODE_NAME "warnmsg"
727#define FIT_CVBSIMG_IMG_NODE_NAME "cvbsimg"
728
729 int err;
730 void *fit;
731
732 err = fit_get_image(part_name, &img->imgdata->buf);
733 if (err) {
734 LTRACEF("fit_get_image %s fail, err=%d\n", part_name, err);
735 return err;
736 }
737 fit = img->imgdata->buf;
738
739 err = fit_load_loadable_image(fit, FIT_FASTLOGO_IMG_NODE_NAME, NULL);
740 if (err) {
741 LTRACEF("fit_load_loadable_image %s fail, err=%d\n",
742 FIT_FASTLOGO_IMG_NODE_NAME, err);
743 return err;
744 }
745
746 err = fit_load_loadable_image(fit, FIT_GUIDELINE_IMG_NODE_NAME, NULL);
747 if (err) {
748 LTRACEF("fit_load_loadable_image %s fail, err=%d\n",
749 FIT_GUIDELINE_IMG_NODE_NAME, err);
750 return err;
751 }
752
753 err = fit_load_loadable_image(fit, FIT_WARNMSG_IMG_NODE_NAME, NULL);
754 if (err) {
755 LTRACEF("fit_load_loadable_image %s fail, err=%d\n",
756 FIT_WARNMSG_IMG_NODE_NAME, err);
757 return err;
758 }
759
760 err = fit_load_loadable_image(fit, FIT_CVBSIMG_IMG_NODE_NAME, NULL);
761 if (err) {
762 LTRACEF("fit_load_loadable_image %s fail, err=%d\n",
763 FIT_CVBSIMG_IMG_NODE_NAME, err);
764 return err;
765 }
766
767 return 0;
768}
769
770/* [TODO] mvoe load_scpcfg_image to mt2712 platform layer to keep SCP_CFG_VA
771 * invisble to others */
772static int load_scpcfg_image(const char *part_name, struct imageinfo_t *img)
773{
774 int err;
775 ssize_t bytes_read;
776 bdev_t *bdev;
777
778 bdev = bio_open_by_label(part_name) ? : bio_open(part_name);
779 if (!bdev)
780 return -ENODEV;
781
782 err = 0;
783 bytes_read = bio_read(bdev, img->imgdata->buf, 0, bdev->total_size);
784 if (bytes_read < bdev->total_size)
785 err = -EIO;
786
787 bio_close(bdev);
788
789 return err;
790}
791
792#if ENABLE_MODEM_LOAD
793extern int load_modem_image(const char *part_name);
794static int load_md_image(const char *part_name, struct imageinfo_t *img)
795{
796 return load_modem_image(part_name);
797}
798#endif
799
800#if ENABLE_HSM_OS_LOAD
801extern int load_hsm_os(const char *part_name);
802static int load_hsm_os_image(const char *part_name, struct imageinfo_t *img)
803{
804 return load_hsm_os(part_name);
805}
806#endif