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