2 * (C) Copyright 2015 Miao Yan <yanmiaoebst@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/fw_cfg.h>
14 static bool fwcfg_present;
15 static bool fwcfg_dma_present;
17 /* Read configuration item using fw_cfg PIO interface */
18 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
19 uint32_t size, void *address)
22 uint8_t *data = address;
25 * writting FW_CFG_INVALID will cause read operation to resume at
26 * last offset, otherwise read will start at offset 0
28 if (entry != FW_CFG_INVALID)
29 outw(entry, FW_CONTROL_PORT);
31 data[i++] = inb(FW_DATA_PORT);
34 /* Read configuration item using fw_cfg DMA interface */
35 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
36 uint32_t size, void *address)
38 struct fw_cfg_dma_access dma;
40 dma.length = cpu_to_be32(size);
41 dma.address = cpu_to_be64((uintptr_t)address);
42 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
45 * writting FW_CFG_INVALID will cause read operation to resume at
46 * last offset, otherwise read will start at offset 0
48 if (entry != FW_CFG_INVALID)
49 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
53 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
54 address, size, be32_to_cpu(dma.control));
56 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
58 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
59 __asm__ __volatile__ ("pause");
62 static bool qemu_fwcfg_present(void)
66 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
67 return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
70 static bool qemu_fwcfg_dma_present(void)
74 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
75 if (dma_enabled & FW_CFG_DMA_ENABLED)
81 static void qemu_fwcfg_read_entry(uint16_t entry,
82 uint32_t length, void *address)
84 if (fwcfg_dma_present)
85 qemu_fwcfg_read_entry_dma(entry, length, address);
87 qemu_fwcfg_read_entry_pio(entry, length, address);
90 int qemu_fwcfg_online_cpus(void)
97 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
99 return le16_to_cpu(nb_cpus);
103 * This function prepares kernel for zboot. It loads kernel data
104 * to 'load_addr', initrd to 'initrd_addr' and kernel command
105 * line using qemu fw_cfg interface.
107 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
110 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
112 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
113 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
115 if (setup_size == 0 || kernel_size == 0) {
116 printf("warning: no kernel available\n");
120 data_addr = load_addr;
121 qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
122 le32_to_cpu(setup_size), data_addr);
123 data_addr += le32_to_cpu(setup_size);
125 qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
126 le32_to_cpu(kernel_size), data_addr);
127 data_addr += le32_to_cpu(kernel_size);
129 data_addr = initrd_addr;
130 qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
131 if (initrd_size == 0) {
132 printf("warning: no initrd available\n");
134 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
135 le32_to_cpu(initrd_size), data_addr);
136 data_addr += le32_to_cpu(initrd_size);
139 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
141 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
142 le32_to_cpu(cmdline_size), data_addr);
144 * if kernel cmdline only contains '\0', (e.g. no -append
145 * when invoking qemu), do not update bootargs
147 if (*data_addr != '\0') {
148 if (setenv("bootargs", data_addr) < 0)
149 printf("warning: unable to change bootargs\n");
153 printf("loading kernel to address %p size %x", load_addr,
154 le32_to_cpu(kernel_size));
156 printf(" initrd %p size %x\n",
158 le32_to_cpu(initrd_size));
165 static int qemu_fwcfg_list_firmware(void)
169 struct fw_cfg_files *files;
171 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
175 count = be32_to_cpu(count);
176 files = malloc(count * sizeof(struct fw_cfg_file));
180 files->count = count;
181 qemu_fwcfg_read_entry(FW_CFG_INVALID,
182 count * sizeof(struct fw_cfg_file),
185 for (i = 0; i < files->count; i++)
186 printf("%-56s\n", files->files[i].name);
191 void qemu_fwcfg_init(void)
193 fwcfg_present = qemu_fwcfg_present();
195 fwcfg_dma_present = qemu_fwcfg_dma_present();
198 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
199 int argc, char * const argv[])
201 if (qemu_fwcfg_list_firmware() < 0)
202 return CMD_RET_FAILURE;
207 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
208 int argc, char * const argv[])
210 int ret = qemu_fwcfg_online_cpus();
212 printf("QEMU fw_cfg interface not found\n");
213 return CMD_RET_FAILURE;
216 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
221 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
222 int argc, char * const argv[])
228 env = getenv("loadaddr");
230 (void *)simple_strtoul(env, NULL, 16) :
231 (void *)CONFIG_LOADADDR;
233 env = getenv("ramdiskaddr");
235 (void *)simple_strtoul(env, NULL, 16) :
236 (void *)CONFIG_RAMDISK_ADDR;
239 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
240 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
241 } else if (argc == 1) {
242 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
245 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
248 static cmd_tbl_t fwcfg_commands[] = {
249 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
250 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
251 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
254 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
257 cmd_tbl_t *fwcfg_cmd;
259 if (!fwcfg_present) {
260 printf("QEMU fw_cfg interface not found\n");
261 return CMD_RET_USAGE;
264 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
265 ARRAY_SIZE(fwcfg_commands));
268 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
269 return CMD_RET_USAGE;
271 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
273 return cmd_process_error(fwcfg_cmd, ret);
277 qfw, 4, 1, do_qemu_fw,
278 "QEMU firmware interface",
280 " - list : print firmware(s) currently loaded\n"
281 " - cpus : print online cpu number\n"
282 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"