xref: /csrg-svn/old/enpload/enpload.c (revision 30988)
1*30988Ssam #ifndef lint
2*30988Ssam static char sccsid[] = "@(#)enpload.c	5.1 (Berkeley from CCI) 04/29/87";
3*30988Ssam #endif
4*30988Ssam 
5*30988Ssam /*
6*30988Ssam  * CMC Ethernet ``Microcode'' Loader.
7*30988Ssam  */
8*30988Ssam #include <stdio.h>
9*30988Ssam #include <a.out.h>
10*30988Ssam 
11*30988Ssam #include <sys/types.h>
12*30988Ssam #include <sys/file.h>
13*30988Ssam #include <sys/ioctl.h>
14*30988Ssam #include <tahoeif/if_enpreg.h>
15*30988Ssam 
16*30988Ssam char	*dev;
17*30988Ssam 
18*30988Ssam main(argc, argv)
19*30988Ssam 	int argc;
20*30988Ssam 	char *argv[];
21*30988Ssam {
22*30988Ssam 	int enp = -1, fd, first = 1, nostart = 0;
23*30988Ssam 
24*30988Ssam 	argc--, argv++;
25*30988Ssam 	if (argc > 0) {
26*30988Ssam 		enp = open(dev = argv[0], O_RDWR);
27*30988Ssam 		if (enp < 0) {
28*30988Ssam 			fprintf(stderr, "enpload: ");
29*30988Ssam 			perror(dev);
30*30988Ssam 			exit(-1);
31*30988Ssam 		}
32*30988Ssam 		argc--, argv++;
33*30988Ssam 	}
34*30988Ssam 	for (; argc > 0; argc--, argv++) {
35*30988Ssam 		if (strcmp(argv[0], "-s") == 0 || strcmp(argv[0], "-S") == 0) {
36*30988Ssam 			nostart++;
37*30988Ssam 			continue;
38*30988Ssam 		}
39*30988Ssam 		if (first) {
40*30988Ssam 			/*
41*30988Ssam 			 * Reset device before first file is loaded.
42*30988Ssam 			 */
43*30988Ssam 			if (ioctl(enp, ENPIORESET) < 0) {
44*30988Ssam 				fprintf(stderr, "enpload: %s: ", dev);
45*30988Ssam 				perror("ioctl (ENPIORESET)");
46*30988Ssam 				exit(-1);
47*30988Ssam 			}
48*30988Ssam 			first = !first;
49*30988Ssam 		}
50*30988Ssam 		if ((fd = open(argv[0], O_RDONLY)) < 0) {
51*30988Ssam 			fprintf(stderr, "enpload: "), perror(argv[0]);
52*30988Ssam 			exit(1);
53*30988Ssam 		}
54*30988Ssam 		enpload(enp, fd, argv[0]);
55*30988Ssam 		close(fd);
56*30988Ssam 	}
57*30988Ssam 	if (enp != -1 && !nostart && ioctl(enp, ENPIOGO) < 0) {
58*30988Ssam 		fprintf(stderr, "enpload: ");
59*30988Ssam 		perror("ioctl (ENPIOGO)");
60*30988Ssam 		exit(-1);
61*30988Ssam 	}
62*30988Ssam 	exit(0);
63*30988Ssam }
64*30988Ssam 
65*30988Ssam #define	RELO		0x03FFFF	/* relocation offset */
66*30988Ssam #define	ENPMSTART	0x0		/* start of memory */
67*30988Ssam #define	BSIZE		512		/* buffer size */
68*30988Ssam char	buff[BSIZE];
69*30988Ssam char	zbuf[BSIZE];
70*30988Ssam 
71*30988Ssam enpload(enp, fd, filename)
72*30988Ssam 	int enp, fd;
73*30988Ssam 	char *filename;
74*30988Ssam {
75*30988Ssam 	int cnt, size, lstart;
76*30988Ssam 	struct exec hdr;
77*30988Ssam 
78*30988Ssam 	if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) {
79*30988Ssam 		fprintf(stderr, "enpload: %s: Read short (header).\n",
80*30988Ssam 		   filename);
81*30988Ssam 		exit(1);
82*30988Ssam 	}
83*30988Ssam 	if (N_BADMAG(hdr)) {
84*30988Ssam 		fprintf(stderr, "enpload: %s: Bad magic number.\n", filename);
85*30988Ssam 		exit(1);
86*30988Ssam 	}
87*30988Ssam 	size = hdr.a_text + hdr.a_data;
88*30988Ssam 	lstart = (ENPMSTART + (hdr.a_entry & RELO)) - 0x1000;
89*30988Ssam 
90*30988Ssam 	printf("%s: Loading %s...", dev, filename);
91*30988Ssam 	(void) lseek(enp, lstart + size, L_SET);
92*30988Ssam 	while (hdr.a_bss >= BSIZE) {
93*30988Ssam 		if (write(enp, zbuf, BSIZE) != BSIZE) {
94*30988Ssam 			fprintf(stderr, "enpload: Bss write error.\n");
95*30988Ssam 			exit(-1);
96*30988Ssam 		}
97*30988Ssam 		hdr.a_bss -= BSIZE;
98*30988Ssam 	}
99*30988Ssam 	if (hdr.a_bss > 0 && write(enp, zbuf, hdr.a_bss) != hdr.a_bss) {
100*30988Ssam 		fprintf(stderr, "enpload: Bss write error.\n");
101*30988Ssam 		exit(-1);
102*30988Ssam 	}
103*30988Ssam 	(void) lseek(enp, lstart, L_SET);
104*30988Ssam 	while (size > BSIZE) {
105*30988Ssam 		cnt = read(fd, buff, BSIZE);
106*30988Ssam 		size -= cnt;
107*30988Ssam 		if (write(enp, buff, cnt) != cnt) {
108*30988Ssam 			fprintf(stderr, "enpload: Write error.\n");
109*30988Ssam 			exit(-1);
110*30988Ssam 		}
111*30988Ssam 	}
112*30988Ssam 	if (size > 0) {
113*30988Ssam 		cnt = read(fd, buff, size);
114*30988Ssam 		if (write(enp, buff, cnt) != cnt) {
115*30988Ssam 			fprintf(stderr, "enpload: Write error.\n");
116*30988Ssam 			exit(-1);
117*30988Ssam 		}
118*30988Ssam 	}
119*30988Ssam 	printf("done.\n");
120*30988Ssam }
121