1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Samsung Electronics
6 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
17 #include <usb_mass_storage.h>
19 #include <linux/delay.h>
20 #include <linux/printk.h>
22 static int ums_read_sector(struct ums *ums_dev,
23 ulong start, lbaint_t blkcnt, void *buf)
25 struct blk_desc *block_dev = &ums_dev->block_dev;
26 lbaint_t blkstart = start + ums_dev->start_sector;
28 return blk_dread(block_dev, blkstart, blkcnt, buf);
31 static int ums_write_sector(struct ums *ums_dev,
32 ulong start, lbaint_t blkcnt, const void *buf)
34 struct blk_desc *block_dev = &ums_dev->block_dev;
35 lbaint_t blkstart = start + ums_dev->start_sector;
37 return blk_dwrite(block_dev, blkstart, blkcnt, buf);
40 static struct ums *ums;
43 static void ums_fini(void)
47 for (i = 0; i < ums_count; i++)
48 free((void *)ums[i].name);
54 #define UMS_NAME_LEN 16
56 static int ums_init(const char *devtype, const char *devnums_part_str)
58 char *s, *t, *devnum_part_str, *name;
59 struct blk_desc *block_dev;
60 struct disk_partition info;
65 s = strdup(devnums_part_str);
73 devnum_part_str = strsep(&t, ",");
77 partnum = part_get_info_by_dev_and_name_or_num(devtype, devnum_part_str,
78 &block_dev, &info, 1);
83 /* Check if the argument is in legacy format. If yes,
84 * expose all partitions by setting the partnum = 0
87 if (!strchr(devnum_part_str, ':'))
90 ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
95 /* if partnum = 0, expose all partitions */
97 ums[ums_count].start_sector = 0;
98 ums[ums_count].num_sectors = block_dev->lba;
100 ums[ums_count].start_sector = info.start;
101 ums[ums_count].num_sectors = info.size;
104 ums[ums_count].read_sector = ums_read_sector;
105 ums[ums_count].write_sector = ums_write_sector;
107 name = malloc(UMS_NAME_LEN);
110 snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
111 ums[ums_count].name = name;
112 ums[ums_count].block_dev = *block_dev;
114 printf("UMS: LUN %d, dev %s %d, hwpart %d, sector %#x, count %#x\n",
115 ums_count, devtype, ums[ums_count].block_dev.devnum,
116 ums[ums_count].block_dev.hwpart,
117 ums[ums_count].start_sector,
118 ums[ums_count].num_sectors);
135 static int do_usb_mass_storage(struct cmd_tbl *cmdtp, int flag,
136 int argc, char *const argv[])
138 const char *usb_controller;
141 unsigned int controller_index;
144 int cable_ready_timeout __maybe_unused;
147 return CMD_RET_USAGE;
149 usb_controller = argv[1];
158 rc = ums_init(devtype, devnum);
160 return CMD_RET_FAILURE;
162 controller_index = (unsigned int)(simple_strtoul(
163 usb_controller, NULL, 0));
164 rc = udc_device_get_by_index(controller_index, &udc);
166 pr_err("Couldn't init USB controller.\n");
167 rc = CMD_RET_FAILURE;
168 goto cleanup_ums_init;
171 rc = fsg_init(ums, ums_count, udc);
173 pr_err("fsg_init failed\n");
174 rc = CMD_RET_FAILURE;
178 rc = g_dnl_register("usb_dnl_ums");
180 pr_err("g_dnl_register failed\n");
181 rc = CMD_RET_FAILURE;
185 /* Timeout unit: seconds */
186 cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
188 if (!g_dnl_board_usb_cable_connected()) {
190 * Won't execute if we don't know whether the cable is
193 puts("Please connect USB cable.\n");
195 while (!g_dnl_board_usb_cable_connected()) {
197 puts("\rCTRL+C - Operation aborted.\n");
198 rc = CMD_RET_SUCCESS;
199 goto cleanup_register;
201 if (!cable_ready_timeout) {
202 puts("\rUSB cable not detected.\n" \
204 rc = CMD_RET_SUCCESS;
205 goto cleanup_register;
208 printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
210 cable_ready_timeout--;
216 dm_usb_gadget_handle_interrupts(udc);
218 rc = fsg_main_thread(NULL);
220 /* Check I/O error */
222 printf("\rCheck USB cable connection\n");
226 printf("\rCTRL+C - Operation aborted\n");
228 rc = CMD_RET_SUCCESS;
229 goto cleanup_register;
232 if (IS_ENABLED(CONFIG_CMD_UMS_ABORT_KEYED)) {
233 /* Abort by pressing any key */
236 printf("\rOperation aborted.\n");
237 rc = CMD_RET_SUCCESS;
238 goto cleanup_register;
255 U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
256 "Use the UMS [USB Mass Storage]",
257 "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
258 " devtype defaults to mmc"