Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com> |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Miao Yan | 9210627 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 10 | #include <qfw.h> |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | |
| 13 | static struct udevice *qfw_dev; |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 14 | |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 15 | static int qemu_fwcfg_cmd_list_firmware(void) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 16 | { |
| 17 | int ret; |
| 18 | struct fw_cfg_file_iter iter; |
| 19 | struct fw_file *file; |
| 20 | |
| 21 | /* make sure fw_list is loaded */ |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 22 | ret = qfw_read_firmware_list(qfw_dev); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 23 | if (ret) |
| 24 | return ret; |
| 25 | |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 26 | for (file = qfw_file_iter_init(qfw_dev, &iter); |
| 27 | !qfw_file_iter_end(&iter); |
| 28 | file = qfw_file_iter_next(&iter)) { |
Simon Glass | 30e0bb8 | 2023-07-15 21:38:48 -0600 | [diff] [blame] | 29 | printf("%08lx %-56s\n", file->addr, file->cfg.name); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 35 | static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag, |
| 36 | int argc, char *const argv[]) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 37 | { |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 38 | if (qemu_fwcfg_cmd_list_firmware() < 0) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 39 | return CMD_RET_FAILURE; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 44 | static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag, |
| 45 | int argc, char *const argv[]) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 46 | { |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 47 | printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev)); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 51 | static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag, |
| 52 | int argc, char *const argv[]) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 53 | { |
| 54 | char *env; |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 55 | ulong load_addr; |
| 56 | ulong initrd_addr; |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 57 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | env = env_get("loadaddr"); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 59 | load_addr = env ? |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 60 | hextoul(env, NULL) : |
| 61 | CONFIG_SYS_LOAD_ADDR; |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 62 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 63 | env = env_get("ramdiskaddr"); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 64 | initrd_addr = env ? |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 65 | hextoul(env, NULL) : |
Tom Rini | 0d5392d | 2022-12-04 10:13:39 -0500 | [diff] [blame] | 66 | #ifdef CFG_RAMDISK_ADDR |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 67 | CFG_RAMDISK_ADDR; |
Miao Yan | 4362676 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 68 | #else |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 69 | 0; |
Miao Yan | 4362676 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 70 | #endif |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 71 | |
| 72 | if (argc == 2) { |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 73 | load_addr = hextoul(argv[0], NULL); |
| 74 | initrd_addr = hextoul(argv[1], NULL); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 75 | } else if (argc == 1) { |
Simon Glass | c1620b3 | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 76 | load_addr = hextoul(argv[0], NULL); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Miao Yan | 4362676 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 79 | if (!load_addr || !initrd_addr) { |
| 80 | printf("missing load or initrd address\n"); |
| 81 | return CMD_RET_FAILURE; |
| 82 | } |
| 83 | |
Simon Glass | 61f3e2e | 2023-01-28 15:00:23 -0700 | [diff] [blame] | 84 | return qemu_fwcfg_setup_kernel(qfw_dev, load_addr, initrd_addr); |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 87 | static struct cmd_tbl fwcfg_commands[] = { |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 88 | 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 93 | static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 94 | char *const argv[]) |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 95 | { |
| 96 | int ret; |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 97 | struct cmd_tbl *fwcfg_cmd; |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 98 | |
Asherah Connor | 4ffa95d | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 99 | ret = qfw_get_dev(&qfw_dev); |
| 100 | if (ret) { |
Miao Yan | 4fcd7f2 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 101 | 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 | |
| 117 | U_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 | ) |