1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
12 * This function prepares kernel for zboot. It loads kernel data
13 * to 'load_addr', initrd to 'initrd_addr' and kernel command
14 * line using qemu fw_cfg interface.
16 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
19 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
21 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
22 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
24 if (setup_size == 0 || kernel_size == 0) {
25 printf("warning: no kernel available\n");
29 data_addr = load_addr;
30 qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
31 le32_to_cpu(setup_size), data_addr);
32 data_addr += le32_to_cpu(setup_size);
34 qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
35 le32_to_cpu(kernel_size), data_addr);
36 data_addr += le32_to_cpu(kernel_size);
38 data_addr = initrd_addr;
39 qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
40 if (initrd_size == 0) {
41 printf("warning: no initrd available\n");
43 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
44 le32_to_cpu(initrd_size), data_addr);
45 data_addr += le32_to_cpu(initrd_size);
48 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
50 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
51 le32_to_cpu(cmdline_size), data_addr);
53 * if kernel cmdline only contains '\0', (e.g. no -append
54 * when invoking qemu), do not update bootargs
56 if (*data_addr != '\0') {
57 if (env_set("bootargs", data_addr) < 0)
58 printf("warning: unable to change bootargs\n");
62 printf("loading kernel to address %p size %x", load_addr,
63 le32_to_cpu(kernel_size));
65 printf(" initrd %p size %x\n",
67 le32_to_cpu(initrd_size));
74 static int qemu_fwcfg_list_firmware(void)
77 struct fw_cfg_file_iter iter;
80 /* make sure fw_list is loaded */
81 ret = qemu_fwcfg_read_firmware_list();
86 for (file = qemu_fwcfg_file_iter_init(&iter);
87 !qemu_fwcfg_file_iter_end(&iter);
88 file = qemu_fwcfg_file_iter_next(&iter)) {
89 printf("%-56s\n", file->cfg.name);
95 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
96 int argc, char * const argv[])
98 if (qemu_fwcfg_list_firmware() < 0)
99 return CMD_RET_FAILURE;
104 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
105 int argc, char * const argv[])
107 int ret = qemu_fwcfg_online_cpus();
109 printf("QEMU fw_cfg interface not found\n");
110 return CMD_RET_FAILURE;
113 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
118 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
119 int argc, char * const argv[])
125 env = env_get("loadaddr");
127 (void *)simple_strtoul(env, NULL, 16) :
128 #ifdef CONFIG_LOADADDR
129 (void *)CONFIG_LOADADDR;
134 env = env_get("ramdiskaddr");
136 (void *)simple_strtoul(env, NULL, 16) :
137 #ifdef CONFIG_RAMDISK_ADDR
138 (void *)CONFIG_RAMDISK_ADDR;
144 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
145 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
146 } else if (argc == 1) {
147 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
150 if (!load_addr || !initrd_addr) {
151 printf("missing load or initrd address\n");
152 return CMD_RET_FAILURE;
155 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
158 static cmd_tbl_t fwcfg_commands[] = {
159 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
160 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
161 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
164 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
167 cmd_tbl_t *fwcfg_cmd;
169 if (!qemu_fwcfg_present()) {
170 printf("QEMU fw_cfg interface not found\n");
171 return CMD_RET_USAGE;
174 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
175 ARRAY_SIZE(fwcfg_commands));
178 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
179 return CMD_RET_USAGE;
181 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
183 return cmd_process_error(fwcfg_cmd, ret);
187 qfw, 4, 1, do_qemu_fw,
188 "QEMU firmware interface",
190 " - list : print firmware(s) currently loaded\n"
191 " - cpus : print online cpu number\n"
192 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"