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