135524Sbostic /*
235524Sbostic * Copyright (c) 1988 The Regents of the University of California.
335524Sbostic * All rights reserved.
435524Sbostic *
535524Sbostic * This code is derived from software contributed to Berkeley by
635524Sbostic * Computer Consoles Inc.
735524Sbostic *
842699Sbostic * %sccs.include.redist.c%
935524Sbostic */
1035524Sbostic
1130988Ssam #ifndef lint
1235524Sbostic char copyright[] =
1335524Sbostic "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
1435524Sbostic All rights reserved.\n";
1535524Sbostic #endif /* not lint */
1630988Ssam
1735524Sbostic #ifndef lint
18*45940Sbostic static char sccsid[] = "@(#)enpload.c 5.4 (Berkeley) 01/14/91";
1935524Sbostic #endif /* not lint */
2035524Sbostic
2130988Ssam /*
2230988Ssam * CMC Ethernet ``Microcode'' Loader.
2330988Ssam */
2430988Ssam #include <sys/types.h>
2530988Ssam #include <sys/file.h>
2630988Ssam #include <sys/ioctl.h>
27*45940Sbostic #include <tahoe/if/if_enpreg.h>
28*45940Sbostic #include <stdio.h>
29*45940Sbostic #include <a.out.h>
3030988Ssam
3130988Ssam char *dev;
3230988Ssam
main(argc,argv)3330988Ssam main(argc, argv)
3430988Ssam int argc;
3530988Ssam char *argv[];
3630988Ssam {
3730988Ssam int enp = -1, fd, first = 1, nostart = 0;
3830988Ssam
3930988Ssam argc--, argv++;
4030988Ssam if (argc > 0) {
4130988Ssam enp = open(dev = argv[0], O_RDWR);
4230988Ssam if (enp < 0) {
4330988Ssam fprintf(stderr, "enpload: ");
4430988Ssam perror(dev);
4530988Ssam exit(-1);
4630988Ssam }
4730988Ssam argc--, argv++;
4830988Ssam }
4930988Ssam for (; argc > 0; argc--, argv++) {
5030988Ssam if (strcmp(argv[0], "-s") == 0 || strcmp(argv[0], "-S") == 0) {
5130988Ssam nostart++;
5230988Ssam continue;
5330988Ssam }
5430988Ssam if (first) {
5530988Ssam /*
5630988Ssam * Reset device before first file is loaded.
5730988Ssam */
5830988Ssam if (ioctl(enp, ENPIORESET) < 0) {
5930988Ssam fprintf(stderr, "enpload: %s: ", dev);
6030988Ssam perror("ioctl (ENPIORESET)");
6130988Ssam exit(-1);
6230988Ssam }
6330988Ssam first = !first;
6430988Ssam }
6530988Ssam if ((fd = open(argv[0], O_RDONLY)) < 0) {
6630988Ssam fprintf(stderr, "enpload: "), perror(argv[0]);
6730988Ssam exit(1);
6830988Ssam }
6930988Ssam enpload(enp, fd, argv[0]);
7030988Ssam close(fd);
7130988Ssam }
7230988Ssam if (enp != -1 && !nostart && ioctl(enp, ENPIOGO) < 0) {
7330988Ssam fprintf(stderr, "enpload: ");
7430988Ssam perror("ioctl (ENPIOGO)");
7530988Ssam exit(-1);
7630988Ssam }
7730988Ssam exit(0);
7830988Ssam }
7930988Ssam
8030988Ssam #define RELO 0x03FFFF /* relocation offset */
8130988Ssam #define ENPMSTART 0x0 /* start of memory */
8230988Ssam #define BSIZE 512 /* buffer size */
8330988Ssam char buff[BSIZE];
8430988Ssam char zbuf[BSIZE];
8530988Ssam
enpload(enp,fd,filename)8630988Ssam enpload(enp, fd, filename)
8730988Ssam int enp, fd;
8830988Ssam char *filename;
8930988Ssam {
9030988Ssam int cnt, size, lstart;
9130988Ssam struct exec hdr;
9230988Ssam
9330988Ssam if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) {
9430988Ssam fprintf(stderr, "enpload: %s: Read short (header).\n",
9530988Ssam filename);
9630988Ssam exit(1);
9730988Ssam }
9830988Ssam if (N_BADMAG(hdr)) {
9930988Ssam fprintf(stderr, "enpload: %s: Bad magic number.\n", filename);
10030988Ssam exit(1);
10130988Ssam }
10230988Ssam size = hdr.a_text + hdr.a_data;
10330988Ssam lstart = (ENPMSTART + (hdr.a_entry & RELO)) - 0x1000;
10430988Ssam
10530988Ssam printf("%s: Loading %s...", dev, filename);
10630988Ssam (void) lseek(enp, lstart + size, L_SET);
10730988Ssam while (hdr.a_bss >= BSIZE) {
10830988Ssam if (write(enp, zbuf, BSIZE) != BSIZE) {
10930988Ssam fprintf(stderr, "enpload: Bss write error.\n");
11030988Ssam exit(-1);
11130988Ssam }
11230988Ssam hdr.a_bss -= BSIZE;
11330988Ssam }
11430988Ssam if (hdr.a_bss > 0 && write(enp, zbuf, hdr.a_bss) != hdr.a_bss) {
11530988Ssam fprintf(stderr, "enpload: Bss write error.\n");
11630988Ssam exit(-1);
11730988Ssam }
11830988Ssam (void) lseek(enp, lstart, L_SET);
11930988Ssam while (size > BSIZE) {
12030988Ssam cnt = read(fd, buff, BSIZE);
12130988Ssam size -= cnt;
12230988Ssam if (write(enp, buff, cnt) != cnt) {
12330988Ssam fprintf(stderr, "enpload: Write error.\n");
12430988Ssam exit(-1);
12530988Ssam }
12630988Ssam }
12730988Ssam if (size > 0) {
12830988Ssam cnt = read(fd, buff, size);
12930988Ssam if (write(enp, buff, cnt) != cnt) {
13030988Ssam fprintf(stderr, "enpload: Write error.\n");
13130988Ssam exit(-1);
13230988Ssam }
13330988Ssam }
13430988Ssam printf("done.\n");
13530988Ssam }
136