27c155d0867c99f3dab054314eb8c1c74559e798
[kernel/u-boot.git] / common / cmd_usbd.c
1 /*
2  * USB Downloader for SAMSUNG Platform
3  *
4  * Copyright (C) 2007-2008 Samsung Electronics
5  * Minkyu Kang <mk7.kang@samsung.com>
6  *
7  */
8
9 #include <common.h>
10 #include <usbd.h>
11 #include <asm/errno.h>
12 #include <malloc.h>
13
14 /* version of USB Downloader Application */
15 #define APP_VERSION     "1.6.0"
16
17 #define OPS_READ        0
18 #define OPS_WRITE       1
19
20 #ifdef CONFIG_CMD_MTDPARTS
21 #include <jffs2/load_kernel.h>
22 static struct part_info *parts[16];
23 #endif
24
25 #ifdef CONFIG_UBIFS_MK
26 #include <mkfs.ubifs.h>
27 #endif
28
29 #ifdef CONFIG_UBINIZE
30 #include <ubinize.h>
31 #endif
32
33 #include <mbr.h>
34
35 static const char pszMe[] = "usbd: ";
36
37 static struct usbd_ops usbd_ops;
38
39 static unsigned int part_id;
40 static unsigned int write_part = 0;
41 static unsigned int fs_offset = 0x0;
42
43 #ifdef CONFIG_USE_YAFFS
44 static unsigned int yaffs_len = 0;
45 static unsigned char yaffs_data[2112];
46 #define YAFFS_PAGE 2112
47 #endif
48
49 #define NAND_PAGE_SIZE 2048
50
51 static unsigned long down_ram_addr;
52
53 static int down_mode;
54
55 /* cpu/${CPU} dependent */
56 extern void do_reset(void);
57
58 /* common commands */
59 extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60 extern int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
61
62 int mtdparts_init(void);
63 int find_dev_and_part(const char*, struct mtd_device**, u8*, struct part_info**);
64
65 /* common/cmd_jffs2.c */
66 extern struct list_head devices;
67
68 static u8 count_mtdparts(void)
69 {
70         struct list_head *dentry, *pentry;
71         struct mtd_device *dev;
72         u8 part_num = 0;
73
74         list_for_each(dentry, &devices) {
75                 dev = list_entry(dentry, struct mtd_device, link);
76
77                 /* list partitions for given device */
78                 list_for_each(pentry, &dev->parts)
79                         part_num++;
80         }
81
82         return part_num;
83 }
84
85 #ifdef CONFIG_CMD_UBI
86 static int check_ubi_mode(void)
87 {
88         char *env_ubifs;
89         int ubi_mode;
90
91         env_ubifs = getenv("ubi");
92         ubi_mode = !strcmp(env_ubifs, "enabled");
93
94         return ubi_mode;
95 }
96 #else
97 #define check_ubi_mode()                0
98 #endif
99
100 #ifdef CONFIG_MTD_PARTITIONS
101 static int get_part_info(void)
102 {
103         struct mtd_device *dev;
104         u8 out_partnum;
105         char part_name[12];
106         char nand_name[12];
107         int i;
108         int part_num;
109         int ubi_mode = 0;
110
111 #if defined(CONFIG_CMD_NAND)
112         sprintf(nand_name, "nand0");
113 #elif defined(CONFIG_CMD_ONENAND)
114         sprintf(nand_name, "onenand0");
115 #else
116         printf("Configure your NAND sub-system\n");
117         return 0;
118 #endif
119
120         if (mtdparts_init())
121                 return 0;
122
123         ubi_mode = check_ubi_mode();
124
125         part_num = count_mtdparts();
126         for (i = 0; i < part_num; i++) {
127                 sprintf(part_name, "%s,%d", nand_name, i);
128
129                 if (find_dev_and_part(part_name, &dev, &out_partnum, &parts[i]))
130                         return -EINVAL;
131         }
132
133         return 0;
134 }
135
136 static int get_part_id(char *name)
137 {
138         int nparts = count_mtdparts();
139         int i;
140
141         for (i = 0; i < nparts; i++) {
142                 if (strcmp(parts[i]->name, name) == 0)
143                         return i;
144         }
145
146         printf("Error: Unknown partition -> %s\n", name);
147         return -1;
148 }
149 #else
150 static int get_part_info(void)
151 {
152         printf("Error: Can't get patition information\n");
153         return -EINVAL;
154 }
155
156 static int get_part_id(char *name)
157 {
158         return 0;
159 }
160 #endif
161
162 static void boot_cmd(char *addr)
163 {
164         char *argv[] = { "bootm", addr };
165         do_bootm(NULL, 0, 2, argv);
166 }
167
168 #if defined(CONFIG_CMD_NAND)
169 extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
170 #elif defined(CONFIG_CMD_ONENAND)
171 extern int do_onenand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
172 #endif
173
174 /* NAND erase and write using nand command */
175 static int nand_cmd(int type, char *p1, char *p2, char *p3)
176 {
177         int ret = 1;
178         char nand_name[12];
179         int (*nand_func) (cmd_tbl_t *, int, int, char **);
180
181 #if defined(CONFIG_CMD_NAND)
182         sprintf(nand_name, "nand");
183         nand_func = do_nand;
184 #elif defined(CONFIG_CMD_ONENAND)
185         sprintf(nand_name, "onenand");
186         nand_func = do_onenand;
187 #else
188         printf("Configure your NAND sub-system\n");
189         return 0;
190 #endif
191
192         if (type == 0) {
193                 char *argv[] = {nand_name, "erase", p1, p2};
194                 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
195                 ret = nand_func(NULL, 0, 4, argv);
196         } else if (type == 1) {
197                 char *argv[] = {nand_name, "write", p1, p2, p3};
198                 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
199                                 argv[3], argv[4]);
200                 ret = nand_func(NULL, 0, 5, argv);
201         } else if (type == 2) {
202                 char *argv[] = {nand_name, "write.yaffs", p1, p2, p3};
203                 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
204                                 argv[3], argv[4]);
205                 ret = nand_func(NULL, 0, 5, argv);
206         } else if (type == 3) {
207                 char *argv[] = {nand_name, "lock", p1, p2};
208                 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
209                 ret = nand_func(NULL, 0, 4, argv);
210         }
211
212         if (ret)
213                 printf("Error: NAND Command\n");
214
215         return ret;
216 }
217
218 #ifdef CONFIG_CMD_UBI
219 int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
220
221 int ubi_cmd(int part, char *p1, char *p2, char *p3)
222 {
223         int ret = 1;
224
225         if (part == RAMDISK_PART_ID) {
226                 char *argv[] = {"ubi", "write", p1, "rootfs.cramfs", p2, p3};
227                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
228                                 argv[3], argv[4], argv[5]);
229                 ret = do_ubi(NULL, 0, 6, argv);
230         } else if (part == FILESYSTEM_PART_ID) {
231                 char *argv[] = {"ubi", "write", p1, "factoryfs.cramfs", p2, p3};
232                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
233                                 argv[3], argv[4], argv[5]);
234                 ret = do_ubi(NULL, 0, 6, argv);
235         } else if (part == FILESYSTEM2_PART_ID) {
236                 char *argv[] = {"ubi", "write", p1, "datafs.ubifs", p2, p3};
237                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
238                                 argv[3], argv[4], argv[5]);
239                 ret = do_ubi(NULL, 0, 6, argv);
240         }
241
242         return ret;
243 }
244 #endif
245
246 #ifdef CONFIG_CMD_MMC
247 #include <mmc.h>
248 #include <fat.h>
249
250 static struct mmc *mmc;
251
252 static int mmc_cmd(int ops, int dev_num, ulong start, lbaint_t cnt, void *addr)
253 {
254         int ret;
255
256         if (ops) {
257                 printf("mmc write 0x%x 0x%x\n", start, cnt);
258                 ret = mmc->block_dev.block_write(dev_num, start, cnt, addr);
259         } else {
260                 printf("mmc read 0x%x 0x%x\n", start, cnt);
261                 ret = mmc->block_dev.block_read(dev_num, start, cnt, addr);
262         }
263
264         if (ret > 0)
265                 ret = 0;
266         else
267                 ret = 1;
268
269         return ret;
270 }
271
272 #define NORMAL_PARTITION        0
273 #define EXTEND_PARTITION        1
274
275 #define EXTEND_PART_TYPE        5
276
277 #define EXTEND_MAX_PART         32
278
279 static unsigned int cur_blk_offset;
280 static unsigned int cur_part_size;
281 static unsigned int cur_part;
282
283 static unsigned int mmc_parts;
284 static unsigned int mmc_part_write;
285
286 static u8 part_mode = 0;
287
288 struct partition_info {
289         u32 size;
290         u32 checksum;
291         u32 res;
292 } __attribute__((packed));
293
294 struct partition_header {
295         u8                      fat32head[16];  /* RFSHEAD identifier */
296         struct partition_info   partition[EXTEND_MAX_PART];
297         u8                      res[112];       /* only data (without MBR) */
298 } __attribute__((packed));
299
300 struct mul_partition_info {
301         u32 lba_begin;          /* absolute address from 0 block */
302         u32 num_sectors;
303 } __attribute__((packed));
304
305 #define MBR_OFFSET      0x10
306
307 struct partition_header part_info;
308 struct mbr mbr_info;
309 struct mul_partition_info mul_info[EXTEND_MAX_PART];
310
311 static int write_mmc_partition(struct usbd_ops *usbd, u32 *ram_addr, u32 len)
312 {
313         unsigned int blocks;
314         int i;
315         int loop;
316         u32 cnt;
317         int ret;
318
319         if (cur_part_size > len) {
320                 blocks = len / usbd->mmc_blk;
321                 ret = -1;
322         } else {
323                 blocks = cur_part_size / usbd->mmc_blk;
324                 ret = len - cur_part_size;
325         }
326
327         if (len % usbd->mmc_blk)
328                 blocks++;
329
330         loop = blocks / usbd->mmc_max;
331         if (blocks % usbd->mmc_max)
332                 loop++;
333
334         for (i = 0; i < loop; i++) {
335                 if (i == 0) {
336                         cnt = blocks % usbd->mmc_max;
337                         if (cnt == 0)
338                                 cnt = usbd->mmc_max;
339                 } else {
340                         cnt = usbd->mmc_max;
341                 }
342
343                 mmc_cmd(OPS_WRITE, usbd->mmc_dev, cur_blk_offset,
344                                 cnt, (void *)*ram_addr);
345
346                 cur_blk_offset += cnt;
347
348                 *ram_addr += (cnt * usbd->mmc_blk);
349         }
350
351         return ret;
352 }
353
354 static int write_file_mmc(struct usbd_ops *usbd, u32 len)
355 {
356         u32 ram_addr;
357         int i;
358         int ret;
359         struct mbr *mbr;
360
361         if (!usbd->mmc_total) {
362                 printf("MMC is not supported!\n");
363                 return 0;
364         }
365
366         ram_addr = (u32)down_ram_addr;
367
368         if (cur_blk_offset == 0) {
369                 boot_sector *bs;
370                 u32 mbr_blk_size;
371
372                 memcpy(&part_info, (void *)ram_addr,
373                                 sizeof(struct partition_header));
374
375                 ram_addr += sizeof(struct partition_header);
376                 len -= sizeof(struct partition_header);
377                 mbr = (struct mbr *)ram_addr;
378                 mbr_blk_size = mbr->parts[0].lba;
379
380                 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
381                         part_mode = NORMAL_PARTITION;
382
383                         /* modify sectors of p1 */
384                         mbr->parts[0].nsectors = usbd->mmc_total -
385                                         (mbr_blk_size +
386                                         mbr->parts[1].nsectors +
387                                         mbr->parts[2].nsectors +
388                                         mbr->parts[3].nsectors);
389
390                         mmc_parts++;
391
392                         /* modify lba_begin of p2 and p3 and p4 */
393                         for (i = 1; i < 4; i++) {
394                                 if (part_info.partition[i].size == 0)
395                                         break;
396
397                                 mmc_parts++;
398                                 mbr->parts[i].lba =
399                                         mbr->parts[i - 1].lba +
400                                         mbr->parts[i - 1].nsectors;
401                         }
402
403                         /* copy MBR */
404                         memcpy(&mbr_info, mbr, sizeof(struct mbr));
405
406                         printf("Total Size: 0x%08x #parts %d\n",
407                                         (unsigned int)usbd->mmc_total,
408                                         mmc_parts);
409                         for (i = 0; i < mmc_parts; i++) {
410                                 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
411                                         mbr_info.parts[i].lba,
412                                         mbr_info.parts[i].nsectors);
413                         }
414
415                         /* write MBR */
416                         mmc_cmd(OPS_WRITE, usbd->mmc_dev, 0,
417                                         mbr_blk_size, (void *)ram_addr);
418
419                         ram_addr += mbr_blk_size * usbd->mmc_blk;
420                         len -= mbr_blk_size * usbd->mmc_blk;
421
422                         cur_blk_offset = mbr_blk_size;
423                         cur_part = 0;
424                         cur_part_size = part_info.partition[0].size;
425
426                         /* modify p1's total sector */
427                         bs = (boot_sector *)ram_addr;
428                         bs->total_sect = mbr_info.parts[0].nsectors;
429
430                         printf("\nWrite Partition %d.. %d blocks\n",
431                                 cur_part + 1,
432                                 part_info.partition[cur_part].size /
433                                 (int)usbd->mmc_blk);
434                 } else {
435                         part_mode = EXTEND_PARTITION;
436
437                         for (i = 0; i < EXTEND_MAX_PART; i++) {
438                                 if (part_info.partition[i].size == 0)
439                                         break;
440                                 mmc_parts++;
441                         }
442
443                         if (mmc_parts == 0)
444                                 return -1;
445                         else
446                                 printf("found %d partitions\n", mmc_parts);
447
448                         /* build partition table */
449
450                         mul_info[0].num_sectors =
451                                 usbd->mmc_total - mbr_blk_size;
452                         for (i = 1; i < mmc_parts; i++) {
453                                 mul_info[i].num_sectors =
454                                         part_info.partition[i].res +
455                                         mbr_blk_size; /* add MBR */
456                         }
457
458                         for (i = 1; i < mmc_parts; i++) {
459                                 mul_info[0].num_sectors -=
460                                         mul_info[i].num_sectors;
461                         }
462
463                         mul_info[0].lba_begin = mbr_blk_size;
464                         for (i = 1; i < mmc_parts; i++) {
465                                 mul_info[i].lba_begin =
466                                         mul_info[i-1].lba_begin +
467                                         mul_info[i-1].num_sectors;
468                         }
469
470                         /* modify MBR of extended partition: p1 */
471                         mbr->parts[0].nsectors =
472                                 usbd->mmc_total - mbr_blk_size;
473
474                         /* modify MBR of first logical partition: p5 */
475                         mbr = (struct mbr *)
476                                 (ram_addr + mbr_blk_size * usbd->mmc_blk);
477
478                         mbr->parts[0].nsectors =
479                                 mul_info[0].num_sectors - mbr_blk_size;
480                         mbr->parts[1].lba=
481                                 mul_info[1].lba_begin - mbr_blk_size;
482
483                         /* modify BPB data of p5 */
484                         bs = (boot_sector *)
485                                 ((u32)mbr + mbr_blk_size * usbd->mmc_blk);
486                         memset(&bs->sectors, 0, 2);
487                         bs->total_sect = mbr->parts[0].nsectors;
488
489                         printf("Total Size: 0x%08x #parts %d\n",
490                                         (unsigned int)usbd->mmc_total,
491                                         mmc_parts);
492                         for (i = 0; i < mmc_parts; i++) {
493                                 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
494                                         mul_info[i].lba_begin,
495                                         mul_info[i].num_sectors);
496                         }
497
498                         cur_blk_offset = 0;
499                         cur_part = 0;
500                         cur_part_size = part_info.partition[0].size;
501
502                         printf("\nWrite Partition %d.. %d blocks\n",
503                                 cur_part + 1,
504                                 part_info.partition[cur_part].size /
505                                 (int)usbd->mmc_blk);
506                 }
507         }
508
509         for (i = cur_part; i < mmc_parts; i++) {
510                 ret = write_mmc_partition(usbd, &ram_addr, len);
511
512                 if (ret < 0) {
513                         cur_part_size -= len;
514                         break;
515                 } else {
516                         cur_part++;
517                         cur_part_size =
518                                 part_info.partition[cur_part].size;
519
520                         if (part_mode == NORMAL_PARTITION) {
521                                 cur_blk_offset =
522                                         mbr_info.parts[cur_part].lba;
523                         } else {
524                                 cur_blk_offset =
525                                         mul_info[cur_part].lba_begin;
526                                 /* modify MBR */
527                                 if (cur_part < mmc_parts) {
528                                         mbr = (struct mbr *)ram_addr;
529                                         mbr->parts[1].lba=
530                                                 mul_info[cur_part+1].lba_begin -
531                                                 mbr->parts[0].lba;
532                                 }
533                         }
534
535                         if (ret == 0)
536                                 break;
537                         else
538                                 len = ret;
539
540                         printf("\nWrite Partition %d.. %d blocks\n",
541                                 cur_part + 1,
542                                 part_info.partition[cur_part].size /
543                                 (int)usbd->mmc_blk);
544                 }
545         }
546
547         return 0;
548 }
549
550 static int write_file_mmc_part(struct usbd_ops *usbd, u32 len)
551 {
552         u32 ram_addr;
553         u32 ofs;
554         int i;
555         struct mbr *mbr;
556         u32 mbr_blk_size;
557
558         ram_addr = (u32)down_ram_addr;
559         mbr = &mbr_info;
560
561         if (cur_blk_offset == 0) {
562                 /* read MBR */
563                 mmc_cmd(OPS_READ, usbd->mmc_dev, 0,
564                         sizeof(struct mbr)/usbd->mmc_blk, (void *)mbr);
565
566                 mbr_blk_size = mbr->parts[0].lba;
567
568                 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
569                         part_mode = NORMAL_PARTITION;
570
571                         for (i = 0; i < 4; i++) {
572                                 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
573                                         mbr_info.parts[i].lba,
574                                         mbr_info.parts[i].nsectors);
575                         }
576
577                         cur_blk_offset =
578                                 mbr->parts[mmc_part_write - 1].lba;
579                         cur_part = mmc_part_write - 1;
580                         cur_part_size =
581                                 usbd->mmc_blk *
582                                 mbr->parts[mmc_part_write - 1].nsectors;
583
584                         if (mmc_part_write == 1) {
585                                 ram_addr += sizeof(struct mbr);
586                                 cur_blk_offset += sizeof(struct mbr) /
587                                         usbd->mmc_blk;
588                                 len -= sizeof(struct mbr);
589                         }
590                 } else {
591                         part_mode = EXTEND_PARTITION;
592
593                         for (i = 1; i < mmc_part_write; i++) {
594                                 ofs = mbr->parts[0].lba + mbr->parts[1].lba;
595                                 printf("P%d start blk: 0x%x, size: 0x%x\n", i,
596                                         ofs, mbr->parts[1].nsectors);
597                                 mmc_cmd(OPS_READ, usbd->mmc_dev, ofs,
598                                         (sizeof(struct mbr) / usbd->mmc_blk),
599                                         (void *)mbr);
600                         }
601
602                         ofs = mbr->parts[0].lba + mbr->parts[1].lba;
603                         printf("P%d start blk: 0x%x, size: 0x%x\n", i,
604                                 ofs, mbr->parts[1].nsectors);
605
606                         ofs += mbr_blk_size; /* skip MBR */
607                         cur_blk_offset = ofs;
608                         cur_part = mmc_part_write - 1;
609                         cur_part_size =
610                                 usbd->mmc_blk *
611                                 mbr->parts[1].nsectors;
612
613                         if (mmc_part_write == 1) {
614                                 boot_sector *bs;
615                                 u32 total_sect;
616                                 /* modify BPB data of p1 */
617                                 mmc_cmd(OPS_READ, usbd->mmc_dev, cur_blk_offset,
618                                         (sizeof(struct mbr) / usbd->mmc_blk),
619                                         mbr);
620
621                                 bs = (boot_sector *)mbr;
622                                 total_sect = bs->total_sect;
623
624                                 bs = (boot_sector *)ram_addr;
625                                 memset(&bs->sectors, 0, 2);
626                                 bs->total_sect = total_sect;
627                         }
628                 }
629         }
630
631         printf("\nWrite Partition %d.. %d blocks\n",
632                 cur_part + 1,
633                 len / (int)usbd->mmc_blk);
634
635         write_mmc_partition(usbd, &ram_addr, len);
636
637         return 0;
638 }
639 #endif
640
641 static unsigned int mbr_offset[16];
642 static int mbr_parts = 0;
643
644 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
645 {
646         unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
647
648         switch (**retptr) {
649                 case 'G':
650                 case 'g':
651                         ret <<= 10;
652                 case 'M':
653                 case 'm':
654                         ret <<= 10;
655                 case 'K':
656                 case 'k':
657                         ret <<= 10;
658                         (*retptr)++;
659                 default:
660                         break;
661         }
662
663         return ret;
664 }
665
666 static void set_mbr_info(struct usbd_ops *usbd, char *ramaddr, u32 len)
667 {
668         char mbr_str[256];
669         char save[16][16];
670         char *p;
671         char *tok;
672         unsigned int size[16];
673         int i = 0;
674
675         strncpy(mbr_str, ramaddr, len);
676         p = mbr_str;
677
678         for (i = 0; ; i++, p = NULL) {
679                 tok = strtok(p, ",");
680                 if (tok == NULL)
681                         break;
682                 strcpy(save[i], tok);
683                 printf("part%d: %s\n", i, save[i]);
684         }
685
686         mbr_parts = i;
687         printf("find %d partitions\n", mbr_parts);
688
689         for (i = 0; i < mbr_parts; i++) {
690                 p = save[i];
691                 size[i] = memsize_parse(p, (const char **)&p) / 512;
692         }
693
694         puts("save the MBR Table...\n");
695         set_mbr_table(0x800, mbr_parts, size, mbr_offset);
696 }
697
698 static int write_mmc_image(struct usbd_ops *usbd, unsigned int len, int part_num)
699 {
700         int ret = 0;
701
702         if (mbr_parts <= part_num) {
703                 printf("Error: MBR table have %d partitions (request %d)\n",
704                                 mbr_parts, part_num);
705                 return 1;
706         }
707
708 #if 0
709         /* modify size of UMS partition */
710         if (part_num == 4 && fs_offset == 0) {
711                 boot_sector *bs;
712                 bs = (boot_sector *)down_ram_addr;
713                 memset(&bs->sectors, 0, 2);
714                 bs->total_sect = usbd->mmc_total - mbr_offset[part_num];
715         }
716 #endif
717         ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
718                         mbr_offset[part_num] + fs_offset,
719                         len / usbd->mmc_blk,
720                         (void *)down_ram_addr);
721
722         fs_offset += (len / usbd->mmc_blk);
723
724         return ret;
725 }
726
727 static int write_fat_file(struct usbd_ops *usbd, char *file_name,
728                         int part_id, int len)
729 {
730 #ifdef CONFIG_FAT_WRITE
731         int ret;
732
733         ret = fat_register_device(&mmc->block_dev, part_id);
734         if (ret < 0) {
735                 printf("error : fat_register_divce\n");
736                 return 0;
737         }
738
739         ret = file_fat_write(file_name, (void *)down_ram_addr, len);
740         if (ret < 0) {
741                 printf("error : writing uImage\n");
742                 return 0;
743         }
744 #else
745         printf("error: doesn't support fat_write\n");
746 #endif
747         return 0;
748 }
749
750 static int write_file_system(char *ramaddr, ulong len, char *offset,
751                 char *length, int part_num, int ubi_update)
752 {
753 #ifdef CONFIG_USE_YAFFS
754         int actual_len = 0;
755         int yaffs_write = 0;
756 #endif
757         int ret = 0;
758
759 #ifdef CONFIG_CMD_UBI
760         /* UBI Update */
761         if (ubi_update) {
762                 sprintf(length, "0x%x", (uint)len);
763                 ret = ubi_cmd(part_id, ramaddr, length, "cont");
764                 return ret;
765         }
766 #endif
767
768         /* Erase entire partition at the first writing */
769         if (write_part == 0 && ubi_update == 0) {
770                 sprintf(offset, "0x%x", (uint)parts[part_num]->offset);
771                 sprintf(length, "0x%x", (uint)parts[part_num]->size);
772                 nand_cmd(0, offset, length, NULL);
773         }
774
775 #ifdef CONFIG_USE_YAFFS
776         /* if using yaffs, wirte size must be 2112*X
777          * so, must have to realloc, and writing */
778         if (!strcmp("yaffs", getenv(parts[part_num]->name))) {
779                 yaffs_write = 1;
780
781                 memcpy((void *)down_ram_addr, yaffs_data, yaffs_len);
782
783                 actual_len = len + yaffs_len;
784                 yaffs_len = actual_len % YAFFS_PAGE;
785                 len = actual_len - yaffs_len;
786
787                 memset(yaffs_data, 0x00, YAFFS_PAGE);
788                 memcpy(yaffs_data, (char *)down_ram_addr + len, yaffs_len);
789         }
790 #endif
791
792         sprintf(offset, "0x%x", (uint)(parts[part_num]->offset + fs_offset));
793         sprintf(length, "0x%x", (uint)len);
794
795 #ifdef CONFIG_USE_YAFFS
796         if (yaffs_write)
797                 ret = nand_cmd(2, ramaddr, offset, length);
798         else
799                 ret = nand_cmd(1, ramaddr, offset, length);
800
801         if (!strcmp("yaffs", getenv(parts[part_num]->name)))
802                 fs_offset += len / YAFFS_PAGE * NAND_PAGE_SIZE;
803         else
804                 fs_offset += len;
805
806 #else
807         fs_offset += len;
808         ret = nand_cmd(1, ramaddr, offset, length);
809 #endif
810
811         return ret;
812 }
813
814 static int qboot_erase = 0;
815
816 /* Erase the qboot */
817 static void erase_qboot_area(void)
818 {
819         char offset[12], length[12];
820         int qboot_id;
821
822         if (qboot_erase)
823                 return;
824
825         qboot_id = get_part_id("qboot");
826
827         if (qboot_id != -1) {
828                 printf("\nCOMMAND_ERASE_QBOOT\n");
829                 sprintf(offset, "%x", parts[qboot_id]->offset);
830                 sprintf(length, "%x", parts[qboot_id]->size);
831                 nand_cmd(0, offset, length, NULL);
832                 qboot_erase = 1;
833         }
834 }
835
836 /* Parsing received data packet and Process data */
837 static int process_data(struct usbd_ops *usbd)
838 {
839         unsigned int cmd = 0, arg = 0, ofs = 0, len = 0, flag = 0;
840         char offset[12], length[12], ramaddr[12];
841         int recvlen = 0;
842         unsigned int blocks = 0;
843         int ret = 0;
844         int ubi_update = 0;
845         int ubi_mode = 0;
846         int img_type;
847
848         sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
849
850         /* Parse command */
851         cmd  = *((ulong *) usbd->rx_data + 0);
852         arg  = *((ulong *) usbd->rx_data + 1);
853         len  = *((ulong *) usbd->rx_data + 2);
854         flag = *((ulong *) usbd->rx_data + 3);
855
856         /* Reset tx buffer */
857         memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
858
859         ubi_mode = check_ubi_mode();
860
861         switch (cmd) {
862         case COMMAND_DOWNLOAD_IMAGE:
863                 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
864
865 #ifdef CONFIG_USE_YAFFS
866                 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
867                 printf("Download to 0x%08x, %d bytes\n",
868                                 (uint)down_ram_addr + yaffs_len, (int)len);
869 #else
870                 if (arg)
871                         down_ram_addr = usbd->ram_addr + 0x1000000;
872                 else
873                         down_ram_addr = usbd->ram_addr;
874
875                 usbd->recv_setup((char *)down_ram_addr, (int)len);
876                 printf("Download to 0x%08x, %d bytes\n",
877                                 (uint)down_ram_addr, (int)len);
878 #endif
879                 /* response */
880                 usbd->send_data(usbd->tx_data, usbd->tx_len);
881
882                 /* Receive image by using dma */
883                 recvlen = usbd->recv_data();
884                 if (recvlen < len) {
885                         printf("Error: wrong image size -> %d/%d\n",
886                                         (int)recvlen, (int)len);
887
888                         /* Retry this commad */
889                         *((ulong *) usbd->tx_data) = STATUS_RETRY;
890                 } else
891                         *((ulong *) usbd->tx_data) = STATUS_DONE;
892
893                 usbd->send_data(usbd->tx_data, usbd->tx_len);
894                 return 1;
895
896         /* Report partition info */
897         case COMMAND_PARTITION_SYNC:
898                 part_id = arg;
899
900 #ifdef CONFIG_CMD_UBI
901                 if (ubi_mode) {
902                         if (part_id == RAMDISK_PART_ID ||
903                             part_id == FILESYSTEM_PART_ID ||
904                             part_id == FILESYSTEM2_PART_ID) {
905                                 /* change to yaffs style */
906                                 get_part_info();
907                         }
908                 } else {
909                         if (part_id == FILESYSTEM3_PART_ID) {
910                                 /* change ubi style */
911                                 get_part_info();
912                         }
913                 }
914 #endif
915
916                 if (part_id == FILESYSTEM3_PART_ID)
917                         part_id = get_part_id("UBI");
918                 else if (part_id == MODEM_PART_ID)
919                         part_id = get_part_id("modem");
920                 else if (part_id == KERNEL_PART_ID)
921                         part_id = get_part_id("kernel");
922                 else if (part_id == BOOT_PART_ID)
923                         part_id = get_part_id("bootloader");
924 #ifdef CONFIG_MIRAGE
925                 if (part_id)
926                         part_id--;
927 #endif
928                 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
929
930                 blocks = parts[part_id]->size / 1024 / 128;
931                 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
932                                 part_id, blocks);
933
934                 *((ulong *) usbd->tx_data) = blocks;
935                 usbd->send_data(usbd->tx_data, usbd->tx_len);
936                 return 1;
937
938         case COMMAND_WRITE_PART_0:
939                 /* Do nothing */
940                 printf("COMMAND_WRITE_PART_0\n");
941                 return 1;
942
943         case COMMAND_WRITE_PART_1:
944                 printf("COMMAND_WRITE_PART_BOOT\n");
945                 part_id = get_part_id("bootloader");
946                 img_type = IMG_BOOT;
947                 break;
948
949         case COMMAND_WRITE_PART_2:
950         case COMMAND_ERASE_PARAMETER:
951                 printf("COMMAND_PARAMETER - not support!\n");
952                 break;
953
954         case COMMAND_WRITE_PART_3:
955                 printf("COMMAND_WRITE_KERNEL\n");
956                 part_id = get_part_id("kernel");
957                 img_type = IMG_KERNEL;
958                 break;
959
960         case COMMAND_WRITE_PART_4:
961                 printf("COMMAND_WRITE_ROOTFS\n");
962                 part_id = get_part_id("Root");
963                 img_type = IMG_FILESYSTEM;
964                 ubi_update = arg;
965                 break;
966
967         case COMMAND_WRITE_PART_5:
968                 printf("COMMAND_WRITE_FACTORYFS\n");
969                 part_id = get_part_id("Fact");
970                 img_type = IMG_FILESYSTEM;
971                 ubi_update = arg;
972                 break;
973
974         case COMMAND_WRITE_PART_6:
975                 printf("COMMAND_WRITE_DATAFS\n");
976                 part_id = get_part_id("Data");
977                 img_type = IMG_FILESYSTEM;
978                 ubi_update = arg;
979                 break;
980
981         case COMMAND_WRITE_PART_7:
982                 printf("COMMAND_WRITE_UBI\n");
983                 part_id = get_part_id("UBI");
984                 img_type = IMG_FILESYSTEM;
985                 ubi_update = 0;
986                 /* someday, it will be deleted */
987                 get_part_info();
988                 break;
989
990         case COMMAND_WRITE_PART_8:
991                 printf("COMMAND_WRITE_MODEM\n");
992                 part_id = get_part_id("modem");
993                 img_type = IMG_MODEM;
994                 break;
995
996 #ifdef CONFIG_CMD_MMC
997         case COMMAND_WRITE_PART_9:
998                 printf("COMMAND_WRITE_MMC\n");
999                 img_type = IMG_MMC;
1000                 mmc_part_write = arg;
1001                 break;
1002 #endif
1003
1004         case COMMAND_WRITE_IMG_0:
1005                 printf("COMMAND_WRITE_MBR\n");
1006                 img_type = IMG_MBR;
1007                 break;
1008
1009         case COMMAND_WRITE_IMG_1:
1010                 printf("COMMAND_WRITE_BOOTLOADER\n");
1011                 img_type = IMG_BOOTLOADER;
1012                 break;
1013
1014         case COMMAND_WRITE_IMG_2:
1015                 printf("COMMAND_WRITE_KERNEL\n");
1016                 img_type = IMG_KERNEL_V2;
1017                 part_id = 2;
1018                 break;
1019
1020         case COMMAND_WRITE_IMG_3:
1021                 printf("COMMAND_WRITE_MODEM\n");
1022                 img_type = IMG_MODEM_V2;
1023                 part_id = 2;
1024                 break;
1025
1026         case COMMAND_WRITE_IMG_4:
1027                 printf("COMMAND_WRITE_BOOT_PART\n");
1028                 part_id = 1;
1029                 img_type = IMG_V2;
1030                 break;
1031
1032         case COMMAND_WRITE_IMG_5:
1033                 printf("COMMAND_WRITE_SYSTEM_PART\n");
1034                 part_id = 2;
1035                 img_type = IMG_V2;
1036                 break;
1037
1038         case COMMAND_WRITE_IMG_6:
1039                 printf("COMMAND_WRITE_UMS_PART\n");
1040                 part_id = 4;
1041                 img_type = IMG_V2;
1042                 break;
1043
1044         case COMMAND_WRITE_UBI_INFO:
1045                 printf("COMMAND_WRITE_UBI_INFO\n");
1046
1047                 if (ubi_mode) {
1048 #ifdef CONFIG_CMD_UBI
1049                         part_id = arg-1;
1050                         sprintf(length, "0x%x", (uint)len);
1051                         ret = ubi_cmd(part_id, ramaddr, length, "begin");
1052                 } else {
1053 #endif
1054                         printf("Error: Not UBI mode\n");
1055                         ret = 1;
1056                 }
1057
1058                 *((ulong *) usbd->tx_data) = ret;
1059                 /* Write image success -> Report status */
1060                 usbd->send_data(usbd->tx_data, usbd->tx_len);
1061
1062                 return !ret;
1063         /* Download complete -> reset */
1064         case COMMAND_RESET_PDA:
1065                 printf("\nDownload finished and Auto reset!\nWait........\n");
1066
1067                 /* Stop USB */
1068                 usbd->usb_stop();
1069
1070                 if (usbd->cpu_reset)
1071                         usbd->cpu_reset();
1072                 else
1073                         do_reset();
1074
1075                 return 0;
1076
1077         /* Error */
1078         case COMMAND_RESET_USB:
1079                 printf("\nError is occured!(maybe previous step)->\
1080                                 Turn off and restart!\n");
1081
1082                 /* Stop USB */
1083                 usbd->usb_stop();
1084                 return 0;
1085
1086         case COMMAND_RAM_BOOT:
1087                 usbd->usb_stop();
1088                 boot_cmd(ramaddr);
1089                 return 0;
1090
1091         case COMMAND_RAMDISK_MODE:
1092                 printf("COMMAND_RAMDISK_MODE\n");
1093 #ifdef CONFIG_RAMDISK_ADDR
1094                 if (arg) {
1095                         down_ram_addr = usbd->ram_addr;
1096                 } else {
1097                         down_ram_addr = CONFIG_RAMDISK_ADDR;
1098                         run_command("run ramboot", 0);
1099                 }
1100 #endif
1101                 return 1;
1102
1103 #ifdef CONFIG_DOWN_PHONE
1104         case COMMAND_DOWN_PHONE:
1105                 printf("COMMAND_RESET_PHONE\n");
1106
1107                 usbd_phone_down();
1108
1109                 *((ulong *) usbd->tx_data) = STATUS_DONE;
1110                 usbd->send_data(usbd->tx_data, usbd->tx_len);
1111                 return 1;
1112
1113         case COMMAND_CHANGE_USB:
1114                 printf("COMMAND_CHANGE_USB\n");
1115
1116                 /* Stop USB */
1117                 usbd->usb_stop();
1118
1119                 usbd_path_change();
1120
1121                 do_reset();
1122                 return 0;
1123 #endif
1124         case COMMAND_CSA_CLEAR:
1125                 printf("COMMAND_CSA_CLEAR\n");
1126                 part_id = get_part_id("csa");
1127                 img_type = IMG_MODEM;
1128                 break;
1129
1130         case COMMAND_PROGRESS:
1131                 if (usbd->set_progress)
1132                         usbd->set_progress(arg);
1133                 return 1;
1134
1135         default:
1136                 printf("Error: Unknown command -> (%d)\n", (int)cmd);
1137                 return 1;
1138         }
1139
1140         /* Erase and Write to NAND */
1141         switch (img_type) {
1142         case IMG_BOOT:
1143                 ofs = parts[part_id]->offset;
1144 #ifdef CONFIG_S5PC1XX
1145                 /* Workaround: for prevent revision mismatch */
1146                 if (cpu_is_s5pc110() && (down_mode != MODE_FORCE)) {
1147                         int img_rev = 1;
1148                         long *img_header = (long *)down_ram_addr;
1149
1150                         if (*img_header == 0xea000012)
1151                                 img_rev = 0;
1152                         else if (*(img_header + 0x400) == 0xea000012)
1153                                 img_rev = 2;
1154
1155                         if (img_rev != s5p_get_cpu_rev()) {
1156                                 printf("CPU revision mismatch!\n"
1157                                         "bootloader is %s%s\n"
1158                                         "download image is %s%s\n",
1159                                         s5p_get_cpu_rev() ? "EVT1" : "EVT0",
1160                                         s5p_get_cpu_rev() == 2 ? "-Fused" : "",
1161                                         img_rev ? "EVT1" : "EVT0",
1162                                         img_rev == 2 ? "-Fused" : "");
1163                                 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1164                                 usbd->send_data(usbd->tx_data, usbd->tx_len);
1165                                 return 0;
1166                         }
1167                 }
1168 #endif
1169 #ifdef CONFIG_SBOOT
1170                 /* Only u-boot.bin is allowed */
1171                 {
1172                         long *img_header = (long *)down_ram_addr;
1173
1174                         if (*img_header != 0xea000018) {
1175                                 printf("\n!!! ERROR !!!\n"
1176                                         "Please download the u-boot.bin.\n"
1177                                         "Other images are not allowed.\n\n");
1178                                 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1179                                 usbd->send_data(usbd->tx_data, usbd->tx_len);
1180                                 return 0;
1181                         }
1182                 }
1183 #endif
1184 #if defined(CONFIG_ENV_IS_IN_NAND) || defined(CONFIG_ENV_IS_IN_ONENAND)
1185                 /* Erase the environment also when write bootloader */
1186                 {
1187                         int param_id;
1188                         param_id = get_part_id("params");
1189
1190                         if (param_id == -1) {
1191                                 sprintf(offset, "%x", CONFIG_ENV_ADDR);
1192                                 sprintf(length, "%x", CONFIG_ENV_SIZE);
1193                         } else {
1194                                 sprintf(offset, "%x", parts[param_id]->offset);
1195                                 sprintf(length, "%x", parts[param_id]->size);
1196                         }
1197                         nand_cmd(0, offset, length, NULL);
1198                 }
1199 #endif
1200                 sprintf(offset, "%x", (uint)ofs);
1201                 if (ofs != 0)
1202                         sprintf(length, "%x", parts[part_id]->size - (uint)ofs);
1203                 else
1204                         sprintf(length, "%x", parts[part_id]->size);
1205
1206                 /* Erase */
1207                 nand_cmd(0, offset, length, NULL);
1208                 /* Write */
1209                 sprintf(length, "%x", (unsigned int) len);
1210                 ret = nand_cmd(1, ramaddr, offset, length);
1211                 break;
1212
1213         case IMG_KERNEL:
1214                 sprintf(offset, "%x", parts[part_id]->offset);
1215                 sprintf(length, "%x", parts[part_id]->size);
1216
1217                 /* Erase */
1218                 nand_cmd(0, offset, length, NULL);
1219                 /* Write */
1220                 sprintf(length, "%x", (unsigned int) len);
1221                 ret = nand_cmd(1, ramaddr, offset, length);
1222
1223                 erase_qboot_area();
1224                 break;
1225
1226         /* File Systems */
1227         case IMG_FILESYSTEM:
1228                 ret = write_file_system(ramaddr, len, offset, length,
1229                                 part_id, ubi_update);
1230
1231                 erase_qboot_area();
1232                 break;
1233
1234         case IMG_MODEM:
1235                 sprintf(offset, "%x", parts[part_id]->offset);
1236                 sprintf(length, "%x", parts[part_id]->size);
1237
1238                 /* Erase */
1239                 if (!arg)
1240                         nand_cmd(0, offset, length, NULL);
1241                 else
1242                         printf("CSA Clear will be skipped temporary\n");
1243
1244                 /* Check ubi image, 0x23494255 is UBI# */
1245                 {
1246                         long *img_header = (long *)down_ram_addr;
1247
1248                         if (*img_header == 0x23494255)
1249                                 goto ubi_img;
1250                 }
1251
1252 #ifdef CONFIG_UBIFS_MK
1253                 void *dest_addr = (void *) down_ram_addr + 0xc00000;
1254                 void *src_addr = (void *) down_ram_addr;
1255                 int leb_size, max_leb_cnt, mkfs_min_io_size;
1256                 unsigned long ubifs_dest_size, ubi_dest_size;
1257 #ifdef CONFIG_S5PC110
1258                 mkfs_min_io_size = 4096;
1259                 leb_size = 248 * 1024;
1260                 max_leb_cnt = 4096;
1261 #elif CONFIG_S5PC210
1262                 mkfs_min_io_size = 2048;
1263                 leb_size = 126 * 1024;
1264                 max_leb_cnt = 4096;
1265 #endif
1266                 printf("Start making ubifs\n");
1267                 ret = mkfs(src_addr, len, dest_addr, &ubifs_dest_size,
1268                            mkfs_min_io_size, leb_size, max_leb_cnt);
1269                 if (ret) {
1270                         printf("Error : making ubifs failed\n");
1271                         goto out;
1272                 }
1273                 printf("Complete making ubifs\n");
1274 #endif
1275
1276 #ifdef CONFIG_UBINIZE
1277                 int peb_size, ubi_min_io_size, subpage_size, vid_hdr_offs;
1278 #ifdef CONFIG_S5PC110
1279                 ubi_min_io_size = 4096;
1280                 peb_size = 256 * 1024;
1281                 subpage_size = 4096;
1282                 vid_hdr_offs = 0;
1283 #elif CONFIG_S5PC210
1284                 ubi_min_io_size = 2048;
1285                 peb_size = 128 * 1024;
1286                 subpage_size = 512;
1287                 vid_hdr_offs = 512;
1288 #endif
1289                 printf("Start ubinizing\n");
1290                 ret = ubinize(dest_addr, ubifs_dest_size,
1291                               src_addr, &ubi_dest_size,
1292                               peb_size, ubi_min_io_size,
1293                               subpage_size, vid_hdr_offs);
1294                 if (ret) {
1295                         printf("Error : ubinizing failed\n");
1296                         goto out;
1297                 }
1298                 printf("Complete ubinizing\n");
1299
1300                 len = (unsigned int) ubi_dest_size;
1301 #endif
1302
1303 ubi_img:
1304                 /* Write : arg (0 Modem) / (1 CSA) */
1305                 if (!arg) {
1306                         sprintf(length, "%x", (unsigned int) len);
1307                         ret = nand_cmd(1, ramaddr, offset, length);
1308                 }
1309 out:
1310                 break;
1311
1312 #ifdef CONFIG_CMD_MMC
1313         case IMG_MMC:
1314
1315                 if (mmc_part_write)
1316                         ret = write_file_mmc_part(usbd, len);
1317                 else
1318                         ret = write_file_mmc(usbd, len);
1319
1320                 erase_qboot_area();
1321                 break;
1322 #endif
1323         case IMG_V2:
1324                 ret = write_mmc_image(usbd, len, part_id);
1325                 break;
1326
1327         case IMG_MBR:
1328 #ifdef CONFIG_CMD_MBR
1329                 set_mbr_info(usbd, (char *)down_ram_addr, len);
1330 #endif
1331                 break;
1332
1333         case IMG_BOOTLOADER:
1334 #ifdef CONFIG_BOOTLADER_SECTOR
1335                 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
1336                                 CONFIG_BOOTLOADER_SECTOR,
1337                                 len / usbd->mmc_blk + 1,
1338                                 (void *)down_ram_addr);
1339 #endif
1340                 break;
1341
1342         case IMG_KERNEL_V2:
1343                 ret = write_fat_file(usbd, "uImage", part_id, len);
1344                 break;
1345
1346         case IMG_MODEM_V2:
1347                 ret = write_fat_file(usbd, "modem.bin", part_id, len);
1348                 break;
1349
1350         default:
1351                 /* Retry? */
1352                 write_part--;
1353         }
1354
1355         if (ret) {
1356                 /* Retry this commad */
1357                 *((ulong *) usbd->tx_data) = STATUS_RETRY;
1358                 usbd->send_data(usbd->tx_data, usbd->tx_len);
1359                 return 1;
1360         } else
1361                 *((ulong *) usbd->tx_data) = STATUS_DONE;
1362
1363         /* Write image success -> Report status */
1364         usbd->send_data(usbd->tx_data, usbd->tx_len);
1365
1366         write_part++;
1367
1368         /* Reset write count for another image */
1369         if (flag) {
1370                 write_part = 0;
1371                 fs_offset = 0;
1372 #ifdef CONFIG_USE_YAFFS
1373                 yaffs_len = 0;
1374 #endif
1375         }
1376
1377         return 1;
1378 }
1379
1380 static const char *recv_key = "SAMSUNG";
1381 static const char *tx_key = "MPL";
1382
1383 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1384 {
1385         struct usbd_ops *usbd;
1386         int err;
1387
1388         if (argc > 1)
1389                 down_mode = simple_strtoul(argv[1], NULL, 10);
1390         else
1391                 down_mode = MODE_NORMAL;
1392
1393         printf("USB Downloader v%s\n", APP_VERSION);
1394
1395         /* get partition info */
1396         err = get_part_info();
1397         if (err)
1398                 return err;
1399
1400         /* interface setting */
1401         usbd = usbd_set_interface(&usbd_ops);
1402         down_ram_addr = usbd->ram_addr;
1403
1404 #ifdef CONFIG_CMD_MBR
1405         /* get mbr info */
1406         mbr_parts = get_mbr_table(mbr_offset);
1407         if (!mbr_parts) {
1408                 char *mbrparts;
1409
1410                 puts("using default MBR\n");
1411
1412                 mbrparts = getenv("mbrparts");
1413                 set_mbr_info(usbd, mbrparts, strlen(mbrparts));
1414         }
1415 #endif
1416
1417         /* init the usb controller */
1418         if (!usbd->usb_init()) {
1419                 usbd->down_cancel();
1420                 return 0;
1421         }
1422         mmc = find_mmc_device(usbd->mmc_dev);
1423         mmc_init(mmc);
1424
1425         /* receive setting */
1426         usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1427
1428         /* detect the download request from Host PC */
1429         if (usbd->recv_data()) {
1430                 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
1431                         printf("Download request from the Host PC\n");
1432                         msleep(30);
1433
1434                         strcpy(usbd->tx_data, tx_key);
1435                         usbd->send_data(usbd->tx_data, usbd->tx_len);
1436                 } else {
1437                         printf("No download request from the Host PC!! 1\n");
1438                         return 0;
1439                 }
1440         } else {
1441                 usbd->down_cancel();
1442                 return 0;
1443         }
1444
1445         usbd->down_start();
1446         printf("Receive the packet\n");
1447
1448         /* receive the data from Host PC */
1449         while (1) {
1450                 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1451
1452                 if (usbd->recv_data()) {
1453                         if (process_data(usbd) == 0)
1454                                 return 0;
1455                 }
1456         }
1457
1458         return 0;
1459 }
1460
1461 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
1462         "Initialize USB device and Run USB Downloader (specific)",
1463         "- normal mode\n"
1464         "usbdown mode - specific mode (0: NORAML, 1: FORCE)"
1465 );