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