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 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Miao Yan | 1868659 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 10 | #include <qfw.h> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | |
| 13 | static struct udevice *qfw_dev; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 14 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 15 | static int qemu_fwcfg_cmd_list_firmware(void) |
Miao Yan | fcf5c04 | 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 | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 22 | ret = qfw_read_firmware_list(qfw_dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 23 | if (ret) |
| 24 | return ret; |
| 25 | |
Asherah Connor | 5b0b43e | 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 | d2e7972 | 2023-07-15 21:38:48 -0600 | [diff] [blame] | 29 | printf("%08lx %-56s\n", file->addr, file->cfg.name); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Simon Glass | 0914011 | 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 | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 37 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 38 | if (qemu_fwcfg_cmd_list_firmware() < 0) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 39 | return CMD_RET_FAILURE; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | 0914011 | 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 | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 46 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 47 | printf("%d cpu(s) online\n", qfw_online_cpus(qfw_dev)); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Simon Glass | 0914011 | 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 | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 53 | { |
| 54 | char *env; |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 55 | ulong load_addr; |
| 56 | ulong initrd_addr; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 57 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | env = env_get("loadaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 59 | load_addr = env ? |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 60 | hextoul(env, NULL) : |
| 61 | CONFIG_SYS_LOAD_ADDR; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 62 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 63 | env = env_get("ramdiskaddr"); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 64 | initrd_addr = env ? |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 65 | hextoul(env, NULL) : |
Tom Rini | d4c8dd1 | 2022-12-04 10:13:39 -0500 | [diff] [blame] | 66 | #ifdef CFG_RAMDISK_ADDR |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 67 | CFG_RAMDISK_ADDR; |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 68 | #else |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 69 | 0; |
Miao Yan | 86e30e6 | 2016-05-22 19:37:18 -0700 | [diff] [blame] | 70 | #endif |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 71 | |
| 72 | if (argc == 2) { |
Simon Glass | e08e6ea | 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 | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 75 | } else if (argc == 1) { |
Simon Glass | e08e6ea | 2023-01-28 15:00:22 -0700 | [diff] [blame] | 76 | load_addr = hextoul(argv[0], NULL); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Miao Yan | 86e30e6 | 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 | 00fa989 | 2023-01-28 15:00:23 -0700 | [diff] [blame] | 84 | return qemu_fwcfg_setup_kernel(qfw_dev, load_addr, initrd_addr); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 87 | static struct cmd_tbl fwcfg_commands[] = { |
Miao Yan | fcf5c04 | 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 | 0914011 | 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 | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 95 | { |
| 96 | int ret; |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 97 | struct cmd_tbl *fwcfg_cmd; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 98 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 99 | ret = qfw_get_dev(&qfw_dev); |
| 100 | if (ret) { |
Miao Yan | fcf5c04 | 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 | ) |