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