blob: f5c90a8ba8964cd3503676f4eeb1f0e3d5ca8115 [file] [log] [blame]
Simon Glass2bf90bd2023-12-03 17:29:27 -07001// 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#include <command.h>
9#include <mapmem.h>
10#include <vsprintf.h>
11#include <asm/zimage.h>
12
Simon Glassa911beb2023-12-03 17:29:30 -070013static void zboot_start(int argc, char *const argv[])
Simon Glass2bf90bd2023-12-03 17:29:27 -070014{
15 const char *s;
16
17 memset(&state, '\0', sizeof(state));
18 if (argc >= 2) {
19 /* argv[1] holds the address of the bzImage */
20 s = argv[1];
21 } else {
22 s = env_get("fileaddr");
23 }
24
25 if (s)
26 state.bzimage_addr = hextoul(s, NULL);
27
28 if (argc >= 3) {
29 /* argv[2] holds the size of the bzImage */
30 state.bzimage_size = hextoul(argv[2], NULL);
31 }
32
33 if (argc >= 4)
34 state.initrd_addr = hextoul(argv[3], NULL);
35 if (argc >= 5)
36 state.initrd_size = hextoul(argv[4], NULL);
37 if (argc >= 6) {
38 /*
39 * When the base_ptr is passed in, we assume that the image is
40 * already loaded at the address given by argv[1] and therefore
41 * the original bzImage is somewhere else, or not accessible.
42 * In any case, we don't need access to the bzImage since all
43 * the processing is assumed to be done.
44 *
45 * So set the base_ptr to the given address, use this arg as the
46 * load address and set bzimage_addr to 0 so we know that it
47 * cannot be proceesed (or processed again).
48 */
49 state.base_ptr = (void *)hextoul(argv[5], NULL);
50 state.load_address = state.bzimage_addr;
51 state.bzimage_addr = 0;
52 }
53 if (argc >= 7)
54 state.cmdline = env_get(argv[6]);
Simon Glassa911beb2023-12-03 17:29:30 -070055}
56
57static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
58 char *const argv[])
59{
60 zboot_start(argc, argv);
Simon Glass2bf90bd2023-12-03 17:29:27 -070061
62 return 0;
63}
64
Simon Glassa911beb2023-12-03 17:29:30 -070065static int _zboot_load(void)
66{
67 int ret;
68
69 ret = zboot_load();
70 if (!ret)
71 ret = env_set_hex("zbootbase", map_to_sysmem(state.base_ptr));
72 if (!ret)
73 ret = env_set_hex("zbootaddr", state.load_address);
74 if (ret)
75 return ret;
76
77 return 0;
78}
79
Simon Glass2bf90bd2023-12-03 17:29:27 -070080static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
81 char *const argv[])
82{
Simon Glassa911beb2023-12-03 17:29:30 -070083 if (_zboot_load())
Simon Glass2bf90bd2023-12-03 17:29:27 -070084 return CMD_RET_FAILURE;
85
86 return 0;
87}
88
Simon Glassa911beb2023-12-03 17:29:30 -070089static int _zboot_setup(void)
Simon Glass2bf90bd2023-12-03 17:29:27 -070090{
91 struct boot_params *base_ptr = state.base_ptr;
92
93 if (!base_ptr) {
94 printf("base is not set: use 'zboot load' first\n");
95 return CMD_RET_FAILURE;
96 }
97 if (zboot_setup()) {
98 puts("Setting up boot parameters failed ...\n");
99 return CMD_RET_FAILURE;
100 }
101
102 return 0;
103}
104
Simon Glassa911beb2023-12-03 17:29:30 -0700105static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
106 char *const argv[])
107{
108 return _zboot_setup();
109}
110
111static void zboot_info(void)
Simon Glass2bf90bd2023-12-03 17:29:27 -0700112{
113 printf("Kernel loaded at %08lx, setup_base=%p\n",
114 state.load_address, state.base_ptr);
Simon Glassa911beb2023-12-03 17:29:30 -0700115}
116
117static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
118 char *const argv[])
119{
120 zboot_info();
Simon Glass2bf90bd2023-12-03 17:29:27 -0700121
122 return 0;
123}
124
Simon Glassa911beb2023-12-03 17:29:30 -0700125static int _zboot_go(void)
126{
127 int ret;
128
129 ret = zboot_go();
130
131 return ret;
132}
133
Simon Glass2bf90bd2023-12-03 17:29:27 -0700134static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
135 char *const argv[])
136{
137 int ret;
138
Simon Glassa911beb2023-12-03 17:29:30 -0700139 ret = _zboot_go();
140 if (ret) {
141 printf("Kernel returned! (err=%d)\n", ret);
142 return CMD_RET_FAILURE;
143 }
Simon Glass2bf90bd2023-12-03 17:29:27 -0700144
Simon Glassa911beb2023-12-03 17:29:30 -0700145 return 0;
Simon Glass2bf90bd2023-12-03 17:29:27 -0700146}
147
148static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
149 char *const argv[])
150{
151 struct boot_params *base_ptr = state.base_ptr;
152
153 if (argc > 1)
154 base_ptr = (void *)hextoul(argv[1], NULL);
155 if (!base_ptr) {
156 printf("No zboot setup_base\n");
157 return CMD_RET_FAILURE;
158 }
159 zimage_dump(base_ptr, true);
160
161 return 0;
162}
163
164/* Note: This defines the complete_zboot() function */
165U_BOOT_SUBCMDS(zboot,
166 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
167 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
168 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
169 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
170 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
171 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
172)
173
174int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
175 char *const argv[], int state_mask)
176{
Simon Glass205374e2023-12-03 17:29:29 -0700177 int ret;
Simon Glass2bf90bd2023-12-03 17:29:27 -0700178
Simon Glass205374e2023-12-03 17:29:29 -0700179 if (flag & ZBOOT_STATE_START)
180 ret = do_zboot_start(cmdtp, flag, argc, argv);
181 if (!ret && (flag & ZBOOT_STATE_LOAD))
182 ret = do_zboot_load(cmdtp, flag, argc, argv);
183 if (!ret && (flag & ZBOOT_STATE_SETUP))
184 ret = do_zboot_setup(cmdtp, flag, argc, argv);
185 if (!ret && (flag & ZBOOT_STATE_INFO))
186 ret = do_zboot_info(cmdtp, flag, argc, argv);
187 if (!ret && (flag & ZBOOT_STATE_GO))
188 ret = do_zboot_go(cmdtp, flag, argc, argv);
189 if (ret)
190 return ret;
Simon Glass2bf90bd2023-12-03 17:29:27 -0700191
192 return 0;
193}
194
195int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
196 char *const argv[], int *repeatable)
197{
198 /* determine if we have a sub command */
199 if (argc > 1) {
200 char *endp;
201
202 hextoul(argv[1], &endp);
203 /*
204 * endp pointing to nul means that argv[1] was just a valid
205 * number, so pass it along to the normal processing
206 */
207 if (*endp)
208 return do_zboot(cmdtp, flag, argc, argv, repeatable);
209 }
210
211 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
212 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
213 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
214
215 return CMD_RET_FAILURE;
216}
217
218U_BOOT_CMDREP_COMPLETE(
219 zboot, 8, do_zboot_parent, "Boot bzImage",
220 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
221 " addr - The optional starting address of the bzimage.\n"
222 " If not set it defaults to the environment\n"
223 " variable \"fileaddr\".\n"
224 " size - The optional size of the bzimage. Defaults to\n"
225 " zero.\n"
226 " initrd addr - The address of the initrd image to use, if any.\n"
227 " initrd size - The size of the initrd image to use, if any.\n"
228 " setup - The address of the kernel setup region, if this\n"
229 " is not at addr\n"
230 " cmdline - Environment variable containing the kernel\n"
231 " command line, to override U-Boot's normal\n"
232 " cmdline generation\n"
233 "\n"
234 "Sub-commands to do part of the zboot sequence:\n"
235 "\tstart [addr [arg ...]] - specify arguments\n"
236 "\tload - load OS image\n"
237 "\tsetup - set up table\n"
238 "\tinfo - show summary info\n"
239 "\tgo - start OS\n"
240 "\tdump [addr] - dump info (optional address of boot params)",
241 complete_zboot
242);