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