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