1 #include <pkgconf/system.h>
3 #if !defined(CYGPKG_NET_FTPCLIENT)
4 #error "Configuration is missing: CYGPKG_NET_FTPCLIENT"
7 #if !defined(CYGPKG_COMPRESS_ZLIB)
8 #error "Configuration is missing: CYGPKG_COMPRESS_ZLIB"
11 #include <pkgconf/compress_zlib.h>
12 #if !defined(CYGSEM_COMPRESS_ZLIB_DEFLATE_MAKES_GZIP)
13 #error "Configuration is missing: CYGSEM_COMPRESS_ZLIB_DEFLATE_MAKES_GZIP"
17 #if !defined(CYGPKG_CRC)
18 #error "Configuration is missing: CYGPKG_CRC"
25 #include <ftpclient.h>
26 #include <cyg/compress/zlib.h>
28 #include <pkgconf/net.h>
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <sys/errno.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h>
38 #include <net/route.h>
40 #include <cyg/infra/diag.h>
41 #include <cyg/kernel/kapi.h>
45 #include <arpa/inet.h>
53 #ifdef CYGHWR_NET_DRIVER_ETH0
54 static struct bootp eth0_bootp_data;
55 static const char *eth0_name = "eth0";
57 #ifdef CYGHWR_NET_DRIVER_ETH1
58 static struct bootp eth1_bootp_data;
59 static const char *eth1_name = "eth1";
63 core_info_t test_core_info = {
64 .fence0 = KCORE_FENCE0_PAT,
65 .fence1 = KCORE_FENCE1_PAT,
66 .fence2 = KCORE_FENCE2_PAT,
67 .fence3 = KCORE_FENCE3_PAT,
68 .crash = KCORE_CRASH_PANIC,
69 .panicstr = "This is a panic string",
72 .physical_banks[0] = { 0x700000, 0x40000 },
73 .physical_banks[1] = { 0x740000, 0x40000 },
82 .dbatl[0] = 0xDEAD0000, .dbatu[0] = 0xDEAD1000,
83 .dbatl[1] = 0xDEAD0001, .dbatu[1] = 0xDEAD1001,
84 .dbatl[2] = 0xDEAD0002, .dbatu[2] = 0xDEAD1002,
85 .dbatl[3] = 0xDEAD0003, .dbatu[3] = 0xDEAD1003,
86 .dbatl[4] = 0xDEAD0004, .dbatu[4] = 0xDEAD1004,
87 .dbatl[5] = 0xDEAD0005, .dbatu[5] = 0xDEAD1005,
88 .dbatl[6] = 0xDEAD0006, .dbatu[6] = 0xDEAD1006,
89 .dbatl[7] = 0xDEAD2007, .dbatu[7] = 0xDEAD3007,
90 .ibatl[0] = 0xDEAD2000, .ibatu[0] = 0xDEAD3000,
91 .ibatl[1] = 0xDEAD2001, .ibatu[1] = 0xDEAD3001,
92 .ibatl[2] = 0xDEAD2002, .ibatu[2] = 0xDEAD3002,
93 .ibatl[3] = 0xDEAD2003, .ibatu[3] = 0xDEAD3003,
94 .ibatl[4] = 0xDEAD2004, .ibatu[4] = 0xDEAD3004,
95 .ibatl[5] = 0xDEAD2005, .ibatu[5] = 0xDEAD3005,
96 .ibatl[6] = 0xDEAD2006, .ibatu[6] = 0xDEAD3006,
97 .ibatl[7] = 0xDEAD2007, .ibatu[7] = 0xDEAD3007,
101 #define FTP_BUFSIZE 4096
104 char buf[FTP_BUFSIZE], *bufp;
108 int (*get_data)(char **ptr, void *param);
112 int _ftp_write_gz(char *buf, int len, void *priv)
114 struct ftp_data *dp = (struct ftp_data *)priv;
120 // Ugly hack - zlib needs a value for 'avail_in' at initialization
121 // time, even though we don't have anything ready yet
122 dp->stream.avail_in = 0;
125 while ((dp->len == 0) && (dp->src_len > 0)) {
126 // Has input buffer been exhausted?
127 if (dp->stream.avail_in == 0) {
128 dp->src_len = (*dp->get_data)(&src, dp->get_param);
129 dp->stream.next_in = src;
130 dp->stream.avail_in = dp->src_len;
132 // See if more data is available to be compressed
133 dp->stream.avail_out = FTP_BUFSIZE;
134 dp->stream.next_out = dp->buf;
135 err = deflate(&dp->stream, dp->src_len ? Z_NO_FLUSH : Z_FINISH);
136 dp->len = FTP_BUFSIZE - dp->stream.avail_out;
139 // Move up to 'len' bytes from internal buffer
144 memcpy(buf, dp->bufp, res);
150 struct _simple_get_data_param {
156 simple_get_data(char **ptr, void *param)
158 struct _simple_get_data_param *gp = (struct _simple_get_data_param *)param;
170 // Parse (scan/copy) network addresses. If the IP address contains
171 // a '/', then use that to determine the netmask and ignore the
175 parse_eth_addrs(char *desired_ip, char *desired_netmask,
176 char *ip, char *netmask, char *broadcast)
179 bool found_netmask = false;
180 int mask_size, mask, ip_addr;
182 // diag_printf("parse - ip: '%s', mask: '%s'\n", desired_ip, desired_netmask);
184 src = desired_ip; dst = ip;
187 // Do something with netmask
189 found_netmask = true;
192 mask_size = (mask_size * 10) + (*src++ - '0');
194 mask = 0xFFFFFFFF << (32-mask_size);
200 if (!found_netmask) {
201 if (!inet_aton(desired_netmask, (struct in_addr *)&mask)) {
202 diag_printf("Error: Invalid netmask '%s'\n", desired_netmask);
203 return false; // Illegal netmask
206 strcpy(netmask, inet_ntoa(*(struct in_addr *)&mask));
207 if (!inet_aton(ip, (struct in_addr *)&ip_addr)) {
208 diag_printf("Error: Invalid IP address '%s'\n", ip);
211 ip_addr = (ip_addr & mask) | ~mask;
212 strcpy(broadcast, inet_ntoa(*(struct in_addr *)&ip_addr));
213 // diag_printf("ip: '%s', netmask: '%s', broadcast: '%s'\n", ip, netmask, broadcast);
217 struct core_dump_param {
222 kcore_seg_t cpu_hdr, ram_hdr;
223 cpu_kcore_hdr_t cpu_info;
224 phys_ram_seg_t cpu_phys_segs[8];
227 #define ALIGN(v,n) ((((v)+((n)-1))/(n))*(n))
230 dump_core(char **ptr, void *param)
232 struct core_dump_param *dp = (struct core_dump_param *)param;
237 // File header (kcore_hdr_t)
239 *ptr = (char *)&dp->hdr;
240 return ALIGN(sizeof(dp->hdr), 8);
242 // CPU header (kcore_seg_t)
244 *ptr = (char *)&dp->cpu_hdr;
245 return ALIGN(sizeof(dp->cpu_hdr), 8);
247 // CPU info (cpu_kcore_hdr_t)
250 *ptr = (char *)&dp->cpu_info;
251 return sizeof(dp->cpu_info);
253 // Physical RAM segment descriptions (phys_ram_seg_t)
254 *ptr = (char *)&dp->cpu_phys_segs[dp->segno];
255 if (++dp->segno == dp->cp->nsegs) {
258 return sizeof(dp->cpu_phys_segs[0]);
260 // RAM header (kcore_seg_t)
262 *ptr = (char *)&dp->ram_hdr;
264 return ALIGN(sizeof(dp->ram_hdr), 8);
267 *ptr = (char *)dp->cpu_phys_segs[dp->segno].start;
268 len = dp->cpu_phys_segs[dp->segno].size;
269 if (++dp->segno == dp->cp->nsegs) {
277 diag_printf("Bad core dump phase: %d\n", dp->phase);
283 expand_file_name(char *file_name, char *path, char *ip)
285 char *fn = nvram_get("dump_name");
286 char *dp = file_name;
289 if (!fn) fn = "XPathOS.%i.core.gz";
290 if (!path) path = "/";
300 fn += 2; // Skip '%i'
312 do_core_dump(core_info_t *cp, char *ip)
314 struct core_dump_param cd;
315 // Caution - this can't be on the stack unless you increase the stack size!
316 static struct ftp_data dp;
318 unsigned long total_ram_size;
320 char *dump_ip, *dump_user, *dump_pass;
322 dump_ip = nvram_get("dump_server");
324 diag_printf("Can't dump: server IP missing in NVRAM\n");
327 dump_user = nvram_get("dump_user");
329 diag_printf("Can't dump: user name missing in NVRAM\n");
332 dump_pass = nvram_get("dump_pass");
334 diag_printf("Can't dump: password missing in NVRAM\n");
337 expand_file_name(file_name, nvram_get("dump_path"), ip);
339 // Fill in core dump structures
340 cd.hdr.c_midmag = KCORE_HDR_MAGIC;
341 cd.hdr.c_hdrsize = ALIGN(sizeof(cd.hdr), 8);
342 cd.hdr.c_seghdrsize = ALIGN(sizeof(cd.cpu_hdr), 8);
343 cd.hdr.c_nseg = cp->nsegs;
345 cd.cpu_hdr.c_midmag = KCORE_CPU_SEGMENT;
346 cd.cpu_hdr.c_size = sizeof(kcore_seg_t)+sizeof(cpu_kcore_hdr_t)+(cp->nsegs*sizeof(phys_ram_seg_t));
347 // Copy info from core-dump area
348 cd.cpu_info.pvr = cp->pvr;
349 cd.cpu_info.sdr1 = cp->sdr1;
350 for (i = 0; i < 8; i++) {
351 cd.cpu_info.sr[i] = cp->sr[i];
352 cd.cpu_info.dbatl[i] = cp->dbatl[i];
353 cd.cpu_info.dbatu[i] = cp->dbatu[i];
354 cd.cpu_info.ibatl[i] = cp->ibatl[i];
355 cd.cpu_info.ibatu[i] = cp->ibatu[i];
357 for (i = 0; i < 8; i++) {
358 cd.cpu_phys_segs[i].start = cp->physical_banks[i].base;
359 cd.cpu_phys_segs[i].size = cp->physical_banks[i].length;
363 cd.ram_hdr.c_midmag = KCORE_DATA_SEGMENT;
364 cd.ram_hdr.c_size = sizeof(kcore_seg_t)+total_ram_size;
370 dp.get_data = dump_core;
374 dp.stream.next_in = 0;
375 dp.stream.avail_in = 0x1000;
376 dp.stream.next_out = dp.buf;
377 dp.stream.avail_out = FTP_BUFSIZE;
378 dp.stream.zalloc = (alloc_func)0;
379 dp.stream.zfree = (free_func)0;
380 dp.stream.opaque = (voidpf)0;
382 err = deflateInit(&dp.stream, Z_DEFAULT_COMPRESSION);
383 diag_printf("err = %d\n", err);
385 diag_printf("... dumping core as %s\n", file_name);
387 wlen = ftp_put_var(dump_ip, dump_user, dump_pass, file_name,
388 _ftp_write_gz, &dp, ftpclient_printf);
389 diag_printf("res = %d\n", wlen);
390 deflateEnd(&dp.stream);
394 scan_args(char *cmdline, int *argc, char **argv, char **alt_cmdline)
399 argv[indx++] = "secondary-load";
400 *alt_cmdline = (char *)0;
402 // Skip over any white space
403 while (*cp && (*cp == ' ')) cp++;
405 // Check for -- break
406 if ((cp[0] == '-') && (cp[1] == '-')) {
407 // End of normal list
409 while (*cp && (*cp == ' ')) cp++;
428 loader_main(char *cmdline)
433 char *argv[MAX_ARGV], *alt_cmdline;
434 bool force_dump = false;
435 bool quick_boot = false;
436 bool verbose = false;
437 char *boot_info = (char *)NULL;
438 // Caution - this can't be on the stack unless you increase the stack size!
439 static struct ftp_data dp;
440 struct _simple_get_data_param gp;
441 #ifdef CYGHWR_NET_DRIVER_ETH0
442 char eth0_ip[32], eth0_netmask[32], eth0_broadcast[32];
444 #ifdef CYGHWR_NET_DRIVER_ETH1
445 char eth1_ip[32], eth1_netmask[32], eth1_broadcast[32];
449 scan_args(cmdline, &argc, argv, &alt_cmdline);
451 i = getopt(argc, argv, "fqv");
464 diag_printf("Invalid return from getopt: %d\n", i);
468 boot_info = argv[optind];
471 if (!(eth_gateway = nvram_get("net_gateway"))) {
472 diag_printf("Error: need network gateway\n");
475 #ifdef CYGHWR_NET_DRIVER_ETH0
476 if (!parse_eth_addrs(nvram_get("net0_ip"), nvram_get("net0_mask"),
477 eth0_ip, eth0_netmask, eth0_broadcast)) {
478 diag_printf("Can't set up ethernet 'net0' - I give up!\n");
481 build_bootp_record(ð0_bootp_data,
488 show_bootp(eth0_name, ð0_bootp_data);
489 if (!init_net(eth0_name, ð0_bootp_data)) {
490 diag_printf("Network initialization failed for eth0\n");
493 #ifdef CYGHWR_NET_DRIVER_ETH1
494 if (!parse_eth_addrs(nvram_get("net1_ip"), nvram_get("net1_mask"),
495 eth1_ip, eth1_netmask, eth1_broadcast)) {
496 diag_printf("Can't set up ethernet 'net1' - I give up!\n");
499 build_bootp_record(ð1_bootp_data,
506 show_bootp(eth1_name, ð1_bootp_data);
507 if (!init_net(eth1_name, ð1_bootp_data)) {
508 diag_printf("Network initialization failed for eth1\n");
514 // See if the 'core dump' page contains an indication that a
515 // dump should be taken
518 do_core_dump(&test_core_info, eth0_ip);
523 len = ftp_get("192.168.1.125", "gthomas", "Zorkle!!", "TEST.dat",
524 (char *)0x200000, 0x100000, ftpclient_printf);
525 diag_printf("res = %d\n", len);
526 if (len <= 0) exit(0);
527 wlen = ftp_put("192.168.1.125", "gthomas", "Zorkle!!", "TEST2.dat",
528 (char *)0x200000, len, ftpclient_printf);
529 diag_printf("res = %d\n", wlen);
531 // Set up to compress data
532 gp.ptr = (char *)0x200000;
535 dp.get_data = simple_get_data;
539 dp.stream.next_in = 0;
540 dp.stream.avail_in = 0x1000;
541 dp.stream.next_out = dp.buf;
542 dp.stream.avail_out = FTP_BUFSIZE;
543 dp.stream.zalloc = (alloc_func)0;
544 dp.stream.zfree = (free_func)0;
545 dp.stream.opaque = (voidpf)0;
547 err = deflateInit(&dp.stream, Z_DEFAULT_COMPRESSION);
548 diag_printf("err = %d\n", err);
549 wlen = ftp_put_var("192.168.1.125", "gthomas", "Zorkle!!", "TEST3.dat.gz",
550 _ftp_write_gz, &dp, ftpclient_printf);
551 diag_printf("res = %d\n", wlen);
552 deflateEnd(&dp.stream);
560 return (loader_main("-f -t test joe -- junk"));