Changeset 223
- Timestamp:
- 02/15/10 17:12:05 (2 years ago)
- Location:
- ap51-flash/trunk
- Files:
-
- 1 added
- 7 modified
- 1 copied
-
Makefile (modified) (3 diffs)
-
ap51-flash.c (modified) (37 diffs)
-
ap51-flash.h (modified) (6 diffs)
-
main.c (modified) (2 diffs)
-
missing-osx.h (modified) (2 diffs)
-
packet.c (modified) (17 diffs)
-
packet.h (modified) (2 diffs)
-
socket.c (added)
-
socket.h (copied) (copied from ap51-flash/trunk/packet.h) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ap51-flash/trunk/Makefile
r222 r223 22 22 OBJCOPY = $(CROSS)objcopy 23 23 WINDRES = $(CROSS)windres 24 OFLAGS = -Os -ggdb24 OFLAGS = -Os 25 25 CFLAGS = -Wall -I. -IWpdPack/Include/ -fno-strict-aliasing -fpack-struct $(OFLAGS) 26 LIB_OBJS= ap51-flash.o uip.o uip_arp.o timer.o clock-arch.o psock.o packet.o 26 LIB_OBJS= ap51-flash.o uip.o uip_arp.o timer.o clock-arch.o psock.o packet.o socket.o 27 27 OBJS = $(LIB_OBJS) main.o 28 28 AP51_RC = ap51-flash-res 29 29 30 30 # enable debug output 31 # CFLAGS += -DDEBUG31 # EXTRA_CFLAGS += -DDEBUG 32 32 # enable packet debug output 33 # CFLAGS += -DPACKET_DEBUG33 # EXTRA_CFLAGS += -DPACKET_DEBUG 34 34 # enable flash from file mode 35 # CFLAGS += -DFLASH_FROM_FILE 35 # EXTRA_CFLAGS += -DFLASH_FROM_FILE 36 # disable libpcap and use raw sockets instead (linux only!) 37 # EXTRA_CFLAGS += -DNO_LIBPCAP 36 38 37 39 # if you change the names here you also need to change the ap51-flash.c code … … 66 68 endif 67 69 70 # detect whether we should link against libpcap 71 ifeq ($(PLATFORM),WIN32) 72 LDFLAGS += -lwpcap 73 else ifeq ($(PLATFORM),OSX) 74 LDFLAGS += -lpcap 75 else ifeq ($(PLATFORM),LINUX) 76 ifeq ($(findstring NO_LIBPCAP,$(EXTRA_CFLAGS)),) 77 LDFLAGS += -lpcap 78 endif 79 endif 80 68 81 REVISION = $(shell if [ -d .svn ]; then \ 69 82 if which svn > /dev/null; then \ … … 92 105 93 106 ap51-flash: $(LIN_OBJS) $(OBJS) Makefile 94 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LIN_OBJS) $(OBJS) $(LDFLAGS) - lpcap -o $@107 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LIN_OBJS) $(OBJS) $(LDFLAGS) -o $@ 95 108 $(STRIP) $@ 96 109 97 110 ap51-flash-static: $(LIN_OBJS) $(OBJS) libap51-flash.a Makefile 98 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LIN_OBJS) $(OBJS) $(LDFLAGS) - lpcap -static -o $@111 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LIN_OBJS) $(OBJS) $(LDFLAGS) -static -o $@ 99 112 $(STRIP) $@ 100 113 101 114 ap51-flash.exe: $(WIN_OBJS) $(OBJS) Makefile 102 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -LWpdPack/Lib/ -DWIN32 -D_CONSOLE -D_MBCS $(WIN_OBJS) $(OBJS) $(LDFLAGS) - lwpcap -static -o $@115 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -LWpdPack/Lib/ -DWIN32 -D_CONSOLE -D_MBCS $(WIN_OBJS) $(OBJS) $(LDFLAGS) -o $@ 103 116 $(STRIP) $@ 104 117 105 118 ap51-flash-osx: $(OSX_OBJ) $(OBJS) Makefile 106 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(OSX_OBJ) $(OBJS) $(LDFLAGS) - lpcap -o $@119 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(OSX_OBJ) $(OBJS) $(LDFLAGS) -o $@ 107 120 $(STRIP) $@ 108 121 -
ap51-flash/trunk/ap51-flash.c
r222 r223 22 22 #include <string.h> 23 23 24 #include "ap51-flash.h" 24 25 #include "uip.h" 25 26 #include "uip_arp.h" 26 27 #include "timer.h" 27 #include "ap51-flash.h"28 28 #include "device-info.h" 29 29 #include "packet.h" 30 #include "socket.h" 30 31 31 32 unsigned char *kernel_buf = 0; … … 53 54 static struct device_info *device_info = &flash_8mb_info; 54 55 static char *kernelpartname = "vmlinux.bin.l7"; 55 pcap_t *pcap_fp = NULL;56 56 57 57 #if defined(EMBEDDED_DATA) && !defined(WIN32) … … 82 82 } 83 83 84 static int pcap_init(char *dev, uip_ipaddr_t* sip, uip_ipaddr_t* dip, struct uip_eth_addr* smac, struct uip_eth_addr* dmac, int special)84 static int ap51_init(char *dev, uip_ipaddr_t* sip, uip_ipaddr_t* dip, struct uip_eth_addr* smac, struct uip_eth_addr* dmac, int special) 85 85 { 86 int arp_replies = 0, arp_grat_packets = 0; 87 char error[PCAP_ERRBUF_SIZE]; 86 int ret, len, arp_replies = 0, arp_grat_packets = 0; 88 87 const unsigned char *packet; 89 struct pcap_pkthdr hdr;90 88 struct ether_header *recv_ethhdr; 91 89 struct ether_arp *recv_arphdr; … … 95 93 96 94 /* Open the output adapter */ 97 if (NULL == (pcap_fp = pcap_open_live(dev, 1500, 1, PCAP_TIMEOUT_MS, error))) { 98 fprintf(stderr, "Error opening adapter: %s\n", error); 99 return -1; 100 } 95 ret = socket_open(dev); 96 if (ret != 0) 97 return ret; 101 98 102 99 arp_packet_init(); … … 109 106 *((unsigned int *)arphdr->arp_tpa) = htonl(ubnt_remote_ip); 110 107 108 #if !defined(OSX) 109 // TODO: This is a seeming bug in OSX - won't work if we turn on non blocking - Lokkju 110 ret = socket_setnonblock(); 111 if (ret != 0) 112 return ret; 113 #endif 114 111 115 fprintf(stderr, "Waiting for device to run auto-detection.\nMake sure, the device is connected directly!\n"); 112 116 … … 114 118 arp_packet_send(); 115 119 116 while (NULL == (packet = pcap_next(pcap_fp, &hdr))) {120 while (NULL == (packet = socket_read(&len))) { 117 121 #if defined(DEBUG) 118 122 printf("No packet.\n"); … … 131 135 } 132 136 133 if ( hdr.len != 60) {137 if (len != 60) { 134 138 #if defined(DEBUG) 135 fprintf(stderr, "Expect arp with length 60, received %d\n", hdr.len);139 fprintf(stderr, "Expect arp with length 60, received %d\n", len); 136 140 #endif 137 141 continue; … … 201 205 if (!special && 0 == P(*dip)[0] && 0 == P(*dip)[1] && 0 == P(*dip)[2] && 0 == P(*dip)[3]) { 202 206 fprintf(stderr, "Telnet for RedBoot not enabled.\n"); 203 return -1;207 return 1; 204 208 } 205 209 … … 220 224 #endif 221 225 222 #if !defined(OSX)223 // TODO: This is a seeming bug in OSX - won't work if we turn on non blocking - Lokkju224 if (0 > pcap_setnonblock(pcap_fp, 1, error)) {225 fprintf(stderr,"Error setting non-blocking mode: %s\n", error);226 return -1;227 }228 #endif229 226 return 0; 230 227 } … … 237 234 if (!str_ptr) { 238 235 fprintf(stderr, "No RedBoot prompt detected. Exit in line %d\n", __LINE__); 239 return -1;236 return 1; 240 237 } 241 238 … … 245 242 if (!str_ptr) { 246 243 fprintf(stderr, "No RedBoot detected. Exit in line %d\n", __LINE__); 247 return -1;244 return 1; 248 245 } 249 246 … … 253 250 fprintf(stderr, "No RedBoot prompt exceeds expected size (%i - expected %i). Exit in line %d\n", 254 251 (int)strlen(str_ptr), (int)sizeof(boot_prompt), __LINE__); 255 return -1;252 return 1; 256 253 } 257 254 … … 276 273 PSOCK_READTO(345, &s->p, '>'); 277 274 278 if (extract_boot_prompt(s) <0)275 if (extract_boot_prompt(s) != 0) 279 276 goto err_close; 280 277 … … 316 313 fprintf(stderr, "rootfs(0x%08x) + kernel(0x%08x) + nvram(0x%08x) exceeds limit of 0x%08lx\n", 317 314 rootfs_size, kernel_size, nvram_part_size, device_info->flash_size); 318 exit(1);315 goto err_close; 319 316 } 320 317 … … 362 359 if (tftp_bytes_sent < (unsigned long)rootfs_size) { 363 360 fprintf(stderr, "Error transferring rootfs, send=%ld, expected=%d\n", tftp_bytes_sent, rootfs_size); 364 exit(1);361 goto err_close; 365 362 } 366 363 … … 428 425 if (tftp_bytes_sent < (unsigned long)kernel_size) { 429 426 fprintf(stderr, "Error transferring kernel, send=%ld, expected=%d\n", tftp_bytes_sent, kernel_size); 430 exit(1);427 goto err_close; 431 428 } 432 429 … … 464 461 465 462 phase++; 466 break;467 463 468 464 } else if (phase == 6) { … … 480 476 if (!uncomp_loader) 481 477 sprintf(str, "fis load %s %s\n", 482 ( 0x1f == fff_data[FFF_KERNEL].buff[0] && 0x8b ==fff_data[FFF_KERNEL].buff[1]? "-d" : "-l"),478 (fff_data[FFF_KERNEL].buff[0] == 0x1f && fff_data[FFF_KERNEL].buff[1] == 0x8b ? "-d" : "-l"), 483 479 kernelpartname); 484 480 else … … 503 499 PSOCK_READTO(485, &s->p, '>'); 504 500 phase++; 505 break;506 501 507 502 } else if (phase == 7) { … … 513 508 PSOCK_SEND_STR(495, &s->p, "reset\n"); 514 509 printf("Done. Restarting device...\n"); 515 exit(0);516 510 phase++; 517 break;511 return TCP_SUCCESS; 518 512 } 519 513 520 514 PSOCK_END(&s->p); 521 return 0;515 return TCP_CONTINUE; 522 516 523 517 err_close: 524 518 PSOCK_CLOSE(&s->p); 525 519 PSOCK_EXIT(&s->p); 526 exit(1);520 return TCP_ERROR; 527 521 } 528 522 … … 534 528 PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer)); 535 529 536 handle_connection(s);530 tcp_status = handle_connection(s); 537 531 } 538 532 … … 544 538 #endif 545 539 546 if (pcap_sendpacket(pcap_fp, uip_buf, uip_len) < 0) { 547 perror("pcap_sendpacket"); 548 exit(1); 549 } 540 socket_write(uip_buf, uip_len); 550 541 } 551 542 … … 639 630 640 631 close(*fd); 641 return 1;632 return 0; 642 633 643 634 err_free: … … 646 637 close(*fd); 647 638 err: 648 return -1;639 return 1; 649 640 } 650 641 651 int ap51_flash(char * device, char *rootfs_filename, char *kernel_filename, int nvram, int uncomp, int special)642 int ap51_flash(char *iface, char *rootfs_filename, char *kernel_filename, int nvram, int uncomp, int special) 652 643 { 653 644 uip_ipaddr_t netmask; 654 645 struct uip_eth_addr srcmac, dstmac, brcmac; 655 pcap_if_t *alldevs = NULL, *d; 656 char *pcap_device, errbuf[PCAP_ERRBUF_SIZE]; 657 int ret, i, if_num = 0, ubnt_img = 0; 646 int ret, ubnt_img = 0, fd, size = 0; 658 647 unsigned char *buf = 0; 659 int fd, size = 0; 660 661 pcap_device = device; 648 char *socket_dev; 649 662 650 uip_init(); 663 651 uip_arp_init(); … … 673 661 674 662 #if defined(FLASH_FROM_FILE) 663 int i; 664 675 665 if (flash_from_file) { 676 666 for (i = 0; i < FFF_NUM; i++) { … … 681 671 &fff_data[i].file_size, &fff_data[i].flash_size, NULL); 682 672 683 if (ret <0)684 return 1;673 if (ret != 0) 674 return ret; 685 675 686 676 printf("Reading %s file %s with %d bytes ...\n", … … 689 679 } 690 680 691 goto init_ pcap;681 goto init_socket; 692 682 } 693 683 #endif … … 697 687 &size, &rootfs_size, &rootfs_buf); 698 688 699 if (ret <0)700 return 1;689 if (ret != 0) 690 return ret; 701 691 702 692 printf("Reading rootfs file %s with %d bytes ...\n", … … 733 723 &size, &kernel_size, &kernel_buf); 734 724 735 if (ret <0)736 return 1;725 if (ret != 0) 726 return ret; 737 727 738 728 printf("Reading kernel file %s with %d bytes ...\n", … … 779 769 780 770 #if defined(FLASH_FROM_FILE) 781 init_ pcap:771 init_socket: 782 772 #endif 783 773 … … 797 787 798 788 /* if the user specified an interface number instead of the name */ 799 if_num = strtol(device, NULL, 10); 800 801 if (pcap_findalldevs(&alldevs, errbuf) == -1) 802 alldevs = NULL; 803 804 /* Print the list */ 805 i = 0; 806 for (d = alldevs; d != NULL; d = d->next) { 807 i++; 808 809 if (if_num == i) { 810 pcap_device = d->name; 811 break; 812 } 813 } 814 815 i = pcap_init(pcap_device, &srcipaddr, &dstipaddr, &srcmac, &dstmac, special); 816 817 if (alldevs) 818 pcap_freealldevs(alldevs); 819 820 if (i < 0) 821 return 1; 789 socket_dev = socket_find_dev_by_index(iface); 790 791 if (!socket_dev) 792 socket_dev = iface; 793 794 ret = ap51_init(socket_dev, &srcipaddr, &dstipaddr, &srcmac, &dstmac, special); 795 796 if (ret != 0) 797 return ret; 822 798 823 799 uip_sethostaddr(srcipaddr); … … 835 811 flash_mode = MODE_REDBOOT; 836 812 813 ret = 1; 814 837 815 switch (flash_mode) { 838 816 case MODE_REDBOOT: … … 841 819 (!fff_data[FFF_KERNEL].fname)) { 842 820 fprintf(stderr, "Error - redboot enabled device detected but rootfs & kernel file not available\n"); 843 return 1;821 goto sock_close; 844 822 } 845 823 } else { 846 824 if (ubnt_img) { 847 825 fprintf(stderr, "Error - you are trying to flash a redboot device with a ubiquiti image!\n"); 848 return 1;826 goto sock_close; 849 827 } 850 828 } … … 856 834 if (NULL == uip_connect(&dstipaddr, htons(TELNET_PORT))) { 857 835 fprintf(stderr, "Error - cannot connect to port %i\n", TELNET_PORT); 858 return 1;836 goto sock_close; 859 837 } 860 838 break; … … 886 864 } else { 887 865 perror("no mem"); 888 return 1;866 goto sock_close; 889 867 } 890 868 } … … 896 874 if (!fff_data[FFF_UBNT].fname) { 897 875 fprintf(stderr, "Error - ubiquiti device detected but ubiquiti file not available\n"); 898 return 1;876 goto sock_close; 899 877 } 900 878 } else { 901 879 if (!ubnt_img) { 902 880 fprintf(stderr, "Error - you are trying to flash a ubiquiti device with redboot images!\n"); 903 return 1;881 goto sock_close; 904 882 } 905 883 } … … 911 889 default: 912 890 fprintf(stderr, "Error - could not auto-detect flash mode!\n"); 913 return 1; 914 } 915 916 return fw_upload(); 891 goto sock_close; 892 } 893 894 ret = fw_upload(); 895 896 sock_close: 897 socket_close(socket_dev); 898 return ret; 917 899 } 918 900 -
ap51-flash/trunk/ap51-flash.h
r222 r223 20 20 #define __ap51_FLASH_H__ 21 21 22 #include <pcap.h>23 22 #include "uipopt.h" 24 23 #include "psock.h" … … 27 26 /* WIN32 */ 28 27 #include <windows.h> 28 #include <pcap.h> 29 29 #include "ap51-flash-res.h" 30 30 #include "missing-win32.h" 31 31 #define PCAP_TIMEOUT_MS 1000 32 #if defined(NO_LIBPCAP) 33 #error NO_LIBPCAP supported on Linux only 34 #endif 32 35 #elif defined(OSX) 33 36 /* OSX */ 37 #include <pcap.h> 34 38 #define O_BINARY 0 35 39 #include <unistd.h> … … 41 45 #undef HTONS 42 46 #define PCAP_TIMEOUT_MS 200 47 #if defined(NO_LIBPCAP) 48 #error NO_LIBPCAP supported on Linux only 49 #endif 43 50 #elif defined(LINUX) 44 51 /* Linux */ … … 52 59 #include <arpa/inet.h> 53 60 #define PCAP_TIMEOUT_MS 200 61 #if !defined(NO_LIBPCAP) 62 #include <pcap.h> 63 #endif 54 64 #else 55 65 #error Unsupported PLATFORM … … 76 86 int flash_size; 77 87 char *fname; 78 char buff[2];88 unsigned char buff[2]; 79 89 }; 80 90 … … 125 135 void handle_uip_conns(void); 126 136 127 extern pcap_t *pcap_fp;128 137 extern unsigned int remote_ip; 129 138 extern unsigned int local_ip; -
ap51-flash/trunk/main.c
r222 r223 25 25 26 26 #include "ap51-flash.h" 27 #include "socket.h" 27 28 28 29 void usage(char *prgname) 29 30 { 30 pcap_if_t *alldevs;31 pcap_if_t *d;32 int i=0;33 char errbuf[PCAP_ERRBUF_SIZE];34 35 31 fprintf(stderr, "Usage:\n"); 36 32 … … 55 51 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"); 56 52 57 /* Retrieve the device list from the local machine */ 58 if (pcap_findalldevs(&alldevs, errbuf) == -1) { 59 fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf); 60 return; 61 } 62 63 /* Print the list */ 64 for(d = alldevs; d != NULL; d = d->next) { 65 i++; 66 fprintf(stderr, "\n%i: %s\n", i, d->name); 67 68 if (!d->description) { 69 fprintf(stderr, "\t(No description available)\n"); 70 continue; 71 } 72 73 unsigned char* p = (unsigned char*)d->description; 74 unsigned char c = 0; 75 fprintf(stderr, "\t(Description: "); 76 while (' ' <= *p) { 77 if (c != ' ' || c != *p) 78 fprintf(stderr, "%c", *p); 79 c = *p++; 80 } 81 fprintf(stderr, ")\n"); 82 } 53 socket_print_all_devices(); 83 54 } 84 55 -
ap51-flash/trunk/missing-osx.h
r221 r223 29 29 #define IPPORT_TFTP 69 30 30 31 struct iphdr {31 /*struct iphdr { 32 32 unsigned int ihl:4; 33 33 unsigned int version:4; … … 41 41 u_int32_t saddr; 42 42 u_int32_t daddr; 43 }; 43 };*/ 44 44 45 45 struct udphdr { -
ap51-flash/trunk/packet.c
r222 r223 17 17 */ 18 18 19 #include <stdio.h> 19 20 #include <stdlib.h> 20 21 #include <string.h> 21 22 22 23 #include "packet.h" 24 #include "ap51-flash.h" 25 #include "socket.h" 23 26 24 27 #define PACKET_BUFF_LEN 1500 … … 33 36 unsigned long tftp_bytes_sent = 0; 34 37 unsigned short tftp_ack_block = 0, tftp_sent_block = 0, xfer_in_progress = 0, write_req_timeout = 4; 38 char tcp_status = TCP_CONTINUE; 35 39 36 40 … … 50 54 void arp_packet_send(void) 51 55 { 52 if (!pcap_sendpacket(pcap_fp, packet_buff, ARP_LEN)) 53 return; 54 55 perror("pcap_sendpacket"); 56 exit(1); 56 socket_write(packet_buff, ARP_LEN); 57 57 } 58 58 … … 73 73 } 74 74 75 static voidtftp_packet_send(int tftp_data_len)75 static int tftp_packet_send(int tftp_data_len) 76 76 { 77 77 unsigned short sum; … … 100 100 #endif 101 101 102 if (!pcap_sendpacket(pcap_fp, packet_buff, TFTP_BASE_LEN + tftp_data_len)) 103 return; 104 105 perror("pcap_sendpacket"); 106 exit(1); 107 } 108 109 static void tftp_write_req(void) 102 return socket_write(packet_buff, TFTP_BASE_LEN + tftp_data_len); 103 } 104 105 static int tftp_write_req(void) 110 106 { 111 107 int tftp_data_len; … … 120 116 tftp_data_len += 2; /* sprintf does not count \0 */ 121 117 122 tftp_packet_send(tftp_data_len);123 } 124 125 static voidread_from_file(void *target_buff, void *data, int len, int seek_pos)118 return tftp_packet_send(tftp_data_len); 119 } 120 121 static int read_from_file(void *target_buff, void *data, int len, int seek_pos) 126 122 { 127 123 struct flash_from_file *fff = (struct flash_from_file *)data; … … 136 132 if (read_len != read(fff->fd, target_buff, read_len)) { 137 133 perror(fff->fname); 138 exit(1);134 return 1; 139 135 } 140 136 } … … 145 141 if (seek_pos == 0) 146 142 memcpy(fff->buff, target_buff, 2); 147 } 148 149 static void tftp_transfer(const unsigned char *packet_buff, unsigned int packet_len) 143 144 return 0; 145 } 146 147 static int tftp_transfer(const unsigned char *packet_buff, unsigned int packet_len) 150 148 { 151 149 struct udphdr *rcv_udphdr = (struct udphdr *)packet_buff; 152 150 unsigned short opcode, block; 153 151 char *file_name; 154 int tftp_data_len ;152 int tftp_data_len, ret; 155 153 156 154 if ((flash_mode == MODE_REDBOOT) && (rcv_udphdr->dest != htons(IPPORT_TFTP))) 157 return ;155 return 1; 158 156 159 157 if ((flash_mode == MODE_TFTP_CLIENT) && (rcv_udphdr->source != htons(IPPORT_TFTP))) 160 return ;158 return 1; 161 159 162 160 opcode = ntohs(*(unsigned short *)(((char *)rcv_udphdr) + sizeof(struct udphdr))); … … 181 179 } else { 182 180 fprintf(stderr, "Unknown file name: %s\n", file_name); 183 exit(1);181 return -1; 184 182 } 185 183 … … 210 208 printf("Image successfully transmitted.\n"); 211 209 printf("Please give the device a couple of minutes to install the new image into the flash.\n"); 212 exit(0);210 return 0; 213 211 } 214 return ;212 return 1; 215 213 } 216 214 … … 226 224 tftp_data_len = tftp_xfer_size - tftp_bytes_sent > 512 ? 512 : tftp_xfer_size - tftp_bytes_sent; 227 225 228 if (flash_from_file) 229 read_from_file(tftp_data + 4, (void *)tftp_xfer_buff, tftp_data_len, tftp_bytes_sent); 230 else 226 if (flash_from_file) { 227 ret = read_from_file(tftp_data + 4, (void *)tftp_xfer_buff, tftp_data_len, tftp_bytes_sent); 228 if (ret != 0) 229 return -1; 230 } else { 231 231 memcpy(tftp_data + 4, (void *)(tftp_xfer_buff + tftp_bytes_sent), tftp_data_len); 232 } 232 233 233 234 tftp_bytes_sent += tftp_data_len; 234 235 tftp_data_len += 4; /* opcode size */ 235 tftp_packet_send(tftp_data_len); 236 237 ret = tftp_packet_send(tftp_data_len); 238 if (ret != 0) 239 return -1; 240 236 241 tftp_sent_block = block; 237 242 /* printf("tftp data out: tftp_sent=%lu, remaining_size=%lu, data_len=%i, block=%d\n", … … 245 250 else 246 251 fprintf(stderr, "Received TFTP error code: %d\n", block); 247 exit(1); 252 253 return -1; 248 254 break; 249 255 default: 250 256 fprintf(stderr, "Unexpected TFTP opcode: %d\n", opcode); 251 exit(1);257 return -1; 252 258 break; 253 259 } 260 261 return 1; 254 262 } 255 263 … … 257 265 { 258 266 const unsigned char *packet; 259 struct pcap_pkthdr hdr;260 267 struct ether_arp *rcv_arphdr; 261 268 struct iphdr *rcv_iphdr; 269 int len, ret; 262 270 263 271 if (flash_mode == MODE_TFTP_CLIENT) { 264 272 printf("Trying to connect to TFTP server on the device ..\n"); 265 tftp_write_req(); 273 ret = tftp_write_req(); 274 if (ret != 0) 275 return ret; 266 276 } 267 277 268 278 while (1) { 269 packet = pcap_next(pcap_fp, &hdr); 279 packet = socket_read(&len); 280 281 if ((flash_mode == MODE_REDBOOT) && (tcp_status != TCP_CONTINUE)) { 282 switch (tcp_status) { 283 case TCP_SUCCESS: 284 return 0; 285 case TCP_ERROR: 286 return 1; 287 default: 288 tcp_status = TCP_CONTINUE; 289 } 290 } 270 291 271 292 if (!packet) { … … 286 307 fprintf(stderr, "TFTP connection timeout .. \n"); 287 308 tftp_write_req(); 309 if (ret != 0) 310 return ret; 311 288 312 write_req_timeout = 2; 289 313 } … … 295 319 switch (ntohs(((struct ether_header *)packet)->ether_type)) { 296 320 case ETHERTYPE_ARP: 297 if ( hdr.len < 60) {298 /* fprintf(stderr, "Expected arp with minimum length %i, received %d\n", 60, hdr.len); */321 if (len < 60) { 322 /* fprintf(stderr, "Expected arp with minimum length %i, received %d\n", 60, len); */ 299 323 continue; 300 324 } … … 332 356 break; 333 357 case ETH_P_IP: 334 if ( hdr.len < 20) {335 fprintf(stderr, "Expected IP with minimum length %i, received %d\n", 20, hdr.len);358 if (len < 20) { 359 fprintf(stderr, "Expected IP with minimum length %i, received %d\n", 20, len); 336 360 continue; 337 361 } … … 351 375 switch (rcv_iphdr->protocol) { 352 376 case IPPROTO_UDP: 353 tftp_transfer(packet + ETH_HLEN + (rcv_iphdr->ihl * 4), 354 hdr.len - ETH_HLEN - (rcv_iphdr->ihl * 4)); 377 ret = tftp_transfer(packet + ETH_HLEN + (rcv_iphdr->ihl * 4), 378 len - ETH_HLEN - (rcv_iphdr->ihl * 4)); 379 380 if (ret < 0) 381 return 1; /* error */ 382 else if (ret == 0) 383 return 0; /* flash successful */ 355 384 break; 356 385 case IPPROTO_TCP: 357 handle_uip_tcp(packet, hdr.len);386 handle_uip_tcp(packet, len); 358 387 break; 359 388 default: -
ap51-flash/trunk/packet.h
r222 r223 17 17 */ 18 18 19 #include "ap51-flash.h"20 21 19 #define TFTP_SRC_PORT 13337 22 20 #define TELNET_PORT 9000 23 21 #define ARP_LEN (sizeof(struct ether_header) + sizeof(struct ether_arp)) 24 22 #define TFTP_BASE_LEN (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr)) 23 24 #define TCP_CONTINUE 0 25 #define TCP_SUCCESS 1 26 #define TCP_ERROR -1 25 27 26 28 extern struct ether_header *ethhdr; … … 31 33 extern unsigned long tftp_bytes_sent; 32 34 extern unsigned short xfer_in_progress; 35 extern char tcp_status; 33 36 34 37 void arp_packet_init(void); -
ap51-flash/trunk/socket.h
r222 r223 17 17 */ 18 18 19 #include "ap51-flash.h" 20 21 #define TFTP_SRC_PORT 13337 22 #define TELNET_PORT 9000 23 #define ARP_LEN (sizeof(struct ether_header) + sizeof(struct ether_arp)) 24 #define TFTP_BASE_LEN (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr)) 25 26 extern struct ether_header *ethhdr; 27 extern struct ether_arp *arphdr; 28 extern struct iphdr *iphdr; 29 extern struct udphdr *udphdr; 30 extern void *tftp_data; 31 extern unsigned long tftp_bytes_sent; 32 extern unsigned short xfer_in_progress; 33 34 void arp_packet_init(void); 35 void arp_packet_send(void); 36 int fw_upload(void); 19 char *socket_find_dev_by_index(char *number); 20 void socket_print_all_devices(void); 21 int socket_open(char *dev); 22 int socket_setnonblock(void); 23 unsigned char *socket_read(int *len); 24 int socket_write(unsigned char *buff, int len); 25 void socket_close(char *dev);
