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 | |
| 13 | static 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 | |
| 60 | static 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 | |
| 73 | static 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 | |
| 90 | static 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 | |
| 99 | static 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 | |
| 110 | static 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 */ |
| 127 | U_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 | |
| 136 | int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc, |
| 137 | char *const argv[], int state_mask) |
| 138 | { |
Simon Glass | 205374e | 2023-12-03 17:29:29 -0700 | [diff] [blame^] | 139 | int ret; |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 140 | |
Simon Glass | 205374e | 2023-12-03 17:29:29 -0700 | [diff] [blame^] | 141 | if (flag & ZBOOT_STATE_START) |
| 142 | ret = do_zboot_start(cmdtp, flag, argc, argv); |
| 143 | if (!ret && (flag & ZBOOT_STATE_LOAD)) |
| 144 | ret = do_zboot_load(cmdtp, flag, argc, argv); |
| 145 | if (!ret && (flag & ZBOOT_STATE_SETUP)) |
| 146 | ret = do_zboot_setup(cmdtp, flag, argc, argv); |
| 147 | if (!ret && (flag & ZBOOT_STATE_INFO)) |
| 148 | ret = do_zboot_info(cmdtp, flag, argc, argv); |
| 149 | if (!ret && (flag & ZBOOT_STATE_GO)) |
| 150 | ret = do_zboot_go(cmdtp, flag, argc, argv); |
| 151 | if (ret) |
| 152 | return ret; |
Simon Glass | 2bf90bd | 2023-12-03 17:29:27 -0700 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc, |
| 158 | char *const argv[], int *repeatable) |
| 159 | { |
| 160 | /* determine if we have a sub command */ |
| 161 | if (argc > 1) { |
| 162 | char *endp; |
| 163 | |
| 164 | hextoul(argv[1], &endp); |
| 165 | /* |
| 166 | * endp pointing to nul means that argv[1] was just a valid |
| 167 | * number, so pass it along to the normal processing |
| 168 | */ |
| 169 | if (*endp) |
| 170 | return do_zboot(cmdtp, flag, argc, argv, repeatable); |
| 171 | } |
| 172 | |
| 173 | do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START | |
| 174 | ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP | |
| 175 | ZBOOT_STATE_INFO | ZBOOT_STATE_GO); |
| 176 | |
| 177 | return CMD_RET_FAILURE; |
| 178 | } |
| 179 | |
| 180 | U_BOOT_CMDREP_COMPLETE( |
| 181 | zboot, 8, do_zboot_parent, "Boot bzImage", |
| 182 | "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n" |
| 183 | " addr - The optional starting address of the bzimage.\n" |
| 184 | " If not set it defaults to the environment\n" |
| 185 | " variable \"fileaddr\".\n" |
| 186 | " size - The optional size of the bzimage. Defaults to\n" |
| 187 | " zero.\n" |
| 188 | " initrd addr - The address of the initrd image to use, if any.\n" |
| 189 | " initrd size - The size of the initrd image to use, if any.\n" |
| 190 | " setup - The address of the kernel setup region, if this\n" |
| 191 | " is not at addr\n" |
| 192 | " cmdline - Environment variable containing the kernel\n" |
| 193 | " command line, to override U-Boot's normal\n" |
| 194 | " cmdline generation\n" |
| 195 | "\n" |
| 196 | "Sub-commands to do part of the zboot sequence:\n" |
| 197 | "\tstart [addr [arg ...]] - specify arguments\n" |
| 198 | "\tload - load OS image\n" |
| 199 | "\tsetup - set up table\n" |
| 200 | "\tinfo - show summary info\n" |
| 201 | "\tgo - start OS\n" |
| 202 | "\tdump [addr] - dump info (optional address of boot params)", |
| 203 | complete_zboot |
| 204 | ); |