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