Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Miao Yan | 1868659 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 10 | #include <qfw.h> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | |
| 13 | static struct udevice *qfw_dev; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * This function prepares kernel for zboot. It loads kernel data |
| 17 | * to 'load_addr', initrd to 'initrd_addr' and kernel command |
| 18 | * line using qemu fw_cfg interface. |
| 19 | */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 20 | static int qemu_fwcfg_cmd_setup_kernel(void *load_addr, void *initrd_addr) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 21 | { |
| 22 | char *data_addr; |
| 23 | uint32_t setup_size, kernel_size, cmdline_size, initrd_size; |
| 24 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 25 | qfw_read_entry(qfw_dev, FW_CFG_SETUP_SIZE, 4, &setup_size); |
| 26 | qfw_read_entry(qfw_dev, FW_CFG_KERNEL_SIZE, 4, &kernel_size); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 27 | |
Pierre-Clément Tosi | 24272ff | 2022-05-25 14:38:55 +0100 | [diff] [blame] | 28 | if (kernel_size == 0) { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 29 | printf("warning: no kernel available\n"); |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | data_addr = load_addr; |
Pierre-Clément Tosi | 24272ff | 2022-05-25 14:38:55 +0100 | [diff] [blame] | 34 | if (setup_size != 0) { |
| 35 | qfw_read_entry(qfw_dev, FW_CFG_SETUP_DATA, |
| 36 | le32_to_cpu(setup_size), data_addr); |
| 37 | data_addr += le32_to_cpu(setup_size); |
| 38 | } |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 39 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 40 | qfw_read_entry(qfw_dev, FW_CFG_KERNEL_DATA, |
| 41 | le32_to_cpu(kernel_size), data_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 42 | data_addr += le32_to_cpu(kernel_size); |
| 43 | |
| 44 | data_addr = initrd_addr; |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 45 | qfw_read_entry(qfw_dev, FW_CFG_INITRD_SIZE, 4, &initrd_size); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 46 | if (initrd_size == 0) { |
| 47 | printf("warning: no initrd available\n"); |
| 48 | } else { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 49 | qfw_read_entry(qfw_dev, FW_CFG_INITRD_DATA, |
| 50 | le32_to_cpu(initrd_size), data_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 51 | data_addr += le32_to_cpu(initrd_size); |
| 52 | } |
| 53 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 54 | qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_SIZE, 4, &cmdline_size); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 55 | if (cmdline_size) { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 56 | qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_DATA, |
| 57 | le32_to_cpu(cmdline_size), data_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 58 | /* |
| 59 | * if kernel cmdline only contains '\0', (e.g. no -append |
| 60 | * when invoking qemu), do not update bootargs |
| 61 | */ |
| 62 | if (*data_addr != '\0') { |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 63 | if (env_set("bootargs", data_addr) < 0) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 64 | printf("warning: unable to change bootargs\n"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | printf("loading kernel to address %p size %x", load_addr, |
| 69 | le32_to_cpu(kernel_size)); |
| 70 | if (initrd_size) |
| 71 | printf(" initrd %p size %x\n", |
| 72 | initrd_addr, |
| 73 | le32_to_cpu(initrd_size)); |
| 74 | else |
| 75 | printf("\n"); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 80 | static int qemu_fwcfg_cmd_list_firmware(void) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 81 | { |
| 82 | int ret; |
| 83 | struct fw_cfg_file_iter iter; |
| 84 | struct fw_file *file; |
| 85 | |
| 86 | /* make sure fw_list is loaded */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 87 | ret = qfw_read_firmware_list(qfw_dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 88 | if (ret) |
| 89 | return ret; |
| 90 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 91 | for (file = qfw_file_iter_init(qfw_dev, &iter); |
| 92 | !qfw_file_iter_end(&iter); |
| 93 | file = qfw_file_iter_next(&iter)) { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 94 | printf("%-56s\n", file->cfg.name); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 100 | static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag, |
| 101 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 102 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 103 | if (qemu_fwcfg_cmd_list_firmware() < 0) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 104 | return CMD_RET_FAILURE; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 109 | static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag, |
| 110 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 111 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 112 | printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev)); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 116 | static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag, |
| 117 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 118 | { |
| 119 | char *env; |
| 120 | void *load_addr; |
| 121 | void *initrd_addr; |
| 122 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 123 | env = env_get("loadaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 124 | load_addr = env ? |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 125 | (void *)hextoul(env, NULL) : |
Tom Rini | 2f45b30 | 2021-08-23 10:25:29 -0400 | [diff] [blame] | 126 | (void *)CONFIG_SYS_LOAD_ADDR; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 127 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 128 | env = env_get("ramdiskaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 129 | initrd_addr = env ? |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 130 | (void *)hextoul(env, NULL) : |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 131 | #ifdef CONFIG_RAMDISK_ADDR |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 132 | (void *)CONFIG_RAMDISK_ADDR; |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 133 | #else |
| 134 | NULL; |
| 135 | #endif |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 136 | |
| 137 | if (argc == 2) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 138 | load_addr = (void *)hextoul(argv[0], NULL); |
| 139 | initrd_addr = (void *)hextoul(argv[1], NULL); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 140 | } else if (argc == 1) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 141 | load_addr = (void *)hextoul(argv[0], NULL); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 144 | if (!load_addr || !initrd_addr) { |
| 145 | printf("missing load or initrd address\n"); |
| 146 | return CMD_RET_FAILURE; |
| 147 | } |
| 148 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 149 | return qemu_fwcfg_cmd_setup_kernel(load_addr, initrd_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 152 | static struct cmd_tbl fwcfg_commands[] = { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 153 | U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""), |
| 154 | U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""), |
| 155 | U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""), |
| 156 | }; |
| 157 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 158 | static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 159 | char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 160 | { |
| 161 | int ret; |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 162 | struct cmd_tbl *fwcfg_cmd; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 163 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 164 | ret = qfw_get_dev(&qfw_dev); |
| 165 | if (ret) { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 166 | printf("QEMU fw_cfg interface not found\n"); |
| 167 | return CMD_RET_USAGE; |
| 168 | } |
| 169 | |
| 170 | fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands, |
| 171 | ARRAY_SIZE(fwcfg_commands)); |
| 172 | argc -= 2; |
| 173 | argv += 2; |
| 174 | if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs) |
| 175 | return CMD_RET_USAGE; |
| 176 | |
| 177 | ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv); |
| 178 | |
| 179 | return cmd_process_error(fwcfg_cmd, ret); |
| 180 | } |
| 181 | |
| 182 | U_BOOT_CMD( |
| 183 | qfw, 4, 1, do_qemu_fw, |
| 184 | "QEMU firmware interface", |
| 185 | "<command>\n" |
| 186 | " - list : print firmware(s) currently loaded\n" |
| 187 | " - cpus : print online cpu number\n" |
| 188 | " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n" |
| 189 | ) |