blob: c3cc1975f9d8b2b0858f6007e89e891ec2e7e2ea [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>
Lukasz Majewski44a6ac92013-03-05 12:10:17 +010021
Stephen Warren803876d2014-05-05 10:40:15 -060022static int ums_read_sector(struct ums *ums_dev,
23 ulong start, lbaint_t blkcnt, void *buf)
24{
Simon Glasse3394752016-02-29 15:25:34 -070025 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060026 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060027
Simon Glass5ee57962016-07-05 17:10:17 -060028 return blk_dread(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060029}
30
31static int ums_write_sector(struct ums *ums_dev,
32 ulong start, lbaint_t blkcnt, const void *buf)
33{
Simon Glasse3394752016-02-29 15:25:34 -070034 struct blk_desc *block_dev = &ums_dev->block_dev;
Stephen Warren803876d2014-05-05 10:40:15 -060035 lbaint_t blkstart = start + ums_dev->start_sector;
Stephen Warren803876d2014-05-05 10:40:15 -060036
Simon Glass5ee57962016-07-05 17:10:17 -060037 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
Stephen Warren803876d2014-05-05 10:40:15 -060038}
39
Stephen Warren9e7d5882015-12-07 11:38:50 -070040static struct ums *ums;
41static int ums_count;
Stephen Warren803876d2014-05-05 10:40:15 -060042
Stephen Warren9e7d5882015-12-07 11:38:50 -070043static void ums_fini(void)
Stephen Warren803876d2014-05-05 10:40:15 -060044{
Stephen Warren9e7d5882015-12-07 11:38:50 -070045 int i;
46
47 for (i = 0; i < ums_count; i++)
48 free((void *)ums[i].name);
49 free(ums);
Fabio Estevam9b9c3f32016-05-27 22:37:10 -030050 ums = NULL;
Stephen Warren9e7d5882015-12-07 11:38:50 -070051 ums_count = 0;
52}
53
54#define UMS_NAME_LEN 16
55
John Tobias9fc92452016-04-13 12:06:51 -070056static int ums_init(const char *devtype, const char *devnums_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070057{
John Tobias9fc92452016-04-13 12:06:51 -070058 char *s, *t, *devnum_part_str, *name;
Simon Glasse3394752016-02-29 15:25:34 -070059 struct blk_desc *block_dev;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060060 struct disk_partition info;
John Tobias9fc92452016-04-13 12:06:51 -070061 int partnum;
Tom Rinib55b6fe2016-04-20 10:59:32 -040062 int ret = -1;
Stephen Warren9e7d5882015-12-07 11:38:50 -070063 struct ums *ums_new;
Stephen Warren803876d2014-05-05 10:40:15 -060064
John Tobias9fc92452016-04-13 12:06:51 -070065 s = strdup(devnums_part_str);
Stephen Warren9e7d5882015-12-07 11:38:50 -070066 if (!s)
67 return -1;
68
69 t = s;
70 ums_count = 0;
71
72 for (;;) {
John Tobias9fc92452016-04-13 12:06:51 -070073 devnum_part_str = strsep(&t, ",");
74 if (!devnum_part_str)
Stephen Warren9e7d5882015-12-07 11:38:50 -070075 break;
76
Sean Anderson159b8f52021-10-29 11:05:54 -040077 partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str,
78 &block_dev, &info, 1);
John Tobias9fc92452016-04-13 12:06:51 -070079
80 if (partnum < 0)
Stephen Warren9e7d5882015-12-07 11:38:50 -070081 goto cleanup;
82
John Tobias9fc92452016-04-13 12:06:51 -070083 /* 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 Warren9e7d5882015-12-07 11:38:50 -070090 /* f_mass_storage.c assumes SECTOR_SIZE sectors */
Tom Rinib55b6fe2016-04-20 10:59:32 -040091 if (block_dev->blksz != SECTOR_SIZE)
Stephen Warren9e7d5882015-12-07 11:38:50 -070092 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -070093
94 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
Tom Rinib55b6fe2016-04-20 10:59:32 -040095 if (!ums_new)
Stephen Warren9e7d5882015-12-07 11:38:50 -070096 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -070097 ums = ums_new;
98
John Tobias9fc92452016-04-13 12:06:51 -070099 /* 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 Warren9e7d5882015-12-07 11:38:50 -0700108 ums[ums_count].read_sector = ums_read_sector;
109 ums[ums_count].write_sector = ums_write_sector;
John Tobias9fc92452016-04-13 12:06:51 -0700110
Stephen Warren9e7d5882015-12-07 11:38:50 -0700111 name = malloc(UMS_NAME_LEN);
Tom Rinib55b6fe2016-04-20 10:59:32 -0400112 if (!name)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700113 goto cleanup;
Stephen Warren9e7d5882015-12-07 11:38:50 -0700114 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 Szyprowski80e832f2020-12-17 11:47:11 +0100118 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 Warren9e7d5882015-12-07 11:38:50 -0700120 ums[ums_count].block_dev.hwpart,
121 ums[ums_count].start_sector,
122 ums[ums_count].num_sectors);
123
124 ums_count++;
125 }
Stephen Warren803876d2014-05-05 10:40:15 -0600126
Tom Rinib55b6fe2016-04-20 10:59:32 -0400127 if (ums_count)
Stephen Warren9e7d5882015-12-07 11:38:50 -0700128 ret = 0;
Stephen Warren5c7648b2014-05-05 10:40:16 -0600129
Stephen Warren9e7d5882015-12-07 11:38:50 -0700130cleanup:
131 free(s);
Stephen Warren803876d2014-05-05 10:40:15 -0600132
Stephen Warren9e7d5882015-12-07 11:38:50 -0700133 if (ret < 0)
134 ums_fini();
Stephen Warren803876d2014-05-05 10:40:15 -0600135
Stephen Warren9e7d5882015-12-07 11:38:50 -0700136 return ret;
Stephen Warren803876d2014-05-05 10:40:15 -0600137}
138
Simon Glassed38aef2020-05-10 11:40:03 -0600139static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
140 int argc, char *const argv[])
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100141{
Stephen Warren5e8c2272014-05-05 10:40:17 -0600142 const char *usb_controller;
143 const char *devtype;
144 const char *devnum;
Stephen Warren5e8c2272014-05-05 10:40:17 -0600145 unsigned int controller_index;
146 int rc;
147 int cable_ready_timeout __maybe_unused;
148
Mateusz Zalegad862f892013-10-04 19:22:26 +0200149 if (argc < 3)
150 return CMD_RET_USAGE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100151
Stephen Warren5e8c2272014-05-05 10:40:17 -0600152 usb_controller = argv[1];
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600153 if (argc >= 4) {
154 devtype = argv[2];
155 devnum = argv[3];
156 } else {
157 devtype = "mmc";
158 devnum = argv[2];
159 }
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200160
Stephen Warren9e7d5882015-12-07 11:38:50 -0700161 rc = ums_init(devtype, devnum);
162 if (rc < 0)
Przemyslaw Marczak0e1e7092013-10-23 14:30:43 +0200163 return CMD_RET_FAILURE;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100164
Stephen Warren5e8c2272014-05-05 10:40:17 -0600165 controller_index = (unsigned int)(simple_strtoul(
166 usb_controller, NULL, 0));
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +0100167 if (usb_gadget_initialize(controller_index)) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900168 pr_err("Couldn't init USB controller.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700169 rc = CMD_RET_FAILURE;
170 goto cleanup_ums_init;
Mateusz Zalegad862f892013-10-04 19:22:26 +0200171 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100172
Ye Libe30ec12020-06-29 10:12:26 +0800173 rc = fsg_init(ums, ums_count, controller_index);
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100174 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900175 pr_err("fsg_init failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700176 rc = CMD_RET_FAILURE;
177 goto cleanup_board;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100178 }
179
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600180 rc = g_dnl_register("usb_dnl_ums");
181 if (rc) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +0900182 pr_err("g_dnl_register failed\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700183 rc = CMD_RET_FAILURE;
184 goto cleanup_board;
Stephen Warrenc611a6b2014-05-01 15:42:10 -0600185 }
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100186
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100187 /* Timeout unit: seconds */
Stephen Warren5e8c2272014-05-05 10:40:17 -0600188 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100189
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200190 if (!g_dnl_board_usb_cable_connected()) {
191 /*
192 * Won't execute if we don't know whether the cable is
193 * connected.
194 */
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100195 puts("Please connect USB cable.\n");
196
Mateusz Zalega21fe3f72014-04-30 13:07:48 +0200197 while (!g_dnl_board_usb_cable_connected()) {
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100198 if (ctrlc()) {
199 puts("\rCTRL+C - Operation aborted.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700200 rc = CMD_RET_SUCCESS;
201 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100202 }
203 if (!cable_ready_timeout) {
204 puts("\rUSB cable not detected.\n" \
205 "Command exit.\n");
Stephen Warren9e7d5882015-12-07 11:38:50 -0700206 rc = CMD_RET_SUCCESS;
207 goto cleanup_register;
Przemyslaw Marczake92cc562014-01-07 15:08:37 +0100208 }
209
210 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
211 mdelay(1000);
212 cable_ready_timeout--;
213 }
214 puts("\r\n");
215 }
216
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100217 while (1) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530218 usb_gadget_handle_interrupts(controller_index);
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200219
220 rc = fsg_main_thread(NULL);
221 if (rc) {
222 /* Check I/O error */
223 if (rc == -EIO)
224 printf("\rCheck USB cable connection\n");
225
226 /* Check CTRL+C */
227 if (rc == -EPIPE)
228 printf("\rCTRL+C - Operation aborted\n");
229
Stephen Warren9e7d5882015-12-07 11:38:50 -0700230 rc = CMD_RET_SUCCESS;
231 goto cleanup_register;
Przemyslaw Marczak06ef7cc2013-10-23 14:30:46 +0200232 }
Patrick Delaunay77498e62019-04-18 17:18:39 +0200233
Svyatoslav Ryhelbb8068d2023-03-20 21:01:43 +0200234 if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) {
235 /* Abort by pressing any key */
236 if (tstc()) {
237 getchar();
238 printf("\rOperation aborted.\n");
239 rc = CMD_RET_SUCCESS;
240 goto cleanup_register;
241 }
242 }
243
Stefan Roese80877fa2022-09-02 14:10:46 +0200244 schedule();
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100245 }
Stephen Warren9e7d5882015-12-07 11:38:50 -0700246
247cleanup_register:
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100248 g_dnl_unregister();
Stephen Warren9e7d5882015-12-07 11:38:50 -0700249cleanup_board:
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +0100250 usb_gadget_release(controller_index);
Stephen Warren9e7d5882015-12-07 11:38:50 -0700251cleanup_ums_init:
252 ums_fini();
253
254 return rc;
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100255}
256
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600257U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
Fabio Estevam726c1232015-02-28 15:11:46 -0300258 "Use the UMS [USB Mass Storage]",
John Tobias9fc92452016-04-13 12:06:51 -0700259 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
Stephen Warrene4e98cd2014-05-05 10:40:18 -0600260 " devtype defaults to mmc"
Lukasz Majewski44a6ac92013-03-05 12:10:17 +0100261);