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 #if defined(CONFIG_CMD_NAND)
169 extern int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
170 #elif defined(CONFIG_CMD_ONENAND)
171 extern int do_onenand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
174 /* NAND erase and write using nand command */
175 static int nand_cmd(int type, char *p1, char *p2, char *p3)
179 int (*nand_func) (cmd_tbl_t *, int, int, char **);
181 #if defined(CONFIG_CMD_NAND)
182 sprintf(nand_name, "nand");
184 #elif defined(CONFIG_CMD_ONENAND)
185 sprintf(nand_name, "onenand");
186 nand_func = do_onenand;
188 printf("Configure your NAND sub-system\n");
193 char *argv[] = {nand_name, "erase", p1, p2};
194 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
195 ret = nand_func(NULL, 0, 4, argv);
196 } else if (type == 1) {
197 char *argv[] = {nand_name, "write", p1, p2, p3};
198 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
200 ret = nand_func(NULL, 0, 5, argv);
201 } else if (type == 2) {
202 char *argv[] = {nand_name, "write.yaffs", p1, p2, p3};
203 printf("%s %s %s %s %s\n", argv[0], argv[1], argv[2],
205 ret = nand_func(NULL, 0, 5, argv);
206 } else if (type == 3) {
207 char *argv[] = {nand_name, "lock", p1, p2};
208 printf("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
209 ret = nand_func(NULL, 0, 4, argv);
213 printf("Error: NAND Command\n");
218 #ifdef CONFIG_CMD_UBI
219 int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
221 int ubi_cmd(int part, char *p1, char *p2, char *p3)
225 if (part == RAMDISK_PART_ID) {
226 char *argv[] = {"ubi", "write", p1, "rootfs.cramfs", p2, p3};
227 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
228 argv[3], argv[4], argv[5]);
229 ret = do_ubi(NULL, 0, 6, argv);
230 } else if (part == FILESYSTEM_PART_ID) {
231 char *argv[] = {"ubi", "write", p1, "factoryfs.cramfs", p2, p3};
232 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
233 argv[3], argv[4], argv[5]);
234 ret = do_ubi(NULL, 0, 6, argv);
235 } else if (part == FILESYSTEM2_PART_ID) {
236 char *argv[] = {"ubi", "write", p1, "datafs.ubifs", p2, p3};
237 printf("%s %s %s %s %s %s\n", argv[0], argv[1], argv[2],
238 argv[3], argv[4], argv[5]);
239 ret = do_ubi(NULL, 0, 6, argv);
246 #ifdef CONFIG_CMD_MMC
250 static struct mmc *mmc;
252 static int mmc_cmd(int ops, int dev_num, ulong start, lbaint_t cnt, void *addr)
257 printf("mmc write 0x%x 0x%x\n", start, cnt);
258 ret = mmc->block_dev.block_write(dev_num, start, cnt, addr);
260 printf("mmc read 0x%x 0x%x\n", start, cnt);
261 ret = mmc->block_dev.block_read(dev_num, start, cnt, addr);
272 #define NORMAL_PARTITION 0
273 #define EXTEND_PARTITION 1
275 #define EXTEND_PART_TYPE 5
277 #define EXTEND_MAX_PART 32
279 static unsigned int cur_blk_offset;
280 static unsigned int cur_part_size;
281 static unsigned int cur_part;
283 static unsigned int mmc_parts;
284 static unsigned int mmc_part_write;
286 static u8 part_mode = 0;
288 struct partition_info {
292 } __attribute__((packed));
294 struct partition_header {
295 u8 fat32head[16]; /* RFSHEAD identifier */
296 struct partition_info partition[EXTEND_MAX_PART];
297 u8 res[112]; /* only data (without MBR) */
298 } __attribute__((packed));
300 struct mul_partition_info {
301 u32 lba_begin; /* absolute address from 0 block */
303 } __attribute__((packed));
305 #define MBR_OFFSET 0x10
307 struct partition_header part_info;
309 struct mul_partition_info mul_info[EXTEND_MAX_PART];
311 static int write_mmc_partition(struct usbd_ops *usbd, u32 *ram_addr, u32 len)
319 if (cur_part_size > len) {
320 blocks = len / usbd->mmc_blk;
323 blocks = cur_part_size / usbd->mmc_blk;
324 ret = len - cur_part_size;
327 if (len % usbd->mmc_blk)
330 loop = blocks / usbd->mmc_max;
331 if (blocks % usbd->mmc_max)
334 for (i = 0; i < loop; i++) {
336 cnt = blocks % usbd->mmc_max;
343 mmc_cmd(OPS_WRITE, usbd->mmc_dev, cur_blk_offset,
344 cnt, (void *)*ram_addr);
346 cur_blk_offset += cnt;
348 *ram_addr += (cnt * usbd->mmc_blk);
354 static int write_file_mmc(struct usbd_ops *usbd, u32 len)
361 if (!usbd->mmc_total) {
362 printf("MMC is not supported!\n");
366 ram_addr = (u32)down_ram_addr;
368 if (cur_blk_offset == 0) {
372 memcpy(&part_info, (void *)ram_addr,
373 sizeof(struct partition_header));
375 ram_addr += sizeof(struct partition_header);
376 len -= sizeof(struct partition_header);
377 mbr = (struct mbr *)ram_addr;
378 mbr_blk_size = mbr->parts[0].lba;
380 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
381 part_mode = NORMAL_PARTITION;
383 /* modify sectors of p1 */
384 mbr->parts[0].nsectors = usbd->mmc_total -
386 mbr->parts[1].nsectors +
387 mbr->parts[2].nsectors +
388 mbr->parts[3].nsectors);
392 /* modify lba_begin of p2 and p3 and p4 */
393 for (i = 1; i < 4; i++) {
394 if (part_info.partition[i].size == 0)
399 mbr->parts[i - 1].lba +
400 mbr->parts[i - 1].nsectors;
404 memcpy(&mbr_info, mbr, sizeof(struct mbr));
406 printf("Total Size: 0x%08x #parts %d\n",
407 (unsigned int)usbd->mmc_total,
409 for (i = 0; i < mmc_parts; i++) {
410 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
411 mbr_info.parts[i].lba,
412 mbr_info.parts[i].nsectors);
416 mmc_cmd(OPS_WRITE, usbd->mmc_dev, 0,
417 mbr_blk_size, (void *)ram_addr);
419 ram_addr += mbr_blk_size * usbd->mmc_blk;
420 len -= mbr_blk_size * usbd->mmc_blk;
422 cur_blk_offset = mbr_blk_size;
424 cur_part_size = part_info.partition[0].size;
426 /* modify p1's total sector */
427 bs = (boot_sector *)ram_addr;
428 bs->total_sect = mbr_info.parts[0].nsectors;
430 printf("\nWrite Partition %d.. %d blocks\n",
432 part_info.partition[cur_part].size /
435 part_mode = EXTEND_PARTITION;
437 for (i = 0; i < EXTEND_MAX_PART; i++) {
438 if (part_info.partition[i].size == 0)
446 printf("found %d partitions\n", mmc_parts);
448 /* build partition table */
450 mul_info[0].num_sectors =
451 usbd->mmc_total - mbr_blk_size;
452 for (i = 1; i < mmc_parts; i++) {
453 mul_info[i].num_sectors =
454 part_info.partition[i].res +
455 mbr_blk_size; /* add MBR */
458 for (i = 1; i < mmc_parts; i++) {
459 mul_info[0].num_sectors -=
460 mul_info[i].num_sectors;
463 mul_info[0].lba_begin = mbr_blk_size;
464 for (i = 1; i < mmc_parts; i++) {
465 mul_info[i].lba_begin =
466 mul_info[i-1].lba_begin +
467 mul_info[i-1].num_sectors;
470 /* modify MBR of extended partition: p1 */
471 mbr->parts[0].nsectors =
472 usbd->mmc_total - mbr_blk_size;
474 /* modify MBR of first logical partition: p5 */
476 (ram_addr + mbr_blk_size * usbd->mmc_blk);
478 mbr->parts[0].nsectors =
479 mul_info[0].num_sectors - mbr_blk_size;
481 mul_info[1].lba_begin - mbr_blk_size;
483 /* modify BPB data of p5 */
485 ((u32)mbr + mbr_blk_size * usbd->mmc_blk);
486 memset(&bs->sectors, 0, 2);
487 bs->total_sect = mbr->parts[0].nsectors;
489 printf("Total Size: 0x%08x #parts %d\n",
490 (unsigned int)usbd->mmc_total,
492 for (i = 0; i < mmc_parts; i++) {
493 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
494 mul_info[i].lba_begin,
495 mul_info[i].num_sectors);
500 cur_part_size = part_info.partition[0].size;
502 printf("\nWrite Partition %d.. %d blocks\n",
504 part_info.partition[cur_part].size /
509 for (i = cur_part; i < mmc_parts; i++) {
510 ret = write_mmc_partition(usbd, &ram_addr, len);
513 cur_part_size -= len;
518 part_info.partition[cur_part].size;
520 if (part_mode == NORMAL_PARTITION) {
522 mbr_info.parts[cur_part].lba;
525 mul_info[cur_part].lba_begin;
527 if (cur_part < mmc_parts) {
528 mbr = (struct mbr *)ram_addr;
530 mul_info[cur_part+1].lba_begin -
540 printf("\nWrite Partition %d.. %d blocks\n",
542 part_info.partition[cur_part].size /
550 static int write_file_mmc_part(struct usbd_ops *usbd, u32 len)
558 ram_addr = (u32)down_ram_addr;
561 if (cur_blk_offset == 0) {
563 mmc_cmd(OPS_READ, usbd->mmc_dev, 0,
564 sizeof(struct mbr)/usbd->mmc_blk, (void *)mbr);
566 mbr_blk_size = mbr->parts[0].lba;
568 if (mbr->parts[0].partition_type != EXTEND_PART_TYPE) {
569 part_mode = NORMAL_PARTITION;
571 for (i = 0; i < 4; i++) {
572 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
573 mbr_info.parts[i].lba,
574 mbr_info.parts[i].nsectors);
578 mbr->parts[mmc_part_write - 1].lba;
579 cur_part = mmc_part_write - 1;
582 mbr->parts[mmc_part_write - 1].nsectors;
584 if (mmc_part_write == 1) {
585 ram_addr += sizeof(struct mbr);
586 cur_blk_offset += sizeof(struct mbr) /
588 len -= sizeof(struct mbr);
591 part_mode = EXTEND_PARTITION;
593 for (i = 1; i < mmc_part_write; i++) {
594 ofs = mbr->parts[0].lba + mbr->parts[1].lba;
595 printf("P%d start blk: 0x%x, size: 0x%x\n", i,
596 ofs, mbr->parts[1].nsectors);
597 mmc_cmd(OPS_READ, usbd->mmc_dev, ofs,
598 (sizeof(struct mbr) / usbd->mmc_blk),
602 ofs = mbr->parts[0].lba + mbr->parts[1].lba;
603 printf("P%d start blk: 0x%x, size: 0x%x\n", i,
604 ofs, mbr->parts[1].nsectors);
606 ofs += mbr_blk_size; /* skip MBR */
607 cur_blk_offset = ofs;
608 cur_part = mmc_part_write - 1;
611 mbr->parts[1].nsectors;
613 if (mmc_part_write == 1) {
616 /* modify BPB data of p1 */
617 mmc_cmd(OPS_READ, usbd->mmc_dev, cur_blk_offset,
618 (sizeof(struct mbr) / usbd->mmc_blk),
621 bs = (boot_sector *)mbr;
622 total_sect = bs->total_sect;
624 bs = (boot_sector *)ram_addr;
625 memset(&bs->sectors, 0, 2);
626 bs->total_sect = total_sect;
631 printf("\nWrite Partition %d.. %d blocks\n",
633 len / (int)usbd->mmc_blk);
635 write_mmc_partition(usbd, &ram_addr, len);
641 static unsigned int mbr_offset[16];
642 static int mbr_parts = 0;
644 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
646 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
666 static void set_mbr_info(struct usbd_ops *usbd, char *ramaddr, u32 len)
672 unsigned int size[16];
675 strncpy(mbr_str, ramaddr, len);
678 for (i = 0; ; i++, p = NULL) {
679 tok = strtok(p, ",");
682 strcpy(save[i], tok);
683 printf("part%d: %s\n", i, save[i]);
687 printf("find %d partitions\n", mbr_parts);
689 for (i = 0; i < mbr_parts; i++) {
691 size[i] = memsize_parse(p, (const char **)&p) / 512;
694 puts("save the MBR Table...\n");
695 set_mbr_table(0x800, mbr_parts, size, mbr_offset);
698 static int write_mmc_image(struct usbd_ops *usbd, unsigned int len, int part_num)
702 if (mbr_parts <= part_num) {
703 printf("Error: MBR table have %d partitions (request %d)\n",
704 mbr_parts, part_num);
709 /* modify size of UMS partition */
710 if (part_num == 4 && fs_offset == 0) {
712 bs = (boot_sector *)down_ram_addr;
713 memset(&bs->sectors, 0, 2);
714 bs->total_sect = usbd->mmc_total - mbr_offset[part_num];
717 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
718 mbr_offset[part_num] + fs_offset,
720 (void *)down_ram_addr);
722 fs_offset += (len / usbd->mmc_blk);
727 static int write_fat_file(struct usbd_ops *usbd, char *file_name,
728 int part_id, int len)
730 #ifdef CONFIG_FAT_WRITE
733 ret = fat_register_device(&mmc->block_dev, part_id);
735 printf("error : fat_register_divce\n");
739 ret = file_fat_write(file_name, (void *)down_ram_addr, len);
741 printf("error : writing uImage\n");
745 printf("error: doesn't support fat_write\n");
750 static int write_file_system(char *ramaddr, ulong len, char *offset,
751 char *length, int part_num, int ubi_update)
753 #ifdef CONFIG_USE_YAFFS
759 #ifdef CONFIG_CMD_UBI
762 sprintf(length, "0x%x", (uint)len);
763 ret = ubi_cmd(part_id, ramaddr, length, "cont");
768 /* Erase entire partition at the first writing */
769 if (write_part == 0 && ubi_update == 0) {
770 sprintf(offset, "0x%x", (uint)parts[part_num]->offset);
771 sprintf(length, "0x%x", (uint)parts[part_num]->size);
772 nand_cmd(0, offset, length, NULL);
775 #ifdef CONFIG_USE_YAFFS
776 /* if using yaffs, wirte size must be 2112*X
777 * so, must have to realloc, and writing */
778 if (!strcmp("yaffs", getenv(parts[part_num]->name))) {
781 memcpy((void *)down_ram_addr, yaffs_data, yaffs_len);
783 actual_len = len + yaffs_len;
784 yaffs_len = actual_len % YAFFS_PAGE;
785 len = actual_len - yaffs_len;
787 memset(yaffs_data, 0x00, YAFFS_PAGE);
788 memcpy(yaffs_data, (char *)down_ram_addr + len, yaffs_len);
792 sprintf(offset, "0x%x", (uint)(parts[part_num]->offset + fs_offset));
793 sprintf(length, "0x%x", (uint)len);
795 #ifdef CONFIG_USE_YAFFS
797 ret = nand_cmd(2, ramaddr, offset, length);
799 ret = nand_cmd(1, ramaddr, offset, length);
801 if (!strcmp("yaffs", getenv(parts[part_num]->name)))
802 fs_offset += len / YAFFS_PAGE * NAND_PAGE_SIZE;
808 ret = nand_cmd(1, ramaddr, offset, length);
814 static int qboot_erase = 0;
816 /* Erase the qboot */
817 static void erase_qboot_area(void)
819 char offset[12], length[12];
825 qboot_id = get_part_id("qboot");
827 if (qboot_id != -1) {
828 printf("\nCOMMAND_ERASE_QBOOT\n");
829 sprintf(offset, "%x", parts[qboot_id]->offset);
830 sprintf(length, "%x", parts[qboot_id]->size);
831 nand_cmd(0, offset, length, NULL);
836 /* Parsing received data packet and Process data */
837 static int process_data(struct usbd_ops *usbd)
839 unsigned int cmd = 0, arg = 0, ofs = 0, len = 0, flag = 0;
840 char offset[12], length[12], ramaddr[12];
842 unsigned int blocks = 0;
848 sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
851 cmd = *((ulong *) usbd->rx_data + 0);
852 arg = *((ulong *) usbd->rx_data + 1);
853 len = *((ulong *) usbd->rx_data + 2);
854 flag = *((ulong *) usbd->rx_data + 3);
856 /* Reset tx buffer */
857 memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
859 ubi_mode = check_ubi_mode();
862 case COMMAND_DOWNLOAD_IMAGE:
863 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
865 #ifdef CONFIG_USE_YAFFS
866 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
867 printf("Download to 0x%08x, %d bytes\n",
868 (uint)down_ram_addr + yaffs_len, (int)len);
871 down_ram_addr = usbd->ram_addr + 0x1000000;
873 down_ram_addr = usbd->ram_addr;
875 usbd->recv_setup((char *)down_ram_addr, (int)len);
876 printf("Download to 0x%08x, %d bytes\n",
877 (uint)down_ram_addr, (int)len);
880 usbd->send_data(usbd->tx_data, usbd->tx_len);
882 /* Receive image by using dma */
883 recvlen = usbd->recv_data();
885 printf("Error: wrong image size -> %d/%d\n",
886 (int)recvlen, (int)len);
888 /* Retry this commad */
889 *((ulong *) usbd->tx_data) = STATUS_RETRY;
891 *((ulong *) usbd->tx_data) = STATUS_DONE;
893 usbd->send_data(usbd->tx_data, usbd->tx_len);
896 /* Report partition info */
897 case COMMAND_PARTITION_SYNC:
900 #ifdef CONFIG_CMD_UBI
902 if (part_id == RAMDISK_PART_ID ||
903 part_id == FILESYSTEM_PART_ID ||
904 part_id == FILESYSTEM2_PART_ID) {
905 /* change to yaffs style */
909 if (part_id == FILESYSTEM3_PART_ID) {
910 /* change ubi style */
916 if (part_id == FILESYSTEM3_PART_ID)
917 part_id = get_part_id("UBI");
918 else if (part_id == MODEM_PART_ID)
919 part_id = get_part_id("modem");
920 else if (part_id == KERNEL_PART_ID)
921 part_id = get_part_id("kernel");
922 else if (part_id == BOOT_PART_ID)
923 part_id = get_part_id("bootloader");
928 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
930 blocks = parts[part_id]->size / 1024 / 128;
931 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
934 *((ulong *) usbd->tx_data) = blocks;
935 usbd->send_data(usbd->tx_data, usbd->tx_len);
938 case COMMAND_WRITE_PART_0:
940 printf("COMMAND_WRITE_PART_0\n");
943 case COMMAND_WRITE_PART_1:
944 printf("COMMAND_WRITE_PART_BOOT\n");
945 part_id = get_part_id("bootloader");
949 case COMMAND_WRITE_PART_2:
950 case COMMAND_ERASE_PARAMETER:
951 printf("COMMAND_PARAMETER - not support!\n");
954 case COMMAND_WRITE_PART_3:
955 printf("COMMAND_WRITE_KERNEL\n");
956 part_id = get_part_id("kernel");
957 img_type = IMG_KERNEL;
960 case COMMAND_WRITE_PART_4:
961 printf("COMMAND_WRITE_ROOTFS\n");
962 part_id = get_part_id("Root");
963 img_type = IMG_FILESYSTEM;
967 case COMMAND_WRITE_PART_5:
968 printf("COMMAND_WRITE_FACTORYFS\n");
969 part_id = get_part_id("Fact");
970 img_type = IMG_FILESYSTEM;
974 case COMMAND_WRITE_PART_6:
975 printf("COMMAND_WRITE_DATAFS\n");
976 part_id = get_part_id("Data");
977 img_type = IMG_FILESYSTEM;
981 case COMMAND_WRITE_PART_7:
982 printf("COMMAND_WRITE_UBI\n");
983 part_id = get_part_id("UBI");
984 img_type = IMG_FILESYSTEM;
986 /* someday, it will be deleted */
990 case COMMAND_WRITE_PART_8:
991 printf("COMMAND_WRITE_MODEM\n");
992 part_id = get_part_id("modem");
993 img_type = IMG_MODEM;
996 #ifdef CONFIG_CMD_MMC
997 case COMMAND_WRITE_PART_9:
998 printf("COMMAND_WRITE_MMC\n");
1000 mmc_part_write = arg;
1004 case COMMAND_WRITE_IMG_0:
1005 printf("COMMAND_WRITE_MBR\n");
1009 case COMMAND_WRITE_IMG_1:
1010 printf("COMMAND_WRITE_BOOTLOADER\n");
1011 img_type = IMG_BOOTLOADER;
1014 case COMMAND_WRITE_IMG_2:
1015 printf("COMMAND_WRITE_KERNEL\n");
1016 img_type = IMG_KERNEL_V2;
1020 case COMMAND_WRITE_IMG_3:
1021 printf("COMMAND_WRITE_MODEM\n");
1022 img_type = IMG_MODEM_V2;
1026 case COMMAND_WRITE_IMG_4:
1027 printf("COMMAND_WRITE_BOOT_PART\n");
1032 case COMMAND_WRITE_IMG_5:
1033 printf("COMMAND_WRITE_SYSTEM_PART\n");
1038 case COMMAND_WRITE_IMG_6:
1039 printf("COMMAND_WRITE_UMS_PART\n");
1044 case COMMAND_WRITE_UBI_INFO:
1045 printf("COMMAND_WRITE_UBI_INFO\n");
1048 #ifdef CONFIG_CMD_UBI
1050 sprintf(length, "0x%x", (uint)len);
1051 ret = ubi_cmd(part_id, ramaddr, length, "begin");
1054 printf("Error: Not UBI mode\n");
1058 *((ulong *) usbd->tx_data) = ret;
1059 /* Write image success -> Report status */
1060 usbd->send_data(usbd->tx_data, usbd->tx_len);
1063 /* Download complete -> reset */
1064 case COMMAND_RESET_PDA:
1065 printf("\nDownload finished and Auto reset!\nWait........\n");
1070 if (usbd->cpu_reset)
1078 case COMMAND_RESET_USB:
1079 printf("\nError is occured!(maybe previous step)->\
1080 Turn off and restart!\n");
1086 case COMMAND_RAM_BOOT:
1091 case COMMAND_RAMDISK_MODE:
1092 printf("COMMAND_RAMDISK_MODE\n");
1093 #ifdef CONFIG_RAMDISK_ADDR
1095 down_ram_addr = usbd->ram_addr;
1097 down_ram_addr = CONFIG_RAMDISK_ADDR;
1098 run_command("run ramboot", 0);
1103 #ifdef CONFIG_DOWN_PHONE
1104 case COMMAND_DOWN_PHONE:
1105 printf("COMMAND_RESET_PHONE\n");
1109 *((ulong *) usbd->tx_data) = STATUS_DONE;
1110 usbd->send_data(usbd->tx_data, usbd->tx_len);
1113 case COMMAND_CHANGE_USB:
1114 printf("COMMAND_CHANGE_USB\n");
1124 case COMMAND_CSA_CLEAR:
1125 printf("COMMAND_CSA_CLEAR\n");
1126 part_id = get_part_id("csa");
1127 img_type = IMG_MODEM;
1130 case COMMAND_PROGRESS:
1131 if (usbd->set_progress)
1132 usbd->set_progress(arg);
1136 printf("Error: Unknown command -> (%d)\n", (int)cmd);
1140 /* Erase and Write to NAND */
1143 ofs = parts[part_id]->offset;
1144 #ifdef CONFIG_S5PC1XX
1145 /* Workaround: for prevent revision mismatch */
1146 if (cpu_is_s5pc110() && (down_mode != MODE_FORCE)) {
1148 long *img_header = (long *)down_ram_addr;
1150 if (*img_header == 0xea000012)
1152 else if (*(img_header + 0x400) == 0xea000012)
1155 if (img_rev != s5p_get_cpu_rev()) {
1156 printf("CPU revision mismatch!\n"
1157 "bootloader is %s%s\n"
1158 "download image is %s%s\n",
1159 s5p_get_cpu_rev() ? "EVT1" : "EVT0",
1160 s5p_get_cpu_rev() == 2 ? "-Fused" : "",
1161 img_rev ? "EVT1" : "EVT0",
1162 img_rev == 2 ? "-Fused" : "");
1163 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1164 usbd->send_data(usbd->tx_data, usbd->tx_len);
1170 /* Only u-boot.bin is allowed */
1172 long *img_header = (long *)down_ram_addr;
1174 if (*img_header != 0xea000018) {
1175 printf("\n!!! ERROR !!!\n"
1176 "Please download the u-boot.bin.\n"
1177 "Other images are not allowed.\n\n");
1178 *((ulong *) usbd->tx_data) = STATUS_ERROR;
1179 usbd->send_data(usbd->tx_data, usbd->tx_len);
1184 #if defined(CONFIG_ENV_IS_IN_NAND) || defined(CONFIG_ENV_IS_IN_ONENAND)
1185 /* Erase the environment also when write bootloader */
1188 param_id = get_part_id("params");
1190 if (param_id == -1) {
1191 sprintf(offset, "%x", CONFIG_ENV_ADDR);
1192 sprintf(length, "%x", CONFIG_ENV_SIZE);
1194 sprintf(offset, "%x", parts[param_id]->offset);
1195 sprintf(length, "%x", parts[param_id]->size);
1197 nand_cmd(0, offset, length, NULL);
1200 sprintf(offset, "%x", (uint)ofs);
1202 sprintf(length, "%x", parts[part_id]->size - (uint)ofs);
1204 sprintf(length, "%x", parts[part_id]->size);
1207 nand_cmd(0, offset, length, NULL);
1209 sprintf(length, "%x", (unsigned int) len);
1210 ret = nand_cmd(1, ramaddr, offset, length);
1214 sprintf(offset, "%x", parts[part_id]->offset);
1215 sprintf(length, "%x", parts[part_id]->size);
1218 nand_cmd(0, offset, length, NULL);
1220 sprintf(length, "%x", (unsigned int) len);
1221 ret = nand_cmd(1, ramaddr, offset, length);
1227 case IMG_FILESYSTEM:
1228 ret = write_file_system(ramaddr, len, offset, length,
1229 part_id, ubi_update);
1235 sprintf(offset, "%x", parts[part_id]->offset);
1236 sprintf(length, "%x", parts[part_id]->size);
1240 nand_cmd(0, offset, length, NULL);
1242 printf("CSA Clear will be skipped temporary\n");
1244 /* Check ubi image, 0x23494255 is UBI# */
1246 long *img_header = (long *)down_ram_addr;
1248 if (*img_header == 0x23494255)
1252 #ifdef CONFIG_UBIFS_MK
1253 void *dest_addr = (void *) down_ram_addr + 0xc00000;
1254 void *src_addr = (void *) down_ram_addr;
1255 int leb_size, max_leb_cnt, mkfs_min_io_size;
1256 unsigned long ubifs_dest_size, ubi_dest_size;
1257 #ifdef CONFIG_S5PC110
1258 mkfs_min_io_size = 4096;
1259 leb_size = 248 * 1024;
1261 #elif CONFIG_S5PC210
1262 mkfs_min_io_size = 2048;
1263 leb_size = 126 * 1024;
1266 printf("Start making ubifs\n");
1267 ret = mkfs(src_addr, len, dest_addr, &ubifs_dest_size,
1268 mkfs_min_io_size, leb_size, max_leb_cnt);
1270 printf("Error : making ubifs failed\n");
1273 printf("Complete making ubifs\n");
1276 #ifdef CONFIG_UBINIZE
1277 int peb_size, ubi_min_io_size, subpage_size, vid_hdr_offs;
1278 #ifdef CONFIG_S5PC110
1279 ubi_min_io_size = 4096;
1280 peb_size = 256 * 1024;
1281 subpage_size = 4096;
1283 #elif CONFIG_S5PC210
1284 ubi_min_io_size = 2048;
1285 peb_size = 128 * 1024;
1289 printf("Start ubinizing\n");
1290 ret = ubinize(dest_addr, ubifs_dest_size,
1291 src_addr, &ubi_dest_size,
1292 peb_size, ubi_min_io_size,
1293 subpage_size, vid_hdr_offs);
1295 printf("Error : ubinizing failed\n");
1298 printf("Complete ubinizing\n");
1300 len = (unsigned int) ubi_dest_size;
1304 /* Write : arg (0 Modem) / (1 CSA) */
1306 sprintf(length, "%x", (unsigned int) len);
1307 ret = nand_cmd(1, ramaddr, offset, length);
1312 #ifdef CONFIG_CMD_MMC
1316 ret = write_file_mmc_part(usbd, len);
1318 ret = write_file_mmc(usbd, len);
1324 ret = write_mmc_image(usbd, len, part_id);
1328 #ifdef CONFIG_CMD_MBR
1329 set_mbr_info(usbd, (char *)down_ram_addr, len);
1333 case IMG_BOOTLOADER:
1334 #ifdef CONFIG_BOOTLADER_SECTOR
1335 ret = mmc_cmd(OPS_WRITE, usbd->mmc_dev,
1336 CONFIG_BOOTLOADER_SECTOR,
1337 len / usbd->mmc_blk + 1,
1338 (void *)down_ram_addr);
1343 ret = write_fat_file(usbd, "uImage", part_id, len);
1347 ret = write_fat_file(usbd, "modem.bin", part_id, len);
1356 /* Retry this commad */
1357 *((ulong *) usbd->tx_data) = STATUS_RETRY;
1358 usbd->send_data(usbd->tx_data, usbd->tx_len);
1361 *((ulong *) usbd->tx_data) = STATUS_DONE;
1363 /* Write image success -> Report status */
1364 usbd->send_data(usbd->tx_data, usbd->tx_len);
1368 /* Reset write count for another image */
1372 #ifdef CONFIG_USE_YAFFS
1380 static const char *recv_key = "SAMSUNG";
1381 static const char *tx_key = "MPL";
1383 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1385 struct usbd_ops *usbd;
1389 down_mode = simple_strtoul(argv[1], NULL, 10);
1391 down_mode = MODE_NORMAL;
1393 printf("USB Downloader v%s\n", APP_VERSION);
1395 /* get partition info */
1396 err = get_part_info();
1400 /* interface setting */
1401 usbd = usbd_set_interface(&usbd_ops);
1402 down_ram_addr = usbd->ram_addr;
1404 #ifdef CONFIG_CMD_MBR
1406 mbr_parts = get_mbr_table(mbr_offset);
1410 puts("using default MBR\n");
1412 mbrparts = getenv("mbrparts");
1413 set_mbr_info(usbd, mbrparts, strlen(mbrparts));
1417 /* init the usb controller */
1418 if (!usbd->usb_init()) {
1419 usbd->down_cancel();
1422 mmc = find_mmc_device(usbd->mmc_dev);
1425 /* receive setting */
1426 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1428 /* detect the download request from Host PC */
1429 if (usbd->recv_data()) {
1430 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
1431 printf("Download request from the Host PC\n");
1434 strcpy(usbd->tx_data, tx_key);
1435 usbd->send_data(usbd->tx_data, usbd->tx_len);
1437 printf("No download request from the Host PC!! 1\n");
1441 usbd->down_cancel();
1446 printf("Receive the packet\n");
1448 /* receive the data from Host PC */
1450 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
1452 if (usbd->recv_data()) {
1453 if (process_data(usbd) == 0)
1461 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
1462 "Initialize USB device and Run USB Downloader (specific)",
1464 "usbdown mode - specific mode (0: NORAML, 1: FORCE)"