xref: /OK3568_Linux_fs/u-boot/cmd/bootuimage.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * (C) Copyright 2019 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <bootm.h>
9 #include <boot_rkimg.h>
10 #include <console.h>
11 #include <image.h>
12 #include <malloc.h>
13 #include <sysmem.h>
14 #include <linux/libfdt.h>
15 #include <asm/arch/hotkey.h>
16 #include <asm/arch/resource_img.h>
17 #include <asm/arch/boot_mode.h>
18 #include <asm/arch/uimage.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
do_boot_uimage_storage(void)22 static void *do_boot_uimage_storage(void)
23 {
24 	return uimage_load_bootables();
25 }
26 
do_boot_uimage_ram(char * const argv[])27 static void *do_boot_uimage_ram(char *const argv[])
28 {
29 	image_header_t *hdr;
30 	int blknum;
31 
32 	hdr = (void *)simple_strtoul(argv[1], NULL, 16);
33 	if (!hdr || !image_check_magic(hdr)) {
34 		UIMG_I("Invalid header");
35 		return NULL;
36 	}
37 
38 	if (image_get_type(hdr) != IH_TYPE_MULTI) {
39 		UIMG_I("Invalid multi images\n");
40 		return NULL;
41 	}
42 
43 	/* reserve this full uImage */
44 	blknum = DIV_ROUND_UP(image_get_image_size(hdr), RK_BLK_SIZE);
45 	if (!sysmem_alloc_base(MEM_UIMAGE_USER, (phys_addr_t)hdr,
46 			       blknum * RK_BLK_SIZE))
47 		return NULL;
48 
49 	return hdr;
50 }
51 
do_boot_uimage(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])52 static int do_boot_uimage(cmd_tbl_t *cmdtp, int flag,
53 			  int argc, char *const argv[])
54 {
55 	char *bootm_args[1];
56 	image_header_t *img;
57 	char uimg_addr[12];
58 	u32 ramdisk_sz = 0;
59 	int ret;
60 
61 	if (argc > 2)
62 		return CMD_RET_USAGE;
63 
64 	printf("\n## Booting Multi uImage ");
65 
66 	if (argc == 1)
67 		img = do_boot_uimage_storage();
68 	else
69 		img = do_boot_uimage_ram(argv);
70 
71 	if (!img) {
72 		UIMG_I("Failed to load multi images\n");
73 		goto out;
74 	}
75 
76 	if (uimage_sysmem_reserve_each(img, &ramdisk_sz))
77 		goto out;
78 
79 	snprintf(uimg_addr, sizeof(uimg_addr), "0x%lx", (ulong)img);
80 	bootm_args[0] = uimg_addr;
81 
82 	printf("at %s\n", uimg_addr);
83 
84 	ret = do_bootm_states(NULL, 0, ARRAY_SIZE(bootm_args), bootm_args,
85 		BOOTM_STATE_START |
86 		BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
87 		BOOTM_STATE_LOADOS |
88 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
89 		BOOTM_STATE_RAMDISK |
90 #endif
91 		BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
92 		BOOTM_STATE_OS_GO, &images, 1);
93 
94 	if (ret && argc != 1) {
95 		uimage_sysmem_free_each(img, ramdisk_sz);
96 		goto out;
97 	}
98 
99 	return CMD_RET_SUCCESS;
100 out:
101 	return CMD_RET_FAILURE;
102 }
103 
104 U_BOOT_CMD(
105 	boot_uimage,  2,     1,      do_boot_uimage,
106 	"Boot Legacy uImage from memory or boot(recovery) partitions",
107 	"boot_uimage [addr]"
108 );
109