blob: 0c49c6074e16ab56ded42f7905d308ae01d8eb9a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yan4fcd7f22016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Miao Yan4fcd7f22016-05-22 19:37:14 -07004 */
5
6#include <common.h>
7#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -07009#include <errno.h>
Miao Yan92106272016-05-22 19:37:17 -070010#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110011#include <dm.h>
12
13static struct udevice *qfw_dev;
Miao Yan4fcd7f22016-05-22 19:37:14 -070014
15/*
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.
19 */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110020static int qemu_fwcfg_cmd_setup_kernel(void *load_addr, void *initrd_addr)
Miao Yan4fcd7f22016-05-22 19:37:14 -070021{
22 char *data_addr;
23 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
24
Asherah Connor4ffa95d2021-03-19 18:21:40 +110025 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);
Miao Yan4fcd7f22016-05-22 19:37:14 -070027
Pierre-Clément Tosi69d5b422022-05-25 14:38:55 +010028 if (kernel_size == 0) {
Andre Przywarac3947ad2022-10-10 18:01:21 +010029 printf("fatal: no kernel available\n");
30 return CMD_RET_FAILURE;
Miao Yan4fcd7f22016-05-22 19:37:14 -070031 }
32
33 data_addr = load_addr;
Pierre-Clément Tosi69d5b422022-05-25 14:38:55 +010034 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);
38 }
Miao Yan4fcd7f22016-05-22 19:37:14 -070039
Asherah Connor4ffa95d2021-03-19 18:21:40 +110040 qfw_read_entry(qfw_dev, FW_CFG_KERNEL_DATA,
41 le32_to_cpu(kernel_size), data_addr);
Miao Yan4fcd7f22016-05-22 19:37:14 -070042 data_addr += le32_to_cpu(kernel_size);
Andre Przywaraa224aa12022-10-10 18:01:20 +010043 env_set_hex("filesize", le32_to_cpu(kernel_size));
Miao Yan4fcd7f22016-05-22 19:37:14 -070044
45 data_addr = initrd_addr;
Asherah Connor4ffa95d2021-03-19 18:21:40 +110046 qfw_read_entry(qfw_dev, FW_CFG_INITRD_SIZE, 4, &initrd_size);
Miao Yan4fcd7f22016-05-22 19:37:14 -070047 if (initrd_size == 0) {
48 printf("warning: no initrd available\n");
49 } else {
Asherah Connor4ffa95d2021-03-19 18:21:40 +110050 qfw_read_entry(qfw_dev, FW_CFG_INITRD_DATA,
51 le32_to_cpu(initrd_size), data_addr);
Miao Yan4fcd7f22016-05-22 19:37:14 -070052 data_addr += le32_to_cpu(initrd_size);
Andre Przywaraa224aa12022-10-10 18:01:20 +010053 env_set_hex("filesize", le32_to_cpu(initrd_size));
Miao Yan4fcd7f22016-05-22 19:37:14 -070054 }
55
Asherah Connor4ffa95d2021-03-19 18:21:40 +110056 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
Miao Yan4fcd7f22016-05-22 19:37:14 -070057 if (cmdline_size) {
Asherah Connor4ffa95d2021-03-19 18:21:40 +110058 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_DATA,
59 le32_to_cpu(cmdline_size), data_addr);
Miao Yan4fcd7f22016-05-22 19:37:14 -070060 /*
61 * if kernel cmdline only contains '\0', (e.g. no -append
62 * when invoking qemu), do not update bootargs
63 */
64 if (*data_addr != '\0') {
Simon Glass6a38e412017-08-03 12:22:09 -060065 if (env_set("bootargs", data_addr) < 0)
Miao Yan4fcd7f22016-05-22 19:37:14 -070066 printf("warning: unable to change bootargs\n");
67 }
68 }
69
70 printf("loading kernel to address %p size %x", load_addr,
71 le32_to_cpu(kernel_size));
72 if (initrd_size)
73 printf(" initrd %p size %x\n",
74 initrd_addr,
75 le32_to_cpu(initrd_size));
76 else
77 printf("\n");
78
79 return 0;
80}
81
Asherah Connor4ffa95d2021-03-19 18:21:40 +110082static int qemu_fwcfg_cmd_list_firmware(void)
Miao Yan4fcd7f22016-05-22 19:37:14 -070083{
84 int ret;
85 struct fw_cfg_file_iter iter;
86 struct fw_file *file;
87
88 /* make sure fw_list is loaded */
Asherah Connor4ffa95d2021-03-19 18:21:40 +110089 ret = qfw_read_firmware_list(qfw_dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -070090 if (ret)
91 return ret;
92
Asherah Connor4ffa95d2021-03-19 18:21:40 +110093 for (file = qfw_file_iter_init(qfw_dev, &iter);
94 !qfw_file_iter_end(&iter);
95 file = qfw_file_iter_next(&iter)) {
Miao Yan4fcd7f22016-05-22 19:37:14 -070096 printf("%-56s\n", file->cfg.name);
97 }
98
99 return 0;
100}
101
Simon Glassed38aef2020-05-10 11:40:03 -0600102static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
103 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -0700104{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100105 if (qemu_fwcfg_cmd_list_firmware() < 0)
Miao Yan4fcd7f22016-05-22 19:37:14 -0700106 return CMD_RET_FAILURE;
107
108 return 0;
109}
110
Simon Glassed38aef2020-05-10 11:40:03 -0600111static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
112 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -0700113{
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100114 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
Miao Yan4fcd7f22016-05-22 19:37:14 -0700115 return 0;
116}
117
Simon Glassed38aef2020-05-10 11:40:03 -0600118static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
119 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -0700120{
121 char *env;
122 void *load_addr;
123 void *initrd_addr;
124
Simon Glass64b723f2017-08-03 12:22:12 -0600125 env = env_get("loadaddr");
Miao Yan4fcd7f22016-05-22 19:37:14 -0700126 load_addr = env ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600127 (void *)hextoul(env, NULL) :
Tom Rini6ec41ad2021-08-23 10:25:29 -0400128 (void *)CONFIG_SYS_LOAD_ADDR;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700129
Simon Glass64b723f2017-08-03 12:22:12 -0600130 env = env_get("ramdiskaddr");
Miao Yan4fcd7f22016-05-22 19:37:14 -0700131 initrd_addr = env ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600132 (void *)hextoul(env, NULL) :
Tom Rini0d5392d2022-12-04 10:13:39 -0500133#ifdef CFG_RAMDISK_ADDR
134 (void *)CFG_RAMDISK_ADDR;
Miao Yan43626762016-05-22 19:37:18 -0700135#else
136 NULL;
137#endif
Miao Yan4fcd7f22016-05-22 19:37:14 -0700138
139 if (argc == 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600140 load_addr = (void *)hextoul(argv[0], NULL);
141 initrd_addr = (void *)hextoul(argv[1], NULL);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700142 } else if (argc == 1) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600143 load_addr = (void *)hextoul(argv[0], NULL);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700144 }
145
Miao Yan43626762016-05-22 19:37:18 -0700146 if (!load_addr || !initrd_addr) {
147 printf("missing load or initrd address\n");
148 return CMD_RET_FAILURE;
149 }
150
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100151 return qemu_fwcfg_cmd_setup_kernel(load_addr, initrd_addr);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700152}
153
Simon Glassed38aef2020-05-10 11:40:03 -0600154static struct cmd_tbl fwcfg_commands[] = {
Miao Yan4fcd7f22016-05-22 19:37:14 -0700155 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, "", ""),
158};
159
Simon Glassed38aef2020-05-10 11:40:03 -0600160static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
161 char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -0700162{
163 int ret;
Simon Glassed38aef2020-05-10 11:40:03 -0600164 struct cmd_tbl *fwcfg_cmd;
Miao Yan4fcd7f22016-05-22 19:37:14 -0700165
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100166 ret = qfw_get_dev(&qfw_dev);
167 if (ret) {
Miao Yan4fcd7f22016-05-22 19:37:14 -0700168 printf("QEMU fw_cfg interface not found\n");
169 return CMD_RET_USAGE;
170 }
171
172 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
173 ARRAY_SIZE(fwcfg_commands));
174 argc -= 2;
175 argv += 2;
176 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
177 return CMD_RET_USAGE;
178
179 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
180
181 return cmd_process_error(fwcfg_cmd, ret);
182}
183
184U_BOOT_CMD(
185 qfw, 4, 1, do_qemu_fw,
186 "QEMU firmware interface",
187 "<command>\n"
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"
191)