blob: b60373024878ee4712ea931cc8cb5116e3e8b53b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskib528f712013-03-05 12:10:17 +01002/*
3 * Copyright (C) 2011 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
5 *
Stephen Warren02585eb2015-12-07 11:38:50 -07006 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
Lukasz Majewskib528f712013-03-05 12:10:17 +01007 */
8
Lukasz Majewskib528f712013-03-05 12:10:17 +01009#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060010#include <blk.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010011#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <errno.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010014#include <g_dnl.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Stephen Warrenabfe8af2014-05-05 10:40:15 -060016#include <part.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020017#include <usb.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010018#include <usb_mass_storage.h>
Patrick Delaunay85d0aec2019-04-18 17:18:39 +020019#include <watchdog.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010020
Stephen Warrenabfe8af2014-05-05 10:40:15 -060021static int ums_read_sector(struct ums *ums_dev,
22 ulong start, lbaint_t blkcnt, void *buf)
23{
Simon Glass4101f682016-02-29 15:25:34 -070024 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060025 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060026
Simon Glassc9f3c5f2016-07-05 17:10:17 -060027 return blk_dread(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-05-05 10:40:15 -060028}
29
30static int ums_write_sector(struct ums *ums_dev,
31 ulong start, lbaint_t blkcnt, const void *buf)
32{
Simon Glass4101f682016-02-29 15:25:34 -070033 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060034 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060035
Simon Glassc9f3c5f2016-07-05 17:10:17 -060036 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-05-05 10:40:15 -060037}
38
Stephen Warren02585eb2015-12-07 11:38:50 -070039static struct ums *ums;
40static int ums_count;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060041
Stephen Warren02585eb2015-12-07 11:38:50 -070042static void ums_fini(void)
Stephen Warrenabfe8af2014-05-05 10:40:15 -060043{
Stephen Warren02585eb2015-12-07 11:38:50 -070044 int i;
45
46 for (i = 0; i < ums_count; i++)
47 free((void *)ums[i].name);
48 free(ums);
Fabio Estevamd4190262016-05-27 22:37:10 -030049 ums = NULL;
Stephen Warren02585eb2015-12-07 11:38:50 -070050 ums_count = 0;
51}
52
53#define UMS_NAME_LEN 16
54
John Tobiasa2e3a1d2016-04-13 12:06:51 -070055static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070056{
John Tobiasa2e3a1d2016-04-13 12:06:51 -070057 char *s, *t, *devnum_part_str, *name;
Simon Glass4101f682016-02-29 15:25:34 -070058 struct blk_desc *block_dev;
Simon Glass05289792020-05-10 11:39:57 -060059 struct disk_partition info;
John Tobiasa2e3a1d2016-04-13 12:06:51 -070060 int partnum;
Tom Rinia238b0d2016-04-20 10:59:32 -040061 int ret = -1;
Stephen Warren02585eb2015-12-07 11:38:50 -070062 struct ums *ums_new;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060063
John Tobiasa2e3a1d2016-04-13 12:06:51 -070064 s = strdup(devnums_part_str);
Stephen Warren02585eb2015-12-07 11:38:50 -070065 if (!s)
66 return -1;
67
68 t = s;
69 ums_count = 0;
70
71 for (;;) {
John Tobiasa2e3a1d2016-04-13 12:06:51 -070072 devnum_part_str = strsep(&t, ",");
73 if (!devnum_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070074 break;
75
John Tobiasa2e3a1d2016-04-13 12:06:51 -070076 partnum = blk_get_device_part_str(devtype, devnum_part_str,
77 &block_dev, &info, 1);
78
79 if (partnum < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -070080 goto cleanup;
81
John Tobiasa2e3a1d2016-04-13 12:06:51 -070082 /* Check if the argument is in legacy format. If yes,
83 * expose all partitions by setting the partnum = 0
84 * e.g. ums 0 mmc 0
85 */
86 if (!strchr(devnum_part_str, ':'))
87 partnum = 0;
88
Stephen Warren02585eb2015-12-07 11:38:50 -070089 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
Tom Rinia238b0d2016-04-20 10:59:32 -040090 if (block_dev->blksz != SECTOR_SIZE)
Stephen Warren02585eb2015-12-07 11:38:50 -070091 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070092
93 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinia238b0d2016-04-20 10:59:32 -040094 if (!ums_new)
Stephen Warren02585eb2015-12-07 11:38:50 -070095 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070096 ums = ums_new;
97
John Tobiasa2e3a1d2016-04-13 12:06:51 -070098 /* if partnum = 0, expose all partitions */
99 if (partnum == 0) {
100 ums[ums_count].start_sector = 0;
101 ums[ums_count].num_sectors = block_dev->lba;
102 } else {
103 ums[ums_count].start_sector = info.start;
104 ums[ums_count].num_sectors = info.size;
105 }
106
Stephen Warren02585eb2015-12-07 11:38:50 -0700107 ums[ums_count].read_sector = ums_read_sector;
108 ums[ums_count].write_sector = ums_write_sector;
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700109
Stephen Warren02585eb2015-12-07 11:38:50 -0700110 name = malloc(UMS_NAME_LEN);
Tom Rinia238b0d2016-04-20 10:59:32 -0400111 if (!name)
Stephen Warren02585eb2015-12-07 11:38:50 -0700112 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -0700113 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
114 ums[ums_count].name = name;
115 ums[ums_count].block_dev = *block_dev;
116
117 printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700118 ums_count, ums[ums_count].block_dev.devnum,
Stephen Warren02585eb2015-12-07 11:38:50 -0700119 ums[ums_count].block_dev.hwpart,
120 ums[ums_count].start_sector,
121 ums[ums_count].num_sectors);
122
123 ums_count++;
124 }
125
Tom Rinia238b0d2016-04-20 10:59:32 -0400126 if (ums_count)
Stephen Warren02585eb2015-12-07 11:38:50 -0700127 ret = 0;
128
129cleanup:
130 free(s);
131
Stephen Warrend0cc4562014-05-05 10:40:16 -0600132 if (ret < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -0700133 ums_fini();
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600134
Stephen Warren02585eb2015-12-07 11:38:50 -0700135 return ret;
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600136}
137
Fabio Estevamb9203422016-05-27 22:37:11 -0300138static int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
Lukasz Majewskib528f712013-03-05 12:10:17 +0100139 int argc, char * const argv[])
140{
Stephen Warren1725f122014-05-05 10:40:17 -0600141 const char *usb_controller;
142 const char *devtype;
143 const char *devnum;
Stephen Warren1725f122014-05-05 10:40:17 -0600144 unsigned int controller_index;
145 int rc;
146 int cable_ready_timeout __maybe_unused;
147
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200148 if (argc < 3)
149 return CMD_RET_USAGE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100150
Stephen Warren1725f122014-05-05 10:40:17 -0600151 usb_controller = argv[1];
Stephen Warren8c600452014-05-05 10:40:18 -0600152 if (argc >= 4) {
153 devtype = argv[2];
154 devnum = argv[3];
155 } else {
156 devtype = "mmc";
157 devnum = argv[2];
158 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100159
Stephen Warren02585eb2015-12-07 11:38:50 -0700160 rc = ums_init(devtype, devnum);
161 if (rc < 0)
Przemyslaw Marczakf4dacf72013-10-23 14:30:43 +0200162 return CMD_RET_FAILURE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100163
Stephen Warren1725f122014-05-05 10:40:17 -0600164 controller_index = (unsigned int)(simple_strtoul(
165 usb_controller, NULL, 0));
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +0100166 if (usb_gadget_initialize(controller_index)) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900167 pr_err("Couldn't init USB controller.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700168 rc = CMD_RET_FAILURE;
169 goto cleanup_ums_init;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200170 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100171
Stephen Warren02585eb2015-12-07 11:38:50 -0700172 rc = fsg_init(ums, ums_count);
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200173 if (rc) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900174 pr_err("fsg_init failed\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700175 rc = CMD_RET_FAILURE;
176 goto cleanup_board;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200177 }
178
Stephen Warren66b88b02014-05-01 15:42:10 -0600179 rc = g_dnl_register("usb_dnl_ums");
180 if (rc) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900181 pr_err("g_dnl_register failed\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700182 rc = CMD_RET_FAILURE;
183 goto cleanup_board;
Stephen Warren66b88b02014-05-01 15:42:10 -0600184 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100185
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100186 /* Timeout unit: seconds */
Stephen Warren1725f122014-05-05 10:40:17 -0600187 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100188
Mateusz Zalega75504e92014-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 Marczak3603e312014-01-07 15:08:37 +0100194 puts("Please connect USB cable.\n");
195
Mateusz Zalega75504e92014-04-30 13:07:48 +0200196 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100197 if (ctrlc()) {
198 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700199 rc = CMD_RET_SUCCESS;
200 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100201 }
202 if (!cable_ready_timeout) {
203 puts("\rUSB cable not detected.\n" \
204 "Command exit.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700205 rc = CMD_RET_SUCCESS;
206 goto cleanup_register;
Przemyslaw Marczak3603e312014-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 Majewskib528f712013-03-05 12:10:17 +0100216 while (1) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +0530217 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak351e9b22013-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 Warren02585eb2015-12-07 11:38:50 -0700229 rc = CMD_RET_SUCCESS;
230 goto cleanup_register;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100231 }
Patrick Delaunay85d0aec2019-04-18 17:18:39 +0200232
233 WATCHDOG_RESET();
Lukasz Majewskib528f712013-03-05 12:10:17 +0100234 }
Stephen Warren02585eb2015-12-07 11:38:50 -0700235
236cleanup_register:
Lukasz Majewskib528f712013-03-05 12:10:17 +0100237 g_dnl_unregister();
Stephen Warren02585eb2015-12-07 11:38:50 -0700238cleanup_board:
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +0100239 usb_gadget_release(controller_index);
Stephen Warren02585eb2015-12-07 11:38:50 -0700240cleanup_ums_init:
241 ums_fini();
242
243 return rc;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100244}
245
Stephen Warren8c600452014-05-05 10:40:18 -0600246U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevamee02a652015-02-28 15:11:46 -0300247 "Use the UMS [USB Mass Storage]",
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700248 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warren8c600452014-05-05 10:40:18 -0600249 " devtype defaults to mmc"
Lukasz Majewskib528f712013-03-05 12:10:17 +0100250);