blob: 7bf700538c80f34ba614b1307e9ecd0e17bfc91e [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>
8#include <errno.h>
Miao Yan18686592016-05-22 19:37:17 -07009#include <qfw.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070010
11/*
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.
15 */
16static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
17{
18 char *data_addr;
19 uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
20
21 qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
22 qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
23
24 if (setup_size == 0 || kernel_size == 0) {
25 printf("warning: no kernel available\n");
26 return -1;
27 }
28
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);
33
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);
37
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");
42 } else {
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);
46 }
47
48 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
49 if (cmdline_size) {
50 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
51 le32_to_cpu(cmdline_size), data_addr);
52 /*
53 * if kernel cmdline only contains '\0', (e.g. no -append
54 * when invoking qemu), do not update bootargs
55 */
56 if (*data_addr != '\0') {
Simon Glass382bee52017-08-03 12:22:09 -060057 if (env_set("bootargs", data_addr) < 0)
Miao Yanfcf5c042016-05-22 19:37:14 -070058 printf("warning: unable to change bootargs\n");
59 }
60 }
61
62 printf("loading kernel to address %p size %x", load_addr,
63 le32_to_cpu(kernel_size));
64 if (initrd_size)
65 printf(" initrd %p size %x\n",
66 initrd_addr,
67 le32_to_cpu(initrd_size));
68 else
69 printf("\n");
70
71 return 0;
72}
73
74static int qemu_fwcfg_list_firmware(void)
75{
76 int ret;
77 struct fw_cfg_file_iter iter;
78 struct fw_file *file;
79
80 /* make sure fw_list is loaded */
81 ret = qemu_fwcfg_read_firmware_list();
82 if (ret)
83 return ret;
84
85
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);
90 }
91
92 return 0;
93}
94
95static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
96 int argc, char * const argv[])
97{
98 if (qemu_fwcfg_list_firmware() < 0)
99 return CMD_RET_FAILURE;
100
101 return 0;
102}
103
104static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
105 int argc, char * const argv[])
106{
107 int ret = qemu_fwcfg_online_cpus();
108 if (ret < 0) {
109 printf("QEMU fw_cfg interface not found\n");
110 return CMD_RET_FAILURE;
111 }
112
113 printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
114
115 return 0;
116}
117
118static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
119 int argc, char * const argv[])
120{
121 char *env;
122 void *load_addr;
123 void *initrd_addr;
124
Simon Glass00caae62017-08-03 12:22:12 -0600125 env = env_get("loadaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -0700126 load_addr = env ?
127 (void *)simple_strtoul(env, NULL, 16) :
Miao Yan86e30e62016-05-22 19:37:18 -0700128#ifdef CONFIG_LOADADDR
Miao Yanfcf5c042016-05-22 19:37:14 -0700129 (void *)CONFIG_LOADADDR;
Miao Yan86e30e62016-05-22 19:37:18 -0700130#else
131 NULL;
132#endif
Miao Yanfcf5c042016-05-22 19:37:14 -0700133
Simon Glass00caae62017-08-03 12:22:12 -0600134 env = env_get("ramdiskaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -0700135 initrd_addr = env ?
136 (void *)simple_strtoul(env, NULL, 16) :
Miao Yan86e30e62016-05-22 19:37:18 -0700137#ifdef CONFIG_RAMDISK_ADDR
Miao Yanfcf5c042016-05-22 19:37:14 -0700138 (void *)CONFIG_RAMDISK_ADDR;
Miao Yan86e30e62016-05-22 19:37:18 -0700139#else
140 NULL;
141#endif
Miao Yanfcf5c042016-05-22 19:37:14 -0700142
143 if (argc == 2) {
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);
148 }
149
Miao Yan86e30e62016-05-22 19:37:18 -0700150 if (!load_addr || !initrd_addr) {
151 printf("missing load or initrd address\n");
152 return CMD_RET_FAILURE;
153 }
154
Miao Yanfcf5c042016-05-22 19:37:14 -0700155 return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
156}
157
158static 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, "", ""),
162};
163
164static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
165{
166 int ret;
167 cmd_tbl_t *fwcfg_cmd;
168
169 if (!qemu_fwcfg_present()) {
170 printf("QEMU fw_cfg interface not found\n");
171 return CMD_RET_USAGE;
172 }
173
174 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
175 ARRAY_SIZE(fwcfg_commands));
176 argc -= 2;
177 argv += 2;
178 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
179 return CMD_RET_USAGE;
180
181 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
182
183 return cmd_process_error(fwcfg_cmd, ret);
184}
185
186U_BOOT_CMD(
187 qfw, 4, 1, do_qemu_fw,
188 "QEMU firmware interface",
189 "<command>\n"
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"
193)