1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
13 static struct udevice *qfw_dev;
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.
20 static int qemu_fwcfg_cmd_setup_kernel(void *load_addr, void *initrd_addr)
23 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
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);
28 if (kernel_size == 0) {
29 printf("warning: no kernel available\n");
33 data_addr = load_addr;
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);
40 qfw_read_entry(qfw_dev, FW_CFG_KERNEL_DATA,
41 le32_to_cpu(kernel_size), data_addr);
42 data_addr += le32_to_cpu(kernel_size);
44 data_addr = initrd_addr;
45 qfw_read_entry(qfw_dev, FW_CFG_INITRD_SIZE, 4, &initrd_size);
46 if (initrd_size == 0) {
47 printf("warning: no initrd available\n");
49 qfw_read_entry(qfw_dev, FW_CFG_INITRD_DATA,
50 le32_to_cpu(initrd_size), data_addr);
51 data_addr += le32_to_cpu(initrd_size);
54 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
56 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_DATA,
57 le32_to_cpu(cmdline_size), data_addr);
59 * if kernel cmdline only contains '\0', (e.g. no -append
60 * when invoking qemu), do not update bootargs
62 if (*data_addr != '\0') {
63 if (env_set("bootargs", data_addr) < 0)
64 printf("warning: unable to change bootargs\n");
68 printf("loading kernel to address %p size %x", load_addr,
69 le32_to_cpu(kernel_size));
71 printf(" initrd %p size %x\n",
73 le32_to_cpu(initrd_size));
80 static int qemu_fwcfg_cmd_list_firmware(void)
83 struct fw_cfg_file_iter iter;
86 /* make sure fw_list is loaded */
87 ret = qfw_read_firmware_list(qfw_dev);
91 for (file = qfw_file_iter_init(qfw_dev, &iter);
92 !qfw_file_iter_end(&iter);
93 file = qfw_file_iter_next(&iter)) {
94 printf("%-56s\n", file->cfg.name);
100 static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
101 int argc, char *const argv[])
103 if (qemu_fwcfg_cmd_list_firmware() < 0)
104 return CMD_RET_FAILURE;
109 static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
110 int argc, char *const argv[])
112 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
116 static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
117 int argc, char *const argv[])
123 env = env_get("loadaddr");
125 (void *)hextoul(env, NULL) :
126 (void *)CONFIG_SYS_LOAD_ADDR;
128 env = env_get("ramdiskaddr");
130 (void *)hextoul(env, NULL) :
131 #ifdef CONFIG_RAMDISK_ADDR
132 (void *)CONFIG_RAMDISK_ADDR;
138 load_addr = (void *)hextoul(argv[0], NULL);
139 initrd_addr = (void *)hextoul(argv[1], NULL);
140 } else if (argc == 1) {
141 load_addr = (void *)hextoul(argv[0], NULL);
144 if (!load_addr || !initrd_addr) {
145 printf("missing load or initrd address\n");
146 return CMD_RET_FAILURE;
149 return qemu_fwcfg_cmd_setup_kernel(load_addr, initrd_addr);
152 static struct cmd_tbl fwcfg_commands[] = {
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, "", ""),
158 static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
162 struct cmd_tbl *fwcfg_cmd;
164 ret = qfw_get_dev(&qfw_dev);
166 printf("QEMU fw_cfg interface not found\n");
167 return CMD_RET_USAGE;
170 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
171 ARRAY_SIZE(fwcfg_commands));
174 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
175 return CMD_RET_USAGE;
177 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
179 return cmd_process_error(fwcfg_cmd, ret);
183 qfw, 4, 1, do_qemu_fw,
184 "QEMU firmware interface",
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"