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 sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
834 cmd = *((ulong *) usbd->rx_data + 0);
835 arg = *((ulong *) usbd->rx_data + 1);
836 len = *((ulong *) usbd->rx_data + 2);
837 flag = *((ulong *) usbd->rx_data + 3);
839 /* Reset tx buffer */
840 memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
842 ubi_mode = check_ubi_mode();
845 case COMMAND_DOWNLOAD_IMAGE:
846 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
848 #ifdef CONFIG_USE_YAFFS
849 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
850 printf("Download to 0x%08x, %d bytes\n",
851 (uint)down_ram_addr + yaffs_len, (int)len);
854 down_ram_addr = usbd->ram_addr + 0x1000000;
856 down_ram_addr = usbd->ram_addr;
858 usbd->recv_setup((char *)down_ram_addr, (int)len);
859 printf("Download to 0x%08x, %d bytes\n",
860 (uint)down_ram_addr, (int)len);
863 usbd->send_data(usbd->tx_data, usbd->tx_len);
865 /* Receive image by using dma */
866 recvlen = usbd->recv_data();
868 printf("Error: wrong image size -> %d/%d\n",
869 (int)recvlen, (int)len);
871 /* Retry this commad */
872 *((ulong *) usbd->tx_data) = STATUS_RETRY;
874 *((ulong *) usbd->tx_data) = STATUS_DONE;
876 usbd->send_data(usbd->tx_data, usbd->tx_len);
879 /* Report partition info */
880 case COMMAND_PARTITION_SYNC:
883 #ifdef CONFIG_CMD_UBI
885 if (part_id == RAMDISK_PART_ID ||
886 part_id == FILESYSTEM_PART_ID ||
887 part_id == FILESYSTEM2_PART_ID) {
888 /* change to yaffs style */
892 if (part_id == FILESYSTEM3_PART_ID) {
893 /* change ubi style */
899 if (part_id == FILESYSTEM3_PART_ID)
900 part_id = get_part_id("UBI");
901 else if (part_id == MODEM_PART_ID)
902 part_id = get_part_id("modem");
903 else if (part_id == KERNEL_PART_ID)
904 part_id = get_part_id("kernel");
905 else if (part_id == BOOT_PART_ID)
906 part_id = get_part_id("bootloader");
911 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
913 blocks = parts[part_id]->size / 1024 / 128;
914 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
917 *((ulong *) usbd->tx_data) = blocks;
918 usbd->send_data(usbd->tx_data, usbd->tx_len);
921 case COMMAND_WRITE_PART_0:
923 printf("COMMAND_WRITE_PART_0\n");
926 case COMMAND_WRITE_PART_1:
927 printf("COMMAND_WRITE_PART_BOOT\n");
928 part_id = get_part_id("bootloader");
932 case COMMAND_WRITE_PART_2:
933 case COMMAND_ERASE_PARAMETER:
934 printf("COMMAND_PARAMETER - not support!\n");
937 case COMMAND_WRITE_PART_3:
938 printf("COMMAND_WRITE_KERNEL\n");
939 part_id = get_part_id("kernel");
940 img_type = IMG_KERNEL;
943 case COMMAND_WRITE_PART_4:
944 printf("COMMAND_WRITE_ROOTFS\n");
945 part_id = get_part_id("Root");
946 img_type = IMG_FILESYSTEM;
950 case COMMAND_WRITE_PART_5:
951 printf("COMMAND_WRITE_FACTORYFS\n");
952 part_id = get_part_id("Fact");
953 img_type = IMG_FILESYSTEM;
957 case COMMAND_WRITE_PART_6:
958 printf("COMMAND_WRITE_DATAFS\n");
959 part_id = get_part_id("Data");
960 img_type = IMG_FILESYSTEM;
964 case COMMAND_WRITE_PART_7:
965 printf("COMMAND_WRITE_UBI\n");
966 part_id = get_part_id("UBI");
967 img_type = IMG_FILESYSTEM;
969 /* someday, it will be deleted */
973 case COMMAND_WRITE_PART_8:
974 printf("COMMAND_WRITE_MODEM\n");
975 part_id = get_part_id("modem");
976 img_type = IMG_MODEM;
979 #ifdef CONFIG_CMD_MMC
980 case COMMAND_WRITE_PART_9:
981 printf("COMMAND_WRITE_MMC\n");
983 mmc_part_write = arg;
987 case COMMAND_WRITE_IMG_0:
988 printf("COMMAND_WRITE_MBR\n");
992 case COMMAND_WRITE_IMG_1:
993 printf("COMMAND_WRITE_BOOTLOADER\n");
994 img_type = IMG_BOOTLOADER;
997 case COMMAND_WRITE_IMG_2:
998 printf("COMMAND_WRITE_KERNEL\n");
999 /* TODO: Not support yet, just return */
1000 *((ulong *) usbd->tx_data) = STATUS_DONE;
1001 usbd->send_data(usbd->tx_data, usbd->tx_len);
1004 case COMMAND_WRITE_IMG_3:
1005 printf("COMMAND_WRITE_MODEM\n");
1006 /* TODO: Not support yet, just return */
1007 *((ulong *) usbd->tx_data) = STATUS_DONE;
1008 usbd->send_data(usbd->tx_data, usbd->tx_len);
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 set_mbr_info(usbd, (char *)down_ram_addr, len);
1316 case IMG_BOOTLOADER:
1317 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
1318 CONFIG_BOOTLOADER_SECTOR,
1319 len / usbd->mmc_blk + 1,
1320 (void *)down_ram_addr);
1329 /* Retry this commad */
1330 *((ulong *) usbd->tx_data) = STATUS_RETRY;
1331 usbd->send_data(usbd->tx_data, usbd->tx_len);
1334 *((ulong *) usbd->tx_data) = STATUS_DONE;
1336 /* Write image success -> Report status */
1337 usbd->send_data(usbd->tx_data, usbd->tx_len);
1341 /* Reset write count for another image */
1345 #ifdef CONFIG_USE_YAFFS
1353 static const char *recv_key = "SAMSUNG";
1354 static const char *tx_key = "MPL";
1356 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1358 struct usbd_ops *usbd;
1362 down_mode = simple_strtoul(argv[1], NULL, 10);
1364 down_mode = MODE_NORMAL;
1366 printf("USB Downloader v%s\n", APP_VERSION);
1368 /* get partition info */
1369 err = get_part_info();
1373 /* interface setting */
1374 usbd = usbd_set_interface(&usbd_ops);
1375 down_ram_addr = usbd->ram_addr;
1378 mbr_parts = get_mbr_table(mbr_offset);
1382 puts("using default MBR\n");
1384 mbrparts = getenv("mbrparts");
1385 set_mbr_info(usbd, mbrparts, strlen(mbrparts));
1388 /* init the usb controller */
1389 if (!usbd->usb_init()) {
1390 usbd->down_cancel();
1393 mmc = find_mmc_device(usbd->mmc_dev);
1396 /* receive setting */
1397 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1399 /* detect the download request from Host PC */
1400 if (usbd->recv_data()) {
1401 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
1402 printf("Download request from the Host PC\n");
1405 strcpy(usbd->tx_data, tx_key);
1406 usbd->send_data(usbd->tx_data, usbd->tx_len);
1408 printf("No download request from the Host PC!! 1\n");
1412 usbd->down_cancel();
1417 printf("Receive the packet\n");
1419 /* receive the data from Host PC */
1421 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1423 if (usbd->recv_data()) {
1424 if (process_data(usbd) == 0)
1432 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
1433 "Initialize USB device and Run USB Downloader (specific)",
1435 "usbdown mode - specific mode (0: NORAML, 1: FORCE)"