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