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