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