blob: d6ecfa60d5a71a7414b3741e84cff569a6e93df0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yan4fcd7f22016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Miao Yan4fcd7f22016-05-22 19:37:14 -07004 */
5
6#include <common.h>
7#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -07009#include <errno.h>
Miao Yan92106272016-05-22 19:37:17 -070010#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110011#include <dm.h>
12
13static struct udevice *qfw_dev;
Miao Yan4fcd7f22016-05-22 19:37:14 -070014
Asherah Connor4ffa95d2021-03-19 18:21:40 +110015static int qemu_fwcfg_cmd_list_firmware(void)
Miao Yan4fcd7f22016-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 Connor4ffa95d2021-03-19 18:21:40 +110022 ret = qfw_read_firmware_list(qfw_dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -070023 if (ret)
24 return ret;
25
Asherah Connor4ffa95d2021-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 Glass30e0bb82023-07-15 21:38:48 -060029 printf("%08lx %-56s\n", file->addr, file->cfg.name);
Miao Yan4fcd7f22016-05-22 19:37:14 -070030 }
31
32 return 0;
33}
34
Simon Glassed38aef2020-05-10 11:40:03 -060035static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag,
36 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -070037{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110038 if (qemu_fwcfg_cmd_list_firmware() < 0)
Miao Yan4fcd7f22016-05-22 19:37:14 -070039 return CMD_RET_FAILURE;
40
41 return 0;
42}
43
Simon Glassed38aef2020-05-10 11:40:03 -060044static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag,
45 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -070046{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110047 printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev));
Miao Yan4fcd7f22016-05-22 19:37:14 -070048 return 0;
49}
50
Simon Glassed38aef2020-05-10 11:40:03 -060051static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag,
52 int argc, char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -070053{
54 char *env;
Simon Glassc1620b32023-01-28 15:00:22 -070055 ulong load_addr;
56 ulong initrd_addr;
Miao Yan4fcd7f22016-05-22 19:37:14 -070057
Simon Glass64b723f2017-08-03 12:22:12 -060058 env = env_get("loadaddr");
Miao Yan4fcd7f22016-05-22 19:37:14 -070059 load_addr = env ?
Simon Glassc1620b32023-01-28 15:00:22 -070060 hextoul(env, NULL) :
61 CONFIG_SYS_LOAD_ADDR;
Miao Yan4fcd7f22016-05-22 19:37:14 -070062
Simon Glass64b723f2017-08-03 12:22:12 -060063 env = env_get("ramdiskaddr");
Miao Yan4fcd7f22016-05-22 19:37:14 -070064 initrd_addr = env ?
Simon Glassc1620b32023-01-28 15:00:22 -070065 hextoul(env, NULL) :
Tom Rini0d5392d2022-12-04 10:13:39 -050066#ifdef CFG_RAMDISK_ADDR
Simon Glassc1620b32023-01-28 15:00:22 -070067 CFG_RAMDISK_ADDR;
Miao Yan43626762016-05-22 19:37:18 -070068#else
Simon Glassc1620b32023-01-28 15:00:22 -070069 0;
Miao Yan43626762016-05-22 19:37:18 -070070#endif
Miao Yan4fcd7f22016-05-22 19:37:14 -070071
72 if (argc == 2) {
Simon Glassc1620b32023-01-28 15:00:22 -070073 load_addr = hextoul(argv[0], NULL);
74 initrd_addr = hextoul(argv[1], NULL);
Miao Yan4fcd7f22016-05-22 19:37:14 -070075 } else if (argc == 1) {
Simon Glassc1620b32023-01-28 15:00:22 -070076 load_addr = hextoul(argv[0], NULL);
Miao Yan4fcd7f22016-05-22 19:37:14 -070077 }
78
Miao Yan43626762016-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 Glass61f3e2e2023-01-28 15:00:23 -070084 return qemu_fwcfg_setup_kernel(qfw_dev, load_addr, initrd_addr);
Miao Yan4fcd7f22016-05-22 19:37:14 -070085}
86
Simon Glassed38aef2020-05-10 11:40:03 -060087static struct cmd_tbl fwcfg_commands[] = {
Miao Yan4fcd7f22016-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 Glassed38aef2020-05-10 11:40:03 -060093static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc,
94 char *const argv[])
Miao Yan4fcd7f22016-05-22 19:37:14 -070095{
96 int ret;
Simon Glassed38aef2020-05-10 11:40:03 -060097 struct cmd_tbl *fwcfg_cmd;
Miao Yan4fcd7f22016-05-22 19:37:14 -070098
Asherah Connor4ffa95d2021-03-19 18:21:40 +110099 ret = qfw_get_dev(&qfw_dev);
100 if (ret) {
Miao Yan4fcd7f22016-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"
124)