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