blob: c9375fb417c149a0175fda15bad7218684f8551f [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
13static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
14 char *const argv[])
15{
16 const char *s;
17
18 memset(&state, '\0', sizeof(state));
19 if (argc >= 2) {
20 /* argv[1] holds the address of the bzImage */
21 s = argv[1];
22 } else {
23 s = env_get("fileaddr");
24 }
25
26 if (s)
27 state.bzimage_addr = hextoul(s, NULL);
28
29 if (argc >= 3) {
30 /* argv[2] holds the size of the bzImage */
31 state.bzimage_size = hextoul(argv[2], NULL);
32 }
33
34 if (argc >= 4)
35 state.initrd_addr = hextoul(argv[3], NULL);
36 if (argc >= 5)
37 state.initrd_size = hextoul(argv[4], NULL);
38 if (argc >= 6) {
39 /*
40 * When the base_ptr is passed in, we assume that the image is
41 * already loaded at the address given by argv[1] and therefore
42 * the original bzImage is somewhere else, or not accessible.
43 * In any case, we don't need access to the bzImage since all
44 * the processing is assumed to be done.
45 *
46 * So set the base_ptr to the given address, use this arg as the
47 * load address and set bzimage_addr to 0 so we know that it
48 * cannot be proceesed (or processed again).
49 */
50 state.base_ptr = (void *)hextoul(argv[5], NULL);
51 state.load_address = state.bzimage_addr;
52 state.bzimage_addr = 0;
53 }
54 if (argc >= 7)
55 state.cmdline = env_get(argv[6]);
56
57 return 0;
58}
59
60static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
61 char *const argv[])
62{
63 if (zboot_load())
64 return CMD_RET_FAILURE;
65
66 if (env_set_hex("zbootbase", map_to_sysmem(state.base_ptr)) ||
67 env_set_hex("zbootaddr", state.load_address))
68 return CMD_RET_FAILURE;
69
70 return 0;
71}
72
73static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
74 char *const argv[])
75{
76 struct boot_params *base_ptr = state.base_ptr;
77
78 if (!base_ptr) {
79 printf("base is not set: use 'zboot load' first\n");
80 return CMD_RET_FAILURE;
81 }
82 if (zboot_setup()) {
83 puts("Setting up boot parameters failed ...\n");
84 return CMD_RET_FAILURE;
85 }
86
87 return 0;
88}
89
90static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
91 char *const argv[])
92{
93 printf("Kernel loaded at %08lx, setup_base=%p\n",
94 state.load_address, state.base_ptr);
95
96 return 0;
97}
98
99static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
100 char *const argv[])
101{
102 int ret;
103
104 ret = zboot_go();
105 printf("Kernel returned! (err=%d)\n", ret);
106
107 return CMD_RET_FAILURE;
108}
109
110static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
111 char *const argv[])
112{
113 struct boot_params *base_ptr = state.base_ptr;
114
115 if (argc > 1)
116 base_ptr = (void *)hextoul(argv[1], NULL);
117 if (!base_ptr) {
118 printf("No zboot setup_base\n");
119 return CMD_RET_FAILURE;
120 }
121 zimage_dump(base_ptr, true);
122
123 return 0;
124}
125
126/* Note: This defines the complete_zboot() function */
127U_BOOT_SUBCMDS(zboot,
128 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
129 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
130 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
131 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
132 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
133 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
134)
135
136int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
137 char *const argv[], int state_mask)
138{
139 int i;
140
141 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
142 struct cmd_tbl *cmd = &zboot_subcmds[i];
143 int mask = 1 << i;
144 int ret;
145
146 if (mask & state_mask) {
147 ret = cmd->cmd(cmd, flag, argc, argv);
148 if (ret)
149 return ret;
150 }
151 }
152
153 return 0;
154}
155
156int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
157 char *const argv[], int *repeatable)
158{
159 /* determine if we have a sub command */
160 if (argc > 1) {
161 char *endp;
162
163 hextoul(argv[1], &endp);
164 /*
165 * endp pointing to nul means that argv[1] was just a valid
166 * number, so pass it along to the normal processing
167 */
168 if (*endp)
169 return do_zboot(cmdtp, flag, argc, argv, repeatable);
170 }
171
172 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
173 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
174 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
175
176 return CMD_RET_FAILURE;
177}
178
179U_BOOT_CMDREP_COMPLETE(
180 zboot, 8, do_zboot_parent, "Boot bzImage",
181 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
182 " addr - The optional starting address of the bzimage.\n"
183 " If not set it defaults to the environment\n"
184 " variable \"fileaddr\".\n"
185 " size - The optional size of the bzimage. Defaults to\n"
186 " zero.\n"
187 " initrd addr - The address of the initrd image to use, if any.\n"
188 " initrd size - The size of the initrd image to use, if any.\n"
189 " setup - The address of the kernel setup region, if this\n"
190 " is not at addr\n"
191 " cmdline - Environment variable containing the kernel\n"
192 " command line, to override U-Boot's normal\n"
193 " cmdline generation\n"
194 "\n"
195 "Sub-commands to do part of the zboot sequence:\n"
196 "\tstart [addr [arg ...]] - specify arguments\n"
197 "\tload - load OS image\n"
198 "\tsetup - set up table\n"
199 "\tinfo - show summary info\n"
200 "\tgo - start OS\n"
201 "\tdump [addr] - dump info (optional address of boot params)",
202 complete_zboot
203);