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