blob: 751701fe73aff2c579e022bbbc2adbaad9859f11 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01002/*
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
5 *
Stephen Warren9e7d5882015-12-07 11:38:50 -07006 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01007 */
8
Lukasz Majewski44a6ac92013-03-05 12:10:17 +01009#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <blk.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010011#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <errno.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010014#include <g_dnl.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Stephen Warren803876d2014-05-05 10:40:15 -060016#include <part.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020017#include <usb.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010018#include <usb_mass_storage.h>
Patrick Delaunay77498e62019-04-18 17:18:39 +020019#include <watchdog.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010022
Stephen Warren803876d2014-05-05 10:40:15 -060023static int ums_read_sector(struct ums *ums_dev,
24 ulong start, lbaint_t blkcnt, void *buf)
25{
Simon Glasse3394752016-02-29 15:25:34 -070026 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060027 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060028
Simon Glass5ee57962016-07-05 17:10:17 -060029 return blk_dread(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060030}
31
32static int ums_write_sector(struct ums *ums_dev,
33 ulong start, lbaint_t blkcnt, const void *buf)
34{
Simon Glasse3394752016-02-29 15:25:34 -070035 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060036 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060037
Simon Glass5ee57962016-07-05 17:10:17 -060038 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060039}
40
Stephen Warren9e7d5882015-12-07 11:38:50 -070041static struct ums *ums;
42static int ums_count;
Stephen Warren803876d2014-05-05 10:40:15 -060043
Stephen Warren9e7d5882015-12-07 11:38:50 -070044static void ums_fini(void)
Stephen Warren803876d2014-05-05 10:40:15 -060045{
Stephen Warren9e7d5882015-12-07 11:38:50 -070046 int i;
47
48 for (i = 0; i < ums_count; i++)
49 free((void *)ums[i].name);
50 free(ums);
Fabio Estevam9b9c3f32016-05-27 22:37:10 -030051 ums = NULL;
Stephen Warren9e7d5882015-12-07 11:38:50 -070052 ums_count = 0;
53}
54
55#define UMS_NAME_LEN 16
56
John Tobias9fc92452016-04-13 12:06:51 -070057static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070058{
John Tobias9fc92452016-04-13 12:06:51 -070059 char *s, *t, *devnum_part_str, *name;
Simon Glasse3394752016-02-29 15:25:34 -070060 struct blk_desc *block_dev;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060061 struct disk_partition info;
John Tobias9fc92452016-04-13 12:06:51 -070062 int partnum;
Tom Rinib55b6fe2016-04-20 10:59:32 -040063 int ret = -1;
Stephen Warren9e7d5882015-12-07 11:38:50 -070064 struct ums *ums_new;
Stephen Warren803876d2014-05-05 10:40:15 -060065
John Tobias9fc92452016-04-13 12:06:51 -070066 s = strdup(devnums_part_str);
Stephen Warren9e7d5882015-12-07 11:38:50 -070067 if (!s)
68 return -1;
69
70 t = s;
71 ums_count = 0;
72
73 for (;;) {
John Tobias9fc92452016-04-13 12:06:51 -070074 devnum_part_str = strsep(&t, ",");
75 if (!devnum_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070076 break;
77
Sean Anderson159b8f52021-10-29 11:05:54 -040078 partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str,
79 &block_dev, &info, 1);
John Tobias9fc92452016-04-13 12:06:51 -070080
81 if (partnum < 0)
Stephen Warren9e7d5882015-12-07 11:38:50 -070082 goto cleanup;
83
John Tobias9fc92452016-04-13 12:06:51 -070084 /* Check if the argument is in legacy format. If yes,
85 * expose all partitions by setting the partnum = 0
86 * e.g. ums 0 mmc 0
87 */
88 if (!strchr(devnum_part_str, ':'))
89 partnum = 0;
90
Stephen Warren9e7d5882015-12-07 11:38:50 -070091 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinib55b6fe2016-04-20 10:59:32 -040092 if (!ums_new)
Stephen Warren9e7d5882015-12-07 11:38:50 -070093 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -070094 ums = ums_new;
95
John Tobias9fc92452016-04-13 12:06:51 -070096 /* if partnum = 0, expose all partitions */
97 if (partnum == 0) {
98 ums[ums_count].start_sector = 0;
99 ums[ums_count].num_sectors = block_dev->lba;
100 } else {
101 ums[ums_count].start_sector = info.start;
102 ums[ums_count].num_sectors = info.size;
103 }
104
Stephen Warren9e7d5882015-12-07 11:38:50 -0700105 ums[ums_count].read_sector = ums_read_sector;
106 ums[ums_count].write_sector = ums_write_sector;
John Tobias9fc92452016-04-13 12:06:51 -0700107
Stephen Warren9e7d5882015-12-07 11:38:50 -0700108 name = malloc(UMS_NAME_LEN);
Tom Rinib55b6fe2016-04-20 10:59:32 -0400109 if (!name)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700110 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -0700111 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
112 ums[ums_count].name = name;
113 ums[ums_count].block_dev = *block_dev;
114
Marek Szyprowski80e832f2020-12-17 11:47:11 +0100115 printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n",
116 ums_count, devtype, ums[ums_count].block_dev.devnum,
Stephen Warren9e7d5882015-12-07 11:38:50 -0700117 ums[ums_count].block_dev.hwpart,
118 ums[ums_count].start_sector,
119 ums[ums_count].num_sectors);
120
121 ums_count++;
122 }
Stephen Warren803876d2014-05-05 10:40:15 -0600123
Tom Rinib55b6fe2016-04-20 10:59:32 -0400124 if (ums_count)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700125 ret = 0;
Stephen Warren5c7648b2014-05-05 10:40:16 -0600126
Stephen Warren9e7d5882015-12-07 11:38:50 -0700127cleanup:
128 free(s);
Stephen Warren803876d2014-05-05 10:40:15 -0600129
Stephen Warren9e7d5882015-12-07 11:38:50 -0700130 if (ret < 0)
131 ums_fini();
Stephen Warren803876d2014-05-05 10:40:15 -0600132
Stephen Warren9e7d5882015-12-07 11:38:50 -0700133 return ret;
Stephen Warren803876d2014-05-05 10:40:15 -0600134}
135
Simon Glassed38aef2020-05-10 11:40:03 -0600136static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
137 int argc, char *const argv[])
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100138{
Stephen Warren5e8c2272014-05-05 10:40:17 -0600139 const char *usb_controller;
140 const char *devtype;
141 const char *devnum;
Stephen Warren5e8c2272014-05-05 10:40:17 -0600142 unsigned int controller_index;
Marek Vasut7786e702023-09-01 11:49:54 +0200143 struct udevice *udc;
Stephen Warren5e8c2272014-05-05 10:40:17 -0600144 int rc;
145 int cable_ready_timeout __maybe_unused;
146
Mateusz Zalegad862f892013-10-04 19:22:26 +0200147 if (argc < 3)
148 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100149
Stephen Warren5e8c2272014-05-05 10:40:17 -0600150 usb_controller = argv[1];
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600151 if (argc >= 4) {
152 devtype = argv[2];
153 devnum = argv[3];
154 } else {
155 devtype = "mmc";
156 devnum = argv[2];
157 }
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200158
Stephen Warren9e7d5882015-12-07 11:38:50 -0700159 rc = ums_init(devtype, devnum);
160 if (rc < 0)
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200161 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100162
Stephen Warren5e8c2272014-05-05 10:40:17 -0600163 controller_index = (unsigned int)(simple_strtoul(
164 usb_controller, NULL, 0));
Marek Vasut7786e702023-09-01 11:49:54 +0200165 rc = udc_device_get_by_index(controller_index, &udc);
166 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900167 pr_err("Couldn't init USB controller.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700168 rc = CMD_RET_FAILURE;
169 goto cleanup_ums_init;
Mateusz Zalegad862f892013-10-04 19:22:26 +0200170 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100171
Marek Vasut7786e702023-09-01 11:49:54 +0200172 rc = fsg_init(ums, ums_count, udc);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100173 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900174 pr_err("fsg_init failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700175 rc = CMD_RET_FAILURE;
176 goto cleanup_board;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100177 }
178
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600179 rc = g_dnl_register("usb_dnl_ums");
180 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900181 pr_err("g_dnl_register failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700182 rc = CMD_RET_FAILURE;
183 goto cleanup_board;
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600184 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100185
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100186 /* Timeout unit: seconds */
Stephen Warren5e8c2272014-05-05 10:40:17 -0600187 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100188
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200189 if (!g_dnl_board_usb_cable_connected()) {
190 /*
191 * Won't execute if we don't know whether the cable is
192 * connected.
193 */
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100194 puts("Please connect USB cable.\n");
195
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200196 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100197 if (ctrlc()) {
198 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700199 rc = CMD_RET_SUCCESS;
200 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100201 }
202 if (!cable_ready_timeout) {
203 puts("\rUSB cable not detected.\n" \
204 "Command exit.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700205 rc = CMD_RET_SUCCESS;
206 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100207 }
208
209 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
210 mdelay(1000);
211 cable_ready_timeout--;
212 }
213 puts("\r\n");
214 }
215
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100216 while (1) {
Marek Vasut7786e702023-09-01 11:49:54 +0200217 dm_usb_gadget_handle_interrupts(udc);
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200218
219 rc = fsg_main_thread(NULL);
220 if (rc) {
221 /* Check I/O error */
222 if (rc == -EIO)
223 printf("\rCheck USB cable connection\n");
224
225 /* Check CTRL+C */
226 if (rc == -EPIPE)
227 printf("\rCTRL+C - Operation aborted\n");
228
Stephen Warren9e7d5882015-12-07 11:38:50 -0700229 rc = CMD_RET_SUCCESS;
230 goto cleanup_register;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200231 }
Patrick Delaunay77498e62019-04-18 17:18:39 +0200232
Svyatoslav Ryhelbb8068d2023-03-20 21:01:43 +0200233 if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) {
234 /* Abort by pressing any key */
235 if (tstc()) {
236 getchar();
237 printf("\rOperation aborted.\n");
238 rc = CMD_RET_SUCCESS;
239 goto cleanup_register;
240 }
241 }
242
Stefan Roese80877fa2022-09-02 14:10:46 +0200243 schedule();
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100244 }
Stephen Warren9e7d5882015-12-07 11:38:50 -0700245
246cleanup_register:
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100247 g_dnl_unregister();
Stephen Warren9e7d5882015-12-07 11:38:50 -0700248cleanup_board:
Marek Vasut7786e702023-09-01 11:49:54 +0200249 udc_device_put(udc);
Stephen Warren9e7d5882015-12-07 11:38:50 -0700250cleanup_ums_init:
251 ums_fini();
252
253 return rc;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100254}
255
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600256U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevam726c1232015-02-28 15:11:46 -0300257 "Use the UMS [USB Mass Storage]",
John Tobias9fc92452016-04-13 12:06:51 -0700258 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600259 " devtype defaults to mmc"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100260);