Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 6 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 7 | #include <env.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Miao Yan | 1868659 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 9 | #include <qfw.h> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | |
| 12 | static struct udevice *qfw_dev; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 13 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 14 | static int qemu_fwcfg_cmd_list_firmware(void) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 15 | { |
| 16 | int ret; |
| 17 | struct fw_cfg_file_iter iter; |
| 18 | struct fw_file *file; |
| 19 | |
| 20 | /* make sure fw_list is loaded */ |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 21 | ret = qfw_read_firmware_list(qfw_dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 22 | if (ret) |
| 23 | return ret; |
| 24 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 25 | for (file = qfw_file_iter_init(qfw_dev, &iter); |
| 26 | !qfw_file_iter_end(&iter); |
| 27 | file = qfw_file_iter_next(&iter)) { |
Simon Glass | d2e7972 | 2023-07-15 21:38:48 -0600 | [diff] [blame] | 28 | printf("%08lx %-56s\n", file->addr, file->cfg.name); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 34 | static int qemu_fwcfg_do_list(struct cmd_tbl *cmdtp, int flag, |
| 35 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 36 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 37 | if (qemu_fwcfg_cmd_list_firmware() < 0) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 38 | return CMD_RET_FAILURE; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 43 | static int qemu_fwcfg_do_cpus(struct cmd_tbl *cmdtp, int flag, |
| 44 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 45 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 46 | printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev)); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 50 | static int qemu_fwcfg_do_load(struct cmd_tbl *cmdtp, int flag, |
| 51 | int argc, char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 52 | { |
| 53 | char *env; |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 54 | ulong load_addr; |
| 55 | ulong initrd_addr; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 56 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 57 | env = env_get("loadaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 58 | load_addr = env ? |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 59 | hextoul(env, NULL) : |
| 60 | CONFIG_SYS_LOAD_ADDR; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 61 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 62 | env = env_get("ramdiskaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 63 | initrd_addr = env ? |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 64 | hextoul(env, NULL) : |
Tom Rini | d4c8dd1 | 2022-12-04 10:13:39 -0500 | [diff] [blame] | 65 | #ifdef CFG_RAMDISK_ADDR |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 66 | CFG_RAMDISK_ADDR; |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 67 | #else |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 68 | 0; |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 69 | #endif |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 70 | |
| 71 | if (argc == 2) { |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 72 | load_addr = hextoul(argv[0], NULL); |
| 73 | initrd_addr = hextoul(argv[1], NULL); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 74 | } else if (argc == 1) { |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 75 | load_addr = hextoul(argv[0], NULL); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 78 | if (!load_addr || !initrd_addr) { |
| 79 | printf("missing load or initrd address\n"); |
| 80 | return CMD_RET_FAILURE; |
| 81 | } |
| 82 | |
Simon Glass | 00fa989 | 2023-01-28 15:00:23 -0700 | [diff] [blame] | 83 | return qemu_fwcfg_setup_kernel(qfw_dev, load_addr, initrd_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 86 | static struct cmd_tbl fwcfg_commands[] = { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 87 | U_BOOT_CMD_MKENT(list, 0, 1, qemu_fwcfg_do_list, "", ""), |
| 88 | U_BOOT_CMD_MKENT(cpus, 0, 1, qemu_fwcfg_do_cpus, "", ""), |
| 89 | U_BOOT_CMD_MKENT(load, 2, 1, qemu_fwcfg_do_load, "", ""), |
| 90 | }; |
| 91 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 92 | static int do_qemu_fw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 93 | char *const argv[]) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 94 | { |
| 95 | int ret; |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 96 | struct cmd_tbl *fwcfg_cmd; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 97 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 98 | ret = qfw_get_dev(&qfw_dev); |
| 99 | if (ret) { |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 100 | printf("QEMU fw_cfg interface not found\n"); |
| 101 | return CMD_RET_USAGE; |
| 102 | } |
| 103 | |
| 104 | fwcfg_cmd = find_cmd_tbl(argv[1], fwcfg_commands, |
| 105 | ARRAY_SIZE(fwcfg_commands)); |
| 106 | argc -= 2; |
| 107 | argv += 2; |
| 108 | if (!fwcfg_cmd || argc > fwcfg_cmd->maxargs) |
| 109 | return CMD_RET_USAGE; |
| 110 | |
| 111 | ret = fwcfg_cmd->cmd(fwcfg_cmd, flag, argc, argv); |
| 112 | |
| 113 | return cmd_process_error(fwcfg_cmd, ret); |
| 114 | } |
| 115 | |
| 116 | U_BOOT_CMD( |
| 117 | qfw, 4, 1, do_qemu_fw, |
| 118 | "QEMU firmware interface", |
| 119 | "<command>\n" |
| 120 | " - list : print firmware(s) currently loaded\n" |
| 121 | " - cpus : print online cpu number\n" |
| 122 | " - load <kernel addr> <initrd addr> : load kernel and initrd (if any), and setup for zboot\n" |
Simon Glass | 10c398d | 2023-11-18 14:04:52 -0700 | [diff] [blame] | 123 | ); |