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> |
| 11 | #include <sandboxblockdev.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 14 | |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 15 | static int host_curr_device = -1; |
| 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 | 8f5fdf3 | 2021-03-15 18:11:09 +1300 | [diff] [blame] | 45 | const char *dev_str; |
| 46 | char *file; |
| 47 | char *ep; |
| 48 | int dev; |
| 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 | f0a4538 | 2021-03-15 18:11:10 +1300 | [diff] [blame] | 64 | dev_str = argv[0]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 65 | dev = hextoul(dev_str, &ep); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 66 | if (*ep) { |
| 67 | printf("** Bad device specification %s **\n", dev_str); |
| 68 | return CMD_RET_USAGE; |
| 69 | } |
Simon Glass | f0a4538 | 2021-03-15 18:11:10 +1300 | [diff] [blame] | 70 | file = argc > 1 ? argv[1] : NULL; |
Simon Glass | 8f5fdf3 | 2021-03-15 18:11:09 +1300 | [diff] [blame] | 71 | |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 72 | return !!host_dev_bind(dev, file, removable); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 75 | static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 76 | char *const argv[]) |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 77 | { |
| 78 | if (argc < 1 || argc > 2) |
| 79 | return CMD_RET_USAGE; |
| 80 | int min_dev = 0; |
| 81 | int max_dev = CONFIG_HOST_MAX_DEVICES - 1; |
| 82 | if (argc >= 2) { |
| 83 | char *ep; |
| 84 | char *dev_str = argv[1]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 85 | int dev = hextoul(dev_str, &ep); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 86 | if (*ep) { |
| 87 | printf("** Bad device specification %s **\n", dev_str); |
| 88 | return CMD_RET_USAGE; |
| 89 | } |
| 90 | min_dev = dev; |
| 91 | max_dev = dev; |
| 92 | } |
| 93 | int dev; |
| 94 | printf("%3s %12s %s\n", "dev", "blocks", "path"); |
| 95 | for (dev = min_dev; dev <= max_dev; dev++) { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 96 | struct blk_desc *blk_dev; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | printf("%3d ", dev); |
| 100 | ret = host_get_dev_err(dev, &blk_dev); |
| 101 | if (ret) { |
| 102 | if (ret == -ENOENT) |
| 103 | puts("Not bound to a backing file\n"); |
| 104 | else if (ret == -ENODEV) |
| 105 | puts("Invalid host device number\n"); |
| 106 | |
| 107 | continue; |
| 108 | } |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 109 | struct host_block_dev *host_dev; |
| 110 | |
| 111 | #ifdef CONFIG_BLK |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 112 | host_dev = dev_get_plat(blk_dev->bdev); |
Simon Glass | 2c2075a | 2016-02-29 15:25:57 -0700 | [diff] [blame] | 113 | #else |
| 114 | host_dev = blk_dev->priv; |
| 115 | #endif |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 116 | printf("%12lu %s\n", (unsigned long)blk_dev->lba, |
| 117 | host_dev->filename); |
| 118 | } |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 122 | static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc, |
| 123 | char *const argv[]) |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 124 | { |
| 125 | int dev; |
| 126 | char *ep; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 127 | struct blk_desc *blk_dev; |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 128 | int ret; |
| 129 | |
| 130 | if (argc < 1 || argc > 3) |
| 131 | return CMD_RET_USAGE; |
| 132 | |
| 133 | if (argc == 1) { |
| 134 | if (host_curr_device < 0) { |
| 135 | printf("No current host device\n"); |
| 136 | return 1; |
| 137 | } |
| 138 | printf("Current host device %d\n", host_curr_device); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 142 | dev = hextoul(argv[1], &ep); |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 143 | if (*ep) { |
| 144 | printf("** Bad device specification %s **\n", argv[2]); |
| 145 | return CMD_RET_USAGE; |
| 146 | } |
| 147 | |
| 148 | ret = host_get_dev_err(dev, &blk_dev); |
| 149 | if (ret) { |
| 150 | if (ret == -ENOENT) |
| 151 | puts("Not bound to a backing file\n"); |
| 152 | else if (ret == -ENODEV) |
| 153 | puts("Invalid host device number\n"); |
| 154 | |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | host_curr_device = dev; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 162 | static struct cmd_tbl cmd_host_sub[] = { |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 163 | U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""), |
| 164 | U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""), |
| 165 | U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""), |
Stefan Brüns | 72acc65 | 2016-08-11 22:52:04 +0200 | [diff] [blame] | 166 | U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""), |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 167 | U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""), |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 168 | U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""), |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 169 | U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""), |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 172 | static int do_host(struct cmd_tbl *cmdtp, int flag, int argc, |
| 173 | char *const argv[]) |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 174 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 175 | struct cmd_tbl *c; |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 176 | |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 177 | /* Skip past 'host' */ |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 178 | argc--; |
| 179 | argv++; |
| 180 | |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 181 | c = find_cmd_tbl(argv[0], cmd_host_sub, |
| 182 | ARRAY_SIZE(cmd_host_sub)); |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 183 | |
| 184 | if (c) |
| 185 | return c->cmd(cmdtp, flag, argc, argv); |
| 186 | else |
| 187 | return CMD_RET_USAGE; |
| 188 | } |
| 189 | |
| 190 | U_BOOT_CMD( |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 191 | host, 8, 1, do_host, |
| 192 | "Miscellaneous host commands", |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 193 | "load hostfs - <addr> <filename> [<bytes> <offset>] - " |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 194 | "load a file from host\n" |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 195 | "host ls hostfs - <filename> - list files on host\n" |
| 196 | "host save hostfs - <addr> <filename> <bytes> [<offset>] - " |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 197 | "save a file to host\n" |
Sébastien Szymanski | ed6481e | 2017-01-19 17:49:13 +0100 | [diff] [blame] | 198 | "host size hostfs - <filename> - determine size of file on host\n" |
Simon Glass | b4d0077 | 2021-03-15 18:11:11 +1300 | [diff] [blame] | 199 | "host bind [-r] <dev> [<filename>] - bind \"host\" device to file\n" |
| 200 | " -r = mark as removable\n" |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 201 | "host info [<dev>] - show device binding & info\n" |
Sjoerd Simons | 3f6b8d8 | 2015-04-13 22:54:23 +0200 | [diff] [blame] | 202 | "host dev [<dev>] - Set or retrieve the current host device\n" |
Sjoerd Simons | 609d7fc | 2015-04-13 22:54:22 +0200 | [diff] [blame] | 203 | "host commands use the \"hostfs\" device. The \"host\" device is used\n" |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 204 | "with standard IO commands such as fatls or ext2load" |
Simon Glass | bf6ce79 | 2012-12-26 09:53:36 +0000 | [diff] [blame] | 205 | ); |