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