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

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

unix.superglobalmegacorp.com

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