blob: a8ddeb4946289e5769a72cdf92a58660307335e4 [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>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060021#include <linux/printk.h>
Lukasz Majewskib528f712013-03-05 12:10:17 +010022
Stephen Warrenabfe8af2014-05-05 10:40:15 -060023static int ums_read_sector(struct ums *ums_dev,
24 ulong start, lbaint_t blkcnt, void *buf)
25{
Simon Glass4101f682016-02-29 15:25:34 -070026 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060027 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060028
Simon Glassc9f3c5f2016-07-05 17:10:17 -060029 return blk_dread(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-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 Glass4101f682016-02-29 15:25:34 -070035 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060036 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060037
Simon Glassc9f3c5f2016-07-05 17:10:17 -060038 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
Stephen Warrenabfe8af2014-05-05 10:40:15 -060039}
40
Stephen Warren02585eb2015-12-07 11:38:50 -070041static struct ums *ums;
42static int ums_count;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060043
Stephen Warren02585eb2015-12-07 11:38:50 -070044static void ums_fini(void)
Stephen Warrenabfe8af2014-05-05 10:40:15 -060045{
Stephen Warren02585eb2015-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 Estevamd4190262016-05-27 22:37:10 -030051 ums = NULL;
Stephen Warren02585eb2015-12-07 11:38:50 -070052 ums_count = 0;
53}
54
55#define UMS_NAME_LEN 16
56
John Tobiasa2e3a1d2016-04-13 12:06:51 -070057static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070058{
John Tobiasa2e3a1d2016-04-13 12:06:51 -070059 char *s, *t, *devnum_part_str, *name;
Simon Glass4101f682016-02-29 15:25:34 -070060 struct blk_desc *block_dev;
Simon Glass05289792020-05-10 11:39:57 -060061 struct disk_partition info;
John Tobiasa2e3a1d2016-04-13 12:06:51 -070062 int partnum;
Tom Rinia238b0d2016-04-20 10:59:32 -040063 int ret = -1;
Stephen Warren02585eb2015-12-07 11:38:50 -070064 struct ums *ums_new;
Stephen Warrenabfe8af2014-05-05 10:40:15 -060065
John Tobiasa2e3a1d2016-04-13 12:06:51 -070066 s = strdup(devnums_part_str);
Stephen Warren02585eb2015-12-07 11:38:50 -070067 if (!s)
68 return -1;
69
70 t = s;
71 ums_count = 0;
72
73 for (;;) {
John Tobiasa2e3a1d2016-04-13 12:06:51 -070074 devnum_part_str = strsep(&t, ",");
75 if (!devnum_part_str)
Stephen Warren02585eb2015-12-07 11:38:50 -070076 break;
77
Sean Anderson1833e682021-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 Tobiasa2e3a1d2016-04-13 12:06:51 -070080
81 if (partnum < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -070082 goto cleanup;
83
John Tobiasa2e3a1d2016-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 Warren02585eb2015-12-07 11:38:50 -070091 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
Tom Rinia238b0d2016-04-20 10:59:32 -040092 if (block_dev->blksz != SECTOR_SIZE)
Stephen Warren02585eb2015-12-07 11:38:50 -070093 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070094
95 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinia238b0d2016-04-20 10:59:32 -040096 if (!ums_new)
Stephen Warren02585eb2015-12-07 11:38:50 -070097 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -070098 ums = ums_new;
99
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700100 /* if partnum = 0, expose all partitions */
101 if (partnum == 0) {
102 ums[ums_count].start_sector = 0;
103 ums[ums_count].num_sectors = block_dev->lba;
104 } else {
105 ums[ums_count].start_sector = info.start;
106 ums[ums_count].num_sectors = info.size;
107 }
108
Stephen Warren02585eb2015-12-07 11:38:50 -0700109 ums[ums_count].read_sector = ums_read_sector;
110 ums[ums_count].write_sector = ums_write_sector;
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700111
Stephen Warren02585eb2015-12-07 11:38:50 -0700112 name = malloc(UMS_NAME_LEN);
Tom Rinia238b0d2016-04-20 10:59:32 -0400113 if (!name)
Stephen Warren02585eb2015-12-07 11:38:50 -0700114 goto cleanup;
Stephen Warren02585eb2015-12-07 11:38:50 -0700115 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
116 ums[ums_count].name = name;
117 ums[ums_count].block_dev = *block_dev;
118
Marek Szyprowskic954ff52020-12-17 11:47:11 +0100119 printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n",
120 ums_count, devtype, ums[ums_count].block_dev.devnum,
Stephen Warren02585eb2015-12-07 11:38:50 -0700121 ums[ums_count].block_dev.hwpart,
122 ums[ums_count].start_sector,
123 ums[ums_count].num_sectors);
124
125 ums_count++;
126 }
127
Tom Rinia238b0d2016-04-20 10:59:32 -0400128 if (ums_count)
Stephen Warren02585eb2015-12-07 11:38:50 -0700129 ret = 0;
130
131cleanup:
132 free(s);
133
Stephen Warrend0cc4562014-05-05 10:40:16 -0600134 if (ret < 0)
Stephen Warren02585eb2015-12-07 11:38:50 -0700135 ums_fini();
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600136
Stephen Warren02585eb2015-12-07 11:38:50 -0700137 return ret;
Stephen Warrenabfe8af2014-05-05 10:40:15 -0600138}
139
Simon Glass09140112020-05-10 11:40:03 -0600140static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
141 int argc, char *const argv[])
Lukasz Majewskib528f712013-03-05 12:10:17 +0100142{
Stephen Warren1725f122014-05-05 10:40:17 -0600143 const char *usb_controller;
144 const char *devtype;
145 const char *devnum;
Stephen Warren1725f122014-05-05 10:40:17 -0600146 unsigned int controller_index;
Marek Vasutf0322602023-09-01 11:49:54 +0200147 struct udevice *udc;
Stephen Warren1725f122014-05-05 10:40:17 -0600148 int rc;
149 int cable_ready_timeout __maybe_unused;
150
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200151 if (argc < 3)
152 return CMD_RET_USAGE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100153
Stephen Warren1725f122014-05-05 10:40:17 -0600154 usb_controller = argv[1];
Stephen Warren8c600452014-05-05 10:40:18 -0600155 if (argc >= 4) {
156 devtype = argv[2];
157 devnum = argv[3];
158 } else {
159 devtype = "mmc";
160 devnum = argv[2];
161 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100162
Stephen Warren02585eb2015-12-07 11:38:50 -0700163 rc = ums_init(devtype, devnum);
164 if (rc < 0)
Przemyslaw Marczakf4dacf72013-10-23 14:30:43 +0200165 return CMD_RET_FAILURE;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100166
Stephen Warren1725f122014-05-05 10:40:17 -0600167 controller_index = (unsigned int)(simple_strtoul(
168 usb_controller, NULL, 0));
Marek Vasutf0322602023-09-01 11:49:54 +0200169 rc = udc_device_get_by_index(controller_index, &udc);
170 if (rc) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900171 pr_err("Couldn't init USB controller.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700172 rc = CMD_RET_FAILURE;
173 goto cleanup_ums_init;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200174 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100175
Marek Vasutf0322602023-09-01 11:49:54 +0200176 rc = fsg_init(ums, ums_count, udc);
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200177 if (rc) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900178 pr_err("fsg_init failed\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700179 rc = CMD_RET_FAILURE;
180 goto cleanup_board;
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200181 }
182
Stephen Warren66b88b02014-05-01 15:42:10 -0600183 rc = g_dnl_register("usb_dnl_ums");
184 if (rc) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +0900185 pr_err("g_dnl_register failed\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700186 rc = CMD_RET_FAILURE;
187 goto cleanup_board;
Stephen Warren66b88b02014-05-01 15:42:10 -0600188 }
Lukasz Majewskib528f712013-03-05 12:10:17 +0100189
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100190 /* Timeout unit: seconds */
Stephen Warren1725f122014-05-05 10:40:17 -0600191 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100192
Mateusz Zalega75504e92014-04-30 13:07:48 +0200193 if (!g_dnl_board_usb_cable_connected()) {
194 /*
195 * Won't execute if we don't know whether the cable is
196 * connected.
197 */
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100198 puts("Please connect USB cable.\n");
199
Mateusz Zalega75504e92014-04-30 13:07:48 +0200200 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100201 if (ctrlc()) {
202 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700203 rc = CMD_RET_SUCCESS;
204 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100205 }
206 if (!cable_ready_timeout) {
207 puts("\rUSB cable not detected.\n" \
208 "Command exit.\n");
Stephen Warren02585eb2015-12-07 11:38:50 -0700209 rc = CMD_RET_SUCCESS;
210 goto cleanup_register;
Przemyslaw Marczak3603e312014-01-07 15:08:37 +0100211 }
212
213 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
214 mdelay(1000);
215 cable_ready_timeout--;
216 }
217 puts("\r\n");
218 }
219
Lukasz Majewskib528f712013-03-05 12:10:17 +0100220 while (1) {
Marek Vasutf0322602023-09-01 11:49:54 +0200221 dm_usb_gadget_handle_interrupts(udc);
Przemyslaw Marczak351e9b22013-10-23 14:30:46 +0200222
223 rc = fsg_main_thread(NULL);
224 if (rc) {
225 /* Check I/O error */
226 if (rc == -EIO)
227 printf("\rCheck USB cable connection\n");
228
229 /* Check CTRL+C */
230 if (rc == -EPIPE)
231 printf("\rCTRL+C - Operation aborted\n");
232
Stephen Warren02585eb2015-12-07 11:38:50 -0700233 rc = CMD_RET_SUCCESS;
234 goto cleanup_register;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100235 }
Patrick Delaunay85d0aec2019-04-18 17:18:39 +0200236
Svyatoslav Ryhela57adac2023-03-20 21:01:43 +0200237 if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) {
238 /* Abort by pressing any key */
239 if (tstc()) {
240 getchar();
241 printf("\rOperation aborted.\n");
242 rc = CMD_RET_SUCCESS;
243 goto cleanup_register;
244 }
245 }
246
Stefan Roese29caf932022-09-02 14:10:46 +0200247 schedule();
Lukasz Majewskib528f712013-03-05 12:10:17 +0100248 }
Stephen Warren02585eb2015-12-07 11:38:50 -0700249
250cleanup_register:
Lukasz Majewskib528f712013-03-05 12:10:17 +0100251 g_dnl_unregister();
Stephen Warren02585eb2015-12-07 11:38:50 -0700252cleanup_board:
Marek Vasutf0322602023-09-01 11:49:54 +0200253 udc_device_put(udc);
Stephen Warren02585eb2015-12-07 11:38:50 -0700254cleanup_ums_init:
255 ums_fini();
256
257 return rc;
Lukasz Majewskib528f712013-03-05 12:10:17 +0100258}
259
Stephen Warren8c600452014-05-05 10:40:18 -0600260U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevamee02a652015-02-28 15:11:46 -0300261 "Use the UMS [USB Mass Storage]",
John Tobiasa2e3a1d2016-04-13 12:06:51 -0700262 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warren8c600452014-05-05 10:40:18 -0600263 " devtype defaults to mmc"
Lukasz Majewskib528f712013-03-05 12:10:17 +0100264);