cmd_usb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * Most of this source has been derived from the Linux USB
  6. * project.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. *
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <asm/byteorder.h>
  30. #include <part.h>
  31. #include <usb.h>
  32. #ifdef CONFIG_USB_STORAGE
  33. static int usb_stor_curr_dev = -1; /* current device */
  34. #endif
  35. /* some display routines (info command) */
  36. char *usb_get_class_desc(unsigned char dclass)
  37. {
  38. switch (dclass) {
  39. case USB_CLASS_PER_INTERFACE:
  40. return "See Interface";
  41. case USB_CLASS_AUDIO:
  42. return "Audio";
  43. case USB_CLASS_COMM:
  44. return "Communication";
  45. case USB_CLASS_HID:
  46. return "Human Interface";
  47. case USB_CLASS_PRINTER:
  48. return "Printer";
  49. case USB_CLASS_MASS_STORAGE:
  50. return "Mass Storage";
  51. case USB_CLASS_HUB:
  52. return "Hub";
  53. case USB_CLASS_DATA:
  54. return "CDC Data";
  55. case USB_CLASS_VENDOR_SPEC:
  56. return "Vendor specific";
  57. default:
  58. return "";
  59. }
  60. }
  61. void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
  62. unsigned char proto)
  63. {
  64. switch (dclass) {
  65. case USB_CLASS_PER_INTERFACE:
  66. printf("See Interface");
  67. break;
  68. case USB_CLASS_HID:
  69. printf("Human Interface, Subclass: ");
  70. switch (subclass) {
  71. case USB_SUB_HID_NONE:
  72. printf("None");
  73. break;
  74. case USB_SUB_HID_BOOT:
  75. printf("Boot ");
  76. switch (proto) {
  77. case USB_PROT_HID_NONE:
  78. printf("None");
  79. break;
  80. case USB_PROT_HID_KEYBOARD:
  81. printf("Keyboard");
  82. break;
  83. case USB_PROT_HID_MOUSE:
  84. printf("Mouse");
  85. break;
  86. default:
  87. printf("reserved");
  88. break;
  89. }
  90. break;
  91. default:
  92. printf("reserved");
  93. break;
  94. }
  95. break;
  96. case USB_CLASS_MASS_STORAGE:
  97. printf("Mass Storage, ");
  98. switch (subclass) {
  99. case US_SC_RBC:
  100. printf("RBC ");
  101. break;
  102. case US_SC_8020:
  103. printf("SFF-8020i (ATAPI)");
  104. break;
  105. case US_SC_QIC:
  106. printf("QIC-157 (Tape)");
  107. break;
  108. case US_SC_UFI:
  109. printf("UFI");
  110. break;
  111. case US_SC_8070:
  112. printf("SFF-8070");
  113. break;
  114. case US_SC_SCSI:
  115. printf("Transp. SCSI");
  116. break;
  117. default:
  118. printf("reserved");
  119. break;
  120. }
  121. printf(", ");
  122. switch (proto) {
  123. case US_PR_CB:
  124. printf("Command/Bulk");
  125. break;
  126. case US_PR_CBI:
  127. printf("Command/Bulk/Int");
  128. break;
  129. case US_PR_BULK:
  130. printf("Bulk only");
  131. break;
  132. default:
  133. printf("reserved");
  134. break;
  135. }
  136. break;
  137. default:
  138. printf("%s", usb_get_class_desc(dclass));
  139. break;
  140. }
  141. }
  142. void usb_display_string(struct usb_device *dev, int index)
  143. {
  144. char buffer[256];
  145. if (index != 0) {
  146. if (usb_string(dev, index, &buffer[0], 256) > 0)
  147. printf("String: \"%s\"", buffer);
  148. }
  149. }
  150. void usb_display_desc(struct usb_device *dev)
  151. {
  152. if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
  153. printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
  154. usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
  155. (dev->descriptor.bcdUSB>>8) & 0xff,
  156. dev->descriptor.bcdUSB & 0xff);
  157. if (strlen(dev->mf) || strlen(dev->prod) ||
  158. strlen(dev->serial))
  159. printf(" - %s %s %s\n", dev->mf, dev->prod,
  160. dev->serial);
  161. if (dev->descriptor.bDeviceClass) {
  162. printf(" - Class: ");
  163. usb_display_class_sub(dev->descriptor.bDeviceClass,
  164. dev->descriptor.bDeviceSubClass,
  165. dev->descriptor.bDeviceProtocol);
  166. printf("\n");
  167. } else {
  168. printf(" - Class: (from Interface) %s\n",
  169. usb_get_class_desc(
  170. dev->config.if_desc[0].desc.bInterfaceClass));
  171. }
  172. printf(" - PacketSize: %d Configurations: %d\n",
  173. dev->descriptor.bMaxPacketSize0,
  174. dev->descriptor.bNumConfigurations);
  175. printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
  176. dev->descriptor.idVendor, dev->descriptor.idProduct,
  177. (dev->descriptor.bcdDevice>>8) & 0xff,
  178. dev->descriptor.bcdDevice & 0xff);
  179. }
  180. }
  181. void usb_display_conf_desc(struct usb_configuration_descriptor *config,
  182. struct usb_device *dev)
  183. {
  184. printf(" Configuration: %d\n", config->bConfigurationValue);
  185. printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
  186. (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
  187. (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
  188. config->bMaxPower*2);
  189. if (config->iConfiguration) {
  190. printf(" - ");
  191. usb_display_string(dev, config->iConfiguration);
  192. printf("\n");
  193. }
  194. }
  195. void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
  196. struct usb_device *dev)
  197. {
  198. printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
  199. printf(" - Alternate Setting %d, Endpoints: %d\n",
  200. ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
  201. printf(" - Class ");
  202. usb_display_class_sub(ifdesc->bInterfaceClass,
  203. ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
  204. printf("\n");
  205. if (ifdesc->iInterface) {
  206. printf(" - ");
  207. usb_display_string(dev, ifdesc->iInterface);
  208. printf("\n");
  209. }
  210. }
  211. void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
  212. {
  213. printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
  214. (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
  215. switch ((epdesc->bmAttributes & 0x03)) {
  216. case 0:
  217. printf("Control");
  218. break;
  219. case 1:
  220. printf("Isochronous");
  221. break;
  222. case 2:
  223. printf("Bulk");
  224. break;
  225. case 3:
  226. printf("Interrupt");
  227. break;
  228. }
  229. printf(" MaxPacket %d", epdesc->wMaxPacketSize);
  230. if ((epdesc->bmAttributes & 0x03) == 0x3)
  231. printf(" Interval %dms", epdesc->bInterval);
  232. printf("\n");
  233. }
  234. /* main routine to diasplay the configs, interfaces and endpoints */
  235. void usb_display_config(struct usb_device *dev)
  236. {
  237. struct usb_config *config;
  238. struct usb_interface *ifdesc;
  239. struct usb_endpoint_descriptor *epdesc;
  240. int i, ii;
  241. config = &dev->config;
  242. usb_display_conf_desc(&config->desc, dev);
  243. for (i = 0; i < config->no_of_if; i++) {
  244. ifdesc = &config->if_desc[i];
  245. usb_display_if_desc(&ifdesc->desc, dev);
  246. for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
  247. epdesc = &ifdesc->ep_desc[ii];
  248. usb_display_ep_desc(epdesc);
  249. }
  250. }
  251. printf("\n");
  252. }
  253. static inline char *portspeed(int speed)
  254. {
  255. if (speed == USB_SPEED_HIGH)
  256. return "480 Mb/s";
  257. else if (speed == USB_SPEED_LOW)
  258. return "1.5 Mb/s";
  259. else
  260. return "12 Mb/s";
  261. }
  262. /* shows the device tree recursively */
  263. void usb_show_tree_graph(struct usb_device *dev, char *pre)
  264. {
  265. int i, index;
  266. int has_child, last_child, port;
  267. index = strlen(pre);
  268. printf(" %s", pre);
  269. /* check if the device has connected children */
  270. has_child = 0;
  271. for (i = 0; i < dev->maxchild; i++) {
  272. if (dev->children[i] != NULL)
  273. has_child = 1;
  274. }
  275. /* check if we are the last one */
  276. last_child = 1;
  277. if (dev->parent != NULL) {
  278. for (i = 0; i < dev->parent->maxchild; i++) {
  279. /* search for children */
  280. if (dev->parent->children[i] == dev) {
  281. /* found our pointer, see if we have a
  282. * little sister
  283. */
  284. port = i;
  285. while (i++ < dev->parent->maxchild) {
  286. if (dev->parent->children[i] != NULL) {
  287. /* found a sister */
  288. last_child = 0;
  289. break;
  290. } /* if */
  291. } /* while */
  292. } /* device found */
  293. } /* for all children of the parent */
  294. printf("\b+-");
  295. /* correct last child */
  296. if (last_child)
  297. pre[index-1] = ' ';
  298. } /* if not root hub */
  299. else
  300. printf(" ");
  301. printf("%d ", dev->devnum);
  302. pre[index++] = ' ';
  303. pre[index++] = has_child ? '|' : ' ';
  304. pre[index] = 0;
  305. printf(" %s (%s, %dmA)\n", usb_get_class_desc(
  306. dev->config.if_desc[0].desc.bInterfaceClass),
  307. portspeed(dev->speed),
  308. dev->config.desc.bMaxPower * 2);
  309. if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
  310. printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
  311. printf(" %s\n", pre);
  312. if (dev->maxchild > 0) {
  313. for (i = 0; i < dev->maxchild; i++) {
  314. if (dev->children[i] != NULL) {
  315. usb_show_tree_graph(dev->children[i], pre);
  316. pre[index] = 0;
  317. }
  318. }
  319. }
  320. }
  321. /* main routine for the tree command */
  322. void usb_show_tree(struct usb_device *dev)
  323. {
  324. char preamble[32];
  325. memset(preamble, 0, 32);
  326. usb_show_tree_graph(dev, &preamble[0]);
  327. }
  328. /******************************************************************************
  329. * usb boot command intepreter. Derived from diskboot
  330. */
  331. #ifdef CONFIG_USB_STORAGE
  332. int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  333. {
  334. char *boot_device = NULL;
  335. char *ep;
  336. int dev, part = 1, rcode;
  337. ulong addr, cnt;
  338. disk_partition_t info;
  339. image_header_t *hdr;
  340. block_dev_desc_t *stor_dev;
  341. #if defined(CONFIG_FIT)
  342. const void *fit_hdr = NULL;
  343. #endif
  344. switch (argc) {
  345. case 1:
  346. addr = CONFIG_SYS_LOAD_ADDR;
  347. boot_device = getenv("bootdevice");
  348. break;
  349. case 2:
  350. addr = simple_strtoul(argv[1], NULL, 16);
  351. boot_device = getenv("bootdevice");
  352. break;
  353. case 3:
  354. addr = simple_strtoul(argv[1], NULL, 16);
  355. boot_device = argv[2];
  356. break;
  357. default:
  358. return cmd_usage(cmdtp);
  359. }
  360. if (!boot_device) {
  361. puts("\n** No boot device **\n");
  362. return 1;
  363. }
  364. dev = simple_strtoul(boot_device, &ep, 16);
  365. stor_dev = usb_stor_get_dev(dev);
  366. if (stor_dev == NULL || stor_dev->type == DEV_TYPE_UNKNOWN) {
  367. printf("\n** Device %d not available\n", dev);
  368. return 1;
  369. }
  370. if (stor_dev->block_read == NULL) {
  371. printf("storage device not initialized. Use usb scan\n");
  372. return 1;
  373. }
  374. if (*ep) {
  375. if (*ep != ':') {
  376. puts("\n** Invalid boot device, use `dev[:part]' **\n");
  377. return 1;
  378. }
  379. part = simple_strtoul(++ep, NULL, 16);
  380. }
  381. if (get_partition_info(stor_dev, part, &info)) {
  382. /* try to boot raw .... */
  383. strncpy((char *)&info.type[0], BOOT_PART_TYPE,
  384. sizeof(BOOT_PART_TYPE));
  385. strncpy((char *)&info.name[0], "Raw", 4);
  386. info.start = 0;
  387. info.blksz = 0x200;
  388. info.size = 2880;
  389. printf("error reading partinfo...try to boot raw\n");
  390. }
  391. if ((strncmp((char *)info.type, BOOT_PART_TYPE,
  392. sizeof(info.type)) != 0) &&
  393. (strncmp((char *)info.type, BOOT_PART_COMP,
  394. sizeof(info.type)) != 0)) {
  395. printf("\n** Invalid partition type \"%.32s\""
  396. " (expect \"" BOOT_PART_TYPE "\")\n",
  397. info.type);
  398. return 1;
  399. }
  400. printf("\nLoading from USB device %d, partition %d: "
  401. "Name: %.32s Type: %.32s\n",
  402. dev, part, info.name, info.type);
  403. debug("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
  404. info.start, info.size, info.blksz);
  405. if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
  406. printf("** Read error on %d:%d\n", dev, part);
  407. return 1;
  408. }
  409. switch (genimg_get_format((void *)addr)) {
  410. case IMAGE_FORMAT_LEGACY:
  411. hdr = (image_header_t *)addr;
  412. if (!image_check_hcrc(hdr)) {
  413. puts("\n** Bad Header Checksum **\n");
  414. return 1;
  415. }
  416. image_print_contents(hdr);
  417. cnt = image_get_image_size(hdr);
  418. break;
  419. #if defined(CONFIG_FIT)
  420. case IMAGE_FORMAT_FIT:
  421. fit_hdr = (const void *)addr;
  422. puts("Fit image detected...\n");
  423. cnt = fit_get_size(fit_hdr);
  424. break;
  425. #endif
  426. default:
  427. puts("** Unknown image type\n");
  428. return 1;
  429. }
  430. cnt += info.blksz - 1;
  431. cnt /= info.blksz;
  432. cnt -= 1;
  433. if (stor_dev->block_read(dev, info.start+1, cnt,
  434. (ulong *)(addr+info.blksz)) != cnt) {
  435. printf("\n** Read error on %d:%d\n", dev, part);
  436. return 1;
  437. }
  438. #if defined(CONFIG_FIT)
  439. /* This cannot be done earlier, we need complete FIT image in RAM
  440. * first
  441. */
  442. if (genimg_get_format((void *)addr) == IMAGE_FORMAT_FIT) {
  443. if (!fit_check_format(fit_hdr)) {
  444. puts("** Bad FIT image format\n");
  445. return 1;
  446. }
  447. fit_print_contents(fit_hdr);
  448. }
  449. #endif
  450. /* Loading ok, update default load address */
  451. load_addr = addr;
  452. flush_cache(addr, (cnt+1)*info.blksz);
  453. /* Check if we should attempt an auto-start */
  454. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
  455. char *local_args[2];
  456. extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
  457. local_args[0] = argv[0];
  458. local_args[1] = NULL;
  459. printf("Automatic boot of image at addr 0x%08lX ...\n", addr);
  460. rcode = do_bootm(cmdtp, 0, 1, local_args);
  461. return rcode;
  462. }
  463. return 0;
  464. }
  465. #endif /* CONFIG_USB_STORAGE */
  466. /******************************************************************************
  467. * usb command intepreter
  468. */
  469. int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  470. {
  471. int i;
  472. struct usb_device *dev = NULL;
  473. extern char usb_started;
  474. #ifdef CONFIG_USB_STORAGE
  475. block_dev_desc_t *stor_dev;
  476. #endif
  477. if (argc < 2)
  478. return cmd_usage(cmdtp);
  479. if ((strncmp(argv[1], "reset", 5) == 0) ||
  480. (strncmp(argv[1], "start", 5) == 0)) {
  481. usb_stop();
  482. printf("(Re)start USB...\n");
  483. i = usb_init();
  484. #ifdef CONFIG_USB_STORAGE
  485. /* try to recognize storage devices immediately */
  486. if (i >= 0)
  487. usb_stor_curr_dev = usb_stor_scan(1);
  488. #endif
  489. return 0;
  490. }
  491. if (strncmp(argv[1], "stop", 4) == 0) {
  492. #ifdef CONFIG_USB_KEYBOARD
  493. if (argc == 2) {
  494. if (usb_kbd_deregister() != 0) {
  495. printf("USB not stopped: usbkbd still"
  496. " using USB\n");
  497. return 1;
  498. }
  499. } else {
  500. /* forced stop, switch console in to serial */
  501. console_assign(stdin, "serial");
  502. usb_kbd_deregister();
  503. }
  504. #endif
  505. printf("stopping USB..\n");
  506. usb_stop();
  507. return 0;
  508. }
  509. if (!usb_started) {
  510. printf("USB is stopped. Please issue 'usb start' first.\n");
  511. return 1;
  512. }
  513. if (strncmp(argv[1], "tree", 4) == 0) {
  514. printf("\nDevice Tree:\n");
  515. usb_show_tree(usb_get_dev_index(0));
  516. return 0;
  517. }
  518. if (strncmp(argv[1], "inf", 3) == 0) {
  519. int d;
  520. if (argc == 2) {
  521. for (d = 0; d < USB_MAX_DEVICE; d++) {
  522. dev = usb_get_dev_index(d);
  523. if (dev == NULL)
  524. break;
  525. usb_display_desc(dev);
  526. usb_display_config(dev);
  527. }
  528. return 0;
  529. } else {
  530. int d;
  531. i = simple_strtoul(argv[2], NULL, 16);
  532. printf("config for device %d\n", i);
  533. for (d = 0; d < USB_MAX_DEVICE; d++) {
  534. dev = usb_get_dev_index(d);
  535. if (dev == NULL)
  536. break;
  537. if (dev->devnum == i)
  538. break;
  539. }
  540. if (dev == NULL) {
  541. printf("*** NO Device avaiable ***\n");
  542. return 0;
  543. } else {
  544. usb_display_desc(dev);
  545. usb_display_config(dev);
  546. }
  547. }
  548. return 0;
  549. }
  550. #ifdef CONFIG_USB_STORAGE
  551. if (strncmp(argv[1], "stor", 4) == 0)
  552. return usb_stor_info();
  553. if (strncmp(argv[1], "part", 4) == 0) {
  554. int devno, ok = 0;
  555. if (argc == 2) {
  556. for (devno = 0; ; ++devno) {
  557. stor_dev = usb_stor_get_dev(devno);
  558. if (stor_dev == NULL)
  559. break;
  560. if (stor_dev->type != DEV_TYPE_UNKNOWN) {
  561. ok++;
  562. if (devno)
  563. printf("\n");
  564. debug("print_part of %x\n", devno);
  565. print_part(stor_dev);
  566. }
  567. }
  568. } else {
  569. devno = simple_strtoul(argv[2], NULL, 16);
  570. stor_dev = usb_stor_get_dev(devno);
  571. if (stor_dev != NULL &&
  572. stor_dev->type != DEV_TYPE_UNKNOWN) {
  573. ok++;
  574. debug("print_part of %x\n", devno);
  575. print_part(stor_dev);
  576. }
  577. }
  578. if (!ok) {
  579. printf("\nno USB devices available\n");
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. if (strcmp(argv[1], "read") == 0) {
  585. if (usb_stor_curr_dev < 0) {
  586. printf("no current device selected\n");
  587. return 1;
  588. }
  589. if (argc == 5) {
  590. unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  591. unsigned long blk = simple_strtoul(argv[3], NULL, 16);
  592. unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
  593. unsigned long n;
  594. printf("\nUSB read: device %d block # %ld, count %ld"
  595. " ... ", usb_stor_curr_dev, blk, cnt);
  596. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  597. n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
  598. (ulong *)addr);
  599. printf("%ld blocks read: %s\n", n,
  600. (n == cnt) ? "OK" : "ERROR");
  601. if (n == cnt)
  602. return 0;
  603. return 1;
  604. }
  605. }
  606. if (strcmp(argv[1], "write") == 0) {
  607. if (usb_stor_curr_dev < 0) {
  608. printf("no current device selected\n");
  609. return 1;
  610. }
  611. if (argc == 5) {
  612. unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  613. unsigned long blk = simple_strtoul(argv[3], NULL, 16);
  614. unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
  615. unsigned long n;
  616. printf("\nUSB write: device %d block # %ld, count %ld"
  617. " ... ", usb_stor_curr_dev, blk, cnt);
  618. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  619. n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
  620. (ulong *)addr);
  621. printf("%ld blocks write: %s\n", n,
  622. (n == cnt) ? "OK" : "ERROR");
  623. if (n == cnt)
  624. return 0;
  625. return 1;
  626. }
  627. }
  628. if (strncmp(argv[1], "dev", 3) == 0) {
  629. if (argc == 3) {
  630. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  631. printf("\nUSB device %d: ", dev);
  632. stor_dev = usb_stor_get_dev(dev);
  633. if (stor_dev == NULL) {
  634. printf("unknown device\n");
  635. return 1;
  636. }
  637. printf("\n Device %d: ", dev);
  638. dev_print(stor_dev);
  639. if (stor_dev->type == DEV_TYPE_UNKNOWN)
  640. return 1;
  641. usb_stor_curr_dev = dev;
  642. printf("... is now current device\n");
  643. return 0;
  644. } else {
  645. printf("\nUSB device %d: ", usb_stor_curr_dev);
  646. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  647. dev_print(stor_dev);
  648. if (stor_dev->type == DEV_TYPE_UNKNOWN)
  649. return 1;
  650. return 0;
  651. }
  652. return 0;
  653. }
  654. #endif /* CONFIG_USB_STORAGE */
  655. return cmd_usage(cmdtp);
  656. }
  657. #ifdef CONFIG_USB_STORAGE
  658. U_BOOT_CMD(
  659. usb, 5, 1, do_usb,
  660. "USB sub-system",
  661. "reset - reset (rescan) USB controller\n"
  662. "usb stop [f] - stop USB [f]=force stop\n"
  663. "usb tree - show USB device tree\n"
  664. "usb info [dev] - show available USB devices\n"
  665. "usb storage - show details of USB storage devices\n"
  666. "usb dev [dev] - show or set current USB storage device\n"
  667. "usb part [dev] - print partition table of one or all USB storage"
  668. " devices\n"
  669. "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
  670. " to memory address `addr'"
  671. "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
  672. " from memory address `addr'"
  673. );
  674. U_BOOT_CMD(
  675. usbboot, 3, 1, do_usbboot,
  676. "boot from USB device",
  677. "loadAddr dev:part"
  678. );
  679. #else
  680. U_BOOT_CMD(
  681. usb, 5, 1, do_usb,
  682. "USB sub-system",
  683. "reset - reset (rescan) USB controller\n"
  684. "usb tree - show USB device tree\n"
  685. "usb info [dev] - show available USB devices"
  686. );
  687. #endif