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 * 8*42699Sbostic * %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*42699Sbostic static char sccsid[] = "@(#)enpload.c 5.3 (Berkeley) 06/01/90"; 1935524Sbostic #endif /* not lint */ 2035524Sbostic 2130988Ssam /* 2230988Ssam * CMC Ethernet ``Microcode'' Loader. 2330988Ssam */ 2430988Ssam #include <stdio.h> 2530988Ssam #include <a.out.h> 2630988Ssam 2730988Ssam #include <sys/types.h> 2830988Ssam #include <sys/file.h> 2930988Ssam #include <sys/ioctl.h> 3030988Ssam #include <tahoeif/if_enpreg.h> 3130988Ssam 3230988Ssam char *dev; 3330988Ssam 3430988Ssam main(argc, argv) 3530988Ssam int argc; 3630988Ssam char *argv[]; 3730988Ssam { 3830988Ssam int enp = -1, fd, first = 1, nostart = 0; 3930988Ssam 4030988Ssam argc--, argv++; 4130988Ssam if (argc > 0) { 4230988Ssam enp = open(dev = argv[0], O_RDWR); 4330988Ssam if (enp < 0) { 4430988Ssam fprintf(stderr, "enpload: "); 4530988Ssam perror(dev); 4630988Ssam exit(-1); 4730988Ssam } 4830988Ssam argc--, argv++; 4930988Ssam } 5030988Ssam for (; argc > 0; argc--, argv++) { 5130988Ssam if (strcmp(argv[0], "-s") == 0 || strcmp(argv[0], "-S") == 0) { 5230988Ssam nostart++; 5330988Ssam continue; 5430988Ssam } 5530988Ssam if (first) { 5630988Ssam /* 5730988Ssam * Reset device before first file is loaded. 5830988Ssam */ 5930988Ssam if (ioctl(enp, ENPIORESET) < 0) { 6030988Ssam fprintf(stderr, "enpload: %s: ", dev); 6130988Ssam perror("ioctl (ENPIORESET)"); 6230988Ssam exit(-1); 6330988Ssam } 6430988Ssam first = !first; 6530988Ssam } 6630988Ssam if ((fd = open(argv[0], O_RDONLY)) < 0) { 6730988Ssam fprintf(stderr, "enpload: "), perror(argv[0]); 6830988Ssam exit(1); 6930988Ssam } 7030988Ssam enpload(enp, fd, argv[0]); 7130988Ssam close(fd); 7230988Ssam } 7330988Ssam if (enp != -1 && !nostart && ioctl(enp, ENPIOGO) < 0) { 7430988Ssam fprintf(stderr, "enpload: "); 7530988Ssam perror("ioctl (ENPIOGO)"); 7630988Ssam exit(-1); 7730988Ssam } 7830988Ssam exit(0); 7930988Ssam } 8030988Ssam 8130988Ssam #define RELO 0x03FFFF /* relocation offset */ 8230988Ssam #define ENPMSTART 0x0 /* start of memory */ 8330988Ssam #define BSIZE 512 /* buffer size */ 8430988Ssam char buff[BSIZE]; 8530988Ssam char zbuf[BSIZE]; 8630988Ssam 8730988Ssam enpload(enp, fd, filename) 8830988Ssam int enp, fd; 8930988Ssam char *filename; 9030988Ssam { 9130988Ssam int cnt, size, lstart; 9230988Ssam struct exec hdr; 9330988Ssam 9430988Ssam if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) { 9530988Ssam fprintf(stderr, "enpload: %s: Read short (header).\n", 9630988Ssam filename); 9730988Ssam exit(1); 9830988Ssam } 9930988Ssam if (N_BADMAG(hdr)) { 10030988Ssam fprintf(stderr, "enpload: %s: Bad magic number.\n", filename); 10130988Ssam exit(1); 10230988Ssam } 10330988Ssam size = hdr.a_text + hdr.a_data; 10430988Ssam lstart = (ENPMSTART + (hdr.a_entry & RELO)) - 0x1000; 10530988Ssam 10630988Ssam printf("%s: Loading %s...", dev, filename); 10730988Ssam (void) lseek(enp, lstart + size, L_SET); 10830988Ssam while (hdr.a_bss >= BSIZE) { 10930988Ssam if (write(enp, zbuf, BSIZE) != BSIZE) { 11030988Ssam fprintf(stderr, "enpload: Bss write error.\n"); 11130988Ssam exit(-1); 11230988Ssam } 11330988Ssam hdr.a_bss -= BSIZE; 11430988Ssam } 11530988Ssam if (hdr.a_bss > 0 && write(enp, zbuf, hdr.a_bss) != hdr.a_bss) { 11630988Ssam fprintf(stderr, "enpload: Bss write error.\n"); 11730988Ssam exit(-1); 11830988Ssam } 11930988Ssam (void) lseek(enp, lstart, L_SET); 12030988Ssam while (size > BSIZE) { 12130988Ssam cnt = read(fd, buff, BSIZE); 12230988Ssam size -= cnt; 12330988Ssam if (write(enp, buff, cnt) != cnt) { 12430988Ssam fprintf(stderr, "enpload: Write error.\n"); 12530988Ssam exit(-1); 12630988Ssam } 12730988Ssam } 12830988Ssam if (size > 0) { 12930988Ssam cnt = read(fd, buff, size); 13030988Ssam if (write(enp, buff, cnt) != cnt) { 13130988Ssam fprintf(stderr, "enpload: Write error.\n"); 13230988Ssam exit(-1); 13330988Ssam } 13430988Ssam } 13530988Ssam printf("done.\n"); 13630988Ssam } 137