blob: d58615040c64dc110fd5aac1dcfe46846668d136 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yanfcf5c042016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Miao Yanfcf5c042016-05-22 19:37:14 -07004 */
5
6#include <common.h>
7#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06008#include <env.h>
Miao Yanfcf5c042016-05-22 19:37:14 -07009#include <errno.h>
Miao Yan18686592016-05-22 19:37:17 -070010#include <qfw.h>
Asherah Connor5b0b43e2021-03-19 18:21:40 +110011#include <dm.h>
12
13static struct udevice *qfw_dev;
Miao Yanfcf5c042016-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 Connor5b0b43e2021-03-19 18:21:40 +110020static int qemu_fwcfg_cmd_setup_kernel(void *load_addr, void *initrd_addr)
Miao Yanfcf5c042016-05-22 19:37:14 -070021{
22 char *data_addr;
23 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
24
Asherah Connor5b0b43e2021-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 Yanfcf5c042016-05-22 19:37:14 -070027
28 if (setup_size == 0 || kernel_size == 0) {
29 printf("warning: no kernel available\n");
30 return -1;
31 }
32
33 data_addr = load_addr;
Asherah Connor5b0b43e2021-03-19 18:21:40 +110034 qfw_read_entry(qfw_dev, FW_CFG_SETUP_DATA,
35 le32_to_cpu(setup_size), data_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -070036 data_addr += le32_to_cpu(setup_size);
37
Asherah Connor5b0b43e2021-03-19 18:21:40 +110038 qfw_read_entry(qfw_dev, FW_CFG_KERNEL_DATA,
39 le32_to_cpu(kernel_size), data_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -070040 data_addr += le32_to_cpu(kernel_size);
41
42 data_addr = initrd_addr;
Asherah Connor5b0b43e2021-03-19 18:21:40 +110043 qfw_read_entry(qfw_dev, FW_CFG_INITRD_SIZE, 4, &initrd_size);
Miao Yanfcf5c042016-05-22 19:37:14 -070044 if (initrd_size == 0) {
45 printf("warning: no initrd available\n");
46 } else {
Asherah Connor5b0b43e2021-03-19 18:21:40 +110047 qfw_read_entry(qfw_dev, FW_CFG_INITRD_DATA,
48 le32_to_cpu(initrd_size), data_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -070049 data_addr += le32_to_cpu(initrd_size);
50 }
51
Asherah Connor5b0b43e2021-03-19 18:21:40 +110052 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
Miao Yanfcf5c042016-05-22 19:37:14 -070053 if (cmdline_size) {
Asherah Connor5b0b43e2021-03-19 18:21:40 +110054 qfw_read_entry(qfw_dev, FW_CFG_CMDLINE_DATA,
55 le32_to_cpu(cmdline_size), data_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -070056 /*
57 * if kernel cmdline only contains '\0', (e.g. no -append
58 * when invoking qemu), do not update bootargs
59 */
60 if (*data_addr != '\0') {
Simon Glass382bee52017-08-03 12:22:09 -060061 if (env_set("bootargs", data_addr) < 0)
Miao Yanfcf5c042016-05-22 19:37:14 -070062 printf("warning: unable to change bootargs\n");
63 }
64 }
65
66 printf("loading kernel to address %p size %x", load_addr,
67 le32_to_cpu(kernel_size));
68 if (initrd_size)
69 printf(" initrd %p size %x\n",
70 initrd_addr,
71 le32_to_cpu(initrd_size));
72 else
73 printf("\n");
74
75 return 0;
76}
77
Asherah Connor5b0b43e2021-03-19 18:21:40 +110078static int qemu_fwcfg_cmd_list_firmware(void)
Miao Yanfcf5c042016-05-22 19:37:14 -070079{
80 int ret;
81 struct fw_cfg_file_iter iter;
82 struct fw_file *file;
83
84 /* make sure fw_list is loaded */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110085 ret = qfw_read_firmware_list(qfw_dev);
Miao Yanfcf5c042016-05-22 19:37:14 -070086 if (ret)
87 return ret;
88
Asherah Connor5b0b43e2021-03-19 18:21:40 +110089 for (file = qfw_file_iter_init(qfw_dev, &iter);
90 !qfw_file_iter_end(&iter);
91 file = qfw_file_iter_next(&iter)) {
Miao Yanfcf5c042016-05-22 19:37:14 -070092 printf("%-56s\n", file->cfg.name);
93 }
94
95 return 0;
96}
97
Simon Glass09140112020-05-10 11:40:03 -060098static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
99 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -0700100{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100101 if (qemu_fwcfg_cmd_list_firmware() < 0)
Miao Yanfcf5c042016-05-22 19:37:14 -0700102 return CMD_RET_FAILURE;
103
104 return 0;
105}
106
Simon Glass09140112020-05-10 11:40:03 -0600107static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
108 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -0700109{
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100110 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
Miao Yanfcf5c042016-05-22 19:37:14 -0700111 return 0;
112}
113
Simon Glass09140112020-05-10 11:40:03 -0600114static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
115 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -0700116{
117 char *env;
118 void *load_addr;
119 void *initrd_addr;
120
Simon Glass00caae62017-08-03 12:22:12 -0600121 env = env_get("loadaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -0700122 load_addr = env ?
Simon Glass7e5f4602021-07-24 09:03:29 -0600123 (void *)hextoul(env, NULL) :
Tom Rini2f45b302021-08-23 10:25:29 -0400124 (void *)CONFIG_SYS_LOAD_ADDR;
Miao Yanfcf5c042016-05-22 19:37:14 -0700125
Simon Glass00caae62017-08-03 12:22:12 -0600126 env = env_get("ramdiskaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -0700127 initrd_addr = env ?
Simon Glass7e5f4602021-07-24 09:03:29 -0600128 (void *)hextoul(env, NULL) :
Miao Yan86e30e62016-05-22 19:37:18 -0700129#ifdef CONFIG_RAMDISK_ADDR
Miao Yanfcf5c042016-05-22 19:37:14 -0700130 (void *)CONFIG_RAMDISK_ADDR;
Miao Yan86e30e62016-05-22 19:37:18 -0700131#else
132 NULL;
133#endif
Miao Yanfcf5c042016-05-22 19:37:14 -0700134
135 if (argc == 2) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600136 load_addr = (void *)hextoul(argv[0], NULL);
137 initrd_addr = (void *)hextoul(argv[1], NULL);
Miao Yanfcf5c042016-05-22 19:37:14 -0700138 } else if (argc == 1) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600139 load_addr = (void *)hextoul(argv[0], NULL);
Miao Yanfcf5c042016-05-22 19:37:14 -0700140 }
141
Miao Yan86e30e62016-05-22 19:37:18 -0700142 if (!load_addr || !initrd_addr) {
143 printf("missing load or initrd address\n");
144 return CMD_RET_FAILURE;
145 }
146
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100147 return qemu_fwcfg_cmd_setup_kernel(load_addr, initrd_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -0700148}
149
Simon Glass09140112020-05-10 11:40:03 -0600150static struct cmd_tbl fwcfg_commands[] = {
Miao Yanfcf5c042016-05-22 19:37:14 -0700151 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
152 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
153 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
154};
155
Simon Glass09140112020-05-10 11:40:03 -0600156static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
157 char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -0700158{
159 int ret;
Simon Glass09140112020-05-10 11:40:03 -0600160 struct cmd_tbl *fwcfg_cmd;
Miao Yanfcf5c042016-05-22 19:37:14 -0700161
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100162 ret = qfw_get_dev(&qfw_dev);
163 if (ret) {
Miao Yanfcf5c042016-05-22 19:37:14 -0700164 printf("QEMU fw_cfg interface not found\n");
165 return CMD_RET_USAGE;
166 }
167
168 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
169 ARRAY_SIZE(fwcfg_commands));
170 argc -= 2;
171 argv += 2;
172 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
173 return CMD_RET_USAGE;
174
175 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
176
177 return cmd_process_error(fwcfg_cmd, ret);
178}
179
180U_BOOT_CMD(
181 qfw, 4, 1, do_qemu_fw,
182 "QEMU firmware interface",
183 "<command>\n"
184 " - list : print firmware(s) currently loaded\n"
185 " - cpus : print online cpu number\n"
186 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
187)