Changeset 222
- Timestamp:
- 02/15/10 17:12:03 (2 years ago)
- Location:
- ap51-flash/trunk
- Files:
-
- 7 modified
-
Makefile (modified) (1 diff)
-
ap51-flash.c (modified) (17 diffs)
-
ap51-flash.h (modified) (3 diffs)
-
main.c (modified) (4 diffs)
-
packet.c (modified) (5 diffs)
-
packet.h (modified) (1 diff)
-
uip-conf.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ap51-flash/trunk/Makefile
r221 r222 32 32 # enable packet debug output 33 33 # CFLAGS += -DPACKET_DEBUG 34 # enable flash from file mode 35 # CFLAGS += -DFLASH_FROM_FILE 34 36 35 37 # if you change the names here you also need to change the ap51-flash.c code -
ap51-flash/trunk/ap51-flash.c
r221 r222 40 40 41 41 char flash_mode = MODE_NONE; 42 int flash_from_file = 0; 43 struct flash_from_file fff_data[FFF_NUM]; 42 44 unsigned int remote_ip; 43 45 unsigned int local_ip; … … 94 96 /* Open the output adapter */ 95 97 if (NULL == (pcap_fp = pcap_open_live(dev, 1500, 1, PCAP_TIMEOUT_MS, error))) { 96 fprintf(stderr, "Error opening adapter: %s\n", error);98 fprintf(stderr, "Error opening adapter: %s\n", error); 97 99 return -1; 98 100 } … … 378 380 unsigned long int x = 0; 379 381 sscanf(str_ptr, "Erase from 0x%08lx", &x); 380 if (0 != x) 381 { 382 if (0 != x) { 382 383 x -= device_info->flash_addr; 383 if (x > device_info->flash_size) 384 { 384 if (x > device_info->flash_size) { 385 385 rootfs_part_size += (x - device_info->flash_size); 386 386 printf("Rootfs partition size now 0x%08x\n", rootfs_part_size); … … 426 426 goto err_close; 427 427 } 428 if (tftp_bytes_sent < (unsigned long)kernel_size) 429 { 428 if (tftp_bytes_sent < (unsigned long)kernel_size) { 430 429 fprintf(stderr, "Error transferring kernel, send=%ld, expected=%d\n", tftp_bytes_sent, kernel_size); 431 430 exit(1); … … 477 476 s->inputbuffer[0] = 0; 478 477 PSOCK_READTO(475, &s->p, '>'); 479 if (!uncomp_loader) 480 { 481 sprintf(str, "fis load %s %s\n", (0x1f == kernel_buf[0] && 0x8b ==kernel_buf[1] ? "-d" : "-l"), kernelpartname); 482 } 483 else 484 { 485 sprintf(str, "fis load %s\n", kernelpartname); 486 } 478 479 if (flash_from_file) { 480 if (!uncomp_loader) 481 sprintf(str, "fis load %s %s\n", 482 (0x1f == fff_data[FFF_KERNEL].buff[0] && 0x8b ==fff_data[FFF_KERNEL].buff[1] ? "-d" : "-l"), 483 kernelpartname); 484 else 485 sprintf(str, "fis load %s\n", kernelpartname); 486 } else { 487 if (!uncomp_loader) 488 sprintf(str, "fis load %s %s\n", 489 (0x1f == kernel_buf[0] && 0x8b ==kernel_buf[1] ? "-d" : "-l"), 490 kernelpartname); 491 else 492 sprintf(str, "fis load %s\n", kernelpartname); 493 } 494 487 495 PSOCK_SEND_STR(477, &s->p, str); 488 496 s->inputbuffer[0] = 0; … … 591 599 } 592 600 593 int ap51_flash(char* device, char* rootfs_filename, char* kernel_filename, int nvram, int uncomp, int special) 601 static int open_image_file(char *fname, int *fd, int *file_size, int *flash_size, unsigned char **buff) 602 { 603 char err_string[265]; 604 605 *fd = open(fname, O_RDONLY | O_BINARY); 606 if (*fd < 0) { 607 perror(fname); 608 goto err; 609 } 610 611 *file_size = lseek(*fd, 0, SEEK_END); 612 if (*file_size < 0) { 613 fprintf(stderr, "Unable to retrieve file size: %s\n", fname); 614 goto err_close; 615 } 616 617 *flash_size = ((*file_size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 618 if (*flash_size >= 8 * 1024 * 1024) { 619 fprintf(stderr, "File size exceeds 8MB limit: %s\n", fname); 620 goto err_close; 621 } 622 623 lseek(*fd, 0, SEEK_SET); 624 625 if (!buff) /* FLASH_FROM_FILE */ 626 return 0; 627 628 *buff = malloc(*flash_size); 629 if (!*buff) { 630 perror("no mem"); 631 goto err_close; 632 } 633 634 if (*file_size != read(*fd, *buff, *file_size)) { 635 sprintf(err_string, "%s read fails: buf=%p, size=%d", fname, *buff, *file_size); 636 perror(err_string); 637 goto err_free; 638 } 639 640 close(*fd); 641 return 1; 642 643 err_free: 644 free(*buff); 645 err_close: 646 close(*fd); 647 err: 648 return -1; 649 } 650 651 int ap51_flash(char *device, char *rootfs_filename, char *kernel_filename, int nvram, int uncomp, int special) 594 652 { 595 653 uip_ipaddr_t netmask; … … 597 655 pcap_if_t *alldevs = NULL, *d; 598 656 char *pcap_device, errbuf[PCAP_ERRBUF_SIZE]; 599 unsigned char* buf= 0;600 int i = 0, if_num= 0;601 int fd, size = 0 , ubnt_img = 0;657 int ret, i, if_num = 0, ubnt_img = 0; 658 unsigned char *buf = 0; 659 int fd, size = 0; 602 660 603 661 pcap_device = device; … … 614 672 nvram_part_size = FLASH_PAGE_SIZE; 615 673 616 /* Root file name? */ 617 if (NULL != rootfs_filename) { 618 if (-1 == (fd = open(rootfs_filename, O_RDONLY | O_BINARY))) { 619 perror(rootfs_filename); 674 #if defined(FLASH_FROM_FILE) 675 if (flash_from_file) { 676 for (i = 0; i < FFF_NUM; i++) { 677 if (!fff_data[i].fname) 678 continue; 679 680 ret = open_image_file(fff_data[i].fname, &fff_data[i].fd, 681 &fff_data[i].file_size, &fff_data[i].flash_size, NULL); 682 683 if (ret < 0) 684 return 1; 685 686 printf("Reading %s file %s with %d bytes ...\n", 687 (i == 0 ? "rootfs" : (i == 1 ? "kernel" : "ubnt")), 688 fff_data[i].fname, fff_data[i].flash_size); 689 } 690 691 goto init_pcap; 692 } 693 #endif 694 695 if (rootfs_filename) { 696 ret = open_image_file(rootfs_filename, &fd, 697 &size, &rootfs_size, &rootfs_buf); 698 699 if (ret < 0) 620 700 return 1; 621 } 622 size = lseek(fd, 0, SEEK_END); 623 rootfs_size = ((size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 624 lseek(fd, 0, SEEK_SET); 625 if (0 != (rootfs_buf = malloc(rootfs_size))) 626 { 627 if (size != read(fd, rootfs_buf, size) || 628 0 >= size || 8 * 1024 * 1024 < size) 629 { 630 char s[265]; 631 sprintf(s, "%s fails: buf=%p, size=%d", rootfs_filename, rootfs_buf, size); 632 perror(s); 633 return 1; 634 } 635 } 636 else 637 { 638 perror("no mem"); 639 return 1; 640 } 641 printf("Reading rootfs file %s with %d bytes ...\n", rootfs_filename, size); 701 702 printf("Reading rootfs file %s with %d bytes ...\n", 703 rootfs_filename, rootfs_size); 704 642 705 } else { 643 706 #if defined(EMBEDDED_DATA) && defined(WIN32) 644 707 HRSRC hRsrc; 645 708 hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_ROOTFS), RT_RCDATA); 646 if (NULL != hRsrc) 647 { 709 if (hRsrc) { 648 710 HGLOBAL hGlobal = LoadResource(NULL, hRsrc); 649 711 buf = LockResource(hGlobal); … … 655 717 #endif 656 718 657 if (0 != buf) 658 { 719 if (buf) { 659 720 rootfs_size = ((size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 660 if (0 != (rootfs_buf = malloc(rootfs_size))) 661 { 721 if (0 != (rootfs_buf = malloc(rootfs_size))) { 662 722 memset(rootfs_buf, 0xff, rootfs_size); 663 723 memmove(rootfs_buf, buf, size); 664 } 665 else 666 { 724 } else { 667 725 perror("no mem"); 668 726 return 1; … … 671 729 } 672 730 673 /* Kernel file name? */ 674 if (NULL != kernel_filename) 675 { 676 if (-1 == (fd = open(kernel_filename, O_RDONLY | O_BINARY))) 677 { 678 perror(kernel_filename); 731 if (kernel_filename) { 732 ret = open_image_file(kernel_filename, &fd, 733 &size, &kernel_size, &kernel_buf); 734 735 if (ret < 0) 679 736 return 1; 680 } 681 size = lseek(fd, 0, SEEK_END); 682 kernel_size = ((size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 683 lseek(fd, 0, SEEK_SET); 684 if (0 != (kernel_buf = malloc(kernel_size))) 685 { 686 if (size != read(fd, kernel_buf, size) || 687 0 >= size || 8 * 1024 * 1024 < size) 688 { 689 char s[265]; 690 sprintf(s, "%s fails: buf=%p, size=%d", kernel_filename, kernel_buf, size); 691 perror(s); 692 return 1; 693 } 694 } 695 else 696 { 697 perror("no mem"); 698 return 1; 699 } 700 printf("Reading kernel file %s with %d bytes ...\n", kernel_filename, size); 737 738 printf("Reading kernel file %s with %d bytes ...\n", 739 kernel_filename, kernel_size); 701 740 } else { 702 741 #if defined(EMBEDDED_DATA) && defined(WIN32) 703 742 HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_KERNEL), RT_RCDATA); 704 if (NULL != hRsrc) 705 { 743 if (hRsrc) { 706 744 HGLOBAL hGlobal = LoadResource(NULL, hRsrc); 707 745 buf = LockResource(hGlobal); … … 713 751 #endif 714 752 715 if (0 != buf) 716 { 753 if (buf) { 717 754 kernel_size = ((size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; 718 if (0 != (kernel_buf = malloc(kernel_size))) 719 { 755 if (0 != (kernel_buf = malloc(kernel_size))) { 720 756 memset(kernel_buf, 0xff, kernel_size); 721 757 memmove(kernel_buf, buf, size); 722 } 723 else 724 { 758 } else { 725 759 perror("no mem"); 726 760 return 1; … … 743 777 return 1; 744 778 } 779 780 #if defined(FLASH_FROM_FILE) 781 init_pcap: 782 #endif 745 783 746 784 srcmac.addr[0] = 0x00; … … 765 803 766 804 /* Print the list */ 805 i = 0; 767 806 for (d = alldevs; d != NULL; d = d->next) { 768 807 i++; … … 798 837 switch (flash_mode) { 799 838 case MODE_REDBOOT: 800 if (ubnt_img) { 801 fprintf(stderr, "You are trying to flash a redboot device with a ubiquiti image!\n"); 802 return 1; 839 if (flash_from_file) { 840 if ((!fff_data[FFF_ROOTFS].fname) || 841 (!fff_data[FFF_KERNEL].fname)) { 842 fprintf(stderr, "Error - redboot enabled device detected but rootfs & kernel file not available\n"); 843 return 1; 844 } 845 } else { 846 if (ubnt_img) { 847 fprintf(stderr, "Error - you are trying to flash a redboot device with a ubiquiti image!\n"); 848 return 1; 849 } 803 850 } 804 851 … … 808 855 809 856 if (NULL == uip_connect(&dstipaddr, htons(TELNET_PORT))) { 810 fprintf(stderr, " Cannot connect to port %i\n", TELNET_PORT);857 fprintf(stderr, "Error - cannot connect to port %i\n", TELNET_PORT); 811 858 return 1; 812 859 } … … 814 861 case MODE_TFTP_CLIENT: 815 862 #if defined(EMBEDDED_DATA) 816 if ( !rootfs_filename) {863 if ((!rootfs_filename) && (!flash_from_file)) { 817 864 818 865 /* free the rootfs and replace it by the ubnt image */ … … 845 892 ubnt_img = 1; 846 893 } 847 #endif 848 if (!ubnt_img) { 849 fprintf(stderr, "You are trying to flash a ubiquiti device with redboot images!\n"); 850 return 1; 851 } 852 853 tftp_xfer_buff = rootfs_buf; 854 tftp_xfer_size = rootfs_size; 894 #endif /* EMBEDDED_DATA */ 895 if (flash_from_file) { 896 if (!fff_data[FFF_UBNT].fname) { 897 fprintf(stderr, "Error - ubiquiti device detected but ubiquiti file not available\n"); 898 return 1; 899 } 900 } else { 901 if (!ubnt_img) { 902 fprintf(stderr, "Error - you are trying to flash a ubiquiti device with redboot images!\n"); 903 return 1; 904 } 905 } 906 907 tftp_xfer_buff = (flash_from_file ? (unsigned char *)&fff_data[FFF_UBNT] : rootfs_buf); 908 tftp_xfer_size = (flash_from_file ? fff_data[FFF_UBNT].flash_size : rootfs_size); 855 909 printf("Ubiquiti device detected - using TFTP client to flash\n"); 856 910 break; 857 911 default: 858 fprintf(stderr, " Could not auto-detect flash mode!\n");912 fprintf(stderr, "Error - could not auto-detect flash mode!\n"); 859 913 return 1; 860 914 } 861 915 862 fw_upload(); 863 return 0; 916 return fw_upload(); 864 917 } 865 918 -
ap51-flash/trunk/ap51-flash.h
r221 r222 71 71 }; 72 72 73 struct flash_from_file { 74 int fd; 75 int file_size; 76 int flash_size; 77 char *fname; 78 char buff[2]; 79 }; 80 73 81 /* flash modes */ 74 82 enum { … … 77 85 MODE_MAYBE_REDBOOT, 78 86 MODE_TFTP_CLIENT, 87 }; 88 89 /* flash from file data */ 90 enum { 91 FFF_ROOTFS = 0, 92 FFF_KERNEL, 93 FFF_UBNT, 94 FFF_NUM, 79 95 }; 80 96 … … 119 135 extern int kernel_size; 120 136 extern char flash_mode; 137 extern int flash_from_file; 138 extern struct flash_from_file fff_data[]; 121 139 122 140 #endif /* __ap51_FLASH_H__ */ -
ap51-flash/trunk/main.c
r202 r222 20 20 #include <stdlib.h> 21 21 #include <string.h> 22 #if defined(FLASH_FROM_FILE) 23 #include <getopt.h> 24 #endif 22 25 23 26 #include "ap51-flash.h" … … 39 42 fprintf(stderr, "%s [ethdevice] ubnt.bin flashes your ubiquiti image\n", prgname); 40 43 fprintf(stderr, "%s -v prints version information\n", prgname); 44 45 #if defined(FLASH_FROM_FILE) 46 fprintf(stderr, "\nFlash from file mode:\n"); 47 fprintf(stderr, "%s --flash-from-file [file options] interface\n", prgname); 48 fprintf(stderr, "Options:\n"); 49 fprintf(stderr, " --flash-from-file enable 'flash from file' mode\n"); 50 fprintf(stderr, " --rootfs path to rootfs file\n"); 51 fprintf(stderr, " --kernel path to kernel file\n"); 52 fprintf(stderr, " --ubnt path to ubiquiti image\n"); 53 #endif 41 54 42 55 fprintf(stderr, "\nThe 'ethdevice' has to be one of the devices that are part of the supported device list which follows.\nYou can either specify its name or the interface number.\n"); … … 72 85 int main(int argc, char* argv[]) 73 86 { 74 int nvram = 0; 75 int uncomp = 0; 76 int special = 0; 87 int nvram = 0, uncomp = 0, special = 0; 88 char *iface = NULL; 89 90 #if defined(FLASH_FROM_FILE) 91 int i, found_args = 1, optchar, option_index; 92 struct option long_options[] = 93 { 94 {"flash-from-file", no_argument, 0, 'f'}, 95 {"rootfs", required_argument, 0, 'r'}, 96 {"kernel", required_argument, 0, 'k'}, 97 {"ubnt", required_argument, 0, 'u'}, 98 {0, 0, 0, 0} 99 }; 100 #endif 77 101 78 102 if ((argc == 2) && (strcmp("-v", argv[1]) == 0)) { … … 99 123 special = 1; 100 124 } 101 // #if defined(DEBUG)102 // special = 1;103 // #endif104 125 105 /* if (argc > 2 && 0 == strcmp("uncomp", argv[argc - 1])) 106 { 126 /*if (argc > 2 && strcmp("uncomp", argv[argc - 1]) == 0) { 107 127 argc--; 108 128 uncomp = 1; 109 129 } 110 130 111 if (argc > 1 && 0 == strcmp("nvram", argv[argc - 1])) 112 { 131 if (argc > 1 && strcmp("nvram", argv[argc - 1]) == 0) { 113 132 argc--; 114 133 nvram = 1; 115 134 }*/ 116 135 117 if (argc < 2) 118 { 136 if (argc < 2) { 119 137 usage(argv[0]); 120 138 return 1; 121 139 } 122 140 123 return ap51_flash(argv[1], 2 < argc ? argv[2] : NULL, 3 < argc ? argv[3] : NULL, nvram, uncomp, special); 141 iface = argv[1]; 142 143 #if defined(FLASH_FROM_FILE) 144 for (i = 0; i < FFF_NUM; i++) 145 fff_data[i].fname = NULL; 146 147 while ((optchar = getopt_long(argc, argv, "fk:r:u:", long_options, &option_index)) != -1) { 148 switch (optchar) { 149 case 'f': 150 flash_from_file = 1; 151 found_args++; 152 break; 153 case 'k': 154 fff_data[FFF_KERNEL].fname = optarg; 155 found_args += 2; 156 break; 157 case 'r': 158 fff_data[FFF_ROOTFS].fname = optarg; 159 found_args += 2; 160 break; 161 case 'u': 162 fff_data[FFF_UBNT].fname = optarg; 163 found_args += 2; 164 break; 165 default: 166 usage(argv[0]); 167 return 1; 168 } 169 } 170 171 if (flash_from_file) { 172 if ((fff_data[FFF_ROOTFS].fname && !fff_data[FFF_KERNEL].fname) || 173 (!fff_data[FFF_ROOTFS].fname && fff_data[FFF_KERNEL].fname)) { 174 fprintf(stderr, "Error - you need to specify kernel and rootfs together or not at all\n"); 175 return 1; 176 } 177 178 if (!fff_data[FFF_ROOTFS].fname && !fff_data[FFF_KERNEL].fname && !fff_data[FFF_UBNT].fname) { 179 fprintf(stderr, "Error - you need to specify at least kernel and rootfs or ubiquiti image file\n"); 180 return 1; 181 } 182 183 if (found_args == argc) { 184 fprintf(stderr, "Error - you need to specify the interface to run 'flash from file' mode\n"); 185 return 1; 186 } else if (found_args + 1 < argc) { 187 fprintf(stderr, "Error - too many arguments to run flash from file mode\n"); 188 return 1; 189 } 190 191 iface = argv[found_args]; 192 } 193 #endif 194 195 return ap51_flash(iface, (argc > 2 ? argv[2] : NULL), (argc > 3 ? argv[3] : NULL), nvram, uncomp, special); 124 196 } -
ap51-flash/trunk/packet.c
r202 r222 123 123 } 124 124 125 static void read_from_file(void *target_buff, void *data, int len, int seek_pos) 126 { 127 struct flash_from_file *fff = (struct flash_from_file *)data; 128 int read_len = len; 129 130 if (fff->file_size < seek_pos) 131 read_len = 0; 132 else if (fff->file_size < seek_pos + len) 133 read_len = fff->file_size - seek_pos; 134 135 if (read_len > 0) { 136 if (read_len != read(fff->fd, target_buff, read_len)) { 137 perror(fff->fname); 138 exit(1); 139 } 140 } 141 142 if (read_len != len) 143 memset(target_buff + read_len, 0, len - read_len); 144 145 if (seek_pos == 0) 146 memcpy(fff->buff, target_buff, 2); 147 } 148 125 149 static void tftp_transfer(const unsigned char *packet_buff, unsigned int packet_len) 126 150 { … … 146 170 file_name = ((char *)rcv_udphdr) + sizeof(struct udphdr) + 2; 147 171 if (strcmp(file_name, "kernel") == 0) { 148 tftp_xfer_buff = kernel_buf;149 tftp_xfer_size = kernel_size;172 tftp_xfer_buff = (flash_from_file ? (unsigned char *)&fff_data[FFF_KERNEL] : kernel_buf); 173 tftp_xfer_size = (flash_from_file ? fff_data[FFF_KERNEL].flash_size : kernel_size); 150 174 printf("Sending kernel, %ld blocks...\n", 151 175 ((tftp_xfer_size + 511) / 512)); 152 176 } else if (strcmp(file_name, "rootfs") == 0) { 153 tftp_xfer_buff = rootfs_buf;154 tftp_xfer_size = rootfs_size;177 tftp_xfer_buff = (flash_from_file ? (unsigned char *)&fff_data[FFF_ROOTFS] : rootfs_buf); 178 tftp_xfer_size = (flash_from_file ? fff_data[FFF_ROOTFS].flash_size : rootfs_size); 155 179 printf("Sending rootfs, %ld blocks...\n", 156 180 ((tftp_xfer_size + 511) / 512)); … … 200 224 *((unsigned short *)(tftp_data + 2)) = htons(block); 201 225 202 if (tftp_xfer_size - tftp_bytes_sent >= 512) { 203 memcpy(tftp_data + 4, (void *)(tftp_xfer_buff + tftp_bytes_sent), 512); 204 tftp_data_len = 512; 205 } else { 206 memcpy(tftp_data + 4, (void *)(tftp_xfer_buff + tftp_bytes_sent), tftp_xfer_size - tftp_bytes_sent); 207 tftp_data_len = tftp_xfer_size - tftp_bytes_sent; 208 } 226 tftp_data_len = tftp_xfer_size - tftp_bytes_sent > 512 ? 512 : tftp_xfer_size - tftp_bytes_sent; 227 228 if (flash_from_file) 229 read_from_file(tftp_data + 4, (void *)tftp_xfer_buff, tftp_data_len, tftp_bytes_sent); 230 else 231 memcpy(tftp_data + 4, (void *)(tftp_xfer_buff + tftp_bytes_sent), tftp_data_len); 209 232 210 233 tftp_bytes_sent += tftp_data_len; … … 231 254 } 232 255 233 voidfw_upload(void)256 int fw_upload(void) 234 257 { 235 258 const unsigned char *packet; … … 342 365 } 343 366 } 344 } 367 368 return 0; 369 } -
ap51-flash/trunk/packet.h
r202 r222 34 34 void arp_packet_init(void); 35 35 void arp_packet_send(void); 36 voidfw_upload(void);36 int fw_upload(void); -
ap51-flash/trunk/uip-conf.h
r194 r222 117 117 * \hideinitializer 118 118 */ 119 #define UIP_CONF_BYTE_ORDER UIP_LITTLE_ENDIAN 119 /* #define UIP_CONF_BYTE_ORDER UIP_LITTLE_ENDIAN */ 120 120 121 121 /**
