aboutsummaryrefslogtreecommitdiff
path: root/clients/net-snk/app/netapps/netflash.c
blob: 6865ecb06abcec66bcc4e46703b23ab85d501ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <netlib/tftp.h>
#include <netlib/dhcp.h>
#include <netlib/ethernet.h>
#include <netlib/ipv4.h>
#include <rtas.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>

int netflash(int argc, char * argv[])
{
	char buf[256];
	int rc;
	int manage_mode = 0;
	static int len = 0x800000; //max flash size
	char * buffer = NULL;
	short arp_failed = 0;
	filename_ip_t fn_ip;
	int fd_device;
	tftp_err_t tftp_err;
	char * ptr;
	uint8_t own_mac[6];

	printf("\n Flasher 1.4 \n");
	memset(&fn_ip, 0, sizeof(filename_ip_t));

	if (argc == 3 && argv[2][0] == '-' && argv[2][1] == 'c' && argv[2][2] == 0)
		manage_mode = 1;
	else if (argc == 3 && 
		argv[2][0] == '-' && argv[2][1] == 'r' && argv[2][2] == 0)
		manage_mode = 1;
	else if (argc == 4 &&
		argv[2][0] == '-' && argv[2][1] == 'f' && argv[2][2] == 0)
	{
		manage_mode = 0;
		buffer = (char *)strtol(argv[1],0,16);
		if ((long)buffer == -1) {
			printf("   Bad buffer address. Exiting...\n");
			return -1;
		}
	}
	else
	{
		printf("   Usage: netflash [options] [<filename>]\n");
		printf("   Options:\n");
		printf("            -f     <filename> flash temporary image\n");
		printf("            -c     commit temporary image\n");
		printf("            -r     reject temporary image\n");
		printf("   Bad arguments. Exiting...\n\n");
		return -1;
	}

	if (manage_mode == 1) {
		if (argv[2][1] == 99)
			return rtas_ibm_manage_flash(1);
		else
			return rtas_ibm_manage_flash(0);
	}

	/* Get mac_addr from device */
	printf("  Reading MAC address from device: ");
	fd_device = socket(0, 0, 0, (char *) own_mac);
	if (fd_device == -1) {
		printf("\nE3000: Could not read MAC address\n");
		return -100;
	}
	else if (fd_device == -2) {
		printf("\nE3006: Could not initialize network device\n");
		return -101;
	}

	printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
	       own_mac[0], own_mac[1], own_mac[2],
	       own_mac[3], own_mac[4], own_mac[5]);

	// init ethernet layer
	set_mac_address(own_mac);

	// identify the BOOTP/DHCP server via broadcasts
	// don't do this, when using DHCP !!!
	//  fn_ip.server_ip = 0xFFFFFFFF;
	//  memset(fn_ip.server_mac, 0xff, 6);

	/* Get ip address for our mac address */
	printf("  Requesting IP address via DHCP: ");
	arp_failed = dhcp(0, &fn_ip, 30);

	if(arp_failed >= 0) {
		// reinit network stack
		set_ipv4_address(fn_ip.own_ip);
	}

	if (arp_failed == -1) {
		printf("\n  DHCP: Could not get ip address\n");
		return 1;
	}

	if (arp_failed == -2) {
		sprintf
		    (buf,"\n  ARP request to TFTP server (%d.%d.%d.%d) failed",
		     ((fn_ip.server_ip >> 24) & 0xFF), ((fn_ip.server_ip >> 16) & 0xFF),
		     ((fn_ip.server_ip >>  8) & 0xFF), ( fn_ip.server_ip        & 0xFF));
		return 1;
	}

	printf("%d.%d.%d.%d\n",
		((fn_ip.own_ip >> 24) & 0xFF), ((fn_ip.own_ip >> 16) & 0xFF), 
		((fn_ip.own_ip >>  8) & 0xFF), (fn_ip.own_ip & 0xFF));

	/* Load file via TFTP into buffer provided by OpenFirmware */

	for(ptr = argv[3]; *ptr != 0; ++ptr)
		if(*ptr == '\\')
			*ptr = '/';

	printf("  Requesting file \"%s\" via TFTP\n",argv[3]);

	strcpy((char *) fn_ip.filename,argv[3]);

	rc = tftp(&fn_ip, (unsigned char*) buffer, len, 20, &tftp_err, 0, 512, 4);

	dhcp_send_release();

	if (rc > 0)
	{
		printf ("  TFTP: Received %s (%d KBytes)\n", fn_ip.filename, rc/1024);
		printf ("  Now flashing:\n");
		rc = rtas_ibm_update_flash_64((long long)buffer, rc);
		return rc;
	}
	else if (rc == -1)
	{
		printf ("  Tftp: Could not load file %s\n", fn_ip.filename);
		return 1;
	}
	else if (rc == -2)
	{
		printf ("  Tftp: Buffer to small for %s\n", fn_ip.filename);
		return 1;
	}
	else if (rc <= -10 && rc >= -15)
	{
		printf("\n  ICMP ERROR: Destination unreachable: ");
		switch(rc) {
			case -ICMP_NET_UNREACHABLE-10:
				printf("net unreachable");
				break;
			case -ICMP_HOST_UNREACHABLE-10:
				printf("host unreachable");
				break;
			case -ICMP_PROTOCOL_UNREACHABLE-10:
				printf("protocol unreachable");
				break;
			case -ICMP_PORT_UNREACHABLE-10:
				printf("port unreachable");
				break;
			case -ICMP_FRAGMENTATION_NEEDED-10:
				printf("fragmentation needed and DF set");
				break;
			case -ICMP_SOURCE_ROUTE_FAILED-10:
				printf("source route failed");
				break;
			default:
				printf(" UNKNOWN: this should not happen!");
				break;
		}
		printf("\n");
		return 1;
	}
	else if(rc < 0)
		printf(" UNKNOWN: rc = %d!", rc);

	return 0;
}