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.4.1"
20 #ifdef CONFIG_CMD_MTDPARTS
21 #include <jffs2/load_kernel.h>
22 static struct part_info *parts[16];
25 static const char pszMe[] = "usbd: ";
27 static struct usbd_ops usbd_ops;
29 static unsigned int part_id;
30 static unsigned int write_part = 0;
31 static unsigned long fs_offset = 0x0;
33 #ifdef CONFIG_USE_YAFFS
34 static unsigned int yaffs_len = 0;
35 static unsigned char yaffs_data[2112];
36 #define YAFFS_PAGE 2112
39 #define NAND_PAGE_SIZE 2048
41 static unsigned long down_ram_addr;
45 /* cpu/${CPU} dependent */
46 extern void do_reset(void);
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[]);
52 int mtdparts_init(void);
53 int find_dev_and_part(const char*, struct mtd_device**, u8*, struct part_info**);
55 /* common/cmd_jffs2.c */
56 extern struct list_head devices;
58 static u8 count_mtdparts(void)
60 struct list_head *dentry, *pentry;
61 struct mtd_device *dev;
64 list_for_each(dentry, &devices) {
65 dev = list_entry(dentry, struct mtd_device, link);
67 /* list partitions for given device */
68 list_for_each(pentry, &dev->parts)
76 static int check_ubi_mode(void)
81 env_ubifs = getenv("ubi");
82 ubi_mode = !strcmp(env_ubifs, "enabled");
87 #define check_ubi_mode() 0
90 #ifdef CONFIG_MTD_PARTITIONS
91 static int get_part_info(void)
93 struct mtd_device *dev;
101 #if defined(CONFIG_CMD_NAND)
102 sprintf(nand_name, "nand0");
103 #elif defined(CONFIG_CMD_ONENAND)
104 sprintf(nand_name, "onenand0");
106 printf("Configure your NAND sub-system\n");
113 ubi_mode = check_ubi_mode();
115 part_num = count_mtdparts();
116 for (i = 0; i < part_num; i++) {
117 sprintf(part_name, "%s,%d", nand_name, i);
119 if (find_dev_and_part(part_name, &dev, &out_partnum, &parts[i]))
126 static int get_part_id(char *name)
128 int nparts = count_mtdparts();
131 for (i = 0; i < nparts; i++) {
132 if (strcmp(parts[i]->name, name) == 0)
136 printf("Error: Unknown partition -> %s\n", name);
140 static int get_part_info(void)
142 printf("Error: Can't get patition information\n");
146 static int get_part_id(char *name)
152 static void boot_cmd(char *addr)
154 char *argv[] = { "bootm", addr };
155 do_bootm(NULL, 0, 2, argv);
158 static void run_cmd(char *cmd)
160 char *argv[] = { "run", cmd };
161 do_run(NULL, 0, 2, argv);
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[]);
170 /* NAND erase and write using nand command */
171 static int nand_cmd(int type, char *p1, char *p2, char *p3)
175 int (*nand_func) (cmd_tbl_t *, int, int, char **);
177 #if defined(CONFIG_CMD_NAND)
178 sprintf(nand_name, "nand");
180 #elif defined(CONFIG_CMD_ONENAND)
181 sprintf(nand_name, "onenand");
182 nand_func = do_onenand;
184 printf("Configure your NAND sub-system\n");
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],
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],
201 ret = nand_func(NULL, 0, 5, argv);
205 printf("Error: NAND Command\n");
210 #ifdef CONFIG_CMD_UBI
211 int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
213 int ubi_cmd(int part, char *p1, char *p2, char *p3)
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);
238 #ifdef CONFIG_CMD_MMC
241 extern int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
243 static int mmc_cmd(int ops, char *p1, char *p2, char *p3)
248 char *argv[] = {"mmc", "write", "0", p1, p2, p3};
249 ret = do_mmcops(NULL, 0, 6, argv);
251 char *argv[] = {"mmc", "read", "0", p1, p2, p3};
252 ret = do_mmcops(NULL, 0, 6, argv);
258 static unsigned int cur_blk_offset;
259 static unsigned int cur_part_size;
260 static unsigned int cur_partition;
262 static unsigned int mmc_parts;
263 static unsigned int mmc_part_write;
265 struct partition_info {
269 } __attribute__((packed));
271 struct partition_header {
272 u8 fat32head[16]; /* RFSHEAD identifier */
273 struct partition_info partition[4];
274 u8 res[448]; /* reserved */
275 } __attribute__((packed));
277 struct partition_table {
284 } __attribute__((packed));
288 struct partition_table partition[4];
290 } __attribute__((packed));
292 #define MBR_OFFSET 0x10
294 struct partition_header part_info;
295 struct mbr_table mbr_info;
297 static int write_mmc_partition(struct usbd_ops *usbd, u32 *ram_addr, u32 len)
300 char offset[12], length[12], ramaddr[12];
306 if (cur_part_size > len) {
307 blocks = len / usbd->mmc_blk;
310 blocks = cur_part_size / usbd->mmc_blk;
311 ret = len - cur_part_size;
314 if (len % usbd->mmc_blk)
317 loop = blocks / usbd->mmc_max;
318 if (blocks % usbd->mmc_max)
321 for (i = 0; i < loop; i++) {
323 cnt = blocks % usbd->mmc_max;
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);
335 cur_blk_offset += cnt;
337 *ram_addr += (cnt * usbd->mmc_blk);
343 static int write_file_mmc(struct usbd_ops *usbd, char *ramaddr, u32 len,
344 char *offset, char *length)
350 ram_addr = (u32)down_ram_addr;
352 if (cur_blk_offset == 0) {
354 struct mbr_table *mbr;
356 memcpy(&part_info, (void *)ram_addr,
357 sizeof(struct partition_header));
359 ram_addr += sizeof(struct partition_header);
360 len -= sizeof(struct partition_header);
361 mbr = (struct mbr_table*)ram_addr;
363 /* modify sectors of p1 */
364 mbr->partition[0].num_sectors = usbd->mmc_total -
366 mbr->partition[1].num_sectors +
367 mbr->partition[2].num_sectors +
368 mbr->partition[3].num_sectors);
372 /* modify lba_begin of p2 and p3 and p4 */
373 for (i = 1; i < 4; i++) {
374 if (part_info.partition[i].size == 0)
378 mbr->partition[i].lba_begin =
379 mbr->partition[i - 1].lba_begin +
380 mbr->partition[i - 1].num_sectors;
384 memcpy(&mbr_info, mbr, sizeof(struct mbr_table));
386 printf("Total Size: 0x%08x #parts %d\n",
387 (unsigned int)usbd->mmc_total, mmc_parts);
388 for (i = 0; i < mmc_parts; i++) {
389 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
390 mbr_info.partition[i].lba_begin,
391 mbr_info.partition[i].num_sectors);
395 sprintf(length, "%x", MBR_OFFSET);
396 sprintf(offset, "%x", 0);
397 sprintf(ramaddr, "0x%x", (u32)ram_addr);
398 ret = mmc_cmd(OPS_WRITE, ramaddr, offset, length);
400 ram_addr += (MBR_OFFSET * usbd->mmc_blk);
401 len -= (MBR_OFFSET * usbd->mmc_blk);
403 cur_blk_offset = MBR_OFFSET;
405 cur_part_size = part_info.partition[0].size;
407 /* modify p1's total sector */
408 bs = (boot_sector *)ram_addr;
409 bs->total_sect = mbr_info.partition[0].num_sectors;
411 printf("\nWrite Partition %d.. %d blocks\n",
413 part_info.partition[cur_partition].size /
417 for (i = cur_partition; i < mmc_parts; i++) {
418 ret = write_mmc_partition(usbd, &ram_addr, len);
421 cur_part_size -= len;
426 part_info.partition[cur_partition].size;
428 mbr_info.partition[cur_partition].lba_begin;
435 printf("\nWrite Partition %d.. %d blocks\n",
437 part_info.partition[cur_partition].size /
445 static int write_file_mmc_part(struct usbd_ops *usbd, char *ramaddr, u32 len,
446 char *offset, char *length)
452 ram_addr = (u32)down_ram_addr;
454 if (cur_blk_offset == 0) {
456 sprintf(length, "%x", MBR_OFFSET);
457 sprintf(offset, "%x", 0);
458 sprintf(ramaddr, "0x%x", (u32)&mbr_info);
459 ret = mmc_cmd(OPS_READ, ramaddr, offset, length);
461 for (i = 0; i < 4; i++) {
462 printf("p%d\t0x%08x\t0x%08x\n", i + 1,
463 mbr_info.partition[i].lba_begin,
464 mbr_info.partition[i].num_sectors);
467 cur_blk_offset = (unsigned int)
468 mbr_info.partition[mmc_part_write - 1].lba_begin;
469 cur_partition = mmc_part_write - 1;
470 cur_part_size = (unsigned int)len;
472 if (mmc_part_write == 1) {
473 ram_addr += sizeof(boot_sector);
474 cur_blk_offset += sizeof(boot_sector) / usbd->mmc_blk;
475 len -= sizeof(boot_sector);
479 printf("\nWrite Partition %d.. %d blocks\n",
481 len / (int)usbd->mmc_blk);
483 write_mmc_partition(usbd, &ram_addr, len);
489 static int write_file_system(char *ramaddr, ulong len, char *offset,
490 char *length, int part_num, int ubi_update)
492 #ifdef CONFIG_USE_YAFFS
498 #ifdef CONFIG_CMD_UBI
501 sprintf(length, "0x%x", (uint)len);
502 ret = ubi_cmd(part_id, ramaddr, length, "cont");
507 /* Erase entire partition at the first writing */
508 if (write_part == 0 && ubi_update == 0) {
509 sprintf(offset, "0x%x", (uint)parts[part_num]->offset);
510 sprintf(length, "0x%x", (uint)parts[part_num]->size);
511 nand_cmd(0, offset, length, NULL);
514 #ifdef CONFIG_USE_YAFFS
515 /* if using yaffs, wirte size must be 2112*X
516 * so, must have to realloc, and writing */
517 if (!strcmp("yaffs", getenv(parts[part_num]->name))) {
520 memcpy((void *)down_ram_addr, yaffs_data, yaffs_len);
522 actual_len = len + yaffs_len;
523 yaffs_len = actual_len % YAFFS_PAGE;
524 len = actual_len - yaffs_len;
526 memset(yaffs_data, 0x00, YAFFS_PAGE);
527 memcpy(yaffs_data, (char *)down_ram_addr + len, yaffs_len);
531 sprintf(offset, "0x%x", (uint)(parts[part_num]->offset + fs_offset));
532 sprintf(length, "0x%x", (uint)len);
534 #ifdef CONFIG_USE_YAFFS
536 ret = nand_cmd(2, ramaddr, offset, length);
538 ret = nand_cmd(1, ramaddr, offset, length);
540 if (!strcmp("yaffs", getenv(parts[part_num]->name)))
541 fs_offset += len / YAFFS_PAGE * NAND_PAGE_SIZE;
547 ret = nand_cmd(1, ramaddr, offset, length);
553 /* Parsing received data packet and Process data */
554 static int process_data(struct usbd_ops *usbd)
556 ulong cmd = 0, arg = 0, len = 0, flag = 0;
557 char offset[12], length[12], ramaddr[12];
559 unsigned int blocks = 0;
565 sprintf(ramaddr, "0x%x", (uint) down_ram_addr);
568 cmd = *((ulong *) usbd->rx_data + 0);
569 arg = *((ulong *) usbd->rx_data + 1);
570 len = *((ulong *) usbd->rx_data + 2);
571 flag = *((ulong *) usbd->rx_data + 3);
573 /* Reset tx buffer */
574 memset(usbd->tx_data, 0, sizeof(usbd->tx_data));
576 ubi_mode = check_ubi_mode();
579 case COMMAND_DOWNLOAD_IMAGE:
580 printf("\nCOMMAND_DOWNLOAD_IMAGE\n");
582 #ifdef CONFIG_USE_YAFFS
583 usbd->recv_setup((char *)down_ram_addr + yaffs_len, (int)len);
584 printf("Download to 0x%08x, %d bytes\n",
585 (uint)down_ram_addr + yaffs_len, (int)len);
587 usbd->recv_setup((char *)down_ram_addr, (int)len);
588 printf("Download to 0x%08x, %d bytes\n",
589 (uint)down_ram_addr, (int)len);
592 usbd->send_data(usbd->tx_data, usbd->tx_len);
594 /* Receive image by using dma */
595 recvlen = usbd->recv_data();
597 printf("Error: wrong image size -> %d/%d\n",
598 (int)recvlen, (int)len);
600 /* Retry this commad */
601 *((ulong *) usbd->tx_data) = STATUS_RETRY;
603 *((ulong *) usbd->tx_data) = STATUS_DONE;
605 usbd->send_data(usbd->tx_data, usbd->tx_len);
608 /* Report partition info */
609 case COMMAND_PARTITION_SYNC:
612 #ifdef CONFIG_CMD_UBI
614 if (part_id == RAMDISK_PART_ID ||
615 part_id == FILESYSTEM_PART_ID ||
616 part_id == FILESYSTEM2_PART_ID) {
617 /* change to yaffs style */
621 if (part_id == FILESYSTEM3_PART_ID) {
622 /* change ubi style */
628 if (part_id == FILESYSTEM3_PART_ID)
629 part_id = get_part_id("UBI");
630 else if (part_id == MODEM_PART_ID)
631 part_id = get_part_id("modem");
632 else if (part_id == KERNEL_PART_ID)
633 part_id = get_part_id("kernel");
638 printf("COMMAND_PARTITION_SYNC - Part%d\n", part_id);
640 blocks = parts[part_id]->size / 1024 / 128;
641 printf("COMMAND_PARTITION_SYNC - Part%d, %d blocks\n",
644 *((ulong *) usbd->tx_data) = blocks;
645 usbd->send_data(usbd->tx_data, usbd->tx_len);
648 case COMMAND_WRITE_PART_0:
650 printf("COMMAND_WRITE_PART_0\n");
653 case COMMAND_WRITE_PART_1:
654 printf("COMMAND_WRITE_PART_BOOT\n");
655 part_id = get_part_id("bootloader");
659 case COMMAND_WRITE_PART_2:
660 case COMMAND_ERASE_PARAMETER:
661 printf("COMMAND_PARAMETER - not support!\n");
664 case COMMAND_WRITE_PART_3:
665 printf("COMMAND_WRITE_KERNEL\n");
666 part_id = get_part_id("kernel");
667 img_type = IMG_KERNEL;
670 case COMMAND_WRITE_PART_4:
671 printf("COMMAND_WRITE_ROOTFS\n");
672 part_id = get_part_id("Root");
673 img_type = IMG_FILESYSTEM;
677 case COMMAND_WRITE_PART_5:
678 printf("COMMAND_WRITE_FACTORYFS\n");
679 part_id = get_part_id("Fact");
680 img_type = IMG_FILESYSTEM;
684 case COMMAND_WRITE_PART_6:
685 printf("COMMAND_WRITE_DATAFS\n");
686 part_id = get_part_id("Data");
687 img_type = IMG_FILESYSTEM;
691 case COMMAND_WRITE_PART_7:
692 printf("COMMAND_WRITE_UBI\n");
693 part_id = get_part_id("UBI");
694 img_type = IMG_FILESYSTEM;
696 /* someday, it will be deleted */
700 case COMMAND_WRITE_PART_8:
701 printf("COMMAND_WRITE_MODEM\n");
702 part_id = get_part_id("modem");
703 img_type = IMG_MODEM;
706 case COMMAND_WRITE_PART_9:
707 printf("COMMAND_WRITE_MMC\n");
709 mmc_part_write = arg;
712 case COMMAND_WRITE_UBI_INFO:
713 printf("COMMAND_WRITE_UBI_INFO\n");
716 #ifdef CONFIG_CMD_UBI
718 sprintf(length, "0x%x", (uint)len);
719 ret = ubi_cmd(part_id, ramaddr, length, "begin");
722 printf("Error: Not UBI mode\n");
726 *((ulong *) usbd->tx_data) = ret;
727 /* Write image success -> Report status */
728 usbd->send_data(usbd->tx_data, usbd->tx_len);
731 /* Download complete -> reset */
732 case COMMAND_RESET_PDA:
733 printf("\nDownload finished and Auto reset!\nWait........\n");
742 case COMMAND_RESET_USB:
743 printf("\nError is occured!(maybe previous step)->\
744 Turn off and restart!\n");
750 case COMMAND_RAM_BOOT:
755 case COMMAND_RAMDISK_MODE:
756 printf("COMMAND_RAMDISK_MODE\n");
757 #ifdef CONFIG_RAMDISK_ADDR
759 down_ram_addr = usbd->ram_addr;
761 down_ram_addr = CONFIG_RAMDISK_ADDR;
767 #ifdef CONFIG_DOWN_PHONE
768 case COMMAND_DOWN_PHONE:
769 printf("COMMAND_RESET_PHONE\n");
773 *((ulong *) usbd->tx_data) = STATUS_DONE;
774 usbd->send_data(usbd->tx_data, usbd->tx_len);
777 case COMMAND_CHANGE_USB:
778 printf("COMMAND_CHANGE_USB\n");
788 case COMMAND_CSA_CLEAR:
789 printf("COMMAND_CSA_CLEAR\n");
790 part_id = get_part_id("csa");
791 img_type = IMG_MODEM;
794 case COMMAND_PROGRESS:
795 if (usbd->set_progress)
796 usbd->set_progress(arg);
800 printf("Error: Unknown command -> (%d)\n", (int)cmd);
804 /* Erase and Write to NAND */
807 #ifdef CONFIG_S5PC1XX
808 /* Workaround: for prevent revision mismatch */
809 if (cpu_is_s5pc110() && (down_mode != MODE_FORCE)) {
811 long *img_header = (long *)down_ram_addr;
813 if (*img_header == 0xea000012)
816 if (img_rev != s5pc1xx_get_cpu_rev()) {
817 printf("CPU revision mismatch!\n");
818 *((ulong *) usbd->tx_data) = STATUS_ERROR;
819 usbd->send_data(usbd->tx_data, usbd->tx_len);
824 #if defined(CONFIG_ENV_IS_IN_NAND) || defined(CONFIG_ENV_IS_IN_ONENAND)
825 /* Erase the environment also when write bootloader */
828 param_id = get_part_id("params");
830 if (param_id == -1) {
831 sprintf(offset, "%x", CONFIG_ENV_OFFSET);
832 sprintf(length, "%x", CONFIG_ENV_SIZE);
834 sprintf(offset, "%x", parts[param_id]->offset);
835 sprintf(length, "%x", parts[param_id]->size);
837 nand_cmd(0, offset, length, NULL);
840 /* Fall through for write bootloader */
842 sprintf(offset, "%x", parts[part_id]->offset);
843 sprintf(length, "%x", parts[part_id]->size);
846 nand_cmd(0, offset, length, NULL);
848 sprintf(length, "%x", (unsigned int) len);
849 ret = nand_cmd(1, ramaddr, offset, length);
854 ret = write_file_system(ramaddr, len, offset, length,
855 part_id, ubi_update);
859 sprintf(offset, "%x", parts[part_id]->offset);
860 sprintf(length, "%x", parts[part_id]->size);
863 nand_cmd(0, offset, length, NULL);
865 /* Write : arg (0 Modem) / (1 CSA) */
867 sprintf(length, "%x", (unsigned int) len);
868 ret = nand_cmd(1, ramaddr, offset, length);
872 #ifdef CONFIG_CMD_MMC
875 ret = write_file_mmc_part(usbd, ramaddr,
876 len, offset, length);
878 ret = write_file_mmc(usbd, ramaddr,
879 len, offset, length);
889 /* Retry this commad */
890 *((ulong *) usbd->tx_data) = STATUS_RETRY;
891 usbd->send_data(usbd->tx_data, usbd->tx_len);
894 *((ulong *) usbd->tx_data) = STATUS_DONE;
896 /* Write image success -> Report status */
897 usbd->send_data(usbd->tx_data, usbd->tx_len);
901 /* Reset write count for another image */
905 #ifdef CONFIG_USE_YAFFS
913 static const char *recv_key = "SAMSUNG";
914 static const char *tx_key = "MPL";
916 int do_usbd_down(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
918 struct usbd_ops *usbd;
922 down_mode = simple_strtoul(argv[1], NULL, 10);
924 down_mode = MODE_NORMAL;
926 printf("USB Downloader v%s\n", APP_VERSION);
928 /* get partition info */
929 err = get_part_info();
933 /* interface setting */
934 usbd = usbd_set_interface(&usbd_ops);
935 down_ram_addr = usbd->ram_addr;
937 /* init the usb controller */
940 /* receive setting */
941 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
943 /* detect the download request from Host PC */
944 if (usbd->recv_data()) {
945 if (strncmp(usbd->rx_data, recv_key, strlen(recv_key)) == 0) {
946 printf("Download request from the Host PC\n");
949 strcpy(usbd->tx_data, tx_key);
950 usbd->send_data(usbd->tx_data, usbd->tx_len);
952 printf("No download request from the Host PC!! 1\n");
956 printf("No download request from the Host PC!!\n");
960 printf("Receive the packet\n");
962 /* receive the data from Host PC */
964 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
966 if (usbd->recv_data()) {
967 if (process_data(usbd) == 0)
975 U_BOOT_CMD(usbdown, CONFIG_SYS_MAXARGS, 1, do_usbd_down,
976 "Initialize USB device and Run USB Downloader (specific)",
978 "usbdown mode - specific mode (0: NORAML, 1: FORCE)\n"