Changeset 223

Show
Ignore:
Timestamp:
02/15/10 17:12:05 (2 years ago)
Author:
marek
Message:

ap51-flash: flash without libpcap (linux only)

Location:
ap51-flash/trunk
Files:
1 added
7 modified
1 copied

Legend:

Unmodified
Added
Removed
  • ap51-flash/trunk/Makefile

    r222 r223  
    2222OBJCOPY = $(CROSS)objcopy 
    2323WINDRES = $(CROSS)windres 
    24 OFLAGS  = -Os -ggdb 
     24OFLAGS  = -Os 
    2525CFLAGS  = -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 
     26LIB_OBJS= ap51-flash.o uip.o uip_arp.o timer.o clock-arch.o psock.o packet.o socket.o 
    2727OBJS    = $(LIB_OBJS) main.o 
    2828AP51_RC = ap51-flash-res 
    2929 
    3030# enable debug output 
    31 # CFLAGS += -DDEBUG 
     31# EXTRA_CFLAGS += -DDEBUG 
    3232# enable packet debug output 
    33 # CFLAGS += -DPACKET_DEBUG 
     33# EXTRA_CFLAGS += -DPACKET_DEBUG 
    3434# 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 
    3638 
    3739# if you change the names here you also need to change the ap51-flash.c code 
     
    6668endif 
    6769 
     70# detect whether we should link against libpcap 
     71ifeq ($(PLATFORM),WIN32) 
     72LDFLAGS += -lwpcap 
     73else ifeq ($(PLATFORM),OSX) 
     74LDFLAGS += -lpcap 
     75else ifeq ($(PLATFORM),LINUX) 
     76ifeq ($(findstring NO_LIBPCAP,$(EXTRA_CFLAGS)),) 
     77LDFLAGS += -lpcap 
     78endif 
     79endif 
     80 
    6881REVISION = $(shell if [ -d .svn ]; then \ 
    6982                                if which svn > /dev/null; then \ 
     
    92105 
    93106ap51-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 $@ 
    95108        $(STRIP) $@ 
    96109 
    97110ap51-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 $@ 
    99112        $(STRIP) $@ 
    100113 
    101114ap51-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 $@ 
    103116        $(STRIP) $@ 
    104117 
    105118ap51-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 $@ 
    107120        $(STRIP) $@ 
    108121 
  • ap51-flash/trunk/ap51-flash.c

    r222 r223  
    2222#include <string.h> 
    2323 
     24#include "ap51-flash.h" 
    2425#include "uip.h" 
    2526#include "uip_arp.h" 
    2627#include "timer.h" 
    27 #include "ap51-flash.h" 
    2828#include "device-info.h" 
    2929#include "packet.h" 
     30#include "socket.h" 
    3031 
    3132unsigned char *kernel_buf = 0; 
     
    5354static struct device_info *device_info = &flash_8mb_info; 
    5455static char *kernelpartname = "vmlinux.bin.l7"; 
    55 pcap_t *pcap_fp = NULL; 
    5656 
    5757#if defined(EMBEDDED_DATA) && !defined(WIN32) 
     
    8282} 
    8383 
    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) 
     84static 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) 
    8585{ 
    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; 
    8887        const unsigned char *packet; 
    89         struct pcap_pkthdr hdr; 
    9088        struct ether_header *recv_ethhdr; 
    9189        struct ether_arp *recv_arphdr; 
     
    9593 
    9694        /* 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; 
    10198 
    10299        arp_packet_init(); 
     
    109106        *((unsigned int *)arphdr->arp_tpa) = htonl(ubnt_remote_ip); 
    110107 
     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 
    111115        fprintf(stderr, "Waiting for device to run auto-detection.\nMake sure, the device is connected directly!\n"); 
    112116 
     
    114118                arp_packet_send(); 
    115119 
    116                 while (NULL == (packet = pcap_next(pcap_fp, &hdr))) { 
     120                while (NULL == (packet = socket_read(&len))) { 
    117121#if defined(DEBUG) 
    118122                        printf("No packet.\n"); 
     
    131135                } 
    132136 
    133                 if (hdr.len != 60) { 
     137                if (len != 60) { 
    134138#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); 
    136140#endif 
    137141                        continue; 
     
    201205        if (!special && 0 == P(*dip)[0] && 0 == P(*dip)[1] && 0 == P(*dip)[2] && 0 == P(*dip)[3]) { 
    202206                fprintf(stderr, "Telnet for RedBoot not enabled.\n"); 
    203                 return -1; 
     207                return 1; 
    204208        } 
    205209 
     
    220224#endif 
    221225 
    222 #if !defined(OSX) 
    223         // TODO: This is a seeming bug in OSX - won't work if we turn on non blocking - Lokkju 
    224         if (0 > pcap_setnonblock(pcap_fp, 1, error)) { 
    225                 fprintf(stderr,"Error setting non-blocking mode: %s\n", error); 
    226                 return -1; 
    227         } 
    228 #endif 
    229226        return 0; 
    230227} 
     
    237234        if (!str_ptr) { 
    238235                fprintf(stderr, "No RedBoot prompt detected. Exit in line %d\n", __LINE__); 
    239                 return -1; 
     236                return 1; 
    240237        } 
    241238 
     
    245242        if (!str_ptr) { 
    246243                fprintf(stderr, "No RedBoot detected. Exit in line %d\n", __LINE__); 
    247                 return -1; 
     244                return 1; 
    248245        } 
    249246 
     
    253250                fprintf(stderr, "No RedBoot prompt exceeds expected size (%i - expected %i). Exit in line %d\n", 
    254251                        (int)strlen(str_ptr), (int)sizeof(boot_prompt), __LINE__); 
    255                 return -1; 
     252                return 1; 
    256253        } 
    257254 
     
    276273                PSOCK_READTO(345, &s->p, '>'); 
    277274 
    278                 if (extract_boot_prompt(s) < 0) 
     275                if (extract_boot_prompt(s) != 0) 
    279276                        goto err_close; 
    280277 
     
    316313                        fprintf(stderr, "rootfs(0x%08x) + kernel(0x%08x) + nvram(0x%08x) exceeds limit of 0x%08lx\n", 
    317314                                rootfs_size, kernel_size, nvram_part_size, device_info->flash_size); 
    318                         exit(1); 
     315                        goto err_close; 
    319316                } 
    320317 
     
    362359                if (tftp_bytes_sent < (unsigned long)rootfs_size) { 
    363360                        fprintf(stderr, "Error transferring rootfs, send=%ld, expected=%d\n", tftp_bytes_sent, rootfs_size); 
    364                         exit(1); 
     361                        goto err_close; 
    365362                } 
    366363 
     
    428425                if (tftp_bytes_sent < (unsigned long)kernel_size) { 
    429426                        fprintf(stderr, "Error transferring kernel, send=%ld, expected=%d\n", tftp_bytes_sent, kernel_size); 
    430                         exit(1); 
     427                        goto err_close; 
    431428                } 
    432429 
     
    464461 
    465462                phase++; 
    466                 break; 
    467463 
    468464        } else if (phase == 6) { 
     
    480476                        if (!uncomp_loader) 
    481477                                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"), 
    483479                                        kernelpartname); 
    484480                        else 
     
    503499                PSOCK_READTO(485, &s->p, '>'); 
    504500                phase++; 
    505                 break; 
    506501 
    507502        } else if (phase == 7) { 
     
    513508                PSOCK_SEND_STR(495, &s->p, "reset\n"); 
    514509                printf("Done. Restarting device...\n"); 
    515                 exit(0); 
    516510                phase++; 
    517                 break; 
     511                return TCP_SUCCESS; 
    518512        } 
    519513 
    520514        PSOCK_END(&s->p); 
    521         return 0; 
     515        return TCP_CONTINUE; 
    522516 
    523517err_close: 
    524518        PSOCK_CLOSE(&s->p); 
    525519        PSOCK_EXIT(&s->p); 
    526         exit(1); 
     520        return TCP_ERROR; 
    527521} 
    528522 
     
    534528                PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer)); 
    535529 
    536         handle_connection(s); 
     530        tcp_status = handle_connection(s); 
    537531} 
    538532 
     
    544538#endif 
    545539 
    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); 
    550541} 
    551542 
     
    639630 
    640631        close(*fd); 
    641         return 1; 
     632        return 0; 
    642633 
    643634err_free: 
     
    646637        close(*fd); 
    647638err: 
    648         return -1; 
     639        return 1; 
    649640} 
    650641 
    651 int ap51_flash(char *device, char *rootfs_filename, char *kernel_filename, int nvram, int uncomp, int special) 
     642int ap51_flash(char *iface, char *rootfs_filename, char *kernel_filename, int nvram, int uncomp, int special) 
    652643{ 
    653644        uip_ipaddr_t netmask; 
    654645        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; 
    658647        unsigned char *buf = 0; 
    659         int fd, size = 0; 
    660  
    661         pcap_device = device; 
     648        char *socket_dev; 
     649 
    662650        uip_init(); 
    663651        uip_arp_init(); 
     
    673661 
    674662#if defined(FLASH_FROM_FILE) 
     663        int i; 
     664 
    675665        if (flash_from_file) { 
    676666                for (i = 0; i < FFF_NUM; i++) { 
     
    681671                                              &fff_data[i].file_size, &fff_data[i].flash_size, NULL); 
    682672 
    683                         if (ret < 0) 
    684                                 return 1; 
     673                        if (ret != 0) 
     674                                return ret; 
    685675 
    686676                        printf("Reading %s file %s with %d bytes ...\n", 
     
    689679                } 
    690680 
    691                 goto init_pcap; 
     681                goto init_socket; 
    692682        } 
    693683#endif 
     
    697687                                      &size, &rootfs_size, &rootfs_buf); 
    698688 
    699                 if (ret < 0) 
    700                         return 1; 
     689                if (ret != 0) 
     690                        return ret; 
    701691 
    702692                printf("Reading rootfs file %s with %d bytes ...\n", 
     
    733723                                      &size, &kernel_size, &kernel_buf); 
    734724 
    735                 if (ret < 0) 
    736                         return 1; 
     725                if (ret != 0) 
     726                        return ret; 
    737727 
    738728                printf("Reading kernel file %s with %d bytes ...\n", 
     
    779769 
    780770#if defined(FLASH_FROM_FILE) 
    781 init_pcap: 
     771init_socket: 
    782772#endif 
    783773 
     
    797787 
    798788        /* 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; 
    822798 
    823799        uip_sethostaddr(srcipaddr); 
     
    835811                flash_mode = MODE_REDBOOT; 
    836812 
     813        ret = 1; 
     814 
    837815        switch (flash_mode) { 
    838816        case MODE_REDBOOT: 
     
    841819                            (!fff_data[FFF_KERNEL].fname)) { 
    842820                                fprintf(stderr, "Error - redboot enabled device detected but rootfs & kernel file not available\n"); 
    843                                 return 1; 
     821                                goto sock_close; 
    844822                        } 
    845823                } else { 
    846824                        if (ubnt_img) { 
    847825                                fprintf(stderr, "Error - you are trying to flash a redboot device with a ubiquiti image!\n"); 
    848                                 return 1; 
     826                                goto sock_close; 
    849827                        } 
    850828                } 
     
    856834                if (NULL == uip_connect(&dstipaddr, htons(TELNET_PORT))) { 
    857835                        fprintf(stderr, "Error - cannot connect to port %i\n", TELNET_PORT); 
    858                         return 1; 
     836                        goto sock_close; 
    859837                } 
    860838                break; 
     
    886864                                } else { 
    887865                                        perror("no mem"); 
    888                                         return 1; 
     866                                        goto sock_close; 
    889867                                } 
    890868                        } 
     
    896874                        if (!fff_data[FFF_UBNT].fname) { 
    897875                                fprintf(stderr, "Error - ubiquiti device detected but ubiquiti file not available\n"); 
    898                                 return 1; 
     876                                goto sock_close; 
    899877                        } 
    900878                } else { 
    901879                        if (!ubnt_img) { 
    902880                                fprintf(stderr, "Error - you are trying to flash a ubiquiti device with redboot images!\n"); 
    903                                 return 1; 
     881                                goto sock_close; 
    904882                        } 
    905883                } 
     
    911889        default: 
    912890                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 
     896sock_close: 
     897        socket_close(socket_dev); 
     898        return ret; 
    917899} 
    918900 
  • ap51-flash/trunk/ap51-flash.h

    r222 r223  
    2020#define __ap51_FLASH_H__ 
    2121 
    22 #include <pcap.h> 
    2322#include "uipopt.h" 
    2423#include "psock.h" 
     
    2726/* WIN32 */ 
    2827#include <windows.h> 
     28#include <pcap.h> 
    2929#include "ap51-flash-res.h" 
    3030#include "missing-win32.h" 
    3131#define PCAP_TIMEOUT_MS 1000 
     32#if defined(NO_LIBPCAP) 
     33#error NO_LIBPCAP supported on Linux only 
     34#endif 
    3235#elif defined(OSX) 
    3336/* OSX */ 
     37#include <pcap.h> 
    3438#define O_BINARY 0 
    3539#include <unistd.h> 
     
    4145#undef HTONS 
    4246#define PCAP_TIMEOUT_MS 200 
     47#if defined(NO_LIBPCAP) 
     48#error NO_LIBPCAP supported on Linux only 
     49#endif 
    4350#elif defined(LINUX) 
    4451/* Linux */ 
     
    5259#include <arpa/inet.h> 
    5360#define PCAP_TIMEOUT_MS 200 
     61#if !defined(NO_LIBPCAP) 
     62#include <pcap.h> 
     63#endif 
    5464#else 
    5565#error Unsupported PLATFORM 
     
    7686        int flash_size; 
    7787        char *fname; 
    78         char buff[2]; 
     88        unsigned char buff[2]; 
    7989}; 
    8090 
     
    125135void handle_uip_conns(void); 
    126136 
    127 extern pcap_t *pcap_fp; 
    128137extern unsigned int remote_ip; 
    129138extern unsigned int local_ip; 
  • ap51-flash/trunk/main.c

    r222 r223  
    2525 
    2626#include "ap51-flash.h" 
     27#include "socket.h" 
    2728 
    2829void usage(char *prgname) 
    2930{ 
    30         pcap_if_t *alldevs; 
    31         pcap_if_t *d; 
    32         int i=0; 
    33         char errbuf[PCAP_ERRBUF_SIZE]; 
    34  
    3531        fprintf(stderr, "Usage:\n"); 
    3632 
     
    5551        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"); 
    5652 
    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(); 
    8354} 
    8455 
  • ap51-flash/trunk/missing-osx.h

    r221 r223  
    2929#define IPPORT_TFTP 69 
    3030 
    31 struct iphdr { 
     31/*struct iphdr { 
    3232        unsigned int ihl:4; 
    3333        unsigned int version:4; 
     
    4141        u_int32_t saddr; 
    4242        u_int32_t daddr; 
    43 }; 
     43};*/ 
    4444 
    4545struct udphdr { 
  • ap51-flash/trunk/packet.c

    r222 r223  
    1717 */ 
    1818 
     19#include <stdio.h> 
    1920#include <stdlib.h> 
    2021#include <string.h> 
    2122 
    2223#include "packet.h" 
     24#include "ap51-flash.h" 
     25#include "socket.h" 
    2326 
    2427#define PACKET_BUFF_LEN 1500 
     
    3336unsigned long tftp_bytes_sent = 0; 
    3437unsigned short tftp_ack_block = 0, tftp_sent_block = 0, xfer_in_progress = 0, write_req_timeout = 4; 
     38char tcp_status = TCP_CONTINUE; 
    3539 
    3640 
     
    5054void arp_packet_send(void) 
    5155{ 
    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); 
    5757} 
    5858 
     
    7373} 
    7474 
    75 static void tftp_packet_send(int tftp_data_len) 
     75static int tftp_packet_send(int tftp_data_len) 
    7676{ 
    7777        unsigned short sum; 
     
    100100#endif 
    101101 
    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 
     105static int tftp_write_req(void) 
    110106{ 
    111107        int tftp_data_len; 
     
    120116        tftp_data_len += 2; /* sprintf does not count \0 */ 
    121117 
    122         tftp_packet_send(tftp_data_len); 
    123 } 
    124  
    125 static void read_from_file(void *target_buff, void *data, int len, int seek_pos) 
     118        return tftp_packet_send(tftp_data_len); 
     119} 
     120 
     121static int read_from_file(void *target_buff, void *data, int len, int seek_pos) 
    126122{ 
    127123        struct flash_from_file *fff = (struct flash_from_file *)data; 
     
    136132                if (read_len != read(fff->fd, target_buff, read_len)) { 
    137133                        perror(fff->fname); 
    138                         exit(1); 
     134                        return 1; 
    139135                } 
    140136        } 
     
    145141        if (seek_pos == 0) 
    146142                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 
     147static int tftp_transfer(const unsigned char *packet_buff, unsigned int packet_len) 
    150148{ 
    151149        struct udphdr *rcv_udphdr = (struct udphdr *)packet_buff; 
    152150        unsigned short opcode, block; 
    153151        char *file_name; 
    154         int tftp_data_len; 
     152        int tftp_data_len, ret; 
    155153 
    156154        if ((flash_mode == MODE_REDBOOT) && (rcv_udphdr->dest != htons(IPPORT_TFTP))) 
    157                 return; 
     155                return 1; 
    158156 
    159157        if ((flash_mode == MODE_TFTP_CLIENT) && (rcv_udphdr->source != htons(IPPORT_TFTP))) 
    160                 return; 
     158                return 1; 
    161159 
    162160        opcode = ntohs(*(unsigned short *)(((char *)rcv_udphdr) + sizeof(struct udphdr))); 
     
    181179                } else { 
    182180                        fprintf(stderr, "Unknown file name: %s\n", file_name); 
    183                         exit(1); 
     181                        return -1; 
    184182                } 
    185183 
     
    210208                                        printf("Image successfully transmitted.\n"); 
    211209                                        printf("Please give the device a couple of minutes to install the new image into the flash.\n"); 
    212                                         exit(0); 
     210                                        return 0; 
    213211                                } 
    214                                 return; 
     212                                return 1; 
    215213                        } 
    216214 
     
    226224                tftp_data_len = tftp_xfer_size - tftp_bytes_sent > 512 ? 512 : tftp_xfer_size - tftp_bytes_sent; 
    227225 
    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 { 
    231231                        memcpy(tftp_data + 4, (void *)(tftp_xfer_buff + tftp_bytes_sent), tftp_data_len); 
     232                } 
    232233 
    233234                tftp_bytes_sent += tftp_data_len; 
    234235                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 
    236241                tftp_sent_block = block; 
    237242                /* printf("tftp data out: tftp_sent=%lu, remaining_size=%lu, data_len=%i, block=%d\n", 
     
    245250                else 
    246251                        fprintf(stderr, "Received TFTP error code: %d\n", block); 
    247                 exit(1); 
     252 
     253                return -1; 
    248254                break; 
    249255        default: 
    250256                fprintf(stderr, "Unexpected TFTP opcode: %d\n", opcode); 
    251                 exit(1); 
     257                return -1; 
    252258                break; 
    253259        } 
     260 
     261        return 1; 
    254262} 
    255263 
     
    257265{ 
    258266        const unsigned char *packet; 
    259         struct pcap_pkthdr hdr; 
    260267        struct ether_arp *rcv_arphdr; 
    261268        struct iphdr *rcv_iphdr; 
     269        int len, ret; 
    262270 
    263271        if (flash_mode == MODE_TFTP_CLIENT) { 
    264272                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; 
    266276        } 
    267277 
    268278        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                } 
    270291 
    271292                if (!packet) { 
     
    286307                                fprintf(stderr, "TFTP connection timeout .. \n"); 
    287308                                tftp_write_req(); 
     309                                if (ret != 0) 
     310                                        return ret; 
     311 
    288312                                write_req_timeout = 2; 
    289313                        } 
     
    295319                switch (ntohs(((struct ether_header *)packet)->ether_type)) { 
    296320                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); */ 
    299323                                continue; 
    300324                        } 
     
    332356                        break; 
    333357                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); 
    336360                                continue; 
    337361                        } 
     
    351375                        switch (rcv_iphdr->protocol) { 
    352376                        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 */ 
    355384                                break; 
    356385                        case IPPROTO_TCP: 
    357                                 handle_uip_tcp(packet, hdr.len); 
     386                                handle_uip_tcp(packet, len); 
    358387                                break; 
    359388                        default: 
  • ap51-flash/trunk/packet.h

    r222 r223  
    1717 */ 
    1818 
    19 #include "ap51-flash.h" 
    20  
    2119#define TFTP_SRC_PORT 13337 
    2220#define TELNET_PORT 9000 
    2321#define ARP_LEN (sizeof(struct ether_header) + sizeof(struct ether_arp)) 
    2422#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 
    2527 
    2628extern struct ether_header *ethhdr; 
     
    3133extern unsigned long tftp_bytes_sent; 
    3234extern unsigned short xfer_in_progress; 
     35extern char tcp_status; 
    3336 
    3437void arp_packet_init(void); 
  • ap51-flash/trunk/socket.h

    r222 r223  
    1717 */ 
    1818 
    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); 
     19char *socket_find_dev_by_index(char *number); 
     20void socket_print_all_devices(void); 
     21int socket_open(char *dev); 
     22int socket_setnonblock(void); 
     23unsigned char *socket_read(int *len); 
     24int socket_write(unsigned char *buff, int len); 
     25void socket_close(char *dev);