Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 44a6ac9 | 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 | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 6 | * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved. |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 12 | #include <errno.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 13 | #include <g_dnl.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 15 | #include <part.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 16 | #include <usb.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 17 | #include <usb_mass_storage.h> |
Patrick Delaunay | 77498e6 | 2019-04-18 17:18:39 +0200 | [diff] [blame] | 18 | #include <watchdog.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 20 | #include <linux/printk.h> |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 21 | |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 22 | static int ums_read_sector(struct ums *ums_dev, |
| 23 | ulong start, lbaint_t blkcnt, void *buf) |
| 24 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 25 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 26 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 27 | |
Simon Glass | 5ee5796 | 2016-07-05 17:10:17 -0600 | [diff] [blame] | 28 | return blk_dread(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static int ums_write_sector(struct ums *ums_dev, |
| 32 | ulong start, lbaint_t blkcnt, const void *buf) |
| 33 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 34 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 35 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 36 | |
Simon Glass | 5ee5796 | 2016-07-05 17:10:17 -0600 | [diff] [blame] | 37 | return blk_dwrite(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 38 | } |
| 39 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 40 | static struct ums *ums; |
| 41 | static int ums_count; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 42 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 43 | static void ums_fini(void) |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 44 | { |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 45 | int i; |
| 46 | |
| 47 | for (i = 0; i < ums_count; i++) |
| 48 | free((void *)ums[i].name); |
| 49 | free(ums); |
Fabio Estevam | 9b9c3f3 | 2016-05-27 22:37:10 -0300 | [diff] [blame] | 50 | ums = NULL; |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 51 | ums_count = 0; |
| 52 | } |
| 53 | |
| 54 | #define UMS_NAME_LEN 16 |
| 55 | |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 56 | static int ums_init(const char *devtype, const char *devnums_part_str) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 57 | { |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 58 | char *s, *t, *devnum_part_str, *name; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 59 | struct blk_desc *block_dev; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 60 | struct disk_partition info; |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 61 | int partnum; |
Tom Rini | b55b6fe | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 62 | int ret = -1; |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 63 | struct ums *ums_new; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 64 | |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 65 | s = strdup(devnums_part_str); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 66 | if (!s) |
| 67 | return -1; |
| 68 | |
| 69 | t = s; |
| 70 | ums_count = 0; |
| 71 | |
| 72 | for (;;) { |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 73 | devnum_part_str = strsep(&t, ","); |
| 74 | if (!devnum_part_str) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 75 | break; |
| 76 | |
Sean Anderson | 159b8f5 | 2021-10-29 11:05:54 -0400 | [diff] [blame] | 77 | partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str, |
| 78 | &block_dev, &info, 1); |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 79 | |
| 80 | if (partnum < 0) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 81 | goto cleanup; |
| 82 | |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 83 | /* Check if the argument is in legacy format. If yes, |
| 84 | * expose all partitions by setting the partnum = 0 |
| 85 | * e.g. ums 0 mmc 0 |
| 86 | */ |
| 87 | if (!strchr(devnum_part_str, ':')) |
| 88 | partnum = 0; |
| 89 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 90 | ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums)); |
Tom Rini | b55b6fe | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 91 | if (!ums_new) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 92 | goto cleanup; |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 93 | ums = ums_new; |
| 94 | |
John Tobias | 9fc9245 | 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 | 9e7d588 | 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 | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 106 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 107 | name = malloc(UMS_NAME_LEN); |
Tom Rini | b55b6fe | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 108 | if (!name) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 109 | goto cleanup; |
Stephen Warren | 9e7d588 | 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 | |
Marek Szyprowski | 80e832f | 2020-12-17 11:47:11 +0100 | [diff] [blame] | 114 | printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n", |
| 115 | ums_count, devtype, ums[ums_count].block_dev.devnum, |
Stephen Warren | 9e7d588 | 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 | } |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 122 | |
Tom Rini | b55b6fe | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 123 | if (ums_count) |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 124 | ret = 0; |
Stephen Warren | 5c7648b | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 125 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 126 | cleanup: |
| 127 | free(s); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 128 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 129 | if (ret < 0) |
| 130 | ums_fini(); |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 131 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 132 | return ret; |
Stephen Warren | 803876d | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 135 | static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, |
| 136 | int argc, char *const argv[]) |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 137 | { |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 138 | const char *usb_controller; |
| 139 | const char *devtype; |
| 140 | const char *devnum; |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 141 | unsigned int controller_index; |
Marek Vasut | 7786e70 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 142 | struct udevice *udc; |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 143 | int rc; |
| 144 | int cable_ready_timeout __maybe_unused; |
| 145 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 146 | if (argc < 3) |
| 147 | return CMD_RET_USAGE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 148 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 149 | usb_controller = argv[1]; |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 150 | if (argc >= 4) { |
| 151 | devtype = argv[2]; |
| 152 | devnum = argv[3]; |
| 153 | } else { |
| 154 | devtype = "mmc"; |
| 155 | devnum = argv[2]; |
| 156 | } |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 157 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 158 | rc = ums_init(devtype, devnum); |
| 159 | if (rc < 0) |
Przemyslaw Marczak | 0e1e709 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 160 | return CMD_RET_FAILURE; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 161 | |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 162 | controller_index = (unsigned int)(simple_strtoul( |
| 163 | usb_controller, NULL, 0)); |
Marek Vasut | 7786e70 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 164 | rc = udc_device_get_by_index(controller_index, &udc); |
| 165 | if (rc) { |
Seung-Woo Kim | 591188e | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 166 | pr_err("Couldn't init USB controller.\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 167 | rc = CMD_RET_FAILURE; |
| 168 | goto cleanup_ums_init; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 169 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 170 | |
Marek Vasut | 7786e70 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 171 | rc = fsg_init(ums, ums_count, udc); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 172 | if (rc) { |
Seung-Woo Kim | 591188e | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 173 | pr_err("fsg_init failed\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 174 | rc = CMD_RET_FAILURE; |
| 175 | goto cleanup_board; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Stephen Warren | c611a6b | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 178 | rc = g_dnl_register("usb_dnl_ums"); |
| 179 | if (rc) { |
Seung-Woo Kim | 591188e | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 180 | pr_err("g_dnl_register failed\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 181 | rc = CMD_RET_FAILURE; |
| 182 | goto cleanup_board; |
Stephen Warren | c611a6b | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 183 | } |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 184 | |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 185 | /* Timeout unit: seconds */ |
Stephen Warren | 5e8c227 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 186 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 187 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 188 | if (!g_dnl_board_usb_cable_connected()) { |
| 189 | /* |
| 190 | * Won't execute if we don't know whether the cable is |
| 191 | * connected. |
| 192 | */ |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 193 | puts("Please connect USB cable.\n"); |
| 194 | |
Mateusz Zalega | 21fe3f7 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 195 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 196 | if (ctrlc()) { |
| 197 | puts("\rCTRL+C - Operation aborted.\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 198 | rc = CMD_RET_SUCCESS; |
| 199 | goto cleanup_register; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 200 | } |
| 201 | if (!cable_ready_timeout) { |
| 202 | puts("\rUSB cable not detected.\n" \ |
| 203 | "Command exit.\n"); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 204 | rc = CMD_RET_SUCCESS; |
| 205 | goto cleanup_register; |
Przemyslaw Marczak | e92cc56 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); |
| 209 | mdelay(1000); |
| 210 | cable_ready_timeout--; |
| 211 | } |
| 212 | puts("\r\n"); |
| 213 | } |
| 214 | |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 215 | while (1) { |
Marek Vasut | 7786e70 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 216 | dm_usb_gadget_handle_interrupts(udc); |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 217 | |
| 218 | rc = fsg_main_thread(NULL); |
| 219 | if (rc) { |
| 220 | /* Check I/O error */ |
| 221 | if (rc == -EIO) |
| 222 | printf("\rCheck USB cable connection\n"); |
| 223 | |
| 224 | /* Check CTRL+C */ |
| 225 | if (rc == -EPIPE) |
| 226 | printf("\rCTRL+C - Operation aborted\n"); |
| 227 | |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 228 | rc = CMD_RET_SUCCESS; |
| 229 | goto cleanup_register; |
Przemyslaw Marczak | 06ef7cc | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 230 | } |
Patrick Delaunay | 77498e6 | 2019-04-18 17:18:39 +0200 | [diff] [blame] | 231 | |
Svyatoslav Ryhel | bb8068d | 2023-03-20 21:01:43 +0200 | [diff] [blame] | 232 | if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) { |
| 233 | /* Abort by pressing any key */ |
| 234 | if (tstc()) { |
| 235 | getchar(); |
| 236 | printf("\rOperation aborted.\n"); |
| 237 | rc = CMD_RET_SUCCESS; |
| 238 | goto cleanup_register; |
| 239 | } |
| 240 | } |
| 241 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 242 | schedule(); |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 243 | } |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 244 | |
| 245 | cleanup_register: |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 246 | g_dnl_unregister(); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 247 | cleanup_board: |
Marek Vasut | 7786e70 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 248 | udc_device_put(udc); |
Stephen Warren | 9e7d588 | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 249 | cleanup_ums_init: |
| 250 | ums_fini(); |
| 251 | |
| 252 | return rc; |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 255 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | 726c123 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 256 | "Use the UMS [USB Mass Storage]", |
John Tobias | 9fc9245 | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 257 | "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n" |
Stephen Warren | e4e98cd | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 258 | " devtype defaults to mmc" |
Lukasz Majewski | 44a6ac9 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 259 | ); |