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 (setup_size == 0 || kernel_size == 0) {
29 printf("warning: no kernel available\n");
33 data_addr = load_addr;
34 qfw_read_entry(qfw_dev, FW_CFG_SETUP_DATA,
35 le32_to_cpu(setup_size), data_addr);
36 data_addr += le32_to_cpu(setup_size);
38 qfw_read_entry(qfw_dev, FW_CFG_KERNEL_DATA,
39 le32_to_cpu(kernel_size), data_addr);
40 data_addr += le32_to_cpu(kernel_size);
42 data_addr = initrd_addr;
43 qfw_read_entry(qfw_dev, FW_CFG_INITRD_SIZE, 4, &initrd_size);
44 if (initrd_size == 0) {
45 printf("warning: no initrd available\n");
47 qfw_read_entry(qfw_dev, FW_CFG_INITRD_DATA,
48 le32_to_cpu(initrd_size), data_addr);
49 data_addr += le32_to_cpu(initrd_size);
52 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
54 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_DATA,
55 le32_to_cpu(cmdline_size), data_addr);
57 * if kernel cmdline only contains '\0', (e.g. no -append
58 * when invoking qemu), do not update bootargs
60 if (*data_addr != '\0') {
61 if (env_set("bootargs", data_addr) < 0)
62 printf("warning: unable to change bootargs\n");
66 printf("loading kernel to address %p size %x", load_addr,
67 le32_to_cpu(kernel_size));
69 printf(" initrd %p size %x\n",
71 le32_to_cpu(initrd_size));
78 static int qemu_fwcfg_cmd_list_firmware(void)
81 struct fw_cfg_file_iter iter;
84 /* make sure fw_list is loaded */
85 ret = qfw_read_firmware_list(qfw_dev);
89 for (file = qfw_file_iter_init(qfw_dev, &iter);
90 !qfw_file_iter_end(&iter);
91 file = qfw_file_iter_next(&iter)) {
92 printf("%-56s\n", file->cfg.name);
98 static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
99 int argc, char *const argv[])
101 if (qemu_fwcfg_cmd_list_firmware() < 0)
102 return CMD_RET_FAILURE;
107 static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
108 int argc, char *const argv[])
110 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
114 static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
115 int argc, char *const argv[])
121 env = env_get("loadaddr");
123 (void *)simple_strtoul(env, NULL, 16) :
124 #ifdef CONFIG_LOADADDR
125 (void *)CONFIG_LOADADDR;
130 env = env_get("ramdiskaddr");
132 (void *)simple_strtoul(env, NULL, 16) :
133 #ifdef CONFIG_RAMDISK_ADDR
134 (void *)CONFIG_RAMDISK_ADDR;
140 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
141 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
142 } else if (argc == 1) {
143 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
146 if (!load_addr || !initrd_addr) {
147 printf("missing load or initrd address\n");
148 return CMD_RET_FAILURE;
151 return qemu_fwcfg_cmd_setup_kernel(load_addr, initrd_addr);
154 static struct cmd_tbl fwcfg_commands[] = {
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, "", ""),
160 static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
164 struct cmd_tbl *fwcfg_cmd;
166 ret = qfw_get_dev(&qfw_dev);
168 printf("QEMU fw_cfg interface not found\n");
169 return CMD_RET_USAGE;
172 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
173 ARRAY_SIZE(fwcfg_commands));
176 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
177 return CMD_RET_USAGE;
179 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
181 return cmd_process_error(fwcfg_cmd, ret);
185 qfw, 4, 1, do_qemu_fw,
186 "QEMU firmware interface",
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"