| 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 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. All advertising materials mentioning features or use of this software |
| 17 | * must display the following acknowledgement: |
| 18 | * This product includes software developed by the University of |
| 19 | * California, Berkeley and its contributors. |
| 20 | * 4. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #ifndef lint |
| 38 | char copyright[] = |
| 39 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ |
| 40 | All rights reserved.\n"; |
| 41 | #endif /* not lint */ |
| 42 | |
| 43 | #ifndef lint |
| 44 | static char sccsid[] = "@(#)enpload.c 5.4 (Berkeley) 1/14/91"; |
| 45 | #endif /* not lint */ |
| 46 | |
| 47 | /* |
| 48 | * CMC Ethernet ``Microcode'' Loader. |
| 49 | */ |
| 50 | #include <sys/types.h> |
| 51 | #include <sys/file.h> |
| 52 | #include <sys/ioctl.h> |
| 53 | #include <tahoe/if/if_enpreg.h> |
| 54 | #include <stdio.h> |
| 55 | #include <a.out.h> |
| 56 | |
| 57 | char *dev; |
| 58 | |
| 59 | main(argc, argv) |
| 60 | int argc; |
| 61 | char *argv[]; |
| 62 | { |
| 63 | int enp = -1, fd, first = 1, nostart = 0; |
| 64 | |
| 65 | argc--, argv++; |
| 66 | if (argc > 0) { |
| 67 | enp = open(dev = argv[0], O_RDWR); |
| 68 | if (enp < 0) { |
| 69 | fprintf(stderr, "enpload: "); |
| 70 | perror(dev); |
| 71 | exit(-1); |
| 72 | } |
| 73 | argc--, argv++; |
| 74 | } |
| 75 | for (; argc > 0; argc--, argv++) { |
| 76 | if (strcmp(argv[0], "-s") == 0 || strcmp(argv[0], "-S") == 0) { |
| 77 | nostart++; |
| 78 | continue; |
| 79 | } |
| 80 | if (first) { |
| 81 | /* |
| 82 | * Reset device before first file is loaded. |
| 83 | */ |
| 84 | if (ioctl(enp, ENPIORESET) < 0) { |
| 85 | fprintf(stderr, "enpload: %s: ", dev); |
| 86 | perror("ioctl (ENPIORESET)"); |
| 87 | exit(-1); |
| 88 | } |
| 89 | first = !first; |
| 90 | } |
| 91 | if ((fd = open(argv[0], O_RDONLY)) < 0) { |
| 92 | fprintf(stderr, "enpload: "), perror(argv[0]); |
| 93 | exit(1); |
| 94 | } |
| 95 | enpload(enp, fd, argv[0]); |
| 96 | close(fd); |
| 97 | } |
| 98 | if (enp != -1 && !nostart && ioctl(enp, ENPIOGO) < 0) { |
| 99 | fprintf(stderr, "enpload: "); |
| 100 | perror("ioctl (ENPIOGO)"); |
| 101 | exit(-1); |
| 102 | } |
| 103 | exit(0); |
| 104 | } |
| 105 | |
| 106 | #define RELO 0x03FFFF /* relocation offset */ |
| 107 | #define ENPMSTART 0x0 /* start of memory */ |
| 108 | #define BSIZE 512 /* buffer size */ |
| 109 | char buff[BSIZE]; |
| 110 | char zbuf[BSIZE]; |
| 111 | |
| 112 | enpload(enp, fd, filename) |
| 113 | int enp, fd; |
| 114 | char *filename; |
| 115 | { |
| 116 | int cnt, size, lstart; |
| 117 | struct exec hdr; |
| 118 | |
| 119 | if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) { |
| 120 | fprintf(stderr, "enpload: %s: Read short (header).\n", |
| 121 | filename); |
| 122 | exit(1); |
| 123 | } |
| 124 | if (N_BADMAG(hdr)) { |
| 125 | fprintf(stderr, "enpload: %s: Bad magic number.\n", filename); |
| 126 | exit(1); |
| 127 | } |
| 128 | size = hdr.a_text + hdr.a_data; |
| 129 | lstart = (ENPMSTART + (hdr.a_entry & RELO)) - 0x1000; |
| 130 | |
| 131 | printf("%s: Loading %s...", dev, filename); |
| 132 | (void) lseek(enp, lstart + size, L_SET); |
| 133 | while (hdr.a_bss >= BSIZE) { |
| 134 | if (write(enp, zbuf, BSIZE) != BSIZE) { |
| 135 | fprintf(stderr, "enpload: Bss write error.\n"); |
| 136 | exit(-1); |
| 137 | } |
| 138 | hdr.a_bss -= BSIZE; |
| 139 | } |
| 140 | if (hdr.a_bss > 0 && write(enp, zbuf, hdr.a_bss) != hdr.a_bss) { |
| 141 | fprintf(stderr, "enpload: Bss write error.\n"); |
| 142 | exit(-1); |
| 143 | } |
| 144 | (void) lseek(enp, lstart, L_SET); |
| 145 | while (size > BSIZE) { |
| 146 | cnt = read(fd, buff, BSIZE); |
| 147 | size -= cnt; |
| 148 | if (write(enp, buff, cnt) != cnt) { |
| 149 | fprintf(stderr, "enpload: Write error.\n"); |
| 150 | exit(-1); |
| 151 | } |
| 152 | } |
| 153 | if (size > 0) { |
| 154 | cnt = read(fd, buff, size); |
| 155 | if (write(enp, buff, cnt) != cnt) { |
| 156 | fprintf(stderr, "enpload: Write error.\n"); |
| 157 | exit(-1); |
| 158 | } |
| 159 | } |
| 160 | printf("done.\n"); |
| 161 | } |