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 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 10 | #include <blk.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 12 | #include <console.h> |
Caleb Connolly | 3eb1276 | 2023-10-11 14:19:42 +0100 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <errno.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 15 | #include <g_dnl.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 17 | #include <part.h> |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 18 | #include <usb.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 19 | #include <usb_mass_storage.h> |
Patrick Delaunay | 85d0aec | 2019-04-18 17:18:39 +0200 | [diff] [blame] | 20 | #include <watchdog.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 22 | #include <linux/printk.h> |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 23 | |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 24 | static int ums_read_sector(struct ums *ums_dev, |
| 25 | ulong start, lbaint_t blkcnt, void *buf) |
| 26 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 27 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 28 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 29 | |
Caleb Connolly | 3eb1276 | 2023-10-11 14:19:42 +0100 | [diff] [blame] | 30 | //printf("%s: blk %s read %lu bytes @ %#lx\n", __func__, block_dev->bdev->name, blkcnt, blkstart); |
| 31 | |
Simon Glass | c9f3c5f | 2016-07-05 17:10:17 -0600 | [diff] [blame] | 32 | return blk_dread(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static int ums_write_sector(struct ums *ums_dev, |
| 36 | ulong start, lbaint_t blkcnt, const void *buf) |
| 37 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 38 | struct blk_desc *block_dev = &ums_dev->block_dev; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 39 | lbaint_t blkstart = start + ums_dev->start_sector; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 40 | |
Simon Glass | c9f3c5f | 2016-07-05 17:10:17 -0600 | [diff] [blame] | 41 | return blk_dwrite(block_dev, blkstart, blkcnt, buf); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 44 | static struct ums *ums; |
| 45 | static int ums_count; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 46 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 47 | static void ums_fini(void) |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 48 | { |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < ums_count; i++) |
| 52 | free((void *)ums[i].name); |
| 53 | free(ums); |
Fabio Estevam | d419026 | 2016-05-27 22:37:10 -0300 | [diff] [blame] | 54 | ums = NULL; |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 55 | ums_count = 0; |
| 56 | } |
| 57 | |
| 58 | #define UMS_NAME_LEN 16 |
| 59 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 60 | static int ums_init(const char *devtype, const char *devnums_part_str) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 61 | { |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 62 | char *s, *t, *devnum_part_str, *name; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 63 | struct blk_desc *block_dev; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 64 | struct disk_partition info; |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 65 | int partnum; |
Tom Rini | a238b0d | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 66 | int ret = -1; |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 67 | struct ums *ums_new; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 68 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 69 | s = strdup(devnums_part_str); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 70 | if (!s) |
| 71 | return -1; |
| 72 | |
| 73 | t = s; |
| 74 | ums_count = 0; |
| 75 | |
| 76 | for (;;) { |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 77 | devnum_part_str = strsep(&t, ","); |
| 78 | if (!devnum_part_str) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 79 | break; |
| 80 | |
Sean Anderson | 1833e68 | 2021-10-29 11:05:54 -0400 | [diff] [blame] | 81 | partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str, |
| 82 | &block_dev, &info, 1); |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 83 | |
| 84 | if (partnum < 0) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 85 | goto cleanup; |
| 86 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 87 | /* Check if the argument is in legacy format. If yes, |
| 88 | * expose all partitions by setting the partnum = 0 |
| 89 | * e.g. ums 0 mmc 0 |
| 90 | */ |
| 91 | if (!strchr(devnum_part_str, ':')) |
| 92 | partnum = 0; |
| 93 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 94 | ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums)); |
Tom Rini | a238b0d | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 95 | if (!ums_new) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 96 | goto cleanup; |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 97 | ums = ums_new; |
| 98 | |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 99 | /* if partnum = 0, expose all partitions */ |
| 100 | if (partnum == 0) { |
| 101 | ums[ums_count].start_sector = 0; |
| 102 | ums[ums_count].num_sectors = block_dev->lba; |
| 103 | } else { |
| 104 | ums[ums_count].start_sector = info.start; |
| 105 | ums[ums_count].num_sectors = info.size; |
| 106 | } |
| 107 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 108 | ums[ums_count].read_sector = ums_read_sector; |
| 109 | ums[ums_count].write_sector = ums_write_sector; |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 110 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 111 | name = malloc(UMS_NAME_LEN); |
Tom Rini | a238b0d | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 112 | if (!name) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 113 | goto cleanup; |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 114 | snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count); |
| 115 | ums[ums_count].name = name; |
| 116 | ums[ums_count].block_dev = *block_dev; |
| 117 | |
Marek Szyprowski | c954ff5 | 2020-12-17 11:47:11 +0100 | [diff] [blame] | 118 | printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n", |
| 119 | ums_count, devtype, ums[ums_count].block_dev.devnum, |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 120 | ums[ums_count].block_dev.hwpart, |
| 121 | ums[ums_count].start_sector, |
| 122 | ums[ums_count].num_sectors); |
| 123 | |
| 124 | ums_count++; |
| 125 | } |
| 126 | |
Tom Rini | a238b0d | 2016-04-20 10:59:32 -0400 | [diff] [blame] | 127 | if (ums_count) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 128 | ret = 0; |
| 129 | |
| 130 | cleanup: |
| 131 | free(s); |
| 132 | |
Stephen Warren | d0cc456 | 2014-05-05 10:40:16 -0600 | [diff] [blame] | 133 | if (ret < 0) |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 134 | ums_fini(); |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 135 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 136 | return ret; |
Stephen Warren | abfe8af | 2014-05-05 10:40:15 -0600 | [diff] [blame] | 137 | } |
| 138 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 139 | static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag, |
| 140 | int argc, char *const argv[]) |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 141 | { |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 142 | const char *usb_controller; |
| 143 | const char *devtype; |
| 144 | const char *devnum; |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 145 | unsigned int controller_index; |
Marek Vasut | f032260 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 146 | struct udevice *udc; |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 147 | int rc; |
| 148 | int cable_ready_timeout __maybe_unused; |
| 149 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 150 | if (argc < 3) |
| 151 | return CMD_RET_USAGE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 152 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 153 | usb_controller = argv[1]; |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 154 | if (argc >= 4) { |
| 155 | devtype = argv[2]; |
| 156 | devnum = argv[3]; |
| 157 | } else { |
| 158 | devtype = "mmc"; |
| 159 | devnum = argv[2]; |
| 160 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 161 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 162 | rc = ums_init(devtype, devnum); |
| 163 | if (rc < 0) |
Przemyslaw Marczak | f4dacf7 | 2013-10-23 14:30:43 +0200 | [diff] [blame] | 164 | return CMD_RET_FAILURE; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 165 | |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 166 | controller_index = (unsigned int)(simple_strtoul( |
| 167 | usb_controller, NULL, 0)); |
Marek Vasut | f032260 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 168 | rc = udc_device_get_by_index(controller_index, &udc); |
| 169 | if (rc) { |
Seung-Woo Kim | 71002b5 | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 170 | pr_err("Couldn't init USB controller.\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 171 | rc = CMD_RET_FAILURE; |
| 172 | goto cleanup_ums_init; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 173 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 174 | |
Marek Vasut | f032260 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 175 | rc = fsg_init(ums, ums_count, udc); |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 176 | if (rc) { |
Seung-Woo Kim | 71002b5 | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 177 | pr_err("fsg_init failed\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 178 | rc = CMD_RET_FAILURE; |
| 179 | goto cleanup_board; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Stephen Warren | 66b88b0 | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 182 | rc = g_dnl_register("usb_dnl_ums"); |
| 183 | if (rc) { |
Seung-Woo Kim | 71002b5 | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 184 | pr_err("g_dnl_register failed\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 185 | rc = CMD_RET_FAILURE; |
| 186 | goto cleanup_board; |
Stephen Warren | 66b88b0 | 2014-05-01 15:42:10 -0600 | [diff] [blame] | 187 | } |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 188 | |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 189 | /* Timeout unit: seconds */ |
Stephen Warren | 1725f12 | 2014-05-05 10:40:17 -0600 | [diff] [blame] | 190 | cable_ready_timeout = UMS_CABLE_READY_TIMEOUT; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 191 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 192 | if (!g_dnl_board_usb_cable_connected()) { |
| 193 | /* |
| 194 | * Won't execute if we don't know whether the cable is |
| 195 | * connected. |
| 196 | */ |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 197 | puts("Please connect USB cable.\n"); |
| 198 | |
Mateusz Zalega | 75504e9 | 2014-04-30 13:07:48 +0200 | [diff] [blame] | 199 | while (!g_dnl_board_usb_cable_connected()) { |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 200 | if (ctrlc()) { |
| 201 | puts("\rCTRL+C - Operation aborted.\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 | if (!cable_ready_timeout) { |
| 206 | puts("\rUSB cable not detected.\n" \ |
| 207 | "Command exit.\n"); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 208 | rc = CMD_RET_SUCCESS; |
| 209 | goto cleanup_register; |
Przemyslaw Marczak | 3603e31 | 2014-01-07 15:08:37 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | printf("\rAuto exit in: %.2d s.", cable_ready_timeout); |
| 213 | mdelay(1000); |
| 214 | cable_ready_timeout--; |
| 215 | } |
| 216 | puts("\r\n"); |
| 217 | } |
| 218 | |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 219 | while (1) { |
Marek Vasut | f032260 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 220 | dm_usb_gadget_handle_interrupts(udc); |
Przemyslaw Marczak | 351e9b2 | 2013-10-23 14:30:46 +0200 | [diff] [blame] | 221 | |
| 222 | rc = fsg_main_thread(NULL); |
| 223 | if (rc) { |
| 224 | /* Check I/O error */ |
| 225 | if (rc == -EIO) |
| 226 | printf("\rCheck USB cable connection\n"); |
| 227 | |
| 228 | /* Check CTRL+C */ |
| 229 | if (rc == -EPIPE) |
| 230 | printf("\rCTRL+C - Operation aborted\n"); |
| 231 | |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 232 | rc = CMD_RET_SUCCESS; |
| 233 | goto cleanup_register; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 234 | } |
Patrick Delaunay | 85d0aec | 2019-04-18 17:18:39 +0200 | [diff] [blame] | 235 | |
Svyatoslav Ryhel | a57adac | 2023-03-20 21:01:43 +0200 | [diff] [blame] | 236 | if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) { |
| 237 | /* Abort by pressing any key */ |
| 238 | if (tstc()) { |
| 239 | getchar(); |
| 240 | printf("\rOperation aborted.\n"); |
| 241 | rc = CMD_RET_SUCCESS; |
| 242 | goto cleanup_register; |
| 243 | } |
| 244 | } |
| 245 | |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 246 | schedule(); |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 247 | } |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 248 | |
| 249 | cleanup_register: |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 250 | g_dnl_unregister(); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 251 | cleanup_board: |
Marek Vasut | f032260 | 2023-09-01 11:49:54 +0200 | [diff] [blame] | 252 | udc_device_put(udc); |
Stephen Warren | 02585eb | 2015-12-07 11:38:50 -0700 | [diff] [blame] | 253 | cleanup_ums_init: |
| 254 | ums_fini(); |
| 255 | |
| 256 | return rc; |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 259 | U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage, |
Fabio Estevam | ee02a65 | 2015-02-28 15:11:46 -0300 | [diff] [blame] | 260 | "Use the UMS [USB Mass Storage]", |
John Tobias | a2e3a1d | 2016-04-13 12:06:51 -0700 | [diff] [blame] | 261 | "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n" |
Stephen Warren | 8c60045 | 2014-05-05 10:40:18 -0600 | [diff] [blame] | 262 | " devtype defaults to mmc" |
Lukasz Majewski | b528f71 | 2013-03-05 12:10:17 +0100 | [diff] [blame] | 263 | ); |