2 * USB Downloader for SAMSUNG Platform
4 * Copyright (C) 2007-2008 Samsung Electronics
5 * Minkyu Kang <mk7.kang@samsung.com>
11 #include <asm/errno.h>
14 /* version of USB Downloader Application */
15 #define APP_VERSION "1.6.0"
20 #ifdef CONFIG_CMD_MTDPARTS
21 #include <jffs2/load_kernel.h>
22 static struct part_info *parts[16];
25 #ifdef CONFIG_UBIFS_MK
26 #include <mkfs.ubifs.h>
35 static const char pszMe[] = "usbd: ";
37 static struct usbd_ops usbd_ops;
39 static unsigned int part_id;
40 static unsigned int write_part = 0;
41 static unsigned int fs_offset = 0x0;
43 #ifdef CONFIG_USE_YAFFS
44 static unsigned int yaffs_len = 0;
45 static unsigned char yaffs_data[2112];
46 #define YAFFS_PAGE 2112
49 #define NAND_PAGE_SIZE 2048
51 static unsigned long down_ram_addr;
55 /* cpu/${CPU} dependent */
56 extern void do_reset(void);
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[]);
62 int mtdparts_init(void);
63 int find_dev_and_part(const char*, struct mtd_device**, u8*, struct part_info**);
65 /* common/cmd_jffs2.c */
66 extern struct list_head devices;
68 static u8 count_mtdparts(void)
70 struct list_head *dentry, *pentry;
71 struct mtd_device *dev;
74 list_for_each(dentry, &devices) {
75 dev = list_entry(dentry, struct mtd_device, link);
77 /* list partitions for given device */
78 list_for_each(pentry, &dev->parts)
86 static int check_ubi_mode(void)
91 env_ubifs = getenv("ubi");
92 ubi_mode = !strcmp(env_ubifs, "enabled");
97 #define check_ubi_mode() 0
100 #ifdef CONFIG_MTD_PARTITIONS
101 static int get_part_info(void)
103 struct mtd_device *dev;
111 #if defined(CONFIG_CMD_NAND)
112 sprintf(nand_name, "nand0");
113 #elif defined(CONFIG_CMD_ONENAND)
114 sprintf(nand_name, "onenand0");
116 printf("Configure your NAND sub-system\n");
123 ubi_mode = check_ubi_mode();
125 part_num = count_mtdparts();
126 for (i = 0; i < part_num; i++) {
127 sprintf(part_name, "%s,%d", nand_name, i);
129 if (find_dev_and_part(part_name, &dev, &out_partnum, &parts[i]))
136 static int get_part_id(char *name)
138 int nparts = count_mtdparts();
141 for (i = 0; i < nparts; i++) {
142 if (strcmp(parts[i]->name, name) == 0)
146 printf("Error: Unknown partition -> %s\n", name);
150 static int get_part_info(void)
152 printf("Error: Can't get patition information\n");
156 static int get_part_id(char *name)
162 static void boot_cmd(char *addr)
164 char *argv[] = { "bootm", addr };
165 do_bootm(NULL, 0, 2, argv);
168 static void run_cmd(char *cmd)
170 char *argv[] = { "run", cmd };
171 do_run(NULL, 0, 2, argv);
174 #if defined(CONFIG_CMD_NAND)
175 extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
176 #elif defined(CONFIG_CMD_ONENAND)
177 extern int do_onenand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
180 /* NAND erase and write using nand command */
181 static int nand_cmd(int type, char *p1, char *p2, char *p3)
185 int (*nand_func) (cmd_tbl_t *, int, int, char **);
187 #if defined(CONFIG_CMD_NAND)
188 sprintf(nand_name, "nand");
190 #elif defined(CONFIG_CMD_ONENAND)
191 sprintf(nand_name, "onenand");
192 nand_func = do_onenand;
194 printf("Configure your NAND sub-system\n");
199 char *argv[] = {nand_name, "erase", p1, p2};
200 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
201 ret = nand_func(NULL, 0, 4, argv);
202 } else if (type == 1) {
203 char *argv[] = {nand_name, "write", p1, p2, p3};
204 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
206 ret = nand_func(NULL, 0, 5, argv);
207 } else if (type == 2) {
208 char *argv[] = {nand_name, "write.yaffs", p1, p2, p3};
209 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
211 ret = nand_func(NULL, 0, 5, argv);
212 } else if (type == 3) {
213 char *argv[] = {nand_name, "lock", p1, p2};
214 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
215 ret = nand_func(NULL, 0, 4, argv);
219 printf("Error: NAND Command\n");
224 #ifdef CONFIG_CMD_UBI
225 int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
227 int ubi_cmd(int part, char *p1, char *p2, char *p3)
231 if (part == RAMDISK_PART_ID) {
232 char *argv[] = {"ubi", "write", p1, "rootfs.cramfs", p2, p3};
233 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
234 argv[3], argv[4], argv[5]);
235 ret = do_ubi(NULL, 0, 6, argv);
236 } else if (part == FILESYSTEM_PART_ID) {
237 char *argv[] = {"ubi", "write", p1, "factoryfs.cramfs", p2, p3};
238 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
239 argv[3], argv[4], argv[5]);
240 ret = do_ubi(NULL, 0, 6, argv);
241 } else if (part == FILESYSTEM2_PART_ID) {
242 char *argv[] = {"ubi", "write", p1, "datafs.ubifs", p2, p3};
243 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
244 argv[3], argv[4], argv[5]);
245 ret = do_ubi(NULL, 0, 6, argv);
252 #ifdef CONFIG_CMD_MMC
256 static struct mmc *mmc;
258 static int mmc_cmd(int ops, int dev_num, ulong start, lbaint_t cnt, void *addr)
263 printf("mmc write 0x%x 0x%x\n", start, cnt);
264 ret = mmc->block_dev.block_write(dev_num, start, cnt, addr);
266 printf("mmc read 0x%x 0x%x\n", start, cnt);
267 ret = mmc->block_dev.block_read(dev_num, start, cnt, addr);
278 #define NORMAL_PARTITION 0
279 #define EXTEND_PARTITION 1
281 #define EXTEND_PART_TYPE 5
283 #define EXTEND_MAX_PART 32
285 static unsigned int cur_blk_offset;
286 static unsigned int cur_part_size;
287 static unsigned int cur_part;
289 static unsigned int mmc_parts;
290 static unsigned int mmc_part_write;
292 static u8 part_mode = 0;
294 struct partition_info {
298 } __attribute__((packed));
300 struct partition_header {
301 u8 fat32head[16]; /* RFSHEAD identifier */
302 struct partition_info partition[EXTEND_MAX_PART];
303 u8 res[112]; /* only data (without MBR) */
304 } __attribute__((packed));
306 struct mul_partition_info {
307 u32 lba_begin; /* absolute address from 0 block */
309 } __attribute__((packed));
311 #define MBR_OFFSET 0x10
313 struct partition_header part_info;
315 struct mul_partition_info mul_info[EXTEND_MAX_PART];
317 static int write_mmc_partition(struct usbd_ops *usbd, u32 *ram_addr, u32 len)
325 if (cur_part_size > len) {
326 blocks = len / usbd->mmc_blk;
329 blocks = cur_part_size / usbd->mmc_blk;
330 ret = len - cur_part_size;
333 if (len % usbd->mmc_blk)
336 loop = blocks / usbd->mmc_max;
337 if (blocks % usbd->mmc_max)
340 for (i = 0; i < loop; i++) {
342 cnt = blocks % usbd->mmc_max;
349 mmc_cmd(OPS_WRITE, usbd->mmc_dev, cur_blk_offset,
350 cnt, (void *)*ram_addr);
352 cur_blk_offset += cnt;
354 *ram_addr += (cnt * usbd->mmc_blk);
360 static int write_file_mmc(struct usbd_ops *usbd, u32 len)
367 if (!usbd->mmc_total) {
368 printf("MMC is not supported!\n");
372 ram_addr = (u32)down_ram_addr;
374 if (cur_blk_offset == 0) {
378 memcpy(&part_info, (void *)ram_addr,
379 sizeof(struct partition_header));
381 ram_addr += sizeof(struct partition_header);
382 len -= sizeof(struct partition_header);
383 mbr = (struct mbr *)ram_addr;
384 mbr_blk_size = mbr->parts[0].lba;
386 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
387 part_mode = NORMAL_PARTITION;
389 /* modify sectors of p1 */
390 mbr->parts[0].nsectors = usbd->mmc_total -
392 mbr->parts[1].nsectors +
393 mbr->parts[2].nsectors +
394 mbr->parts[3].nsectors);
398 /* modify lba_begin of p2 and p3 and p4 */
399 for (i = 1; i < 4; i++) {
400 if (part_info.partition[i].size == 0)
405 mbr->parts[i - 1].lba +
406 mbr->parts[i - 1].nsectors;
410 memcpy(&mbr_info, mbr, sizeof(struct mbr));
412 printf("Total Size: 0x%08x #parts %d\n",
413 (unsigned int)usbd->mmc_total,
415 for (i = 0; i < mmc_parts; i++) {
416 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
417 mbr_info.parts[i].lba,
418 mbr_info.parts[i].nsectors);
422 mmc_cmd(OPS_WRITE, usbd->mmc_dev, 0,
423 mbr_blk_size, (void *)ram_addr);
425 ram_addr += mbr_blk_size * usbd->mmc_blk;
426 len -= mbr_blk_size * usbd->mmc_blk;
428 cur_blk_offset = mbr_blk_size;
430 cur_part_size = part_info.partition[0].size;
432 /* modify p1's total sector */
433 bs = (boot_sector *)ram_addr;
434 bs->total_sect = mbr_info.parts[0].nsectors;
436 printf("\nWrite Partition %d.. %d blocks\n",
438 part_info.partition[cur_part].size /
441 part_mode = EXTEND_PARTITION;
443 for (i = 0; i < EXTEND_MAX_PART; i++) {
444 if (part_info.partition[i].size == 0)
452 printf("found %d partitions\n", mmc_parts);
454 /* build partition table */
456 mul_info[0].num_sectors =
457 usbd->mmc_total - mbr_blk_size;
458 for (i = 1; i < mmc_parts; i++) {
459 mul_info[i].num_sectors =
460 part_info.partition[i].res +
461 mbr_blk_size; /* add MBR */
464 for (i = 1; i < mmc_parts; i++) {
465 mul_info[0].num_sectors -=
466 mul_info[i].num_sectors;
469 mul_info[0].lba_begin = mbr_blk_size;
470 for (i = 1; i < mmc_parts; i++) {
471 mul_info[i].lba_begin =
472 mul_info[i-1].lba_begin +
473 mul_info[i-1].num_sectors;
476 /* modify MBR of extended partition: p1 */
477 mbr->parts[0].nsectors =
478 usbd->mmc_total - mbr_blk_size;
480 /* modify MBR of first logical partition: p5 */
482 (ram_addr + mbr_blk_size * usbd->mmc_blk);
484 mbr->parts[0].nsectors =
485 mul_info[0].num_sectors - mbr_blk_size;
487 mul_info[1].lba_begin - mbr_blk_size;
489 /* modify BPB data of p5 */
491 ((u32)mbr + mbr_blk_size * usbd->mmc_blk);
492 memset(&bs->sectors, 0, 2);
493 bs->total_sect = mbr->parts[0].nsectors;
495 printf("Total Size: 0x%08x #parts %d\n",
496 (unsigned int)usbd->mmc_total,
498 for (i = 0; i < mmc_parts; i++) {
499 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
500 mul_info[i].lba_begin,
501 mul_info[i].num_sectors);
506 cur_part_size = part_info.partition[0].size;
508 printf("\nWrite Partition %d.. %d blocks\n",
510 part_info.partition[cur_part].size /
515 for (i = cur_part; i < mmc_parts; i++) {
516 ret = write_mmc_partition(usbd, &ram_addr, len);
519 cur_part_size -= len;
524 part_info.partition[cur_part].size;
526 if (part_mode == NORMAL_PARTITION) {
528 mbr_info.parts[cur_part].lba;
531 mul_info[cur_part].lba_begin;
533 if (cur_part < mmc_parts) {
534 mbr = (struct mbr *)ram_addr;
536 mul_info[cur_part+1].lba_begin -
546 printf("\nWrite Partition %d.. %d blocks\n",
548 part_info.partition[cur_part].size /
556 static int write_file_mmc_part(struct usbd_ops *usbd, u32 len)
564 ram_addr = (u32)down_ram_addr;
567 if (cur_blk_offset == 0) {
569 mmc_cmd(OPS_READ, usbd->mmc_dev, 0,
570 sizeof(struct mbr)/usbd->mmc_blk, (void *)mbr);
572 mbr_blk_size = mbr->parts[0].lba;
574 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
575 part_mode = NORMAL_PARTITION;
577 for (i = 0; i < 4; i++) {
578 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
579 mbr_info.parts[i].lba,
580 mbr_info.parts[i].nsectors);
584 mbr->parts[mmc_part_write - 1].lba;
585 cur_part = mmc_part_write - 1;
588 mbr->parts[mmc_part_write - 1].nsectors;
590 if (mmc_part_write == 1) {
591 ram_addr += sizeof(struct mbr);
592 cur_blk_offset += sizeof(struct mbr) /
594 len -= sizeof(struct mbr);
597 part_mode = EXTEND_PARTITION;
599 for (i = 1; i < mmc_part_write; i++) {
600 ofs = mbr->parts[0].lba + mbr->parts[1].lba;
601 printf("P%d start blk: 0x%x, size: 0x%x\n", i,
602 ofs, mbr->parts[1].nsectors);
603 mmc_cmd(OPS_READ, usbd->mmc_dev, ofs,
604 (sizeof(struct mbr) / usbd->mmc_blk),
608 ofs = mbr->parts[0].lba + mbr->parts[1].lba;
609 printf("P%d start blk: 0x%x, size: 0x%x\n", i,
610 ofs, mbr->parts[1].nsectors);
612 ofs += mbr_blk_size; /* skip MBR */
613 cur_blk_offset = ofs;
614 cur_part = mmc_part_write - 1;
617 mbr->parts[1].nsectors;
619 if (mmc_part_write == 1) {
622 /* modify BPB data of p1 */
623 mmc_cmd(OPS_READ, usbd->mmc_dev, cur_blk_offset,
624 (sizeof(struct mbr) / usbd->mmc_blk),
627 bs = (boot_sector *)mbr;
628 total_sect = bs->total_sect;
630 bs = (boot_sector *)ram_addr;
631 memset(&bs->sectors, 0, 2);
632 bs->total_sect = total_sect;
637 printf("\nWrite Partition %d.. %d blocks\n",
639 len / (int)usbd->mmc_blk);
641 write_mmc_partition(usbd, &ram_addr, len);
647 static unsigned int mbr_offset[16];
648 static int mbr_parts = 0;
650 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
652 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
672 static void set_mbr_info(struct usbd_ops *usbd, char *ramaddr, u32 len)
678 unsigned int size[16];
681 strncpy(mbr_str, ramaddr, len);
684 for (i = 0; ; i++, p = NULL) {
685 tok = strtok(p, ",");
688 strcpy(save[i], tok);
689 printf("part%d: %s\n", i, save[i]);
693 printf("find %d partitions\n", mbr_parts);
695 for (i = 0; i < mbr_parts; i++) {
697 size[i] = memsize_parse(p, (const char **)&p) / 512;
700 puts("save the MBR Table...\n");
701 set_mbr_table(0x800, mbr_parts, size, mbr_offset);
704 static int write_mmc_image(struct usbd_ops *usbd, unsigned int len, int part_num)
708 if (mbr_parts <= part_num) {
709 printf("Error: MBR table have %d partitions (request %d)\n",
710 mbr_parts, part_num);
715 /* modify size of UMS partition */
716 if (part_num == 4 && fs_offset == 0) {
718 bs = (boot_sector *)down_ram_addr;
719 memset(&bs->sectors, 0, 2);
720 bs->total_sect = usbd->mmc_total - mbr_offset[part_num];
723 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
724 mbr_offset[part_num] + fs_offset,
726 (void *)down_ram_addr);
728 fs_offset += (len / usbd->mmc_blk);
733 static int write_file_system(char *ramaddr, ulong len, char *offset,
734 char *length, int part_num, int ubi_update)
736 #ifdef CONFIG_USE_YAFFS
742 #ifdef CONFIG_CMD_UBI
745 sprintf(length, "0x%x", (uint)len);
746 ret = ubi_cmd(part_id, ramaddr, length, "cont");
751 /* Erase entire partition at the first writing */
752 if (write_part == 0 && ubi_update == 0) {
753 sprintf(offset, "0x%x", (uint)parts[part_num]->offset);
754 sprintf(length, "0x%x", (uint)parts[part_num]->size);
755 nand_cmd(0, offset, length, NULL);
758 #ifdef CONFIG_USE_YAFFS
759 /* if using yaffs, wirte size must be 2112*X
760 * so, must have to realloc, and writing */
761 if (!strcmp("yaffs", getenv(parts[part_num]->name))) {
764 memcpy((void *)down_ram_addr, yaffs_data, yaffs_len);
766 actual_len = len + yaffs_len;
767 yaffs_len = actual_len % YAFFS_PAGE;
768 len = actual_len - yaffs_len;
770 memset(yaffs_data, 0x00, YAFFS_PAGE);
771 memcpy(yaffs_data, (char *)down_ram_addr + len, yaffs_len);
775 sprintf(offset, "0x%x", (uint)(parts[part_num]->offset + fs_offset));
776 sprintf(length, "0x%x", (uint)len);
778 #ifdef CONFIG_USE_YAFFS
780 ret = nand_cmd(2, ramaddr, offset, length);
782 ret = nand_cmd(1, ramaddr, offset, length);
784 if (!strcmp("yaffs", getenv(parts[part_num]->name)))
785 fs_offset += len / YAFFS_PAGE * NAND_PAGE_SIZE;
791 ret = nand_cmd(1, ramaddr, offset, length);
797 static int qboot_erase = 0;
799 /* Erase the qboot */
800 static void erase_qboot_area(void)
802 char offset[12], length[12];
808 qboot_id = get_part_id("qboot");
810 if (qboot_id != -1) {
811 printf("\nCOMMAND_ERASE_QBOOT\n");
812 sprintf(offset, "%x", parts[qboot_id]->offset);
813 sprintf(length, "%x", parts[qboot_id]->size);
814 nand_cmd(0, offset, length, NULL);
819 /* Parsing received data packet and Process data */
820 static int process_data(struct usbd_ops *usbd)
822 unsigned int cmd = 0, arg = 0, ofs = 0, len = 0, flag = 0;
823 char offset[12], length[12], ramaddr[12];
825 unsigned int blocks = 0;
831 block_dev_desc_t *block_dev;
833 sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
836 cmd = *((ulong *) usbd->rx_data + 0);
837 arg = *((ulong *) usbd->rx_data + 1);
838 len = *((ulong *) usbd->rx_data + 2);
839 flag = *((ulong *) usbd->rx_data + 3);
841 /* Reset tx buffer */
842 memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
844 ubi_mode = check_ubi_mode();
847 case COMMAND_DOWNLOAD_IMAGE:
848 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
850 #ifdef CONFIG_USE_YAFFS
851 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
852 printf("Download to 0x%08x, %d bytes\n",
853 (uint)down_ram_addr + yaffs_len, (int)len);
856 down_ram_addr = usbd->ram_addr + 0x1000000;
858 down_ram_addr = usbd->ram_addr;
860 usbd->recv_setup((char *)down_ram_addr, (int)len);
861 printf("Download to 0x%08x, %d bytes\n",
862 (uint)down_ram_addr, (int)len);
865 usbd->send_data(usbd->tx_data, usbd->tx_len);
867 /* Receive image by using dma */
868 recvlen = usbd->recv_data();
870 printf("Error: wrong image size -> %d/%d\n",
871 (int)recvlen, (int)len);
873 /* Retry this commad */
874 *((ulong *) usbd->tx_data) = STATUS_RETRY;
876 *((ulong *) usbd->tx_data) = STATUS_DONE;
878 usbd->send_data(usbd->tx_data, usbd->tx_len);
881 /* Report partition info */
882 case COMMAND_PARTITION_SYNC:
885 #ifdef CONFIG_CMD_UBI
887 if (part_id == RAMDISK_PART_ID ||
888 part_id == FILESYSTEM_PART_ID ||
889 part_id == FILESYSTEM2_PART_ID) {
890 /* change to yaffs style */
894 if (part_id == FILESYSTEM3_PART_ID) {
895 /* change ubi style */
901 if (part_id == FILESYSTEM3_PART_ID)
902 part_id = get_part_id("UBI");
903 else if (part_id == MODEM_PART_ID)
904 part_id = get_part_id("modem");
905 else if (part_id == KERNEL_PART_ID)
906 part_id = get_part_id("kernel");
907 else if (part_id == BOOT_PART_ID)
908 part_id = get_part_id("bootloader");
913 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
915 blocks = parts[part_id]->size / 1024 / 128;
916 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
919 *((ulong *) usbd->tx_data) = blocks;
920 usbd->send_data(usbd->tx_data, usbd->tx_len);
923 case COMMAND_WRITE_PART_0:
925 printf("COMMAND_WRITE_PART_0\n");
928 case COMMAND_WRITE_PART_1:
929 printf("COMMAND_WRITE_PART_BOOT\n");
930 part_id = get_part_id("bootloader");
934 case COMMAND_WRITE_PART_2:
935 case COMMAND_ERASE_PARAMETER:
936 printf("COMMAND_PARAMETER - not support!\n");
939 case COMMAND_WRITE_PART_3:
940 printf("COMMAND_WRITE_KERNEL\n");
941 part_id = get_part_id("kernel");
942 img_type = IMG_KERNEL;
945 case COMMAND_WRITE_PART_4:
946 printf("COMMAND_WRITE_ROOTFS\n");
947 part_id = get_part_id("Root");
948 img_type = IMG_FILESYSTEM;
952 case COMMAND_WRITE_PART_5:
953 printf("COMMAND_WRITE_FACTORYFS\n");
954 part_id = get_part_id("Fact");
955 img_type = IMG_FILESYSTEM;
959 case COMMAND_WRITE_PART_6:
960 printf("COMMAND_WRITE_DATAFS\n");
961 part_id = get_part_id("Data");
962 img_type = IMG_FILESYSTEM;
966 case COMMAND_WRITE_PART_7:
967 printf("COMMAND_WRITE_UBI\n");
968 part_id = get_part_id("UBI");
969 img_type = IMG_FILESYSTEM;
971 /* someday, it will be deleted */
975 case COMMAND_WRITE_PART_8:
976 printf("COMMAND_WRITE_MODEM\n");
977 part_id = get_part_id("modem");
978 img_type = IMG_MODEM;
981 #ifdef CONFIG_CMD_MMC
982 case COMMAND_WRITE_PART_9:
983 printf("COMMAND_WRITE_MMC\n");
985 mmc_part_write = arg;
989 case COMMAND_WRITE_IMG_0:
990 printf("COMMAND_WRITE_MBR\n");
994 case COMMAND_WRITE_IMG_1:
995 printf("COMMAND_WRITE_BOOTLOADER\n");
996 img_type = IMG_BOOTLOADER;
999 case COMMAND_WRITE_IMG_2:
1000 printf("COMMAND_WRITE_KERNEL\n");
1001 img_type = IMG_KERNEL_V2;
1005 case COMMAND_WRITE_IMG_3:
1006 printf("COMMAND_WRITE_MODEM\n");
1007 img_type = IMG_MODEM_V2;
1011 case COMMAND_WRITE_IMG_4:
1012 printf("COMMAND_WRITE_BOOT_PART\n");
1017 case COMMAND_WRITE_IMG_5:
1018 printf("COMMAND_WRITE_SYSTEM_PART\n");
1023 case COMMAND_WRITE_IMG_6:
1024 printf("COMMAND_WRITE_UMS_PART\n");
1029 case COMMAND_WRITE_UBI_INFO:
1030 printf("COMMAND_WRITE_UBI_INFO\n");
1033 #ifdef CONFIG_CMD_UBI
1035 sprintf(length, "0x%x", (uint)len);
1036 ret = ubi_cmd(part_id, ramaddr, length, "begin");
1039 printf("Error: Not UBI mode\n");
1043 *((ulong *) usbd->tx_data) = ret;
1044 /* Write image success -> Report status */
1045 usbd->send_data(usbd->tx_data, usbd->tx_len);
1048 /* Download complete -> reset */
1049 case COMMAND_RESET_PDA:
1050 printf("\nDownload finished and Auto reset!\nWait........\n");
1055 if (usbd->cpu_reset)
1063 case COMMAND_RESET_USB:
1064 printf("\nError is occured!(maybe previous step)->\
1065 Turn off and restart!\n");
1071 case COMMAND_RAM_BOOT:
1076 case COMMAND_RAMDISK_MODE:
1077 printf("COMMAND_RAMDISK_MODE\n");
1078 #ifdef CONFIG_RAMDISK_ADDR
1080 down_ram_addr = usbd->ram_addr;
1082 down_ram_addr = CONFIG_RAMDISK_ADDR;
1088 #ifdef CONFIG_DOWN_PHONE
1089 case COMMAND_DOWN_PHONE:
1090 printf("COMMAND_RESET_PHONE\n");
1094 *((ulong *) usbd->tx_data) = STATUS_DONE;
1095 usbd->send_data(usbd->tx_data, usbd->tx_len);
1098 case COMMAND_CHANGE_USB:
1099 printf("COMMAND_CHANGE_USB\n");
1109 case COMMAND_CSA_CLEAR:
1110 printf("COMMAND_CSA_CLEAR\n");
1111 part_id = get_part_id("csa");
1112 img_type = IMG_MODEM;
1115 case COMMAND_PROGRESS:
1116 if (usbd->set_progress)
1117 usbd->set_progress(arg);
1121 printf("Error: Unknown command -> (%d)\n", (int)cmd);
1125 /* Erase and Write to NAND */
1128 ofs = parts[part_id]->offset;
1129 #ifdef CONFIG_S5PC1XX
1130 /* Workaround: for prevent revision mismatch */
1131 if (cpu_is_s5pc110() && (down_mode != MODE_FORCE)) {
1133 long *img_header = (long *)down_ram_addr;
1135 if (*img_header == 0xea000012)
1137 else if (*(img_header + 0x400) == 0xea000012)
1140 if (img_rev != s5p_get_cpu_rev()) {
1141 printf("CPU revision mismatch!\n"
1142 "bootloader is %s%s\n"
1143 "download image is %s%s\n",
1144 s5p_get_cpu_rev() ? "EVT1" : "EVT0",
1145 s5p_get_cpu_rev() == 2 ? "-Fused" : "",
1146 img_rev ? "EVT1" : "EVT0",
1147 img_rev == 2 ? "-Fused" : "");
1148 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1149 usbd->send_data(usbd->tx_data, usbd->tx_len);
1155 /* Only u-boot.bin is allowed */
1157 long *img_header = (long *)down_ram_addr;
1159 if (*img_header != 0xea000018) {
1160 printf("\n!!! ERROR !!!\n"
1161 "Please download the u-boot.bin.\n"
1162 "Other images are not allowed.\n\n");
1163 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1164 usbd->send_data(usbd->tx_data, usbd->tx_len);
1169 #if defined(CONFIG_ENV_IS_IN_NAND) || defined(CONFIG_ENV_IS_IN_ONENAND)
1170 /* Erase the environment also when write bootloader */
1173 param_id = get_part_id("params");
1175 if (param_id == -1) {
1176 sprintf(offset, "%x", CONFIG_ENV_ADDR);
1177 sprintf(length, "%x", CONFIG_ENV_SIZE);
1179 sprintf(offset, "%x", parts[param_id]->offset);
1180 sprintf(length, "%x", parts[param_id]->size);
1182 nand_cmd(0, offset, length, NULL);
1185 sprintf(offset, "%x", (uint)ofs);
1187 sprintf(length, "%x", parts[part_id]->size - (uint)ofs);
1189 sprintf(length, "%x", parts[part_id]->size);
1192 nand_cmd(0, offset, length, NULL);
1194 sprintf(length, "%x", (unsigned int) len);
1195 ret = nand_cmd(1, ramaddr, offset, length);
1199 sprintf(offset, "%x", parts[part_id]->offset);
1200 sprintf(length, "%x", parts[part_id]->size);
1203 nand_cmd(0, offset, length, NULL);
1205 sprintf(length, "%x", (unsigned int) len);
1206 ret = nand_cmd(1, ramaddr, offset, length);
1212 case IMG_FILESYSTEM:
1213 ret = write_file_system(ramaddr, len, offset, length,
1214 part_id, ubi_update);
1220 sprintf(offset, "%x", parts[part_id]->offset);
1221 sprintf(length, "%x", parts[part_id]->size);
1225 nand_cmd(0, offset, length, NULL);
1227 printf("CSA Clear will be skipped temporary\n");
1229 /* Check ubi image, 0x23494255 is UBI# */
1231 long *img_header = (long *)down_ram_addr;
1233 if (*img_header == 0x23494255)
1237 #ifdef CONFIG_UBIFS_MK
1238 void *dest_addr = (void *) down_ram_addr + 0xc00000;
1239 void *src_addr = (void *) down_ram_addr;
1240 int leb_size, max_leb_cnt, mkfs_min_io_size;
1241 unsigned long ubifs_dest_size, ubi_dest_size;
1242 #ifdef CONFIG_S5PC110
1243 mkfs_min_io_size = 4096;
1244 leb_size = 248 * 1024;
1246 #elif CONFIG_S5PC210
1247 mkfs_min_io_size = 2048;
1248 leb_size = 126 * 1024;
1251 printf("Start making ubifs\n");
1252 ret = mkfs(src_addr, len, dest_addr, &ubifs_dest_size,
1253 mkfs_min_io_size, leb_size, max_leb_cnt);
1255 printf("Error : making ubifs failed\n");
1258 printf("Complete making ubifs\n");
1261 #ifdef CONFIG_UBINIZE
1262 int peb_size, ubi_min_io_size, subpage_size, vid_hdr_offs;
1263 #ifdef CONFIG_S5PC110
1264 ubi_min_io_size = 4096;
1265 peb_size = 256 * 1024;
1266 subpage_size = 4096;
1268 #elif CONFIG_S5PC210
1269 ubi_min_io_size = 2048;
1270 peb_size = 128 * 1024;
1274 printf("Start ubinizing\n");
1275 ret = ubinize(dest_addr, ubifs_dest_size,
1276 src_addr, &ubi_dest_size,
1277 peb_size, ubi_min_io_size,
1278 subpage_size, vid_hdr_offs);
1280 printf("Error : ubinizing failed\n");
1283 printf("Complete ubinizing\n");
1285 len = (unsigned int) ubi_dest_size;
1289 /* Write : arg (0 Modem) / (1 CSA) */
1291 sprintf(length, "%x", (unsigned int) len);
1292 ret = nand_cmd(1, ramaddr, offset, length);
1297 #ifdef CONFIG_CMD_MMC
1301 ret = write_file_mmc_part(usbd, len);
1303 ret = write_file_mmc(usbd, len);
1309 ret = write_mmc_image(usbd, len, part_id);
1313 #ifdef CONFIG_CMD_MBR
1314 set_mbr_info(usbd, (char *)down_ram_addr, len);
1318 case IMG_BOOTLOADER:
1319 #ifdef CONFIG_BOOTLADER_SECTOR
1320 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
1321 CONFIG_BOOTLOADER_SECTOR,
1322 len / usbd->mmc_blk + 1,
1323 (void *)down_ram_addr);
1329 block_dev = mmc_get_dev(0);
1332 printf("no mmc block dev\n");
1337 ret = fat_register_device(block_dev, part_id);
1339 printf("error : fat_register_divce\n");
1344 ret = file_fat_write("uImage", down_ram_addr, len);
1346 printf("error : writing uImage\n");
1354 block_dev = mmc_get_dev(0);
1357 printf("no mmc block dev\n");
1362 ret = fat_register_device(block_dev, part_id);
1364 printf("error : fat_register_divce\n");
1369 ret = file_fat_write("modem.bin", down_ram_addr, len);
1371 printf("error : writing modem.bin\n");
1383 /* Retry this commad */
1384 *((ulong *) usbd->tx_data) = STATUS_RETRY;
1385 usbd->send_data(usbd->tx_data, usbd->tx_len);
1388 *((ulong *) usbd->tx_data) = STATUS_DONE;
1390 /* Write image success -> Report status */
1391 usbd->send_data(usbd->tx_data, usbd->tx_len);
1395 /* Reset write count for another image */
1399 #ifdef CONFIG_USE_YAFFS
1407 static const char *recv_key = "SAMSUNG";
1408 static const char *tx_key = "MPL";
1410 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1412 struct usbd_ops *usbd;
1416 down_mode = simple_strtoul(argv[1], NULL, 10);
1418 down_mode = MODE_NORMAL;
1420 printf("USB Downloader v%s\n", APP_VERSION);
1422 /* get partition info */
1423 err = get_part_info();
1427 /* interface setting */
1428 usbd = usbd_set_interface(&usbd_ops);
1429 down_ram_addr = usbd->ram_addr;
1431 #ifdef CONFIG_CMD_MBR
1433 mbr_parts = get_mbr_table(mbr_offset);
1437 puts("using default MBR\n");
1439 mbrparts = getenv("mbrparts");
1440 set_mbr_info(usbd, mbrparts, strlen(mbrparts));
1444 /* init the usb controller */
1445 if (!usbd->usb_init()) {
1446 usbd->down_cancel();
1449 mmc = find_mmc_device(usbd->mmc_dev);
1452 /* receive setting */
1453 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1455 /* detect the download request from Host PC */
1456 if (usbd->recv_data()) {
1457 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
1458 printf("Download request from the Host PC\n");
1461 strcpy(usbd->tx_data, tx_key);
1462 usbd->send_data(usbd->tx_data, usbd->tx_len);
1464 printf("No download request from the Host PC!! 1\n");
1468 usbd->down_cancel();
1473 printf("Receive the packet\n");
1475 /* receive the data from Host PC */
1477 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1479 if (usbd->recv_data()) {
1480 if (process_data(usbd) == 0)
1488 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
1489 "Initialize USB device and Run USB Downloader (specific)",
1491 "usbdown mode - specific mode (0: NORAML, 1: FORCE)"