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