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