Show
Ignore:
Timestamp:
06/24/2009 07:07:47 PM (9 months ago)
Author:
BrainSlayer
Message:

tftp server added, supports wiligear, ubiquiti and dd-wrt webflash format

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ar5315_microredboot/microredboot/ecos/packages/redboot/current/src/net/net_io.c

    r12281 r12368  
    5656#include <redboot.h> 
    5757#include <net/net.h> 
    58 #include <cyg/hal/hal_misc.h>   // Helper functions 
    59 #include <cyg/hal/hal_if.h>     // HAL I/O interfaces 
     58#include <cyg/hal/hal_misc.h>  // Helper functions 
     59#include <cyg/hal/hal_if.h>    // HAL I/O interfaces 
    6060#include <cyg/hal/drv_api.h> 
    6161#include <cyg/hal/hal_intr.h> 
    62 #include <cyg/infra/cyg_ass.h>         // assertion macros 
     62#include <cyg/infra/cyg_ass.h> // assertion macros 
    6363 
    6464#ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
     
    6666 
    6767RedBoot_config_option("GDB connection port", 
    68                       gdb_port, 
    69                       ALWAYS_ENABLED, true, 
    70                       CONFIG_INT, 
    71                       CYGNUM_REDBOOT_NETWORKING_TCP_PORT 
    72     ); 
     68                      gdb_port, 
     69                      ALWAYS_ENABLED, true, 
     70                      CONFIG_INT, CYGNUM_REDBOOT_NETWORKING_TCP_PORT); 
    7371//RedBoot_config_option("Network debug at boot time",  
    7472//                      net_debug,  
     
    7876//    ); 
    7977#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1) 
    80 RedBoot_config_option("Default network device",  
    81                       net_device,  
    82                       ALWAYS_ENABLED, true, 
    83                       CONFIG_NETPORT, 
    84                       "" 
    85     ); 
     78RedBoot_config_option("Default network device", 
     79                      net_device, ALWAYS_ENABLED, true, CONFIG_NETPORT, ""); 
    8680#endif 
    8781// Note: the following options are related.  If 'bootp' is false, then 
     
    9791#endif 
    9892RedBoot_config_option("Use BOOTP for network configuration", 
    99                       bootp,  
    100                       ALWAYS_ENABLED, true, 
    101                       CONFIG_BOOL, 
    102                       !CYGSEM_REDBOOT_DEFAULT_NO_BOOTP 
    103     ); 
     93                      bootp, 
     94                      ALWAYS_ENABLED, true, 
     95                      CONFIG_BOOL, !CYGSEM_REDBOOT_DEFAULT_NO_BOOTP); 
    10496RedBoot_config_option("Local IP address", 
    105                       bootp_my_ip, 
    106                       "bootp", false, 
    107                       CONFIG_IP, 
    108                       0 
    109     ); 
     97                      bootp_my_ip, "bootp", false, CONFIG_IP, 0); 
    11098#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY 
    11199RedBoot_config_option("Local IP address mask", 
    112                       bootp_my_ip_mask, 
    113                       "bootp", false, 
    114                       CONFIG_IP, 
    115                       0 
    116     ); 
     100                      bootp_my_ip_mask, "bootp", false, CONFIG_IP, 0); 
    117101RedBoot_config_option("Gateway IP address", 
    118                       bootp_my_gateway_ip, 
    119                       "bootp", false, 
    120                       CONFIG_IP, 
    121                       0 
    122     ); 
     102                      bootp_my_gateway_ip, "bootp", false, CONFIG_IP, 0); 
    123103#endif 
    124104RedBoot_config_option("Default server IP address", 
    125                       bootp_server_ip, 
    126                       ALWAYS_ENABLED, true, 
    127                       CONFIG_IP, 
    128                       0 
    129     ); 
     105                      bootp_server_ip, ALWAYS_ENABLED, true, CONFIG_IP, 0); 
    130106 
    131107// Note: the following options are related too. 
    132108RedBoot_config_option("Force console for special debug messages", 
    133                       info_console_force,  
    134                       ALWAYS_ENABLED, true, 
    135                       CONFIG_BOOL, 
    136                       false 
    137     ); 
     109                      info_console_force, 
     110                      ALWAYS_ENABLED, true, CONFIG_BOOL, false); 
    138111RedBoot_config_option("Console number for special debug messages", 
    139                       info_console_number,  
    140                       "info_console_force", true, 
    141                       CONFIG_INT, 
    142                       0 
    143     ); 
     112                      info_console_number, 
     113                      "info_console_force", true, CONFIG_INT, 0); 
    144114#endif 
    145115 
     
    148118#ifdef DEBUG_TCP 
    149119int show_tcp = 0; 
    150 #endif  
     120#endif 
    151121 
    152122static tcp_socket_t tcp_sock; 
     
    166136static void net_io_flush(void); 
    167137static void net_io_revert_console(void); 
    168 static void net_io_putc(void*, cyg_uint8); 
     138static void net_io_putc(void *, cyg_uint8); 
    169139 
    170140// Special characters used by Telnet - must be interpretted here 
    171 #define TELNET_IAC    0xFF // Interpret as command (escape) 
    172 #define TELNET_IP     0xF4 // Interrupt process 
    173 #define TELNET_WONT   0xFC // I Won't do it 
    174 #define TELNET_DO     0xFD // Will you XXX 
    175 #define TELNET_TM     0x06 // Time marker (special DO/WONT after IP) 
    176  
    177 static cyg_bool 
    178 _net_io_getc_nonblock(void* __ch_data, cyg_uint8* ch) 
    179 
    180     if (in_buflen == 0) { 
    181         __tcp_poll(); 
    182         if (tcp_sock.state == _CLOSE_WAIT) { 
    183             // This connection is breaking 
    184             if (tcp_sock.data_bytes == 0 && tcp_sock.rxcnt == 0) { 
    185                 __tcp_close(&tcp_sock); 
    186                 return false; 
    187             } 
    188         } 
    189         if (tcp_sock.state == _CLOSED) { 
    190             // The connection is gone 
    191             net_io_revert_console(); 
    192             *ch = '\n'; 
    193             return true; 
    194         } 
    195         in_buflen = __tcp_read(&tcp_sock, in_buf, sizeof(in_buf)); 
    196         in_bufp = in_buf; 
     141#define TELNET_IAC    0xFF      // Interpret as command (escape) 
     142#define TELNET_IP     0xF4      // Interrupt process 
     143#define TELNET_WONT   0xFC      // I Won't do it 
     144#define TELNET_DO     0xFD      // Will you XXX 
     145#define TELNET_TM     0x06      // Time marker (special DO/WONT after IP) 
     146 
     147static cyg_bool _net_io_getc_nonblock(void *__ch_data, cyg_uint8 * ch) 
     148
     149        if (in_buflen == 0) { 
     150                __tcp_poll(); 
     151                if (tcp_sock.state == _CLOSE_WAIT) { 
     152                        // This connection is breaking 
     153                        if (tcp_sock.data_bytes == 0 && tcp_sock.rxcnt == 0) { 
     154                                __tcp_close(&tcp_sock); 
     155                                return false; 
     156                        } 
     157                } 
     158                if (tcp_sock.state == _CLOSED) { 
     159                        // The connection is gone 
     160                        net_io_revert_console(); 
     161                        *ch = '\n'; 
     162                        return true; 
     163                } 
     164                in_buflen = __tcp_read(&tcp_sock, in_buf, sizeof(in_buf)); 
     165                in_bufp = in_buf; 
    197166#ifdef DEBUG_TCP 
    198         if (show_tcp && (in_buflen > 0)) { 
    199             int old_console; 
    200             old_console = start_console();   
    201             diag_printf("%s:%d\n", __FUNCTION__, __LINE__);   
    202             diag_dump_buf(in_buf, in_buflen);   
    203             end_console(old_console); 
    204         } 
    205 #endif // DEBUG_TCP 
    206     } 
    207     if (in_buflen) { 
    208         *ch = *in_bufp++; 
    209         in_buflen--; 
    210         return true; 
    211     } else { 
    212         return false; 
    213     } 
    214 
    215  
    216 static cyg_bool 
    217 net_io_getc_nonblock(void* __ch_data, cyg_uint8* ch) 
    218 
    219     cyg_uint8 esc; 
    220  
    221     if (!_net_io_getc_nonblock(__ch_data, ch)) 
    222         return false; 
    223  
    224     if (gdb_active || *ch != TELNET_IAC) 
    225         return true; 
    226  
    227     // Telnet escape - need to read/handle more 
    228     while (!_net_io_getc_nonblock(__ch_data, &esc)) ; 
    229  
    230     switch (esc) { 
    231     case TELNET_IAC: 
    232         // The other special case - escaped escape 
    233         return true; 
    234     case TELNET_IP: 
    235         // Special case for ^C == Interrupt Process 
    236         *ch = 0x03;   
    237         // Just in case the other end needs synchronizing 
    238         net_io_putc(__ch_data, TELNET_IAC); 
    239         net_io_putc(__ch_data, TELNET_WONT); 
    240         net_io_putc(__ch_data, TELNET_TM); 
    241         net_io_flush(); 
    242         return true; 
    243     case TELNET_DO: 
    244         // Telnet DO option 
    245         while (!_net_io_getc_nonblock(__ch_data, &esc)) ;                 
    246         // Respond with WONT option 
    247         net_io_putc(__ch_data, TELNET_IAC); 
    248         net_io_putc(__ch_data, TELNET_WONT); 
    249         net_io_putc(__ch_data, esc); 
    250         return false;  // Ignore this whole thing! 
    251     default: 
    252         return false; 
    253     } 
    254 
    255  
    256 static cyg_uint8 
    257 net_io_getc(void* __ch_data) 
    258 
    259     cyg_uint8 ch; 
    260     int idle_timeout = 10;  // 10ms 
    261  
    262     CYGARC_HAL_SAVE_GP(); 
    263     while (true) { 
    264         if (net_io_getc_nonblock(__ch_data, &ch)) break; 
    265         if (--idle_timeout == 0) { 
    266             net_io_flush(); 
    267             idle_timeout = 10; 
    268         } 
    269     } 
    270     CYGARC_HAL_RESTORE_GP(); 
    271     return ch; 
     167                if (show_tcp && (in_buflen > 0)) { 
     168                        int old_console; 
     169                        old_console = start_console(); 
     170                        diag_printf("%s:%d\n", __FUNCTION__, __LINE__); 
     171                        diag_dump_buf(in_buf, in_buflen); 
     172                        end_console(old_console); 
     173                } 
     174#endif                          // DEBUG_TCP 
     175        } 
     176        if (in_buflen) { 
     177                *ch = *in_bufp++; 
     178                in_buflen--; 
     179                return true; 
     180        } else { 
     181                return false; 
     182        } 
     183
     184 
     185static cyg_bool net_io_getc_nonblock(void *__ch_data, cyg_uint8 * ch) 
     186
     187        cyg_uint8 esc; 
     188 
     189        if (!_net_io_getc_nonblock(__ch_data, ch)) 
     190                return false; 
     191 
     192        if (gdb_active || *ch != TELNET_IAC) 
     193                return true; 
     194 
     195        // Telnet escape - need to read/handle more 
     196        while (!_net_io_getc_nonblock(__ch_data, &esc)) ; 
     197 
     198        switch (esc) { 
     199        case TELNET_IAC: 
     200                // The other special case - escaped escape 
     201                return true; 
     202        case TELNET_IP: 
     203                // Special case for ^C == Interrupt Process 
     204                *ch = 0x03; 
     205                // Just in case the other end needs synchronizing 
     206                net_io_putc(__ch_data, TELNET_IAC); 
     207                net_io_putc(__ch_data, TELNET_WONT); 
     208                net_io_putc(__ch_data, TELNET_TM); 
     209                net_io_flush(); 
     210                return true; 
     211        case TELNET_DO: 
     212                // Telnet DO option 
     213                while (!_net_io_getc_nonblock(__ch_data, &esc)) ; 
     214                // Respond with WONT option 
     215                net_io_putc(__ch_data, TELNET_IAC); 
     216                net_io_putc(__ch_data, TELNET_WONT); 
     217                net_io_putc(__ch_data, esc); 
     218                return false;   // Ignore this whole thing! 
     219        default: 
     220                return false; 
     221        } 
     222
     223 
     224static cyg_uint8 net_io_getc(void *__ch_data) 
     225
     226        cyg_uint8 ch; 
     227        int idle_timeout = 10;  // 10ms 
     228 
     229        CYGARC_HAL_SAVE_GP(); 
     230        while (true) { 
     231                if (net_io_getc_nonblock(__ch_data, &ch)) 
     232                        break; 
     233                if (--idle_timeout == 0) { 
     234                        net_io_flush(); 
     235                        idle_timeout = 10; 
     236                } 
     237        } 
     238        CYGARC_HAL_RESTORE_GP(); 
     239        return ch; 
     240
     241 
     242static void net_io_flush(void) 
     243
     244        int n; 
     245        char *bp = out_buf; 
     246 
     247#ifdef DEBUG_TCP 
     248        if (show_tcp) { 
     249                int old_console; 
     250                old_console = start_console(); 
     251                diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
     252                diag_dump_buf(out_buf, out_buflen); 
     253                end_console(old_console); 
     254        } 
     255#endif                          // SHOW_TCP 
     256        n = __tcp_write_block(&tcp_sock, bp, out_buflen); 
     257        if (n < 0) { 
     258                // The connection is gone! 
     259                net_io_revert_console(); 
     260        } else { 
     261                out_buflen -= n; 
     262                bp += n; 
     263        } 
     264        out_bufp = out_buf; 
     265        out_buflen = 0; 
     266        // Check interrupt flag 
     267        if (CYGACC_CALL_IF_CONSOLE_INTERRUPT_FLAG()) { 
     268                CYGACC_CALL_IF_CONSOLE_INTERRUPT_FLAG_SET(0); 
     269                cyg_hal_user_break(0); 
     270        } 
     271
     272 
     273static void net_io_putc(void *__ch_data, cyg_uint8 c) 
     274
     275        static bool have_dollar, have_hash; 
     276        static int hash_count; 
     277 
     278        CYGARC_HAL_SAVE_GP(); 
     279        *out_bufp++ = c; 
     280        if (c == '$') 
     281                have_dollar = true; 
     282        if (have_dollar && (c == '#')) { 
     283                have_hash = true; 
     284                hash_count = 0; 
     285        } 
     286        if ((++out_buflen == sizeof(out_buf)) || 
     287            (flush_output_lines && c == '\n') || 
     288            (have_hash && (++hash_count == 3))) { 
     289                net_io_flush(); 
     290                have_dollar = false; 
     291        } 
     292        CYGARC_HAL_RESTORE_GP(); 
    272293} 
    273294 
    274295static void 
    275 net_io_flush(void) 
    276 
    277     int n; 
    278     char *bp = out_buf; 
    279  
     296net_io_write(void *__ch_data, const cyg_uint8 * __buf, cyg_uint32 __len) 
     297
     298        int old_console; 
     299 
     300        old_console = start_console(); 
     301        diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
     302        end_console(old_console); 
     303#if 0 
     304        CYGARC_HAL_SAVE_GP(); 
     305 
     306        while (__len-- > 0) 
     307                net_io_putc(__ch_data, *__buf++); 
     308 
     309        CYGARC_HAL_RESTORE_GP(); 
     310#endif 
     311
     312 
     313static void net_io_read(void *__ch_data, cyg_uint8 * __buf, cyg_uint32 __len) 
     314
     315        int old_console; 
     316 
     317        old_console = start_console(); 
     318        diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
     319        end_console(old_console); 
     320#if 0 
     321        CYGARC_HAL_SAVE_GP(); 
     322 
     323        while (__len-- > 0) 
     324                *__buf++ = net_io_getc(__ch_data); 
     325 
     326        CYGARC_HAL_RESTORE_GP(); 
     327#endif 
     328
     329 
     330static cyg_bool net_io_getc_timeout(void *__ch_data, cyg_uint8 * ch) 
     331
     332        int delay_count; 
     333        cyg_bool res; 
     334 
     335        CYGARC_HAL_SAVE_GP(); 
     336        net_io_flush();         // Make sure any output has been sent 
     337        delay_count = _timeout; 
     338 
     339        for (;;) { 
     340                res = net_io_getc_nonblock(__ch_data, ch); 
     341                if (res || 0 == delay_count--) 
     342                        break; 
     343        } 
     344 
     345        CYGARC_HAL_RESTORE_GP(); 
     346 
     347        return res; 
     348
     349 
     350static int net_io_control(void *__ch_data, __comm_control_cmd_t __func, ...) 
     351
     352        static int vector = 0; 
     353        int ret = 0; 
     354        static int irq_state = 0; 
     355 
     356        CYGARC_HAL_SAVE_GP(); 
     357 
     358        switch (__func) { 
     359        case __COMMCTL_IRQ_ENABLE: 
     360                irq_state = 1; 
     361                if (vector == 0) { 
     362                        vector = eth_drv_int_vector(); 
     363                } 
     364                HAL_INTERRUPT_UNMASK(vector); 
     365                break; 
     366        case __COMMCTL_IRQ_DISABLE: 
     367                ret = irq_state; 
     368                irq_state = 0; 
     369                if (vector == 0) { 
     370                        vector = eth_drv_int_vector(); 
     371                } 
     372                HAL_INTERRUPT_MASK(vector); 
     373                break; 
     374        case __COMMCTL_DBG_ISR_VECTOR: 
     375                ret = vector; 
     376                break; 
     377        case __COMMCTL_SET_TIMEOUT: 
     378                { 
     379                        va_list ap; 
     380 
     381                        va_start(ap, __func); 
     382 
     383                        ret = _timeout; 
     384                        _timeout = va_arg(ap, cyg_uint32); 
     385 
     386                        va_end(ap); 
     387                        break; 
     388                } 
     389        case __COMMCTL_FLUSH_OUTPUT: 
     390                net_io_flush(); 
     391                break; 
     392        case __COMMCTL_ENABLE_LINE_FLUSH: 
     393                flush_output_lines = true; 
     394                break; 
     395        case __COMMCTL_DISABLE_LINE_FLUSH: 
     396                flush_output_lines = false; 
     397                break; 
     398        default: 
     399                break; 
     400        } 
     401        CYGARC_HAL_RESTORE_GP(); 
     402        return ret; 
     403
     404 
     405static int 
     406net_io_isr(void *__ch_data, int *__ctrlc, 
     407           CYG_ADDRWORD __vector, CYG_ADDRWORD __data) 
     408
     409        char ch; 
     410 
     411        CYGARC_HAL_SAVE_GP(); 
     412        *__ctrlc = 0; 
     413        if (net_io_getc_nonblock(__ch_data, &ch)) { 
     414                if (ch == 0x03) { 
     415                        *__ctrlc = 1; 
     416                } 
     417        } 
     418        CYGARC_HAL_RESTORE_GP(); 
     419        return CYG_ISR_HANDLED; 
     420
     421 
     422// TEMP 
     423 
     424int start_console(void) 
     425
     426        int cur_console = 
     427            CYGACC_CALL_IF_SET_CONSOLE_COMM 
     428            (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
     429 
     430#ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
     431        int i = 0; 
     432        if (flash_get_config("info_console_force", &i, CONFIG_BOOL)) 
     433                if (i) 
     434                        if (!flash_get_config 
     435                            ("info_console_number", &i, CONFIG_INT)) 
     436                                i = 0;  // the default, if that call failed. 
     437        if (i) 
     438                CYGACC_CALL_IF_SET_CONSOLE_COMM(i); 
     439        else 
     440#endif 
     441                CYGACC_CALL_IF_SET_CONSOLE_COMM(0); 
     442 
     443        return cur_console; 
     444
     445 
     446void end_console(int old_console) 
     447
     448        // Restore original console 
     449        CYGACC_CALL_IF_SET_CONSOLE_COMM(old_console); 
     450
     451 
     452// TEMP 
     453 
     454static void net_io_revert_console(void) 
     455
     456#ifdef CYGPKG_REDBOOT_ANY_CONSOLE 
     457        console_selected = false; 
     458#endif 
     459        CYGACC_CALL_IF_SET_CONSOLE_COMM(orig_console); 
     460        CYGACC_CALL_IF_SET_DEBUG_COMM(orig_debug); 
     461        console_echo = true; 
     462
     463 
     464static void net_io_assume_console(void) 
     465
     466#ifdef CYGPKG_REDBOOT_ANY_CONSOLE 
     467        console_selected = true; 
     468#endif 
     469        console_echo = false; 
     470        orig_console = 
     471            CYGACC_CALL_IF_SET_CONSOLE_COMM 
     472            (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
     473        CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); 
     474        orig_debug = 
     475            CYGACC_CALL_IF_SET_DEBUG_COMM 
     476            (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
     477        CYGACC_CALL_IF_SET_DEBUG_COMM(TCP_CHANNEL); 
     478
     479 
     480static void net_io_init(void) 
     481
     482        static int init = 0; 
     483        if (!init) { 
     484                hal_virtual_comm_table_t *comm; 
     485                int cur = 
     486                    CYGACC_CALL_IF_SET_CONSOLE_COMM 
     487                    (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
     488 
     489                // Setup procs in the vector table 
     490                CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); 
     491                comm = CYGACC_CALL_IF_CONSOLE_PROCS(); 
     492                //CYGACC_COMM_IF_CH_DATA_SET(*comm, chan); 
     493                CYGACC_COMM_IF_WRITE_SET(*comm, net_io_write); 
     494                CYGACC_COMM_IF_READ_SET(*comm, net_io_read); 
     495                CYGACC_COMM_IF_PUTC_SET(*comm, net_io_putc); 
     496                CYGACC_COMM_IF_GETC_SET(*comm, net_io_getc); 
     497                CYGACC_COMM_IF_CONTROL_SET(*comm, net_io_control); 
     498                CYGACC_COMM_IF_DBG_ISR_SET(*comm, net_io_isr); 
     499                CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, net_io_getc_timeout); 
     500 
     501                // Disable interrupts via this interface to set static 
     502                // state into correct state. 
     503                net_io_control(comm, __COMMCTL_IRQ_DISABLE); 
     504 
     505                // Restore original console 
     506                CYGACC_CALL_IF_SET_CONSOLE_COMM(cur); 
     507 
     508                init = 1; 
     509                gdb_active = false; 
     510        } 
     511        __tcp_listen(&tcp_sock, gdb_port); 
     512        state = tcp_sock.state; 
    280513#ifdef DEBUG_TCP 
    281     if (show_tcp) { 
    282         int old_console; 
    283         old_console = start_console();   
    284         diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
    285         diag_dump_buf(out_buf, out_buflen);   
    286         end_console(old_console); 
    287     } 
    288 #endif // SHOW_TCP 
    289     n = __tcp_write_block(&tcp_sock, bp, out_buflen); 
    290     if (n < 0) { 
    291         // The connection is gone! 
    292         net_io_revert_console(); 
    293     } else { 
    294         out_buflen -= n; 
    295         bp += n; 
    296     } 
    297     out_bufp = out_buf;  out_buflen = 0; 
    298     // Check interrupt flag 
    299     if (CYGACC_CALL_IF_CONSOLE_INTERRUPT_FLAG()) { 
    300         CYGACC_CALL_IF_CONSOLE_INTERRUPT_FLAG_SET(0); 
    301         cyg_hal_user_break(0); 
    302     } 
    303 
    304  
    305 static void 
    306 net_io_putc(void* __ch_data, cyg_uint8 c) 
    307 
    308     static bool have_dollar, have_hash; 
    309     static int hash_count; 
    310  
    311     CYGARC_HAL_SAVE_GP(); 
    312     *out_bufp++ = c; 
    313     if (c == '$') have_dollar = true; 
    314     if (have_dollar && (c == '#')) { 
    315         have_hash = true; 
    316         hash_count = 0; 
    317     } 
    318     if ((++out_buflen == sizeof(out_buf)) || 
    319         (flush_output_lines && c == '\n') || 
    320         (have_hash && (++hash_count == 3))) { 
    321         net_io_flush(); 
    322         have_dollar = false; 
    323     } 
    324     CYGARC_HAL_RESTORE_GP(); 
    325 
    326  
    327 static void 
    328 net_io_write(void* __ch_data, const cyg_uint8* __buf, cyg_uint32 __len) 
    329 
    330     int old_console; 
    331  
    332     old_console = start_console(); 
    333     diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
    334     end_console(old_console); 
    335 #if 0 
    336     CYGARC_HAL_SAVE_GP(); 
    337  
    338     while(__len-- > 0) 
    339         net_io_putc(__ch_data, *__buf++); 
    340  
    341     CYGARC_HAL_RESTORE_GP(); 
    342 #endif 
    343 
    344  
    345 static void 
    346 net_io_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len) 
    347 
    348     int old_console; 
    349  
    350     old_console = start_console(); 
    351     diag_printf("%s.%d\n", __FUNCTION__, __LINE__); 
    352     end_console(old_console); 
    353 #if 0 
    354     CYGARC_HAL_SAVE_GP(); 
    355  
    356     while(__len-- > 0) 
    357         *__buf++ = net_io_getc(__ch_data); 
    358  
    359     CYGARC_HAL_RESTORE_GP(); 
    360 #endif 
    361 
    362  
    363 static cyg_bool 
    364 net_io_getc_timeout(void* __ch_data, cyg_uint8* ch) 
    365 
    366     int delay_count; 
    367     cyg_bool res; 
    368  
    369     CYGARC_HAL_SAVE_GP(); 
    370     net_io_flush();  // Make sure any output has been sent 
    371     delay_count = _timeout; 
    372  
    373     for(;;) { 
    374         res = net_io_getc_nonblock(__ch_data, ch); 
    375         if (res || 0 == delay_count--) 
    376             break; 
    377     } 
    378  
    379     CYGARC_HAL_RESTORE_GP(); 
    380  
    381     return res; 
    382 
    383  
    384 static int 
    385 net_io_control(void *__ch_data, __comm_control_cmd_t __func, ...) 
    386 
    387     static int vector = 0; 
    388     int ret = 0; 
    389     static int irq_state = 0; 
    390  
    391     CYGARC_HAL_SAVE_GP(); 
    392  
    393     switch (__func) { 
    394     case __COMMCTL_IRQ_ENABLE: 
    395         irq_state = 1; 
    396         if (vector == 0) { 
    397             vector = eth_drv_int_vector(); 
    398         } 
    399         HAL_INTERRUPT_UNMASK(vector);  
    400         break; 
    401     case __COMMCTL_IRQ_DISABLE: 
    402         ret = irq_state; 
    403         irq_state = 0; 
    404         if (vector == 0) { 
    405             vector = eth_drv_int_vector(); 
    406         } 
    407         HAL_INTERRUPT_MASK(vector); 
    408         break; 
    409     case __COMMCTL_DBG_ISR_VECTOR: 
    410         ret = vector; 
    411         break; 
    412     case __COMMCTL_SET_TIMEOUT: 
    413     { 
    414         va_list ap; 
    415  
    416         va_start(ap, __func); 
    417  
    418         ret = _timeout; 
    419         _timeout = va_arg(ap, cyg_uint32); 
    420  
    421         va_end(ap); 
    422         break; 
    423     } 
    424     case __COMMCTL_FLUSH_OUTPUT: 
    425         net_io_flush(); 
    426         break; 
    427     case __COMMCTL_ENABLE_LINE_FLUSH: 
    428         flush_output_lines = true; 
    429         break; 
    430     case __COMMCTL_DISABLE_LINE_FLUSH: 
    431         flush_output_lines = false; 
    432         break; 
    433     default: 
    434         break; 
    435     } 
    436     CYGARC_HAL_RESTORE_GP(); 
    437     return ret; 
    438 
    439  
    440 static int 
    441 net_io_isr(void *__ch_data, int* __ctrlc,  
    442            CYG_ADDRWORD __vector, CYG_ADDRWORD __data) 
    443 
    444     char ch; 
    445  
    446     CYGARC_HAL_SAVE_GP(); 
    447     *__ctrlc = 0; 
    448     if (net_io_getc_nonblock(__ch_data, &ch)) { 
    449         if (ch == 0x03) { 
    450             *__ctrlc = 1; 
    451         } 
    452     } 
    453     CYGARC_HAL_RESTORE_GP(); 
    454     return CYG_ISR_HANDLED; 
    455 
    456  
    457 // TEMP 
    458  
    459 int  
    460 start_console(void) 
    461 
    462     int cur_console = 
    463         CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
    464  
    465 #ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
    466     int i = 0; 
    467     if ( flash_get_config( "info_console_force", &i, CONFIG_BOOL) ) 
    468         if ( i ) 
    469             if ( ! flash_get_config( "info_console_number", &i, CONFIG_INT) ) 
    470                 i = 0; // the default, if that call failed. 
    471     if ( i ) 
    472         CYGACC_CALL_IF_SET_CONSOLE_COMM(i); 
    473     else 
    474 #endif 
    475         CYGACC_CALL_IF_SET_CONSOLE_COMM(0); 
    476  
    477     return cur_console; 
    478 
    479  
    480 void 
    481 end_console(int old_console) 
    482 
    483     // Restore original console 
    484     CYGACC_CALL_IF_SET_CONSOLE_COMM(old_console); 
    485 
    486 // TEMP 
    487  
    488 static void 
    489 net_io_revert_console(void) 
    490 
    491 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE 
    492     console_selected = false; 
    493 #endif 
    494     CYGACC_CALL_IF_SET_CONSOLE_COMM(orig_console); 
    495     CYGACC_CALL_IF_SET_DEBUG_COMM(orig_debug); 
    496     console_echo = true; 
    497 
    498  
    499 static void 
    500 net_io_assume_console(void) 
    501 
    502 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE 
    503     console_selected = true; 
    504 #endif 
    505     console_echo = false; 
    506     orig_console = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
    507     CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); 
    508     orig_debug = CYGACC_CALL_IF_SET_DEBUG_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
    509     CYGACC_CALL_IF_SET_DEBUG_COMM(TCP_CHANNEL); 
    510 
    511  
    512 static void 
    513 net_io_init(void) 
    514 
    515     static int init = 0; 
    516     if (!init) { 
    517         hal_virtual_comm_table_t* comm; 
    518         int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT); 
    519  
    520         // Setup procs in the vector table 
    521         CYGACC_CALL_IF_SET_CONSOLE_COMM(TCP_CHANNEL); 
    522         comm = CYGACC_CALL_IF_CONSOLE_PROCS(); 
    523         //CYGACC_COMM_IF_CH_DATA_SET(*comm, chan); 
    524         CYGACC_COMM_IF_WRITE_SET(*comm, net_io_write); 
    525         CYGACC_COMM_IF_READ_SET(*comm, net_io_read); 
    526         CYGACC_COMM_IF_PUTC_SET(*comm, net_io_putc); 
    527         CYGACC_COMM_IF_GETC_SET(*comm, net_io_getc); 
    528         CYGACC_COMM_IF_CONTROL_SET(*comm, net_io_control); 
    529         CYGACC_COMM_IF_DBG_ISR_SET(*comm, net_io_isr); 
    530         CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, net_io_getc_timeout); 
    531  
    532         // Disable interrupts via this interface to set static 
    533         // state into correct state. 
    534         net_io_control( comm, __COMMCTL_IRQ_DISABLE ); 
    535          
    536         // Restore original console 
    537         CYGACC_CALL_IF_SET_CONSOLE_COMM(cur); 
    538  
    539         init = 1; 
    540         gdb_active = false; 
    541     } 
    542     __tcp_listen(&tcp_sock, gdb_port); 
    543     state = tcp_sock.state;  
    544 #ifdef DEBUG_TCP 
    545     diag_printf("show tcp = %p\n", (void *)&show_tcp); 
     514        diag_printf("show tcp = %p\n", (void *)&show_tcp); 
    546515#endif 
    547516} 
    548517 
    549518// Check for incoming TCP debug connection 
    550 void 
    551 net_io_test(bool is_idle) 
    552 
    553     if (!is_idle) return;  // Only care about idle case 
    554     if (!have_net) return; 
    555     __tcp_poll(); 
    556     if (state != tcp_sock.state) { 
    557         // Something has changed 
    558         if (tcp_sock.state == _ESTABLISHED) { 
    559             // A new connection has arrived 
    560             net_io_assume_console(); 
    561             in_bufp = in_buf;  in_buflen = 1;  *in_bufp = '\r'; 
    562             out_bufp = out_buf;  out_buflen = 0; 
    563         } 
    564         if (tcp_sock.state == _CLOSED) { 
    565             net_io_init();  // Get ready for another connection 
    566         } 
    567     } 
    568     state = tcp_sock.state; 
     519void net_io_test(bool is_idle) 
     520
     521        if (!is_idle) 
     522                return;         // Only care about idle case 
     523        if (!have_net) 
     524                return; 
     525        __tcp_poll(); 
     526        if (state != tcp_sock.state) { 
     527                // Something has changed 
     528                if (tcp_sock.state == _ESTABLISHED) { 
     529                        // A new connection has arrived 
     530                        net_io_assume_console(); 
     531                        in_bufp = in_buf; 
     532                        in_buflen = 1; 
     533                        *in_bufp = '\r'; 
     534                        out_bufp = out_buf; 
     535                        out_buflen = 0; 
     536                } 
     537                if (tcp_sock.state == _CLOSED) { 
     538                        net_io_init();  // Get ready for another connection 
     539                } 
     540        } 
     541        state = tcp_sock.state; 
    569542} 
    570543 
     
    582555 
    583556// Define table boundaries 
    584 CYG_HAL_TABLE_BEGIN( __NETDEVTAB__, netdev ); 
    585 CYG_HAL_TABLE_END( __NETDEVTAB_END__, netdev ); 
     557CYG_HAL_TABLE_BEGIN(__NETDEVTAB__, netdev); 
     558CYG_HAL_TABLE_END(__NETDEVTAB_END__, netdev); 
    586559 
    587560RedBoot_init(net_init, RedBoot_INIT_LAST); 
    588561 
    589 static void 
    590 show_addrs(void) 
    591 
    592     diag_printf("IP: %s", inet_ntoa((in_addr_t *)&__local_ip_addr)); 
     562static void show_addrs(void) 
     563
     564        diag_printf("IP: %s", inet_ntoa((in_addr_t *) & __local_ip_addr)); 
    593565#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY 
    594     diag_printf("/%s", inet_ntoa((in_addr_t *)&__local_ip_mask)); 
    595     diag_printf(", Gateway: %s\n", inet_ntoa((in_addr_t *)&__local_ip_gate)); 
     566        diag_printf("/%s", inet_ntoa((in_addr_t *) & __local_ip_mask)); 
     567        diag_printf(", Gateway: %s\n", 
     568                    inet_ntoa((in_addr_t *) & __local_ip_gate)); 
    596569#else 
    597     diag_printf(", "); 
    598 #endif 
    599     diag_printf("Default server: %s", inet_ntoa(&my_bootp_info.bp_siaddr)); 
     570       diag_printf(", "); 
     571#endif 
     572       diag_printf("Default server: %s", inet_ntoa(&my_bootp_info.bp_siaddr)); 
    600573#ifdef CYGPKG_REDBOOT_NETWORKING_DNS 
    601     show_dns(); 
    602 #endif 
    603     diag_printf("\n"); 
     574       show_dns(); 
     575#endif 
     576       diag_printf("\n"); 
    604577} 
    605578 
    606579#ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
    607 static void 
    608 flash_get_IP(char *id, ip_addr_t *val) 
    609 
    610     ip_addr_t my_ip; 
    611     int i; 
    612  
    613     if (flash_get_config(id, &my_ip, CONFIG_IP)) { 
    614         if (my_ip[0] != 0 || my_ip[1] != 0 || 
    615             my_ip[2] != 0 || my_ip[3] != 0) { 
    616             // 'id' is set to something so let it override any static IP 
    617             for (i=0; i<4; i++) 
    618                 (*val)[i] = my_ip[i]; 
    619         }         
    620     } 
    621 
    622 #endif 
    623  
    624 static cyg_netdevtab_entry_t * 
    625 net_devtab_entry(unsigned index) 
    626 
    627     cyg_netdevtab_entry_t *t = &__NETDEVTAB__[index]; 
    628  
    629     if (t < &__NETDEVTAB__[0] || t >= &__NETDEVTAB_END__) 
     580static void flash_get_IP(char *id, ip_addr_t * val) 
     581
     582        ip_addr_t my_ip; 
     583        int i; 
     584 
     585        if (flash_get_config(id, &my_ip, CONFIG_IP)) { 
     586                if (my_ip[0] != 0 || my_ip[1] != 0 || 
     587                    my_ip[2] != 0 || my_ip[3] != 0) { 
     588                        // 'id' is set to something so let it override any static IP 
     589                        for (i = 0; i < 4; i++) 
     590                                (*val)[i] = my_ip[i]; 
     591                } 
     592        } 
     593
     594#endif 
     595 
     596static cyg_netdevtab_entry_t *net_devtab_entry(unsigned index) 
     597
     598        cyg_netdevtab_entry_t *t = &__NETDEVTAB__[index]; 
     599 
     600        if (t < &__NETDEVTAB__[0] || t >= &__NETDEVTAB_END__) 
     601                return NULL; 
     602 
     603        return t; 
     604
     605 
     606const char *net_devname(unsigned index) 
     607
     608        cyg_netdevtab_entry_t *t = net_devtab_entry(index); 
     609        if (t) 
     610                return t->name; 
    630611        return NULL; 
    631  
    632     return t; 
    633 
    634  
    635 const char * 
    636 net_devname(unsigned index) 
    637 
    638     cyg_netdevtab_entry_t *t = net_devtab_entry(index); 
    639     if (t) 
    640         return t->name; 
    641     return NULL; 
    642 
    643  
    644 int 
    645 net_devindex(char *name) 
    646 
    647     const char *devname; 
    648     int index; 
    649  
    650     for (index = 0; (devname = net_devname(index)) != NULL; index++) 
    651         if (!strcmp(name, devname)) 
    652             return index; 
    653     return -1; 
    654 
    655  
    656 static void 
    657 show_eth_info(void) 
    658 
    659     diag_printf("Ethernet %s: MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", 
    660                 __local_enet_sc->dev_name, 
    661                 __local_enet_addr[0], 
    662                 __local_enet_addr[1], 
    663                 __local_enet_addr[2], 
    664                 __local_enet_addr[3], 
    665                 __local_enet_addr[4], 
    666                 __local_enet_addr[5]); 
    667 
    668  
    669 void 
    670 net_init(void) 
    671 
    672     cyg_netdevtab_entry_t *t; 
    673     unsigned index; 
    674     struct eth_drv_sc *primary_net = (struct eth_drv_sc *)0; 
     612
     613 
     614int net_devindex(char *name) 
     615
     616        const char *devname; 
     617        int index; 
     618 
     619        for (index = 0; (devname = net_devname(index)) != NULL; index++) 
     620                if (!strcmp(name, devname)) 
     621                        return index; 
     622        return -1; 
     623
     624 
     625static void show_eth_info(void) 
     626
     627        diag_printf("Ethernet %s: MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", 
     628                    __local_enet_sc->dev_name, 
     629                    __local_enet_addr[0], 
     630                    __local_enet_addr[1], 
     631                    __local_enet_addr[2], 
     632                    __local_enet_addr[3], 
     633                    __local_enet_addr[4], __local_enet_addr[5]); 
     634
     635 
     636void net_init(void) 
     637
     638        cyg_netdevtab_entry_t *t; 
     639        unsigned index; 
     640        struct eth_drv_sc *primary_net = (struct eth_drv_sc *)0; 
    675641#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1) 
    676     char *default_devname; 
    677     int default_index; 
     642       char *default_devname; 
     643       int default_index; 
    678644#endif 
    679645#ifdef CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR 
    680     char ip_addr[16]; 
    681 #endif 
    682  
    683     // Set defaults as appropriate 
     646       char ip_addr[16]; 
     647#endif 
     648 
     649       // Set defaults as appropriate 
    684650#ifdef CYGSEM_REDBOOT_DEFAULT_NO_BOOTP 
    685     use_bootp = false; 
     651       use_bootp = false; 
    686652#else 
    687     use_bootp = true; 
     653       use_bootp = true; 
    688654#endif 
    689655#ifdef CYGDBG_REDBOOT_NET_DEBUG 
    690     net_debug = true; 
     656       net_debug = true; 
    691657#else 
    692     net_debug = false; 
    693 #endif 
    694     gdb_port = CYGNUM_REDBOOT_NETWORKING_TCP_PORT; 
     658       net_debug = false; 
     659#endif 
     660       gdb_port = CYGNUM_REDBOOT_NETWORKING_TCP_PORT; 
    695661#ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
    696     // Fetch values from saved config data, if available 
     662       // Fetch values from saved config data, if available 
    697663#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1) 
    698     flash_get_config("net_device", &default_devname, CONFIG_NETPORT); 
     664       flash_get_config("net_device", &default_devname, CONFIG_NETPORT); 
    699665#endif 
    700666//    flash_get_config("net_debug", &net_debug, CONFIG_BOOL); 
    701     flash_get_config("gdb_port", &gdb_port, CONFIG_INT); 
    702     flash_get_config("bootp", &use_bootp, CONFIG_BOOL); 
    703     if (!use_bootp) { 
    704         flash_get_IP("bootp_my_ip", &__local_ip_addr); 
     667       flash_get_config("gdb_port", &gdb_port, CONFIG_INT); 
     668       flash_get_config("bootp", &use_bootp, CONFIG_BOOL); 
     669       if (!use_bootp) { 
     670               flash_get_IP("bootp_my_ip", &__local_ip_addr); 
    705671#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY 
    706         flash_get_IP("bootp_my_ip_mask", &__local_ip_mask); 
    707         flash_get_IP("bootp_my_gateway_ip", &__local_ip_gate); 
    708 #endif 
    709    
     672               flash_get_IP("bootp_my_ip_mask", &__local_ip_mask); 
     673               flash_get_IP("bootp_my_gateway_ip", &__local_ip_gate); 
     674#endif 
     675       
    710676#endif 
    711677# ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG 
    712     // Don't override if the user has deliberately set something more 
    713     // verbose. 
    714     if (0 == cyg_io_eth_net_debug) 
    715         cyg_io_eth_net_debug = net_debug; 
     678       // Don't override if the user has deliberately set something more 
     679       // verbose. 
     680       if (0 == cyg_io_eth_net_debug) 
     681               cyg_io_eth_net_debug = net_debug; 
    716682# endif 
    717     have_net = false; 
    718     // Make sure the recv buffers are set up 
    719     eth_drv_buffers_init(); 
    720     __pktbuf_init(); 
    721  
    722     // Initialize network device(s). 
     683       have_net = false; 
     684       // Make sure the recv buffers are set up 
     685       eth_drv_buffers_init(); 
     686       __pktbuf_init(); 
     687 
     688       // Initialize network device(s). 
    723689#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1) 
    724     default_index = net_devindex(default_devname); 
    725     if (default_index < 0) 
    726         default_index = 0; 
     690       default_index = net_devindex(default_devname); 
     691       if (default_index < 0) 
     692               default_index = 0; 
    727693#ifdef CYGSEM_REDBOOT_NETWORK_INIT_ONE_DEVICE 
    728     if ((t = net_devtab_entry(default_index)) != NULL && t->init(t)) { 
    729         t->status = CYG_NETDEVTAB_STATUS_AVAIL; 
    730         primary_net = __local_enet_sc; 
    731     } else 
    732 #endif 
    733 #endif 
    734     for (index = 0; (t = net_devtab_entry(index)) != NULL; index++) { 
     694       if ((t = net_devtab_entry(default_index)) != NULL && t->init(t)) { 
     695               t->status = CYG_NETDEVTAB_STATUS_AVAIL; 
     696               primary_net = __local_enet_sc; 
     697       } else 
     698#endif 
     699#endif 
     700               for (index = 0; (t = net_devtab_entry(index)) != NULL; index++) { 
    735701#ifdef CYGSEM_REDBOOT_NETWORK_INIT_ONE_DEVICE 
    736         if (index == default_index) 
    737             continue; 
    738 #endif 
    739         if (t->init(t)) { 
    740             t->status = CYG_NETDEVTAB_STATUS_AVAIL; 
    741             if (primary_net == (struct eth_drv_sc *)0) { 
    742                 primary_net = __local_enet_sc; 
    743            
     702                       if (index == default_index) 
     703                               continue; 
     704#endif 
     705                       if (t->init(t)) { 
     706                               t->status = CYG_NETDEVTAB_STATUS_AVAIL; 
     707                               if (primary_net == (struct eth_drv_sc *)0) { 
     708                                       primary_net = __local_enet_sc; 
     709                               
    744710#if defined(CYGHWR_NET_DRIVERS) && (CYGHWR_NET_DRIVERS > 1) 
    745             if (index == default_index) { 
    746                 primary_net = __local_enet_sc; 
    747            
    748 #endif 
    749        
    750    
    751     __local_enet_sc = primary_net; 
    752  
    753     if (!__local_enet_sc) { 
    754         diag_printf("No network interfaces found\n"); 
    755         return; 
    756     }     
    757     // Initialize the network [if present] 
     711                               if (index == default_index) { 
     712                                       primary_net = __local_enet_sc; 
     713                               
     714#endif 
     715                       
     716               
     717       __local_enet_sc = primary_net; 
     718 
     719       if (!__local_enet_sc) { 
     720               diag_printf("No network interfaces found\n"); 
     721               return; 
     722        } 
     723       // Initialize the network [if present] 
    758724/*    if (use_bootp) { 
    759725        if (__bootp_find_local_ip(&my_bootp_info) == 0) { 
     
    772738    } else { 
    773739*/ 
    774         if (__local_ip_addr[0] == 0 && __local_ip_addr[1] == 0 && 
    775                 __local_ip_addr[2] == 0 && __local_ip_addr[3] == 0) { 
    776                 __local_ip_addr[0]=192; 
    777                 __local_ip_addr[1]=168; 
    778                 __local_ip_addr[2]=1; 
    779                 __local_ip_addr[3]=1; 
     740       if (__local_ip_addr[0] == 0 && __local_ip_addr[1] == 0 && 
     741           __local_ip_addr[2] == 0 && __local_ip_addr[3] == 0) { 
     742               __local_ip_addr[0] = 192; 
     743               __local_ip_addr[1] = 168; 
     744               __local_ip_addr[2] = 1; 
     745               __local_ip_addr[3] = 1; 
    780746//                __local_ip_mask[0]=255; 
    781747//                __local_ip_mask[1]=255; 
    782748//                __local_ip_mask[2]=0; 
    783749//                __local_ip_mask[3]=0; 
    784         } 
    785                  
    786         { 
    787  
    788  
    789         enet_addr_t enet_addr; 
    790         have_net = true;  // Assume values in FLASH were OK 
    791         // Tell the world that we are using this fixed IP address 
    792         if (__arp_request((ip_addr_t *)__local_ip_addr, &enet_addr, 1) >= 0) { 
    793             diag_printf("Warning: IP address %s in use\n", inet_ntoa((in_addr_t *)&__local_ip_addr)); 
    794         } 
    795         } 
     750        } 
     751 
     752        { 
     753 
     754                enet_addr_t enet_addr; 
     755                have_net = true;        // Assume values in FLASH were OK 
     756                // Tell the world that we are using this fixed IP address 
     757                if (__arp_request((ip_addr_t *) __local_ip_addr, &enet_addr, 1) 
     758                    >= 0) { 
     759                        diag_printf("Warning: IP address %s in use\n", 
     760                                    inet_ntoa((in_addr_t *) & __local_ip_addr)); 
     761                } 
     762        } 
    796763//    } 
    797     if (have_net) { 
    798         show_eth_info(); 
     764       if (have_net) { 
     765               show_eth_info(); 
    799766#ifdef CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR 
    800         diag_sprintf(ip_addr, "%d.%d.%d.%d",  
    801                      CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR); 
    802         inet_aton(ip_addr, &my_bootp_info.bp_siaddr); 
     767                diag_sprintf(ip_addr, "%d.%d.%d.%d", 
     768                            CYGDAT_REDBOOT_DEFAULT_BOOTP_SERVER_IP_ADDR); 
     769               inet_aton(ip_addr, &my_bootp_info.bp_siaddr); 
    803770#endif 
    804771#ifdef CYGSEM_REDBOOT_FLASH_CONFIG 
    805         flash_get_IP("bootp_server_ip", (ip_addr_t *)&my_bootp_info.bp_siaddr); 
     772                flash_get_IP("bootp_server_ip", 
     773                             (ip_addr_t *) & my_bootp_info.bp_siaddr); 
    806774#endif 
    807775#ifdef CYGPKG_REDBOOT_NETWORKING_DNS 
    808         redboot_dns_res_init(); 
    809 #endif 
    810         show_addrs(); 
    811         net_io_init(); 
    812     } 
    813 
    814  
    815 static char usage[] = "[-l <local_ip_address>[/<mask_len>]] [-h <server_address>]"; 
     776                redboot_dns_res_init(); 
     777#endif 
     778                show_addrs(); 
     779                net_io_init(); 
     780        } 
     781
     782 
     783static char usage[] = 
     784    "[-l <local_ip_address>[/<mask_len>]] [-h <server_address>]"; 
    816785 
    817786// Exported CLI function 
    818787static void do_ip_addr(int argc, char *argv[]); 
    819 RedBoot_cmd("ip_address",  
    820             "Set/change IP addresses",  
    821             usage, 
    822             do_ip_addr 
    823     ); 
    824  
    825 void  
    826 do_ip_addr(int argc, char *argv[]) 
    827 
    828     struct option_info opts[3]; 
    829     char *ip_addr, *host_addr; 
    830     bool ip_addr_set, host_addr_set; 
    831     struct sockaddr_in host; 
     788RedBoot_cmd("ip_address", "Set/change IP addresses", usage, do_ip_addr); 
     789 
     790void do_ip_addr(int argc, char *argv[]) 
     791
     792        struct option_info opts[3]; 
     793        char *ip_addr, *host_addr; 
     794        bool ip_addr_set, host_addr_set; 
     795        struct sockaddr_in host; 
    832796#ifdef CYGPKG_REDBOOT_NETWORKING_DNS 
    833     char *dns_addr; 
    834     bool dns_addr_set; 
    835 #endif 
    836     int num_opts; 
    837  
    838     init_opts(&opts[0], 'l', true, OPTION_ARG_TYPE_STR,  
    839               (void *)&ip_addr, (bool *)&ip_addr_set, "local IP address"); 
    840     init_opts(&opts[1], 'h', true, OPTION_ARG_TYPE_STR,  
    841               (void *)&host_addr, (bool *)&host_addr_set, "default server address"); 
    842     num_opts = 2; 
     797        char *dns_addr; 
     798        bool dns_addr_set; 
     799#endif 
     800        int num_opts; 
     801 
     802        init_opts(&opts[0], 'l', true, OPTION_ARG_TYPE_STR, 
     803                  (void *)&ip_addr, (bool *) & ip_addr_set, "local IP address"); 
     804        init_opts(&opts[1], 'h', true, OPTION_ARG_TYPE_STR, 
     805                  (void *)&host_addr, (bool *) & host_addr_set, 
     806                  "default server address"); 
     807        num_opts = 2; 
    843808#ifdef CYGPKG_REDBOOT_NETWORKING_DNS 
    844     init_opts(&opts[2], 'd', true, OPTION_ARG_TYPE_STR,  
    845               (void *)&dns_addr, (bool *)&dns_addr_set, "DNS server address"); 
    846     num_opts++; 
    847 #endif 
    848  
    849     CYG_ASSERT(num_opts <= NUM_ELEMS(opts), "Too many options"); 
    850  
    851     if (!scan_opts(argc, argv, 1, opts, num_opts, 0, 0, "")) { 
    852         return; 
    853     } 
    854     if (ip_addr_set) { 
     809        init_opts(&opts[2], 'd', true, OPTION_ARG_TYPE_STR, 
     810                  (void *)&dns_addr, (bool *) & dns_addr_set, 
     811                  "DNS server address"); 
     812        num_opts++; 
     813#endif 
     814 
     815        CYG_ASSERT(num_opts <= NUM_ELEMS(opts), "Too many options"); 
     816 
     817        if (!scan_opts(argc, argv, 1, opts, num_opts, 0, 0, "")) { 
     818                return; 
     819        } 
     820        if (ip_addr_set) { 
    855821#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY 
    856         char *slash_pos; 
    857         /* see if the (optional) mask length was given */ 
    858         if( (slash_pos = strchr(ip_addr, '/')) ) { 
    859             int mask_len; 
    860             unsigned long mask; 
    861             *slash_pos = '\0'; 
    862             slash_pos++; 
    863             if( !parse_num(slash_pos, (unsigned long *)&mask_len, 0, 0) ||   
    864                 mask_len <= 0 || mask_len > 32 ) { 
    865                 diag_printf("Invalid mask length: %s\n", slash_pos); 
    866                 return; 
    867             } 
    868             mask = htonl((0xffffffff << (32-mask_len))&0xffffffff); 
    869             memcpy(&__local_ip_mask, &mask, 4); 
    870         } 
    871 #endif         
    872         if (!_gethostbyname(ip_addr, (in_addr_t *)&host)) { 
    873             diag_printf("Invalid local IP address: %s\n", ip_addr); 
    874             return; 
    875         } 
    876         // Of course, each address goes in its own place :-) 
    877         memcpy(&__local_ip_addr, &host.sin_addr, sizeof(host.sin_addr)); 
    878     } 
    879     if (host_addr_set) { 
    880         if (!_gethostbyname(host_addr, (in_addr_t *)&host)) { 
    881             diag_printf("Invalid server address: %s\n", host_addr); 
    882             return; 
    883         } 
    884         my_bootp_info.bp_siaddr = host.sin_addr; 
    885     } 
     822                char *slash_pos; 
     823                /* see if the (optional) mask length was given */ 
     824                if ((slash_pos = strchr(ip_addr, '/'))) { 
     825                        int mask_len; 
     826                        unsigned long mask; 
     827                        *slash_pos = '\0'; 
     828                        slash_pos++; 
     829                        if (!parse_num 
     830                            (slash_pos, (unsigned long *)&mask_len, 0, 0) 
     831                            || mask_len <= 0 || mask_len > 32) { 
     832                                diag_printf("Invalid mask length: %s\n", 
     833                                            slash_pos); 
     834                                return; 
     835                        } 
     836                        mask = 
     837                            htonl((0xffffffff << (32 - mask_len)) & 0xffffffff); 
     838                        memcpy(&__local_ip_mask, &mask, 4); 
     839                } 
     840#endif 
     841                if (!_gethostbyname(ip_addr, (in_addr_t *) & host)) { 
     842                        diag_printf("Invalid local IP address: %s\n", ip_addr); 
     843                        return; 
     844                } 
     845                // Of course, each address goes in its own place :-) 
     846                memcpy(&__local_ip_addr, &host.sin_addr, sizeof(host.sin_addr)); 
     847        } 
     848        if (host_addr_set) { 
     849                if (!_gethostbyname(host_addr, (in_addr_t *) & host)) { 
     850                        diag_printf("Invalid server address: %s\n", host_addr); 
     851                        return; 
     852                } 
     853                my_bootp_info.bp_siaddr = host.sin_addr; 
     854        } 
    886855#ifdef CYGPKG_REDBOOT_NETWORKING_DNS 
    887     if (dns_addr_set) { 
    888         set_dns(dns_addr); 
    889    
    890 #endif 
    891     show_addrs(); 
    892     if (!have_net) { 
    893         have_net = true; 
    894         net_io_init(); 
    895    
     856       if (dns_addr_set) { 
     857               set_dns(dns_addr); 
     858       
     859#endif 
     860       show_addrs(); 
     861       if (!have_net) { 
     862               have_net = true; 
     863               net_io_init(); 
     864       
    896865} 
    897866