Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 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 | #include <command.h> |
| 9 | #include <mapmem.h> |
| 10 | #include <vsprintf.h> |
| 11 | #include <asm/zimage.h> |
| 12 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 13 | static void zboot_start(int argc, char *const argv[]) |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 14 | { |
| 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 Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc, |
| 58 | char *const argv[]) |
| 59 | { |
| 60 | zboot_start(argc, argv); |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | f6d7ebd | 2023-12-03 17:29:31 -0700 | [diff] [blame^] | 65 | static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 66 | char *const argv[]) |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 67 | { |
| 68 | int ret; |
| 69 | |
| 70 | ret = zboot_load(); |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 71 | if (ret) |
| 72 | return ret; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 77 | static int _zboot_setup(void) |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 78 | { |
| 79 | struct boot_params *base_ptr = state.base_ptr; |
| 80 | |
| 81 | if (!base_ptr) { |
| 82 | printf("base is not set: use 'zboot load' first\n"); |
| 83 | return CMD_RET_FAILURE; |
| 84 | } |
| 85 | if (zboot_setup()) { |
| 86 | puts("Setting up boot parameters failed ...\n"); |
| 87 | return CMD_RET_FAILURE; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 93 | static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc, |
| 94 | char *const argv[]) |
| 95 | { |
| 96 | return _zboot_setup(); |
| 97 | } |
| 98 | |
| 99 | static void zboot_info(void) |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 100 | { |
| 101 | printf("Kernel loaded at %08lx, setup_base=%p\n", |
| 102 | state.load_address, state.base_ptr); |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 106 | char *const argv[]) |
| 107 | { |
| 108 | zboot_info(); |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 113 | static int _zboot_go(void) |
| 114 | { |
| 115 | int ret; |
| 116 | |
| 117 | ret = zboot_go(); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 122 | static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc, |
| 123 | char *const argv[]) |
| 124 | { |
| 125 | int ret; |
| 126 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 127 | ret = _zboot_go(); |
| 128 | if (ret) { |
| 129 | printf("Kernel returned! (err=%d)\n", ret); |
| 130 | return CMD_RET_FAILURE; |
| 131 | } |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 132 | |
Simon Glass | a911beb | 2023-12-03 17:29:30 -0700 | [diff] [blame] | 133 | return 0; |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 137 | char *const argv[]) |
| 138 | { |
| 139 | struct boot_params *base_ptr = state.base_ptr; |
| 140 | |
| 141 | if (argc > 1) |
| 142 | base_ptr = (void *)hextoul(argv[1], NULL); |
| 143 | if (!base_ptr) { |
| 144 | printf("No zboot setup_base\n"); |
| 145 | return CMD_RET_FAILURE; |
| 146 | } |
| 147 | zimage_dump(base_ptr, true); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* Note: This defines the complete_zboot() function */ |
| 153 | U_BOOT_SUBCMDS(zboot, |
| 154 | U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""), |
| 155 | U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""), |
| 156 | U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""), |
| 157 | U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""), |
| 158 | U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""), |
| 159 | U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""), |
| 160 | ) |
| 161 | |
| 162 | int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc, |
| 163 | char *const argv[], int state_mask) |
| 164 | { |
Simon Glass | 205374e | 2023-12-03 17:29:29 -0700 | [diff] [blame] | 165 | int ret; |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 166 | |
Simon Glass | 205374e | 2023-12-03 17:29:29 -0700 | [diff] [blame] | 167 | if (flag & ZBOOT_STATE_START) |
| 168 | ret = do_zboot_start(cmdtp, flag, argc, argv); |
| 169 | if (!ret && (flag & ZBOOT_STATE_LOAD)) |
| 170 | ret = do_zboot_load(cmdtp, flag, argc, argv); |
| 171 | if (!ret && (flag & ZBOOT_STATE_SETUP)) |
| 172 | ret = do_zboot_setup(cmdtp, flag, argc, argv); |
| 173 | if (!ret && (flag & ZBOOT_STATE_INFO)) |
| 174 | ret = do_zboot_info(cmdtp, flag, argc, argv); |
| 175 | if (!ret && (flag & ZBOOT_STATE_GO)) |
| 176 | ret = do_zboot_go(cmdtp, flag, argc, argv); |
| 177 | if (ret) |
| 178 | return ret; |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc, |
| 184 | char *const argv[], int *repeatable) |
| 185 | { |
| 186 | /* determine if we have a sub command */ |
| 187 | if (argc > 1) { |
| 188 | char *endp; |
| 189 | |
| 190 | hextoul(argv[1], &endp); |
| 191 | /* |
| 192 | * endp pointing to nul means that argv[1] was just a valid |
| 193 | * number, so pass it along to the normal processing |
| 194 | */ |
| 195 | if (*endp) |
| 196 | return do_zboot(cmdtp, flag, argc, argv, repeatable); |
| 197 | } |
| 198 | |
| 199 | do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START | |
| 200 | ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP | |
| 201 | ZBOOT_STATE_INFO | ZBOOT_STATE_GO); |
| 202 | |
| 203 | return CMD_RET_FAILURE; |
| 204 | } |
| 205 | |
| 206 | U_BOOT_CMDREP_COMPLETE( |
| 207 | zboot, 8, do_zboot_parent, "Boot bzImage", |
| 208 | "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n" |
| 209 | " addr - The optional starting address of the bzimage.\n" |
| 210 | " If not set it defaults to the environment\n" |
| 211 | " variable \"fileaddr\".\n" |
| 212 | " size - The optional size of the bzimage. Defaults to\n" |
| 213 | " zero.\n" |
| 214 | " initrd addr - The address of the initrd image to use, if any.\n" |
| 215 | " initrd size - The size of the initrd image to use, if any.\n" |
| 216 | " setup - The address of the kernel setup region, if this\n" |
| 217 | " is not at addr\n" |
| 218 | " cmdline - Environment variable containing the kernel\n" |
| 219 | " command line, to override U-Boot's normal\n" |
| 220 | " cmdline generation\n" |
| 221 | "\n" |
| 222 | "Sub-commands to do part of the zboot sequence:\n" |
| 223 | "\tstart [addr [arg ...]] - specify arguments\n" |
| 224 | "\tload - load OS image\n" |
| 225 | "\tsetup - set up table\n" |
| 226 | "\tinfo - show summary info\n" |
| 227 | "\tgo - start OS\n" |
| 228 | "\tdump [addr] - dump info (optional address of boot params)", |
| 229 | complete_zboot |
| 230 | ); |