usbd: fix kernel partition sync
[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.4.1"
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 static const char pszMe[] = "usbd: ";
26
27 static struct usbd_ops usbd_ops;
28
29 static unsigned int part_id;
30 static unsigned int write_part = 0;
31 static unsigned long fs_offset = 0x0;
32
33 #ifdef CONFIG_USE_YAFFS
34 static unsigned int yaffs_len = 0;
35 static unsigned char yaffs_data[2112];
36 #define YAFFS_PAGE 2112
37 #endif
38
39 #define NAND_PAGE_SIZE 2048
40
41 static unsigned long down_ram_addr;
42
43 static int down_mode;
44
45 /* cpu/${CPU} dependent */
46 extern void do_reset(void);
47
48 /* common commands */
49 extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50 extern int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
51
52 int mtdparts_init(void);
53 int find_dev_and_part(const char*, struct mtd_device**, u8*, struct part_info**);
54
55 /* common/cmd_jffs2.c */
56 extern struct list_head devices;
57
58 static u8 count_mtdparts(void)
59 {
60         struct list_head *dentry, *pentry;
61         struct mtd_device *dev;
62         u8 part_num = 0;
63
64         list_for_each(dentry, &devices) {
65                 dev = list_entry(dentry, struct mtd_device, link);
66
67                 /* list partitions for given device */
68                 list_for_each(pentry, &dev->parts)
69                         part_num++;
70         }
71
72         return part_num;
73 }
74
75 #ifdef CONFIG_CMD_UBI
76 static int check_ubi_mode(void)
77 {
78         char *env_ubifs;
79         int ubi_mode;
80
81         env_ubifs = getenv("ubi");
82         ubi_mode = !strcmp(env_ubifs, "enabled");
83
84         return ubi_mode;
85 }
86 #else
87 #define check_ubi_mode()                0
88 #endif
89
90 #ifdef CONFIG_MTD_PARTITIONS
91 static int get_part_info(void)
92 {
93         struct mtd_device *dev;
94         u8 out_partnum;
95         char part_name[12];
96         char nand_name[12];
97         int i;
98         int part_num;
99         int ubi_mode = 0;
100
101 #if defined(CONFIG_CMD_NAND)
102         sprintf(nand_name, "nand0");
103 #elif defined(CONFIG_CMD_ONENAND)
104         sprintf(nand_name, "onenand0");
105 #else
106         printf("Configure your NAND sub-system\n");
107         return 0;
108 #endif
109
110         if (mtdparts_init())
111                 return 0;
112
113         ubi_mode = check_ubi_mode();
114
115         part_num = count_mtdparts();
116         for (i = 0; i < part_num; i++) {
117                 sprintf(part_name, "%s,%d", nand_name, i);
118
119                 if (find_dev_and_part(part_name, &dev, &out_partnum, &parts[i]))
120                         return -EINVAL;
121         }
122
123         return 0;
124 }
125
126 static int get_part_id(char *name)
127 {
128         int nparts = count_mtdparts();
129         int i;
130
131         for (i = 0; i < nparts; i++) {
132                 if (strcmp(parts[i]->name, name) == 0)
133                         return i;
134         }
135
136         printf("Error: Unknown partition -> %s\n", name);
137         return -1;
138 }
139 #else
140 static int get_part_info(void)
141 {
142         printf("Error: Can't get patition information\n");
143         return -EINVAL;
144 }
145
146 static int get_part_id(char *name)
147 {
148         return 0;
149 }
150 #endif
151
152 static void boot_cmd(char *addr)
153 {
154         char *argv[] = { "bootm", addr };
155         do_bootm(NULL, 0, 2, argv);
156 }
157
158 static void run_cmd(char *cmd)
159 {
160         char *argv[] = { "run", cmd };
161         do_run(NULL, 0, 2, argv);
162 }
163
164 #if defined(CONFIG_CMD_NAND)
165 extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
166 #elif defined(CONFIG_CMD_ONENAND)
167 extern int do_onenand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
168 #endif
169
170 /* NAND erase and write using nand command */
171 static int nand_cmd(int type, char *p1, char *p2, char *p3)
172 {
173         int ret = 1;
174         char nand_name[12];
175         int (*nand_func) (cmd_tbl_t *, int, int, char **);
176
177 #if defined(CONFIG_CMD_NAND)
178         sprintf(nand_name, "nand");
179         nand_func = do_nand;
180 #elif defined(CONFIG_CMD_ONENAND)
181         sprintf(nand_name, "onenand");
182         nand_func = do_onenand;
183 #else
184         printf("Configure your NAND sub-system\n");
185         return 0;
186 #endif
187
188         if (type == 0) {
189                 char *argv[] = {nand_name, "erase", p1, p2};
190                 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
191                 ret = nand_func(NULL, 0, 4, argv);
192         } else if (type == 1) {
193                 char *argv[] = {nand_name, "write", p1, p2, p3};
194                 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
195                                 argv[3], argv[4]);
196                 ret = nand_func(NULL, 0, 5, argv);
197         } else if (type == 2) {
198                 char *argv[] = {nand_name, "write.yaffs", p1, p2, p3};
199                 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
200                                 argv[3], argv[4]);
201                 ret = nand_func(NULL, 0, 5, argv);
202         }
203
204         if (ret)
205                 printf("Error: NAND Command\n");
206
207         return ret;
208 }
209
210 #ifdef CONFIG_CMD_UBI
211 int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
212
213 int ubi_cmd(int part, char *p1, char *p2, char *p3)
214 {
215         int ret = 1;
216
217         if (part == RAMDISK_PART_ID) {
218                 char *argv[] = {"ubi", "write", p1, "rootfs.cramfs", p2, p3};
219                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
220                                 argv[3], argv[4], argv[5]);
221                 ret = do_ubi(NULL, 0, 6, argv);
222         } else if (part == FILESYSTEM_PART_ID) {
223                 char *argv[] = {"ubi", "write", p1, "factoryfs.cramfs", p2, p3};
224                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
225                                 argv[3], argv[4], argv[5]);
226                 ret = do_ubi(NULL, 0, 6, argv);
227         } else if (part == FILESYSTEM2_PART_ID) {
228                 char *argv[] = {"ubi", "write", p1, "datafs.ubifs", p2, p3};
229                 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
230                                 argv[3], argv[4], argv[5]);
231                 ret = do_ubi(NULL, 0, 6, argv);
232         }
233
234         return ret;
235 }
236 #endif
237
238 #ifdef CONFIG_CMD_MMC
239 #include <fat.h>
240
241 extern int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
242
243 static int mmc_cmd(int ops, char *p1, char *p2, char *p3)
244 {
245         int ret;
246
247         if (ops) {
248                 char *argv[] = {"mmc", "write", "0", p1, p2, p3};
249                 ret = do_mmcops(NULL, 0, 6, argv);
250         } else {
251                 char *argv[] = {"mmc", "read", "0", p1, p2, p3};
252                 ret = do_mmcops(NULL, 0, 6, argv);
253         }
254
255         return ret;
256 }
257
258 static unsigned int cur_blk_offset;
259 static unsigned int cur_part_size;
260 static unsigned int cur_partition;
261
262 static unsigned int mmc_parts;
263 static unsigned int mmc_part_write;
264
265 struct partition_info {
266         u32 size;
267         u32 checksum;
268         u32 res;
269 } __attribute__((packed));
270
271 struct partition_header {
272         u8                      fat32head[16];  /* RFSHEAD identifier */
273         struct partition_info   partition[4];
274         u8                      res[448];       /* reserved */
275 } __attribute__((packed));
276
277 struct partition_table {
278         u8      boot_flag;
279         u8      chs_begin[3];
280         u8      type_code;
281         u8      chs_end[3];
282         u32     lba_begin;
283         u32     num_sectors;
284 } __attribute__((packed));
285
286 struct mbr_table {
287         u8                      boot_code[446];
288         struct partition_table  partition[4];
289         u16                     signature;
290 } __attribute__((packed));
291
292 #define MBR_OFFSET      0x10
293
294 struct partition_header part_info;
295 struct mbr_table mbr_info;
296
297 static int write_mmc_partition(struct usbd_ops *usbd, u32 *ram_addr, u32 len)
298 {
299         unsigned int blocks;
300         char offset[12], length[12], ramaddr[12];
301         int i;
302         int loop;
303         u32 cnt;
304         int ret;
305
306         if (cur_part_size > len) {
307                 blocks = len / usbd->mmc_blk;
308                 ret = -1;
309         } else {
310                 blocks = cur_part_size / usbd->mmc_blk;
311                 ret = len - cur_part_size;
312         }
313
314         if (len % usbd->mmc_blk)
315                 blocks++;
316
317         loop = blocks / usbd->mmc_max;
318         if (blocks % usbd->mmc_max)
319                 loop++;
320
321         for (i = 0; i < loop; i++) {
322                 if (i == 0) {
323                         cnt = blocks % usbd->mmc_max;
324                         if (cnt == 0)
325                                 cnt = usbd->mmc_max;
326                 } else {
327                         cnt = usbd->mmc_max;
328                 }
329
330                 sprintf(length, "%x", cnt);
331                 sprintf(offset, "%x", cur_blk_offset);
332                 sprintf(ramaddr, "0x%x", *ram_addr);
333                 mmc_cmd(OPS_WRITE, ramaddr, offset, length);
334
335                 cur_blk_offset += cnt;
336
337                 *ram_addr += (cnt * usbd->mmc_blk);
338         }
339
340         return ret;
341 }
342
343 static int write_file_mmc(struct usbd_ops *usbd, char *ramaddr, u32 len,
344                 char *offset, char *length)
345 {
346         u32 ram_addr;
347         int i;
348         int ret;
349
350         ram_addr = (u32)down_ram_addr;
351
352         if (cur_blk_offset == 0) {
353                 boot_sector *bs;
354                 struct mbr_table *mbr;
355
356                 memcpy(&part_info, (void *)ram_addr,
357                                 sizeof(struct partition_header));
358
359                 ram_addr += sizeof(struct partition_header);
360                 len -= sizeof(struct partition_header);
361                 mbr = (struct mbr_table*)ram_addr;
362
363                 /* modify sectors of p1 */
364                 mbr->partition[0].num_sectors = usbd->mmc_total -
365                                 (MBR_OFFSET +
366                                 mbr->partition[1].num_sectors +
367                                 mbr->partition[2].num_sectors +
368                                 mbr->partition[3].num_sectors);
369
370                 /* modify lba_begin of p2 and p3 and p4 */
371                 for (i = 1; i < 4; i++) {
372                         mmc_parts++;
373                         if (part_info.partition[i].size == 0)
374                                 break;
375
376                         mbr->partition[i].lba_begin =
377                                 mbr->partition[i - 1].lba_begin +
378                                 mbr->partition[i - 1].num_sectors;
379                 }
380
381                 /* copy MBR */
382                 memcpy(&mbr_info, mbr, sizeof(struct mbr_table));
383
384                 printf("Total Size: 0x%08x #parts %d\n",
385                                 (unsigned int)usbd->mmc_total, mmc_parts);
386                 for (i = 0; i < mmc_parts; i++) {
387                         printf("p%d\t0x%08x\t0x%08x\n", i + 1,
388                                 mbr_info.partition[i].lba_begin,
389                                 mbr_info.partition[i].num_sectors);
390                 }
391
392                 /* write MBR */
393                 sprintf(length, "%x", MBR_OFFSET);
394                 sprintf(offset, "%x", 0);
395                 sprintf(ramaddr, "0x%x", (u32)ram_addr);
396                 ret = mmc_cmd(OPS_WRITE, ramaddr, offset, length);
397
398                 ram_addr += (MBR_OFFSET * usbd->mmc_blk);
399                 len -= (MBR_OFFSET * usbd->mmc_blk);
400
401                 cur_blk_offset = MBR_OFFSET;
402                 cur_partition = 0;
403                 cur_part_size = part_info.partition[0].size;
404
405                 /* modify p1's total sector */
406                 bs = (boot_sector *)ram_addr;
407                 bs->total_sect = mbr_info.partition[0].num_sectors;
408
409                 printf("\nWrite Partition %d.. %d blocks\n",
410                         cur_partition + 1,
411                         part_info.partition[cur_partition].size /
412                         (int)usbd->mmc_blk);
413         }
414
415         for (i = cur_partition; i < mmc_parts; i++) {
416                 ret = write_mmc_partition(usbd, &ram_addr, len);
417
418                 if (ret < 0) {
419                         cur_part_size -= len;
420                         break;
421                 } else {
422                         cur_partition++;
423                         cur_part_size =
424                                 part_info.partition[cur_partition].size;
425                         cur_blk_offset =
426                                 mbr_info.partition[cur_partition].lba_begin;
427
428                         if (ret == 0)
429                                 break;
430                         else
431                                 len = ret;
432
433                         printf("\nWrite Partition %d.. %d blocks\n",
434                                 cur_partition + 1,
435                                 part_info.partition[cur_partition].size /
436                                 (int)usbd->mmc_blk);
437                 }
438         }
439
440         return 0;
441 }
442
443 static int write_file_mmc_part(struct usbd_ops *usbd, char *ramaddr, u32 len,
444                 char *offset, char *length)
445 {
446         u32 ram_addr;
447         int ret;
448         int i;
449
450         ram_addr = (u32)down_ram_addr;
451
452         if (cur_blk_offset == 0) {
453                 /* read MBR */
454                 sprintf(length, "%x", MBR_OFFSET);
455                 sprintf(offset, "%x", 0);
456                 sprintf(ramaddr, "0x%x", (u32)&mbr_info);
457                 ret = mmc_cmd(OPS_READ, ramaddr, offset, length);
458
459                 for (i = 0; i < 4; i++) {
460                         printf("p%d\t0x%08x\t0x%08x\n", i + 1,
461                                 mbr_info.partition[i].lba_begin,
462                                 mbr_info.partition[i].num_sectors);
463                 }
464
465                 cur_blk_offset = (unsigned int)
466                         mbr_info.partition[mmc_part_write - 1].lba_begin;
467                 cur_partition = mmc_part_write - 1;
468                 cur_part_size = (unsigned int)len;
469
470                 if (mmc_part_write == 1) {
471                         ram_addr += sizeof(boot_sector);
472                         cur_blk_offset += sizeof(boot_sector) / usbd->mmc_blk;
473                         len -= sizeof(boot_sector);
474                 }
475         }
476
477         printf("\nWrite Partition %d.. %d blocks\n",
478                 cur_partition + 1,
479                 len / (int)usbd->mmc_blk);
480
481         write_mmc_partition(usbd, &ram_addr, len);
482
483         return 0;
484 }
485 #endif
486
487 static int write_file_system(char *ramaddr, ulong len, char *offset,
488                 char *length, int part_num, int ubi_update)
489 {
490 #ifdef CONFIG_USE_YAFFS
491         int actual_len = 0;
492         int yaffs_write = 0;
493 #endif
494         int ret = 0;
495
496 #ifdef CONFIG_CMD_UBI
497         /* UBI Update */
498         if (ubi_update) {
499                 sprintf(length, "0x%x", (uint)len);
500                 ret = ubi_cmd(part_id, ramaddr, length, "cont");
501                 return ret;
502         }
503 #endif
504
505         /* Erase entire partition at the first writing */
506         if (write_part == 0 && ubi_update == 0) {
507                 sprintf(offset, "0x%x", (uint)parts[part_num]->offset);
508                 sprintf(length, "0x%x", (uint)parts[part_num]->size);
509                 nand_cmd(0, offset, length, NULL);
510         }
511
512 #ifdef CONFIG_USE_YAFFS
513         /* if using yaffs, wirte size must be 2112*X
514          * so, must have to realloc, and writing */
515         if (!strcmp("yaffs", getenv(parts[part_num]->name))) {
516                 yaffs_write = 1;
517
518                 memcpy((void *)down_ram_addr, yaffs_data, yaffs_len);
519
520                 actual_len = len + yaffs_len;
521                 yaffs_len = actual_len % YAFFS_PAGE;
522                 len = actual_len - yaffs_len;
523
524                 memset(yaffs_data, 0x00, YAFFS_PAGE);
525                 memcpy(yaffs_data, (char *)down_ram_addr + len, yaffs_len);
526         }
527 #endif
528
529         sprintf(offset, "0x%x", (uint)(parts[part_num]->offset + fs_offset));
530         sprintf(length, "0x%x", (uint)len);
531
532 #ifdef CONFIG_USE_YAFFS
533         if (yaffs_write)
534                 ret = nand_cmd(2, ramaddr, offset, length);
535         else
536                 ret = nand_cmd(1, ramaddr, offset, length);
537
538         if (!strcmp("yaffs", getenv(parts[part_num]->name)))
539                 fs_offset += len / YAFFS_PAGE * NAND_PAGE_SIZE;
540         else
541                 fs_offset += len;
542
543 #else
544         fs_offset += len;
545         ret = nand_cmd(1, ramaddr, offset, length);
546 #endif
547
548         return ret;
549 }
550
551 /* Parsing received data packet and Process data */
552 static int process_data(struct usbd_ops *usbd)
553 {
554         ulong cmd = 0, arg = 0, len = 0, flag = 0;
555         char offset[12], length[12], ramaddr[12];
556         int recvlen = 0;
557         unsigned int blocks = 0;
558         int ret = 0;
559         int ubi_update = 0;
560         int ubi_mode = 0;
561         int img_type;
562
563         sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
564
565         /* Parse command */
566         cmd  = *((ulong *) usbd->rx_data + 0);
567         arg  = *((ulong *) usbd->rx_data + 1);
568         len  = *((ulong *) usbd->rx_data + 2);
569         flag = *((ulong *) usbd->rx_data + 3);
570
571         /* Reset tx buffer */
572         memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
573
574         ubi_mode = check_ubi_mode();
575
576         switch (cmd) {
577         case COMMAND_DOWNLOAD_IMAGE:
578                 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
579
580 #ifdef CONFIG_USE_YAFFS
581                 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
582                 printf("Download to 0x%08x, %d bytes\n",
583                                 (uint)down_ram_addr + yaffs_len, (int)len);
584 #else
585                 usbd->recv_setup((char *)down_ram_addr, (int)len);
586                 printf("Download to 0x%08x, %d bytes\n",
587                                 (uint)down_ram_addr, (int)len);
588 #endif
589                 /* response */
590                 usbd->send_data(usbd->tx_data, usbd->tx_len);
591
592                 /* Receive image by using dma */
593                 recvlen = usbd->recv_data();
594                 if (recvlen < len) {
595                         printf("Error: wrong image size -> %d/%d\n",
596                                         (int)recvlen, (int)len);
597
598                         /* Retry this commad */
599                         *((ulong *) usbd->tx_data) = STATUS_RETRY;
600                 } else
601                         *((ulong *) usbd->tx_data) = STATUS_DONE;
602
603                 usbd->send_data(usbd->tx_data, usbd->tx_len);
604                 return 1;
605
606         /* Report partition info */
607         case COMMAND_PARTITION_SYNC:
608                 part_id = arg;
609
610 #ifdef CONFIG_CMD_UBI
611                 if (ubi_mode) {
612                         if (part_id == RAMDISK_PART_ID ||
613                             part_id == FILESYSTEM_PART_ID ||
614                             part_id == FILESYSTEM2_PART_ID) {
615                                 /* change to yaffs style */
616                                 get_part_info();
617                         }
618                 } else {
619                         if (part_id == FILESYSTEM3_PART_ID) {
620                                 /* change ubi style */
621                                 get_part_info();
622                         }
623                 }
624 #endif
625
626                 if (part_id == FILESYSTEM3_PART_ID)
627                         part_id = get_part_id("UBI");
628                 else if (part_id == MODEM_PART_ID)
629                         part_id = get_part_id("modem");
630                 else if (part_id == KERNEL_PART_ID)
631                         part_id = get_part_id("kernel");
632 #ifdef CONFIG_MIRAGE
633                 if (part_id)
634                         part_id--;
635 #endif
636                 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
637
638                 blocks = parts[part_id]->size / 1024 / 128;
639                 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
640                                 part_id, blocks);
641
642                 *((ulong *) usbd->tx_data) = blocks;
643                 usbd->send_data(usbd->tx_data, usbd->tx_len);
644                 return 1;
645
646         case COMMAND_WRITE_PART_0:
647                 /* Do nothing */
648                 printf("COMMAND_WRITE_PART_0\n");
649                 return 1;
650
651         case COMMAND_WRITE_PART_1:
652                 printf("COMMAND_WRITE_PART_BOOT\n");
653                 part_id = get_part_id("bootloader");
654                 img_type = IMG_BOOT;
655                 break;
656
657         case COMMAND_WRITE_PART_2:
658         case COMMAND_ERASE_PARAMETER:
659                 printf("COMMAND_PARAMETER - not support!\n");
660                 break;
661
662         case COMMAND_WRITE_PART_3:
663                 printf("COMMAND_WRITE_KERNEL\n");
664                 part_id = get_part_id("kernel");
665                 img_type = IMG_KERNEL;
666                 break;
667
668         case COMMAND_WRITE_PART_4:
669                 printf("COMMAND_WRITE_ROOTFS\n");
670                 part_id = get_part_id("Root");
671                 img_type = IMG_FILESYSTEM;
672                 ubi_update = arg;
673                 break;
674
675         case COMMAND_WRITE_PART_5:
676                 printf("COMMAND_WRITE_FACTORYFS\n");
677                 part_id = get_part_id("Fact");
678                 img_type = IMG_FILESYSTEM;
679                 ubi_update = arg;
680                 break;
681
682         case COMMAND_WRITE_PART_6:
683                 printf("COMMAND_WRITE_DATAFS\n");
684                 part_id = get_part_id("Data");
685                 img_type = IMG_FILESYSTEM;
686                 ubi_update = arg;
687                 break;
688
689         case COMMAND_WRITE_PART_7:
690                 printf("COMMAND_WRITE_UBI\n");
691                 part_id = get_part_id("UBI");
692                 img_type = IMG_FILESYSTEM;
693                 ubi_update = 0;
694                 /* someday, it will be deleted */
695                 get_part_info();
696                 break;
697
698         case COMMAND_WRITE_PART_8:
699                 printf("COMMAND_WRITE_MODEM\n");
700                 part_id = get_part_id("modem");
701                 img_type = IMG_MODEM;
702                 break;
703
704         case COMMAND_WRITE_PART_9:
705                 printf("COMMAND_WRITE_MMC\n");
706                 img_type = IMG_MMC;
707                 mmc_part_write = arg;
708                 break;
709
710         case COMMAND_WRITE_UBI_INFO:
711                 printf("COMMAND_WRITE_UBI_INFO\n");
712
713                 if (ubi_mode) {
714 #ifdef CONFIG_CMD_UBI
715                         part_id = arg-1;
716                         sprintf(length, "0x%x", (uint)len);
717                         ret = ubi_cmd(part_id, ramaddr, length, "begin");
718                 } else {
719 #endif
720                         printf("Error: Not UBI mode\n");
721                         ret = 1;
722                 }
723
724                 *((ulong *) usbd->tx_data) = ret;
725                 /* Write image success -> Report status */
726                 usbd->send_data(usbd->tx_data, usbd->tx_len);
727
728                 return !ret;
729         /* Download complete -> reset */
730         case COMMAND_RESET_PDA:
731                 printf("\nDownload finished and Auto reset!\nWait........\n");
732
733                 /* Stop USB */
734                 usbd->usb_stop();
735
736                 do_reset();
737                 return 0;
738
739         /* Error */
740         case COMMAND_RESET_USB:
741                 printf("\nError is occured!(maybe previous step)->\
742                                 Turn off and restart!\n");
743
744                 /* Stop USB */
745                 usbd->usb_stop();
746                 return 0;
747
748         case COMMAND_RAM_BOOT:
749                 usbd->usb_stop();
750                 boot_cmd(ramaddr);
751                 return 0;
752
753         case COMMAND_RAMDISK_MODE:
754                 printf("COMMAND_RAMDISK_MODE\n");
755 #ifdef CONFIG_RAMDISK_ADDR
756                 if (arg) {
757                         down_ram_addr = usbd->ram_addr;
758                 } else {
759                         down_ram_addr = CONFIG_RAMDISK_ADDR;
760                         run_cmd("ramboot");
761                 }
762 #endif
763                 return 1;
764
765 #ifdef CONFIG_DOWN_PHONE
766         case COMMAND_DOWN_PHONE:
767                 printf("COMMAND_RESET_PHONE\n");
768
769                 usbd_phone_down();
770
771                 *((ulong *) usbd->tx_data) = STATUS_DONE;
772                 usbd->send_data(usbd->tx_data, usbd->tx_len);
773                 return 1;
774
775         case COMMAND_CHANGE_USB:
776                 printf("COMMAND_CHANGE_USB\n");
777
778                 /* Stop USB */
779                 usbd->usb_stop();
780
781                 usbd_path_change();
782
783                 do_reset();
784                 return 0;
785 #endif
786         case COMMAND_CSA_CLEAR:
787                 printf("COMMAND_CSA_CLEAR\n");
788                 part_id = get_part_id("csa");
789                 img_type = IMG_MODEM;
790                 break;
791
792         case COMMAND_PROGRESS:
793                 if (usbd->set_progress)
794                         usbd->set_progress(arg);
795                 return 1;
796
797         default:
798                 printf("Error: Unknown command -> (%d)\n", (int)cmd);
799                 return 1;
800         }
801
802         /* Erase and Write to NAND */
803         switch (img_type) {
804         case IMG_BOOT:
805 #ifdef CONFIG_S5PC1XX
806                 /* Workaround: for prevent revision mismatch */
807                 if (cpu_is_s5pc110() && (down_mode != MODE_FORCE)) {
808                         int img_rev = 1;
809                         long *img_header = (long *)down_ram_addr;
810
811                         if (*img_header == 0xea000012)
812                                 img_rev = 0;
813
814                         if (img_rev != s5pc1xx_get_cpu_rev()) {
815                                 printf("CPU revision mismatch!\n");
816                                 *((ulong *) usbd->tx_data) = STATUS_ERROR;
817                                 usbd->send_data(usbd->tx_data, usbd->tx_len);
818                                 return 0;
819                         }
820                 }
821 #endif
822 #if defined(CONFIG_ENV_IS_IN_NAND) || defined(CONFIG_ENV_IS_IN_ONENAND)
823                 /* Erase the environment also when write bootloader */
824                 {
825                         int param_id;
826                         param_id = get_part_id("params");
827
828                         if (param_id == -1) {
829                                 sprintf(offset, "%x", CONFIG_ENV_OFFSET);
830                                 sprintf(length, "%x", CONFIG_ENV_SIZE);
831                         } else {
832                                 sprintf(offset, "%x", parts[param_id]->offset);
833                                 sprintf(length, "%x", parts[param_id]->size);
834                         }
835                         nand_cmd(0, offset, length, NULL);
836                 }
837 #endif
838                 /* Fall through for write bootloader */
839         case IMG_KERNEL:
840                 sprintf(offset, "%x", parts[part_id]->offset);
841                 sprintf(length, "%x", parts[part_id]->size);
842
843                 /* Erase */
844                 nand_cmd(0, offset, length, NULL);
845                 /* Write */
846                 sprintf(length, "%x", (unsigned int) len);
847                 ret = nand_cmd(1, ramaddr, offset, length);
848                 break;
849
850         /* File Systems */
851         case IMG_FILESYSTEM:
852                 ret = write_file_system(ramaddr, len, offset, length,
853                                 part_id, ubi_update);
854                 break;
855
856         case IMG_MODEM:
857                 sprintf(offset, "%x", parts[part_id]->offset);
858                 sprintf(length, "%x", parts[part_id]->size);
859
860                 /* Erase */
861                 nand_cmd(0, offset, length, NULL);
862
863                 /* Write : arg (0 Modem) / (1 CSA) */
864                 if (!arg) {
865                         sprintf(length, "%x", (unsigned int) len);
866                         ret = nand_cmd(1, ramaddr, offset, length);
867                 }
868                 break;
869
870 #ifdef CONFIG_CMD_MMC
871         case IMG_MMC:
872                 if (mmc_part_write)
873                         ret = write_file_mmc_part(usbd, ramaddr,
874                                                 len, offset, length);
875                 else
876                         ret = write_file_mmc(usbd, ramaddr,
877                                                 len, offset, length);
878                 break;
879 #endif
880
881         default:
882                 /* Retry? */
883                 write_part--;
884         }
885
886         if (ret) {
887                 /* Retry this commad */
888                 *((ulong *) usbd->tx_data) = STATUS_RETRY;
889                 usbd->send_data(usbd->tx_data, usbd->tx_len);
890                 return 1;
891         } else
892                 *((ulong *) usbd->tx_data) = STATUS_DONE;
893
894         /* Write image success -> Report status */
895         usbd->send_data(usbd->tx_data, usbd->tx_len);
896
897         write_part++;
898
899         /* Reset write count for another image */
900         if (flag) {
901                 write_part = 0;
902                 fs_offset = 0;
903 #ifdef CONFIG_USE_YAFFS
904                 yaffs_len = 0;
905 #endif
906         }
907
908         return 1;
909 }
910
911 static const char *recv_key = "SAMSUNG";
912 static const char *tx_key = "MPL";
913
914 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
915 {
916         struct usbd_ops *usbd;
917         int err;
918
919         if (argc > 1)
920                 down_mode = simple_strtoul(argv[1], NULL, 10);
921         else
922                 down_mode = MODE_NORMAL;
923
924         printf("USB Downloader v%s\n", APP_VERSION);
925
926         /* get partition info */
927         err = get_part_info();
928         if (err)
929                 return err;
930
931         /* interface setting */
932         usbd = usbd_set_interface(&usbd_ops);
933         down_ram_addr = usbd->ram_addr;
934
935         /* init the usb controller */
936         usbd->usb_init();
937
938         /* receive setting */
939         usbd->recv_setup(usbd->rx_data, usbd->rx_len);
940
941         /* detect the download request from Host PC */
942         if (usbd->recv_data()) {
943                 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
944                         printf("Download request from the Host PC\n");
945                         msleep(30);
946
947                         strcpy(usbd->tx_data, tx_key);
948                         usbd->send_data(usbd->tx_data, usbd->tx_len);
949                 } else {
950                         printf("No download request from the Host PC!! 1\n");
951                         return 0;
952                 }
953         } else {
954                 printf("No download request from the Host PC!!\n");
955                 return 0;
956         }
957
958         printf("Receive the packet\n");
959
960         /* receive the data from Host PC */
961         while (1) {
962                 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
963
964                 if (usbd->recv_data()) {
965                         if (process_data(usbd) == 0)
966                                 return 0;
967                 }
968         }
969
970         return 0;
971 }
972
973 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
974         "Initialize USB device and Run USB Downloader (specific)",
975         "- normal mode\n"
976         "usbdown mode - specific mode (0: NORAML, 1: FORCE)\n"
977 );