1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  * (C) Copyright 2002
5  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
6  */
7 
8 #define LOG_CATEGORY	LOGC_BOOT
9 
10 #include <command.h>
11 #include <env.h>
12 #include <mapmem.h>
13 #include <vsprintf.h>
14 #include <asm/zimage.h>
15 
do_zboot_start(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])16 static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
17 			  char *const argv[])
18 {
19 	ulong bzimage_addr = 0, bzimage_size, initrd_addr, initrd_size;
20 	const char *s, *cmdline;
21 	ulong base_addr;
22 	int i;
23 
24 	log_debug("argc %d:", argc);
25 	for (i = 0; i < argc; i++)
26 		log_debug(" %s", argv[i]);
27 	log_debug("\n");
28 
29 	/* argv[1] holds the address of the bzImage */
30 	s = cmd_arg1(argc, argv) ? : env_get("fileaddr");
31 	if (s)
32 		bzimage_addr = hextoul(s, NULL);
33 	bzimage_size = argc > 2 ? hextoul(argv[2], NULL) : 0;
34 	initrd_addr = argc > 3 ? hextoul(argv[3], NULL) : 0;
35 	initrd_size = argc > 4 ? hextoul(argv[4], NULL) : 0;
36 	base_addr = argc > 5 ? hextoul(argv[5], NULL) : 0;
37 	cmdline = argc > 6 ? env_get(argv[6]) : NULL;
38 
39 	zboot_start(bzimage_addr, bzimage_size, initrd_addr, initrd_size,
40 		    base_addr, cmdline);
41 
42 	return 0;
43 }
44 
do_zboot_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])45 static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
46 			 char *const argv[])
47 {
48 	int ret;
49 
50 	ret = zboot_load();
51 	if (ret)
52 		return ret;
53 
54 	return 0;
55 }
56 
do_zboot_setup(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])57 static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
58 			  char *const argv[])
59 {
60 	if (!state.base_ptr) {
61 		printf("base is not set: use 'zboot load' first\n");
62 		return CMD_RET_FAILURE;
63 	}
64 	if (zboot_setup()) {
65 		puts("Setting up boot parameters failed ...\n");
66 		return CMD_RET_FAILURE;
67 	}
68 
69 	if (zboot_setup())
70 		return CMD_RET_FAILURE;
71 
72 	return 0;
73 }
74 
do_zboot_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])75 static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
76 			 char *const argv[])
77 {
78 	zboot_info();
79 
80 	return 0;
81 }
82 
do_zboot_go(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])83 static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
84 		       char *const argv[])
85 {
86 	int ret;
87 
88 	ret = zboot_go();
89 	if (ret) {
90 		printf("Kernel returned! (err=%d)\n", ret);
91 		return CMD_RET_FAILURE;
92 	}
93 
94 	return 0;
95 }
96 
do_zboot_dump(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])97 static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
98 			 char *const argv[])
99 {
100 	struct boot_params *base_ptr = state.base_ptr;
101 
102 	if (argc > 1)
103 		base_ptr = (void *)hextoul(argv[1], NULL);
104 	if (!base_ptr) {
105 		printf("No zboot setup_base\n");
106 		return CMD_RET_FAILURE;
107 	}
108 	zimage_dump(base_ptr, true);
109 
110 	return 0;
111 }
112 
113 /* Note: This defines the complete_zboot() function */
114 U_BOOT_SUBCMDS(zboot,
115 	U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
116 	U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
117 	U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
118 	U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
119 	U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
120 	U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
121 )
122 
do_zboot_states(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[],int state_mask)123 int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
124 		    char *const argv[], int state_mask)
125 {
126 	int ret = 0;
127 
128 	log_debug("state_mask %x\n", state_mask);
129 	if (state_mask & ZBOOT_STATE_START)
130 		ret = do_zboot_start(cmdtp, flag, argc, argv);
131 	if (!ret && (state_mask & ZBOOT_STATE_LOAD))
132 		ret = do_zboot_load(cmdtp, flag, argc, argv);
133 	if (!ret && (state_mask & ZBOOT_STATE_SETUP))
134 		ret = do_zboot_setup(cmdtp, flag, argc, argv);
135 	if (!ret && (state_mask & ZBOOT_STATE_INFO))
136 		ret = do_zboot_info(cmdtp, flag, argc, argv);
137 	if (!ret && (state_mask & ZBOOT_STATE_GO))
138 		ret = do_zboot_go(cmdtp, flag, argc, argv);
139 	if (ret)
140 		return ret;
141 
142 	return 0;
143 }
144 
do_zboot_parent(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[],int * repeatable)145 int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
146 		    char *const argv[], int *repeatable)
147 {
148 	/* determine if we have a sub command */
149 	if (argc > 1) {
150 		char *endp;
151 
152 		hextoul(argv[1], &endp);
153 		/*
154 		 * endp pointing to nul means that argv[1] was just a valid
155 		 * number, so pass it along to the normal processing
156 		 */
157 		if (*endp)
158 			return do_zboot(cmdtp, flag, argc, argv, repeatable);
159 	}
160 
161 	do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
162 			ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
163 			ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
164 
165 	return CMD_RET_FAILURE;
166 }
167 
168 U_BOOT_CMDREP_COMPLETE(
169 	zboot, 8, do_zboot_parent, "Boot bzImage",
170 	"[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
171 	"      addr -        The optional starting address of the bzimage.\n"
172 	"                    If not set it defaults to the environment\n"
173 	"                    variable \"fileaddr\".\n"
174 	"      size -        The optional size of the bzimage. Defaults to\n"
175 	"                    zero.\n"
176 	"      initrd addr - The address of the initrd image to use, if any.\n"
177 	"      initrd size - The size of the initrd image to use, if any.\n"
178 	"      setup -       The address of the kernel setup region, if this\n"
179 	"                    is not at addr\n"
180 	"      cmdline -     Environment variable containing the kernel\n"
181 	"                    command line, to override U-Boot's normal\n"
182 	"                    cmdline generation\n"
183 	"\n"
184 	"Sub-commands to do part of the zboot sequence:\n"
185 	"\tstart [addr [arg ...]] - specify arguments\n"
186 	"\tload   - load OS image\n"
187 	"\tsetup  - set up table\n"
188 	"\tinfo   - show summary info\n"
189 	"\tgo     - start OS\n"
190 	"\tdump [addr]    - dump info (optional address of boot params)",
191 	complete_zboot
192 );
193