9f03ab60ab185555b4c7e22a02040a2f9bdd637f
[oweals/u-boot.git] / cmd / qemu_fw_cfg.c
1 /*
2  * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <errno.h>
10 #include <malloc.h>
11 #include <qemu_fw_cfg.h>
12 #include <asm/io.h>
13 #include <linux/list.h>
14
15 static bool fwcfg_present;
16 static bool fwcfg_dma_present;
17
18 static LIST_HEAD(fw_list);
19
20 /* Read configuration item using fw_cfg PIO interface */
21 static void qemu_fwcfg_read_entry_pio(uint16_t entry,
22                 uint32_t size, void *address)
23 {
24         uint32_t i = 0;
25         uint8_t *data = address;
26
27         /*
28          * writting FW_CFG_INVALID will cause read operation to resume at
29          * last offset, otherwise read will start at offset 0
30          */
31         if (entry != FW_CFG_INVALID)
32                 outw(entry, FW_CONTROL_PORT);
33         while (size--)
34                 data[i++] = inb(FW_DATA_PORT);
35 }
36
37 /* Read configuration item using fw_cfg DMA interface */
38 static void qemu_fwcfg_read_entry_dma(uint16_t entry,
39                 uint32_t size, void *address)
40 {
41         struct fw_cfg_dma_access dma;
42
43         dma.length = cpu_to_be32(size);
44         dma.address = cpu_to_be64((uintptr_t)address);
45         dma.control = cpu_to_be32(FW_CFG_DMA_READ);
46
47         /*
48          * writting FW_CFG_INVALID will cause read operation to resume at
49          * last offset, otherwise read will start at offset 0
50          */
51         if (entry != FW_CFG_INVALID)
52                 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
53
54         barrier();
55
56         debug("qemu_fwcfg_dma_read_entry: addr %p, length %u control 0x%x\n",
57               address, size, be32_to_cpu(dma.control));
58
59         outl(cpu_to_be32((uint32_t)&dma), FW_DMA_PORT_HIGH);
60
61         while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_ERROR)
62                 __asm__ __volatile__ ("pause");
63 }
64
65 static bool qemu_fwcfg_present(void)
66 {
67         uint32_t qemu;
68
69         qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
70         return be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE;
71 }
72
73 static bool qemu_fwcfg_dma_present(void)
74 {
75         uint8_t dma_enabled;
76
77         qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
78         if (dma_enabled & FW_CFG_DMA_ENABLED)
79                 return true;
80
81         return false;
82 }
83
84 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
85 {
86         if (fwcfg_dma_present)
87                 qemu_fwcfg_read_entry_dma(entry, length, address);
88         else
89                 qemu_fwcfg_read_entry_pio(entry, length, address);
90 }
91
92 int qemu_fwcfg_online_cpus(void)
93 {
94         uint16_t nb_cpus;
95
96         if (!fwcfg_present)
97                 return -ENODEV;
98
99         qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
100
101         return le16_to_cpu(nb_cpus);
102 }
103
104 /*
105  * This function prepares kernel for zboot. It loads kernel data
106  * to 'load_addr', initrd to 'initrd_addr' and kernel command
107  * line using qemu fw_cfg interface.
108  */
109 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
110 {
111         char *data_addr;
112         uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
113
114         qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
115         qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
116
117         if (setup_size == 0 || kernel_size == 0) {
118                 printf("warning: no kernel available\n");
119                 return -1;
120         }
121
122         data_addr = load_addr;
123         qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
124                               le32_to_cpu(setup_size), data_addr);
125         data_addr += le32_to_cpu(setup_size);
126
127         qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
128                               le32_to_cpu(kernel_size), data_addr);
129         data_addr += le32_to_cpu(kernel_size);
130
131         data_addr = initrd_addr;
132         qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
133         if (initrd_size == 0) {
134                 printf("warning: no initrd available\n");
135         } else {
136                 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
137                                       le32_to_cpu(initrd_size), data_addr);
138                 data_addr += le32_to_cpu(initrd_size);
139         }
140
141         qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
142         if (cmdline_size) {
143                 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
144                                       le32_to_cpu(cmdline_size), data_addr);
145                 /*
146                  * if kernel cmdline only contains '\0', (e.g. no -append
147                  * when invoking qemu), do not update bootargs
148                  */
149                 if (*data_addr != '\0') {
150                         if (setenv("bootargs", data_addr) < 0)
151                                 printf("warning: unable to change bootargs\n");
152                 }
153         }
154
155         printf("loading kernel to address %p size %x", load_addr,
156                le32_to_cpu(kernel_size));
157         if (initrd_size)
158                 printf(" initrd %p size %x\n",
159                        initrd_addr,
160                        le32_to_cpu(initrd_size));
161         else
162                 printf("\n");
163
164         return 0;
165 }
166
167 int qemu_fwcfg_read_firmware_list(void)
168 {
169         int i;
170         uint32_t count;
171         struct fw_file *file;
172         struct list_head *entry;
173
174         /* don't read it twice */
175         if (!list_empty(&fw_list))
176                 return 0;
177
178         qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
179         if (!count)
180                 return 0;
181
182         count = be32_to_cpu(count);
183         for (i = 0; i < count; i++) {
184                 file = malloc(sizeof(*file));
185                 if (!file) {
186                         printf("error: allocating resource\n");
187                         goto err;
188                 }
189                 qemu_fwcfg_read_entry(FW_CFG_INVALID,
190                                       sizeof(struct fw_cfg_file), &file->cfg);
191                 file->addr = 0;
192                 list_add_tail(&file->list, &fw_list);
193         }
194
195         return 0;
196
197 err:
198         list_for_each(entry, &fw_list) {
199                 file = list_entry(entry, struct fw_file, list);
200                 free(file);
201         }
202
203         return -ENOMEM;
204 }
205
206 struct fw_file *qemu_fwcfg_find_file(const char *name)
207 {
208         struct list_head *entry;
209         struct fw_file *file;
210
211         list_for_each(entry, &fw_list) {
212                 file = list_entry(entry, struct fw_file, list);
213                 if (!strcmp(file->cfg.name, name))
214                         return file;
215         }
216
217         return NULL;
218 }
219
220 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
221 {
222         iter->entry = fw_list.next;
223         return list_entry(iter->entry, struct fw_file, list);
224 }
225
226 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
227 {
228         iter->entry = iter->entry->next;
229         return list_entry(iter->entry, struct fw_file, list);
230 }
231
232 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
233 {
234         return iter->entry == &fw_list;
235 }
236
237 static int qemu_fwcfg_list_firmware(void)
238 {
239         int ret;
240         struct fw_cfg_file_iter iter;
241         struct fw_file *file;
242
243         /* make sure fw_list is loaded */
244         ret = qemu_fwcfg_read_firmware_list();
245         if (ret)
246                 return ret;
247
248
249         for (file = qemu_fwcfg_file_iter_init(&iter);
250              !qemu_fwcfg_file_iter_end(&iter);
251              file = qemu_fwcfg_file_iter_next(&iter)) {
252                 printf("%-56s\n", file->cfg.name);
253         }
254
255         return 0;
256 }
257
258 void qemu_fwcfg_init(void)
259 {
260         fwcfg_present = qemu_fwcfg_present();
261         if (fwcfg_present)
262                 fwcfg_dma_present = qemu_fwcfg_dma_present();
263 }
264
265 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
266                 int argc, char * const argv[])
267 {
268         if (qemu_fwcfg_list_firmware() < 0)
269                 return CMD_RET_FAILURE;
270
271         return 0;
272 }
273
274 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
275                 int argc, char * const argv[])
276 {
277         int ret = qemu_fwcfg_online_cpus();
278         if (ret < 0) {
279                 printf("QEMU fw_cfg interface not found\n");
280                 return CMD_RET_FAILURE;
281         }
282
283         printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
284
285         return 0;
286 }
287
288 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
289                 int argc, char * const argv[])
290 {
291         char *env;
292         void *load_addr;
293         void *initrd_addr;
294
295         env = getenv("loadaddr");
296         load_addr = env ?
297                 (void *)simple_strtoul(env, NULL, 16) :
298                 (void *)CONFIG_LOADADDR;
299
300         env = getenv("ramdiskaddr");
301         initrd_addr = env ?
302                 (void *)simple_strtoul(env, NULL, 16) :
303                 (void *)CONFIG_RAMDISK_ADDR;
304
305         if (argc == 2) {
306                 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
307                 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
308         } else if (argc == 1) {
309                 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
310         }
311
312         return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
313 }
314
315 static cmd_tbl_t fwcfg_commands[] = {
316         U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
317         U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
318         U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
319 };
320
321 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
322 {
323         int ret;
324         cmd_tbl_t *fwcfg_cmd;
325
326         if (!fwcfg_present) {
327                 printf("QEMU fw_cfg interface not found\n");
328                 return CMD_RET_USAGE;
329         }
330
331         fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
332                                  ARRAY_SIZE(fwcfg_commands));
333         argc -= 2;
334         argv += 2;
335         if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
336                 return CMD_RET_USAGE;
337
338         ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
339
340         return cmd_process_error(fwcfg_cmd, ret);
341 }
342
343 U_BOOT_CMD(
344         qfw,    4,      1,      do_qemu_fw,
345         "QEMU firmware interface",
346         "<command>\n"
347         "    - list                             : print firmware(s) currently loaded\n"
348         "    - cpus                             : print online cpu number\n"
349         "    - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
350 )