Annotation of qemu/roms/SLOF/clients/net-snk/app/netapps/netflash.c, revision 1.1.1.2

1.1       root        1: /******************************************************************************
                      2:  * Copyright (c) 2004, 2008 IBM Corporation
                      3:  * All rights reserved.
                      4:  * This program and the accompanying materials
                      5:  * are made available under the terms of the BSD License
                      6:  * which accompanies this distribution, and is available at
                      7:  * http://www.opensource.org/licenses/bsd-license.php
                      8:  *
                      9:  * Contributors:
                     10:  *     IBM Corporation - initial implementation
                     11:  *****************************************************************************/
                     12: 
                     13: #include <netlib/tftp.h>
                     14: #include <netlib/dhcp.h>
                     15: #include <netlib/ethernet.h>
                     16: #include <netlib/ipv4.h>
                     17: #include <rtas.h>
                     18: #include <stdio.h>
                     19: #include <string.h>
                     20: #include <stdlib.h>
                     21: #include <sys/socket.h>
1.1.1.2 ! root       22: #include "netapps.h"
1.1       root       23: 
                     24: int netflash(int argc, char * argv[])
                     25: {
                     26:        char buf[256];
                     27:        int rc;
                     28:        int manage_mode = 0;
                     29:        static int len = 0x800000; //max flash size
                     30:        char * buffer = NULL;
                     31:        short arp_failed = 0;
                     32:        filename_ip_t fn_ip;
                     33:        int fd_device;
                     34:        tftp_err_t tftp_err;
                     35:        char * ptr;
                     36:        uint8_t own_mac[6];
                     37: 
                     38:        printf("\n Flasher 1.4 \n");
                     39:        memset(&fn_ip, 0, sizeof(filename_ip_t));
                     40: 
                     41:        if (argc == 3 && argv[2][0] == '-' && argv[2][1] == 'c' && argv[2][2] == 0)
                     42:                manage_mode = 1;
                     43:        else if (argc == 3 && 
                     44:                argv[2][0] == '-' && argv[2][1] == 'r' && argv[2][2] == 0)
                     45:                manage_mode = 1;
                     46:        else if (argc == 4 &&
                     47:                argv[2][0] == '-' && argv[2][1] == 'f' && argv[2][2] == 0)
                     48:        {
                     49:                manage_mode = 0;
                     50:                buffer = (char *)strtol(argv[1],0,16);
                     51:                if ((long)buffer == -1) {
                     52:                        printf("   Bad buffer address. Exiting...\n");
                     53:                        return -1;
                     54:                }
                     55:        }
                     56:        else
                     57:        {
                     58:                printf("   Usage: netflash [options] [<filename>]\n");
                     59:                printf("   Options:\n");
                     60:                printf("            -f     <filename> flash temporary image\n");
                     61:                printf("            -c     commit temporary image\n");
                     62:                printf("            -r     reject temporary image\n");
                     63:                printf("   Bad arguments. Exiting...\n\n");
                     64:                return -1;
                     65:        }
                     66: 
                     67:        if (manage_mode == 1) {
                     68:                if (argv[2][1] == 99)
                     69:                        return rtas_ibm_manage_flash(1);
                     70:                else
                     71:                        return rtas_ibm_manage_flash(0);
                     72:        }
                     73: 
                     74:        /* Get mac_addr from device */
                     75:        printf("  Reading MAC address from device: ");
                     76:        fd_device = socket(0, 0, 0, (char *) own_mac);
                     77:        if (fd_device == -1) {
                     78:                printf("\nE3000: Could not read MAC address\n");
                     79:                return -100;
                     80:        }
                     81:        else if (fd_device == -2) {
                     82:                printf("\nE3006: Could not initialize network device\n");
                     83:                return -101;
                     84:        }
                     85: 
                     86:        printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                     87:               own_mac[0], own_mac[1], own_mac[2],
                     88:               own_mac[3], own_mac[4], own_mac[5]);
                     89: 
                     90:        // init ethernet layer
                     91:        set_mac_address(own_mac);
                     92: 
                     93:        // identify the BOOTP/DHCP server via broadcasts
                     94:        // don't do this, when using DHCP !!!
                     95:        //  fn_ip.server_ip = 0xFFFFFFFF;
                     96:        //  memset(fn_ip.server_mac, 0xff, 6);
                     97: 
                     98:        /* Get ip address for our mac address */
                     99:        printf("  Requesting IP address via DHCP: ");
                    100:        arp_failed = dhcp(0, &fn_ip, 30);
                    101: 
                    102:        if(arp_failed >= 0) {
                    103:                // reinit network stack
                    104:                set_ipv4_address(fn_ip.own_ip);
                    105:        }
                    106: 
                    107:        if (arp_failed == -1) {
                    108:                printf("\n  DHCP: Could not get ip address\n");
                    109:                return 1;
                    110:        }
                    111: 
                    112:        if (arp_failed == -2) {
                    113:                sprintf
                    114:                    (buf,"\n  ARP request to TFTP server (%d.%d.%d.%d) failed",
                    115:                     ((fn_ip.server_ip >> 24) & 0xFF), ((fn_ip.server_ip >> 16) & 0xFF),
                    116:                     ((fn_ip.server_ip >>  8) & 0xFF), ( fn_ip.server_ip        & 0xFF));
                    117:                return 1;
                    118:        }
                    119: 
                    120:        printf("%d.%d.%d.%d\n",
                    121:                ((fn_ip.own_ip >> 24) & 0xFF), ((fn_ip.own_ip >> 16) & 0xFF), 
                    122:                ((fn_ip.own_ip >>  8) & 0xFF), (fn_ip.own_ip & 0xFF));
                    123: 
                    124:        /* Load file via TFTP into buffer provided by OpenFirmware */
                    125: 
                    126:        for(ptr = argv[3]; *ptr != 0; ++ptr)
                    127:                if(*ptr == '\\')
                    128:                        *ptr = '/';
                    129: 
                    130:        printf("  Requesting file \"%s\" via TFTP\n",argv[3]);
                    131: 
                    132:        strcpy((char *) fn_ip.filename,argv[3]);
                    133: 
                    134:        rc = tftp(&fn_ip, (unsigned char*) buffer, len, 20, &tftp_err, 0, 512, 4);
                    135: 
                    136:        dhcp_send_release();
                    137: 
                    138:        if (rc > 0)
                    139:        {
                    140:                printf ("  TFTP: Received %s (%d KBytes)\n", fn_ip.filename, rc/1024);
                    141:                printf ("  Now flashing:\n");
                    142:                rc = rtas_ibm_update_flash_64((long long)buffer, rc);
                    143:                return rc;
                    144:        }
                    145:        else if (rc == -1)
                    146:        {
                    147:                printf ("  Tftp: Could not load file %s\n", fn_ip.filename);
                    148:                return 1;
                    149:        }
                    150:        else if (rc == -2)
                    151:        {
                    152:                printf ("  Tftp: Buffer to small for %s\n", fn_ip.filename);
                    153:                return 1;
                    154:        }
                    155:        else if (rc <= -10 && rc >= -15)
                    156:        {
                    157:                printf("\n  ICMP ERROR: Destination unreachable: ");
                    158:                switch(rc) {
                    159:                        case -ICMP_NET_UNREACHABLE-10:
                    160:                                printf("net unreachable");
                    161:                                break;
                    162:                        case -ICMP_HOST_UNREACHABLE-10:
                    163:                                printf("host unreachable");
                    164:                                break;
                    165:                        case -ICMP_PROTOCOL_UNREACHABLE-10:
                    166:                                printf("protocol unreachable");
                    167:                                break;
                    168:                        case -ICMP_PORT_UNREACHABLE-10:
                    169:                                printf("port unreachable");
                    170:                                break;
                    171:                        case -ICMP_FRAGMENTATION_NEEDED-10:
                    172:                                printf("fragmentation needed and DF set");
                    173:                                break;
                    174:                        case -ICMP_SOURCE_ROUTE_FAILED-10:
                    175:                                printf("source route failed");
                    176:                                break;
                    177:                        default:
                    178:                                printf(" UNKNOWN: this should not happen!");
                    179:                                break;
                    180:                }
                    181:                printf("\n");
                    182:                return 1;
                    183:        }
                    184:        else if(rc < 0)
                    185:                printf(" UNKNOWN: rc = %d!", rc);
                    186: 
                    187:        return 0;
                    188: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.