blob: 1b8c775ebf5ab10cffac06998f1a043dd3ff2b84 [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
Asherah Connor5b0b43e2021-03-19 18:21:40 +110015static int qemu_fwcfg_cmd_list_firmware(void)
Miao Yanfcf5c042016-05-22 19:37:14 -070016{
17 int ret;
18 struct fw_cfg_file_iter iter;
19 struct fw_file *file;
20
21 /* make sure fw_list is loaded */
Asherah Connor5b0b43e2021-03-19 18:21:40 +110022 ret = qfw_read_firmware_list(qfw_dev);
Miao Yanfcf5c042016-05-22 19:37:14 -070023 if (ret)
24 return ret;
25
Asherah Connor5b0b43e2021-03-19 18:21:40 +110026 for (file = qfw_file_iter_init(qfw_dev, &iter);
27 !qfw_file_iter_end(&iter);
28 file = qfw_file_iter_next(&iter)) {
Simon Glassd2e79722023-07-15 21:38:48 -060029 printf("%08lx %-56s\n", file->addr, file->cfg.name);
Miao Yanfcf5c042016-05-22 19:37:14 -070030 }
31
32 return 0;
33}
34
Simon Glass09140112020-05-10 11:40:03 -060035static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
36 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -070037{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110038 if (qemu_fwcfg_cmd_list_firmware() < 0)
Miao Yanfcf5c042016-05-22 19:37:14 -070039 return CMD_RET_FAILURE;
40
41 return 0;
42}
43
Simon Glass09140112020-05-10 11:40:03 -060044static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
45 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -070046{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110047 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
Miao Yanfcf5c042016-05-22 19:37:14 -070048 return 0;
49}
50
Simon Glass09140112020-05-10 11:40:03 -060051static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
52 int argc, char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -070053{
54 char *env;
Simon Glasse08e6ea2023-01-28 15:00:22 -070055 ulong load_addr;
56 ulong initrd_addr;
Miao Yanfcf5c042016-05-22 19:37:14 -070057
Simon Glass00caae62017-08-03 12:22:12 -060058 env = env_get("loadaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -070059 load_addr = env ?
Simon Glasse08e6ea2023-01-28 15:00:22 -070060 hextoul(env, NULL) :
61 CONFIG_SYS_LOAD_ADDR;
Miao Yanfcf5c042016-05-22 19:37:14 -070062
Simon Glass00caae62017-08-03 12:22:12 -060063 env = env_get("ramdiskaddr");
Miao Yanfcf5c042016-05-22 19:37:14 -070064 initrd_addr = env ?
Simon Glasse08e6ea2023-01-28 15:00:22 -070065 hextoul(env, NULL) :
Tom Rinid4c8dd12022-12-04 10:13:39 -050066#ifdef CFG_RAMDISK_ADDR
Simon Glasse08e6ea2023-01-28 15:00:22 -070067 CFG_RAMDISK_ADDR;
Miao Yan86e30e62016-05-22 19:37:18 -070068#else
Simon Glasse08e6ea2023-01-28 15:00:22 -070069 0;
Miao Yan86e30e62016-05-22 19:37:18 -070070#endif
Miao Yanfcf5c042016-05-22 19:37:14 -070071
72 if (argc == 2) {
Simon Glasse08e6ea2023-01-28 15:00:22 -070073 load_addr = hextoul(argv[0], NULL);
74 initrd_addr = hextoul(argv[1], NULL);
Miao Yanfcf5c042016-05-22 19:37:14 -070075 } else if (argc == 1) {
Simon Glasse08e6ea2023-01-28 15:00:22 -070076 load_addr = hextoul(argv[0], NULL);
Miao Yanfcf5c042016-05-22 19:37:14 -070077 }
78
Miao Yan86e30e62016-05-22 19:37:18 -070079 if (!load_addr || !initrd_addr) {
80 printf("missing load or initrd address\n");
81 return CMD_RET_FAILURE;
82 }
83
Simon Glass00fa9892023-01-28 15:00:23 -070084 return qemu_fwcfg_setup_kernel(qfw_dev, load_addr, initrd_addr);
Miao Yanfcf5c042016-05-22 19:37:14 -070085}
86
Simon Glass09140112020-05-10 11:40:03 -060087static struct cmd_tbl fwcfg_commands[] = {
Miao Yanfcf5c042016-05-22 19:37:14 -070088 U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""),
89 U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""),
90 U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""),
91};
92
Simon Glass09140112020-05-10 11:40:03 -060093static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
94 char *const argv[])
Miao Yanfcf5c042016-05-22 19:37:14 -070095{
96 int ret;
Simon Glass09140112020-05-10 11:40:03 -060097 struct cmd_tbl *fwcfg_cmd;
Miao Yanfcf5c042016-05-22 19:37:14 -070098
Asherah Connor5b0b43e2021-03-19 18:21:40 +110099 ret = qfw_get_dev(&qfw_dev);
100 if (ret) {
Miao Yanfcf5c042016-05-22 19:37:14 -0700101 printf("QEMU fw_cfg interface not found\n");
102 return CMD_RET_USAGE;
103 }
104
105 fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands,
106 ARRAY_SIZE(fwcfg_commands));
107 argc -= 2;
108 argv += 2;
109 if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs)
110 return CMD_RET_USAGE;
111
112 ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv);
113
114 return cmd_process_error(fwcfg_cmd, ret);
115}
116
117U_BOOT_CMD(
118 qfw, 4, 1, do_qemu_fw,
119 "QEMU firmware interface",
120 "<command>\n"
121 " - list : print firmware(s) currently loaded\n"
122 " - cpus : print online cpu number\n"
123 " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n"
Simon Glass10c398d2023-11-18 14:04:52 -0700124);