2 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <qemu_fw_cfg.h>
13 #include <linux/list.h>
15 static bool fwcfg_present;
16 static bool fwcfg_dma_present;
18 static LIST_HEAD(fw_list);
20 /* Read configuration item using fw_cfg PIO interface */
21 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
22 uint32_t size, void *address)
25 uint8_t *data = address;
28 * writting FW_CFG_INVALID will cause read operation to resume at
29 * last offset, otherwise read will start at offset 0
31 if (entry != FW_CFG_INVALID)
32 outw(entry, FW_CONTROL_PORT);
34 data[i++] = inb(FW_DATA_PORT);
37 /* Read configuration item using fw_cfg DMA interface */
38 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
39 uint32_t size, void *address)
41 struct fw_cfg_dma_access dma;
43 dma.length = cpu_to_be32(size);
44 dma.address = cpu_to_be64((uintptr_t)address);
45 dma.control = cpu_to_be32(FW_CFG_DMA_READ);
48 * writting FW_CFG_INVALID will cause read operation to resume at
49 * last offset, otherwise read will start at offset 0
51 if (entry != FW_CFG_INVALID)
52 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
56 debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
57 address, size, be32_to_cpu(dma.control));
59 outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
61 while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
62 __asm__ __volatile__ ("pause");
65 static bool qemu_fwcfg_present(void)
69 qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
70 return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
73 static bool qemu_fwcfg_dma_present(void)
77 qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
78 if (dma_enabled & FW_CFG_DMA_ENABLED)
84 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
86 if (fwcfg_dma_present)
87 qemu_fwcfg_read_entry_dma(entry, length, address);
89 qemu_fwcfg_read_entry_pio(entry, length, address);
92 int qemu_fwcfg_online_cpus(void)
99 qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
101 return le16_to_cpu(nb_cpus);
105 * This function prepares kernel for zboot. It loads kernel data
106 * to 'load_addr', initrd to 'initrd_addr' and kernel command
107 * line using qemu fw_cfg interface.
109 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
112 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
114 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
115 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
117 if (setup_size == 0 || kernel_size == 0) {
118 printf("warning: no kernel available\n");
122 data_addr = load_addr;
123 qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
124 le32_to_cpu(setup_size), data_addr);
125 data_addr += le32_to_cpu(setup_size);
127 qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
128 le32_to_cpu(kernel_size), data_addr);
129 data_addr += le32_to_cpu(kernel_size);
131 data_addr = initrd_addr;
132 qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
133 if (initrd_size == 0) {
134 printf("warning: no initrd available\n");
136 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
137 le32_to_cpu(initrd_size), data_addr);
138 data_addr += le32_to_cpu(initrd_size);
141 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
143 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
144 le32_to_cpu(cmdline_size), data_addr);
146 * if kernel cmdline only contains '\0', (e.g. no -append
147 * when invoking qemu), do not update bootargs
149 if (*data_addr != '\0') {
150 if (setenv("bootargs", data_addr) < 0)
151 printf("warning: unable to change bootargs\n");
155 printf("loading kernel to address %p size %x", load_addr,
156 le32_to_cpu(kernel_size));
158 printf(" initrd %p size %x\n",
160 le32_to_cpu(initrd_size));
167 int qemu_fwcfg_read_firmware_list(void)
171 struct fw_file *file;
172 struct list_head *entry;
174 /* don't read it twice */
175 if (!list_empty(&fw_list))
178 qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
182 count = be32_to_cpu(count);
183 for (i = 0; i < count; i++) {
184 file = malloc(sizeof(*file));
186 printf("error: allocating resource\n");
189 qemu_fwcfg_read_entry(FW_CFG_INVALID,
190 sizeof(struct fw_cfg_file), &file->cfg);
192 list_add_tail(&file->list, &fw_list);
198 list_for_each(entry, &fw_list) {
199 file = list_entry(entry, struct fw_file, list);
206 struct fw_file *qemu_fwcfg_find_file(const char *name)
208 struct list_head *entry;
209 struct fw_file *file;
211 list_for_each(entry, &fw_list) {
212 file = list_entry(entry, struct fw_file, list);
213 if (!strcmp(file->cfg.name, name))
220 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
222 iter->entry = fw_list.next;
223 return list_entry(iter->entry, struct fw_file, list);
226 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
228 iter->entry = iter->entry->next;
229 return list_entry(iter->entry, struct fw_file, list);
232 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
234 return iter->entry == &fw_list;
237 static int qemu_fwcfg_list_firmware(void)
240 struct fw_cfg_file_iter iter;
241 struct fw_file *file;
243 /* make sure fw_list is loaded */
244 ret = qemu_fwcfg_read_firmware_list();
249 for (file = qemu_fwcfg_file_iter_init(&iter);
250 !qemu_fwcfg_file_iter_end(&iter);
251 file = qemu_fwcfg_file_iter_next(&iter)) {
252 printf("%-56s\n", file->cfg.name);
258 void qemu_fwcfg_init(void)
260 fwcfg_present = qemu_fwcfg_present();
262 fwcfg_dma_present = qemu_fwcfg_dma_present();
265 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
266 int argc, char * const argv[])
268 if (qemu_fwcfg_list_firmware() < 0)
269 return CMD_RET_FAILURE;
274 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
275 int argc, char * const argv[])
277 int ret = qemu_fwcfg_online_cpus();
279 printf("QEMU fw_cfg interface not found\n");
280 return CMD_RET_FAILURE;
283 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
288 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
289 int argc, char * const argv[])
295 env = getenv("loadaddr");
297 (void *)simple_strtoul(env, NULL, 16) :
298 (void *)CONFIG_LOADADDR;
300 env = getenv("ramdiskaddr");
302 (void *)simple_strtoul(env, NULL, 16) :
303 (void *)CONFIG_RAMDISK_ADDR;
306 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
307 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
308 } else if (argc == 1) {
309 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
312 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
315 static cmd_tbl_t fwcfg_commands[] = {
316 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
317 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
318 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
321 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 cmd_tbl_t *fwcfg_cmd;
326 if (!fwcfg_present) {
327 printf("QEMU fw_cfg interface not found\n");
328 return CMD_RET_USAGE;
331 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
332 ARRAY_SIZE(fwcfg_commands));
335 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
336 return CMD_RET_USAGE;
338 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
340 return cmd_process_error(fwcfg_cmd, ret);
344 qfw, 4, 1, do_qemu_fw,
345 "QEMU firmware interface",
347 " - list : print firmware(s) currently loaded\n"
348 " - cpus : print online cpu number\n"
349 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"