c6730bf2d3ef722eba8e538103b7e3ba47aafc7e
[platform/kernel/u-boot.git] / cmd / qfw.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 <qfw.h>
11
12 /*
13  * This function prepares kernel for zboot. It loads kernel data
14  * to 'load_addr', initrd to 'initrd_addr' and kernel command
15  * line using qemu fw_cfg interface.
16  */
17 static int qemu_fwcfg_setup_kernel(void *load_addr, void *initrd_addr)
18 {
19         char *data_addr;
20         uint32_t setup_size, kernel_size, cmdline_size, initrd_size;
21
22         qemu_fwcfg_read_entry(FW_CFG_SETUP_SIZE, 4, &setup_size);
23         qemu_fwcfg_read_entry(FW_CFG_KERNEL_SIZE, 4, &kernel_size);
24
25         if (setup_size == 0 || kernel_size == 0) {
26                 printf("warning: no kernel available\n");
27                 return -1;
28         }
29
30         data_addr = load_addr;
31         qemu_fwcfg_read_entry(FW_CFG_SETUP_DATA,
32                               le32_to_cpu(setup_size), data_addr);
33         data_addr += le32_to_cpu(setup_size);
34
35         qemu_fwcfg_read_entry(FW_CFG_KERNEL_DATA,
36                               le32_to_cpu(kernel_size), data_addr);
37         data_addr += le32_to_cpu(kernel_size);
38
39         data_addr = initrd_addr;
40         qemu_fwcfg_read_entry(FW_CFG_INITRD_SIZE, 4, &initrd_size);
41         if (initrd_size == 0) {
42                 printf("warning: no initrd available\n");
43         } else {
44                 qemu_fwcfg_read_entry(FW_CFG_INITRD_DATA,
45                                       le32_to_cpu(initrd_size), data_addr);
46                 data_addr += le32_to_cpu(initrd_size);
47         }
48
49         qemu_fwcfg_read_entry(FW_CFG_CMDLINE_SIZE, 4, &cmdline_size);
50         if (cmdline_size) {
51                 qemu_fwcfg_read_entry(FW_CFG_CMDLINE_DATA,
52                                       le32_to_cpu(cmdline_size), data_addr);
53                 /*
54                  * if kernel cmdline only contains '\0', (e.g. no -append
55                  * when invoking qemu), do not update bootargs
56                  */
57                 if (*data_addr != '\0') {
58                         if (setenv("bootargs", data_addr) < 0)
59                                 printf("warning: unable to change bootargs\n");
60                 }
61         }
62
63         printf("loading kernel to address %p size %x", load_addr,
64                le32_to_cpu(kernel_size));
65         if (initrd_size)
66                 printf(" initrd %p size %x\n",
67                        initrd_addr,
68                        le32_to_cpu(initrd_size));
69         else
70                 printf("\n");
71
72         return 0;
73 }
74
75 static int qemu_fwcfg_list_firmware(void)
76 {
77         int ret;
78         struct fw_cfg_file_iter iter;
79         struct fw_file *file;
80
81         /* make sure fw_list is loaded */
82         ret = qemu_fwcfg_read_firmware_list();
83         if (ret)
84                 return ret;
85
86
87         for (file = qemu_fwcfg_file_iter_init(&iter);
88              !qemu_fwcfg_file_iter_end(&iter);
89              file = qemu_fwcfg_file_iter_next(&iter)) {
90                 printf("%-56s\n", file->cfg.name);
91         }
92
93         return 0;
94 }
95
96 static int qemu_fwcfg_do_list(cmd_tbl_t *cmdtp, int flag,
97                 int argc, char * const argv[])
98 {
99         if (qemu_fwcfg_list_firmware() < 0)
100                 return CMD_RET_FAILURE;
101
102         return 0;
103 }
104
105 static int qemu_fwcfg_do_cpus(cmd_tbl_t *cmdtp, int flag,
106                 int argc, char * const argv[])
107 {
108         int ret = qemu_fwcfg_online_cpus();
109         if (ret < 0) {
110                 printf("QEMU fw_cfg interface not found\n");
111                 return CMD_RET_FAILURE;
112         }
113
114         printf("%d cpu(s) online\n", qemu_fwcfg_online_cpus());
115
116         return 0;
117 }
118
119 static int qemu_fwcfg_do_load(cmd_tbl_t *cmdtp, int flag,
120                 int argc, char * const argv[])
121 {
122         char *env;
123         void *load_addr;
124         void *initrd_addr;
125
126         env = getenv("loadaddr");
127         load_addr = env ?
128                 (void *)simple_strtoul(env, NULL, 16) :
129                 (void *)CONFIG_LOADADDR;
130
131         env = getenv("ramdiskaddr");
132         initrd_addr = env ?
133                 (void *)simple_strtoul(env, NULL, 16) :
134                 (void *)CONFIG_RAMDISK_ADDR;
135
136         if (argc == 2) {
137                 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
138                 initrd_addr = (void *)simple_strtoul(argv[1], NULL, 16);
139         } else if (argc == 1) {
140                 load_addr = (void *)simple_strtoul(argv[0], NULL, 16);
141         }
142
143         return qemu_fwcfg_setup_kernel(load_addr, initrd_addr);
144 }
145
146 static cmd_tbl_t fwcfg_commands[] = {
147         U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
148         U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
149         U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
150 };
151
152 static int do_qemu_fw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
153 {
154         int ret;
155         cmd_tbl_t *fwcfg_cmd;
156
157         if (!qemu_fwcfg_present()) {
158                 printf("QEMU fw_cfg interface not found\n");
159                 return CMD_RET_USAGE;
160         }
161
162         fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
163                                  ARRAY_SIZE(fwcfg_commands));
164         argc -= 2;
165         argv += 2;
166         if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
167                 return CMD_RET_USAGE;
168
169         ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
170
171         return cmd_process_error(fwcfg_cmd, ret);
172 }
173
174 U_BOOT_CMD(
175         qfw,    4,      1,      do_qemu_fw,
176         "QEMU firmware interface",
177         "<command>\n"
178         "    - list                             : print firmware(s) currently loaded\n"
179         "    - cpus                             : print online cpu number\n"
180         "    - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
181 )