Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 5 | * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved. |
| 6 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 14 | #include <g_dnl.h> |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 15 | #include <part.h> |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 16 | #include <usb.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 17 | #include <usb_mass_storage.h> |
| 18 | |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 19 | static int ums_read_sector(struct ums *ums_dev, |
| 20 | ulong start, lbaint_t blkcnt, void *buf) |
| 21 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 22 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 23 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 24 | |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 25 | return block_dev->block_read(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int ums_write_sector(struct ums *ums_dev, |
| 29 | ulong start, lbaint_t blkcnt, const void *buf) |
| 30 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 31 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 32 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 33 | |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 34 | return block_dev->block_write(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 37 | static struct ums *ums; |
| 38 | static int ums_count; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 39 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 40 | static void ums_fini(void) |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 41 | { |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < ums_count; i++) |
| 45 | free((void *)ums[i].name); |
| 46 | free(ums); |
| 47 | ums = 0; |
| 48 | ums_count = 0; |
| 49 | } |
| 50 | |
| 51 | #define UMS_NAME_LEN 16 |
| 52 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 53 | static int ums_init(const char *devtype, const char *devnums_part_str) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 54 | { |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 55 | char *s, *t, *devnum_part_str, *name; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | struct blk_desc *block_dev; |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 57 | disk_partition_t info; |
| 58 | int partnum; |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 59 | int ret; |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 60 | struct ums *ums_new; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 61 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 62 | s = strdup(devnums_part_str); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 63 | if (!s) |
| 64 | return -1; |
| 65 | |
| 66 | t = s; |
| 67 | ums_count = 0; |
| 68 | |
| 69 | for (;;) { |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 70 | devnum_part_str = strsep(&t, ","); |
| 71 | if (!devnum_part_str) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 72 | break; |
| 73 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 74 | partnum = blk_get_device_part_str(devtype, devnum_part_str, |
| 75 | &block_dev, &info, 1); |
| 76 | |
| 77 | if (partnum < 0) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 78 | goto cleanup; |
| 79 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 80 | /* Check if the argument is in legacy format. If yes, |
| 81 | * expose all partitions by setting the partnum = 0 |
| 82 | * e.g. ums 0 mmc 0 |
| 83 | */ |
| 84 | if (!strchr(devnum_part_str, ':')) |
| 85 | partnum = 0; |
| 86 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 87 | /* f_mass_storage.c assumes SECTOR_SIZE sectors */ |
| 88 | if (block_dev->blksz != SECTOR_SIZE) { |
| 89 | ret = -1; |
| 90 | goto cleanup; |
| 91 | } |
| 92 | |
| 93 | ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums)); |
| 94 | if (!ums_new) { |
| 95 | ret = -1; |
| 96 | goto cleanup; |
| 97 | } |
| 98 | ums = ums_new; |
| 99 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 100 | /* if partnum = 0, expose all partitions */ |
| 101 | if (partnum == 0) { |
| 102 | ums[ums_count].start_sector = 0; |
| 103 | ums[ums_count].num_sectors = block_dev->lba; |
| 104 | } else { |
| 105 | ums[ums_count].start_sector = info.start; |
| 106 | ums[ums_count].num_sectors = info.size; |
| 107 | } |
| 108 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 109 | ums[ums_count].read_sector = ums_read_sector; |
| 110 | ums[ums_count].write_sector = ums_write_sector; |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 111 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 112 | name = malloc(UMS_NAME_LEN); |
| 113 | if (!name) { |
| 114 | ret = -1; |
| 115 | goto cleanup; |
| 116 | } |
| 117 | snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count); |
| 118 | ums[ums_count].name = name; |
| 119 | ums[ums_count].block_dev = *block_dev; |
| 120 | |
| 121 | printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 122 | ums_count, ums[ums_count].block_dev.devnum, |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 123 | ums[ums_count].block_dev.hwpart, |
| 124 | ums[ums_count].start_sector, |
| 125 | ums[ums_count].num_sectors); |
| 126 | |
| 127 | ums_count++; |
| 128 | } |
| 129 | |
| 130 | if (!ums_count) |
| 131 | ret = -1; |
| 132 | else |
| 133 | ret = 0; |
| 134 | |
| 135 | cleanup: |
| 136 | free(s); |
| 137 | |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 138 | if (ret < 0) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 139 | ums_fini(); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 140 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 141 | return ret; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 142 | } |
| 143 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 144 | int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag, |
| 145 | int argc, char * const argv[]) |
| 146 | { |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 147 | const char *usb_controller; |
| 148 | const char *devtype; |
| 149 | const char *devnum; |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 150 | unsigned int controller_index; |
| 151 | int rc; |
| 152 | int cable_ready_timeout __maybe_unused; |
| 153 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 154 | if (argc < 3) |
| 155 | return CMD_RET_USAGE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 156 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 157 | usb_controller = argv[1]; |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 158 | if (argc >= 4) { |
| 159 | devtype = argv[2]; |
| 160 | devnum = argv[3]; |
| 161 | } else { |
| 162 | devtype = "mmc"; |
| 163 | devnum = argv[2]; |
| 164 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 165 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 166 | rc = ums_init(devtype, devnum); |
| 167 | if (rc < 0) |
Przemyslaw Marczak | f4dacf7 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 168 | return CMD_RET_FAILURE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 169 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 170 | controller_index = (unsigned int)(simple_strtoul( |
| 171 | usb_controller, NULL, 0)); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 172 | if (board_usb_init(controller_index, USB_INIT_DEVICE)) { |
| 173 | error("Couldn't init USB controller."); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 174 | rc = CMD_RET_FAILURE; |
| 175 | goto cleanup_ums_init; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 176 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 177 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 178 | rc = fsg_init(ums, ums_count); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 179 | if (rc) { |
| 180 | error("fsg_init failed"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 181 | rc = CMD_RET_FAILURE; |
| 182 | goto cleanup_board; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Stephen Warren | 66b88b0 | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 185 | rc = g_dnl_register("usb_dnl_ums"); |
| 186 | if (rc) { |
| 187 | error("g_dnl_register failed"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 188 | rc = CMD_RET_FAILURE; |
| 189 | goto cleanup_board; |
Stephen Warren | 66b88b0 | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 190 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 191 | |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 192 | /* Timeout unit: seconds */ |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 193 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 194 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 195 | if (!g_dnl_board_usb_cable_connected()) { |
| 196 | /* |
| 197 | * Won't execute if we don't know whether the cable is |
| 198 | * connected. |
| 199 | */ |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 200 | puts("Please connect USB cable.\n"); |
| 201 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 202 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 203 | if (ctrlc()) { |
| 204 | puts("\rCTRL+C - Operation aborted.\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 205 | rc = CMD_RET_SUCCESS; |
| 206 | goto cleanup_register; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 207 | } |
| 208 | if (!cable_ready_timeout) { |
| 209 | puts("\rUSB cable not detected.\n" \ |
| 210 | "Command exit.\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 211 | rc = CMD_RET_SUCCESS; |
| 212 | goto cleanup_register; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); |
| 216 | mdelay(1000); |
| 217 | cable_ready_timeout--; |
| 218 | } |
| 219 | puts("\r\n"); |
| 220 | } |
| 221 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 222 | while (1) { |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 223 | usb_gadget_handle_interrupts(controller_index); |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 224 | |
| 225 | rc = fsg_main_thread(NULL); |
| 226 | if (rc) { |
| 227 | /* Check I/O error */ |
| 228 | if (rc == -EIO) |
| 229 | printf("\rCheck USB cable connection\n"); |
| 230 | |
| 231 | /* Check CTRL+C */ |
| 232 | if (rc == -EPIPE) |
| 233 | printf("\rCTRL+C - Operation aborted\n"); |
| 234 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 235 | rc = CMD_RET_SUCCESS; |
| 236 | goto cleanup_register; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 237 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 238 | } |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 239 | |
| 240 | cleanup_register: |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 241 | g_dnl_unregister(); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 242 | cleanup_board: |
Inha Song | 375f2d7 | 2015-03-03 17:32:05 +0100 | [diff] [blame] | 243 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 244 | cleanup_ums_init: |
| 245 | ums_fini(); |
| 246 | |
| 247 | return rc; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 250 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | ee02a65 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 251 | "Use the UMS [USB Mass Storage]", |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame^] | 252 | "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n" |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 253 | " devtype defaults to mmc" |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 254 | ); |