Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, Google Inc. |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 8 | #include <fs.h> |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 9 | #include <part.h> |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 10 | #include <sandbox_host.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <dm/device_compat.h> |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
| 13 | #include <dm/uclass-internal.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 15 | #include <linux/log2.h> |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 16 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 18 | char *const argv[]) |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 19 | { |
Tom Rini | 089078a | 2013-10-10 10:46:22 -0400 | [diff] [blame] | 20 | return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX); |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 23 | static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc, |
| 24 | char *const argv[]) |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 25 | { |
| 26 | return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX); |
| 27 | } |
| 28 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 29 | static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc, |
| 30 | char *const argv[]) |
Stefan Brüns | 72acc65 | 2016-08-11 22:52:04 +0200 | [diff] [blame] | 31 | { |
| 32 | return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX); |
| 33 | } |
| 34 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 35 | static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc, |
| 36 | char *const argv[]) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 37 | { |
Tom Rini | 089078a | 2013-10-10 10:46:22 -0400 | [diff] [blame] | 38 | return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX); |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 41 | static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc, |
| 42 | char *const argv[]) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 43 | { |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 44 | bool removable = false; |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 45 | struct udevice *dev; |
| 46 | const char *label; |
Simon Glass | 8f5fdf3 | 2021-03-15 18:11:09 +1300 | [diff] [blame] | 47 | char *file; |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 48 | unsigned long blksz = DEFAULT_BLKSZ; |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 49 | int ret; |
Simon Glass | 8f5fdf3 | 2021-03-15 18:11:09 +1300 | [diff] [blame] | 50 | |
Simon Glass | f0a4538 | 2021-03-15 18:11:10 +1300 | [diff] [blame] | 51 | /* Skip 'bind' */ |
| 52 | argc--; |
| 53 | argv++; |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 54 | if (argc < 2) |
| 55 | return CMD_RET_USAGE; |
| 56 | |
| 57 | if (!strcmp(argv[0], "-r")) { |
| 58 | removable = true; |
| 59 | argc--; |
| 60 | argv++; |
| 61 | } |
| 62 | |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 63 | if (argc < 2 || argc > 3) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 64 | return CMD_RET_USAGE; |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 65 | label = argv[0]; |
Bin Meng | c3e784c | 2023-09-26 16:43:32 +0800 | [diff] [blame] | 66 | file = argv[1]; |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 67 | if (argc > 2) { |
| 68 | blksz = dectoul(argv[2], NULL); |
| 69 | if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) { |
| 70 | printf("blksz must be >= 512 and power of 2\n"); |
| 71 | return CMD_RET_FAILURE; |
| 72 | } |
| 73 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 74 | |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 75 | ret = host_create_attach_file(label, file, removable, blksz, &dev); |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 76 | if (ret) { |
| 77 | printf("Cannot create device / bind file\n"); |
| 78 | return CMD_RET_FAILURE; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * parse_host_label() - Parse a device label or sequence number |
| 86 | * |
| 87 | * This shows an error if it returns NULL |
| 88 | * |
| 89 | * @label: String containing the label or sequence number |
| 90 | * Returns: Associated device, or NULL if not found |
| 91 | */ |
| 92 | static struct udevice *parse_host_label(const char *label) |
| 93 | { |
| 94 | struct udevice *dev; |
| 95 | |
| 96 | dev = host_find_by_label(label); |
| 97 | if (!dev) { |
| 98 | int devnum; |
| 99 | char *ep; |
| 100 | |
| 101 | devnum = hextoul(label, &ep); |
| 102 | if (*ep || |
| 103 | uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) { |
| 104 | printf("No such device '%s'\n", label); |
| 105 | return NULL; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return dev; |
| 110 | } |
| 111 | |
| 112 | static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc, |
| 113 | char *const argv[]) |
| 114 | { |
| 115 | struct udevice *dev; |
| 116 | const char *label; |
| 117 | int ret; |
| 118 | |
| 119 | if (argc < 2) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 120 | return CMD_RET_USAGE; |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 121 | |
| 122 | label = argv[1]; |
| 123 | dev = parse_host_label(label); |
| 124 | if (!dev) |
| 125 | return CMD_RET_FAILURE; |
| 126 | |
| 127 | ret = host_detach_file(dev); |
| 128 | if (ret) { |
| 129 | printf("Cannot detach file (err=%d)\n", ret); |
| 130 | return CMD_RET_FAILURE; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 131 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 132 | |
| 133 | ret = device_unbind(dev); |
| 134 | if (ret) { |
| 135 | printf("Cannot attach file\n"); |
| 136 | ret = device_unbind(dev); |
| 137 | if (ret) |
| 138 | printf("Cannot unbind device '%s'\n", dev->name); |
| 139 | return CMD_RET_FAILURE; |
| 140 | } |
Simon Glass | 8f5fdf3 | 2021-03-15 18:11:09 +1300 | [diff] [blame] | 141 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 142 | return 0; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 145 | static void show_host_dev(struct udevice *dev) |
| 146 | { |
| 147 | struct host_sb_plat *plat = dev_get_plat(dev); |
| 148 | struct blk_desc *desc; |
| 149 | struct udevice *blk; |
| 150 | int ret; |
| 151 | |
| 152 | printf("%3d ", dev_seq(dev)); |
| 153 | if (!plat->fd) { |
| 154 | printf("Not bound to a backing file\n"); |
| 155 | return; |
| 156 | } |
| 157 | ret = blk_get_from_parent(dev, &blk); |
| 158 | if (ret) /* cannot happen */ |
| 159 | return; |
| 160 | |
| 161 | desc = dev_get_uclass_plat(blk); |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 162 | printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz, |
| 163 | plat->label, plat->filename); |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 164 | } |
| 165 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 166 | static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 167 | char *const argv[]) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 168 | { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 169 | struct udevice *dev; |
| 170 | |
| 171 | if (argc < 1) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 172 | return CMD_RET_USAGE; |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 173 | |
| 174 | dev = NULL; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 175 | if (argc >= 2) { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 176 | dev = parse_host_label(argv[1]); |
| 177 | if (!dev) |
| 178 | return CMD_RET_FAILURE; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 179 | } |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 180 | |
Bin Meng | c437378 | 2023-09-26 16:43:36 +0800 | [diff] [blame] | 181 | printf("%3s %12s %6s %-15s %s\n", |
| 182 | "dev", "blocks", "blksz", "label", "path"); |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 183 | if (dev) { |
| 184 | show_host_dev(dev); |
| 185 | } else { |
| 186 | struct uclass *uc; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 187 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 188 | uclass_id_foreach_dev(UCLASS_HOST, dev, uc) |
| 189 | show_host_dev(dev); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 190 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 191 | |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 195 | static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc, |
| 196 | char *const argv[]) |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 197 | { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 198 | struct udevice *dev; |
| 199 | const char *label; |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 200 | |
| 201 | if (argc < 1 || argc > 3) |
| 202 | return CMD_RET_USAGE; |
| 203 | |
| 204 | if (argc == 1) { |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 205 | struct host_sb_plat *plat; |
| 206 | |
| 207 | dev = host_get_cur_dev(); |
| 208 | if (!dev) { |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 209 | printf("No current host device\n"); |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 210 | return CMD_RET_FAILURE; |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 211 | } |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 212 | plat = dev_get_plat(dev); |
| 213 | printf("Current host device: %d: %s\n", dev_seq(dev), |
| 214 | plat->label); |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 218 | label = argv[1]; |
| 219 | dev = parse_host_label(argv[1]); |
| 220 | if (!dev) |
| 221 | return CMD_RET_FAILURE; |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 222 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 223 | host_set_cur_dev(dev); |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 224 | |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 228 | static struct cmd_tbl cmd_host_sub[] = { |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 229 | U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""), |
| 230 | U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""), |
| 231 | U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""), |
Stefan Brüns | 72acc65 | 2016-08-11 22:52:04 +0200 | [diff] [blame] | 232 | U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""), |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 233 | U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""), |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 234 | U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""), |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 235 | U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""), |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 236 | U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""), |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 239 | static int do_host(struct cmd_tbl *cmdtp, int flag, int argc, |
| 240 | char *const argv[]) |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 241 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 242 | struct cmd_tbl *c; |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 243 | |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 244 | /* Skip past 'host' */ |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 245 | argc--; |
| 246 | argv++; |
| 247 | |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 248 | c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub)); |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 249 | |
| 250 | if (c) |
| 251 | return c->cmd(cmdtp, flag, argc, argv); |
| 252 | else |
| 253 | return CMD_RET_USAGE; |
| 254 | } |
| 255 | |
| 256 | U_BOOT_CMD( |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 257 | host, 8, 1, do_host, |
| 258 | "Miscellaneous host commands", |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 259 | "load hostfs - <addr> <filename> [<bytes> <offset>] - " |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 260 | "load a file from host\n" |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 261 | "host ls hostfs - <filename> - list files on host\n" |
| 262 | "host save hostfs - <addr> <filename> <bytes> [<offset>] - " |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 263 | "save a file to host\n" |
Sébastien Szymanski | ed6481e | 2017-01-19 17:49:13 +0100 | [diff] [blame] | 264 | "host size hostfs - <filename> - determine size of file on host\n" |
Bin Meng | 881e475 | 2023-09-26 16:43:33 +0800 | [diff] [blame] | 265 | "host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n" |
| 266 | " and optionally set the device's logical block size\n" |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 267 | " -r = mark as removable\n" |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 268 | "host unbind <label> - unbind file from \"host\" device\n" |
| 269 | "host info [<label>] - show device binding & info\n" |
| 270 | "host dev [<label>] - set or retrieve the current host device\n" |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 271 | "host commands use the \"hostfs\" device. The \"host\" device is used\n" |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 272 | "with standard IO commands such as fatls or ext2load" |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 273 | ); |