mkboot.c revision 1.5 1 /* $NetBSD: mkboot.c,v 1.5 2003/08/07 16:27:43 agc Exp $
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)mkboot.c 8.1 (Berkeley) 7/15/93
32 */
33
34 #include <sys/cdefs.h>
35
36 #ifndef lint
37 __COPYRIGHT(
38 "@(#) Copyright (c) 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #ifdef notdef
44 static char sccsid[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90";
45 #endif
46 __RCSID("$NetBSD: mkboot.c,v 1.5 2003/08/07 16:27:43 agc Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/endian.h>
53
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include "volhdr.h"
61
62 #define LIF_NUMDIR 8
63
64 #define LIF_VOLSTART 0
65 #define LIF_VOLSIZE sizeof(struct lifvol)
66 #define LIF_DIRSTART 512
67 #define LIF_DIRSIZE (LIF_NUMDIR * sizeof(struct lifdir))
68 #define LIF_FILESTART 8192
69
70 #define btolifs(b) (((b) + (SECTSIZE - 1)) / SECTSIZE)
71 #define lifstob(s) ((s) * SECTSIZE)
72
73 int lpflag;
74 int loadpoint;
75 struct load ld;
76 struct lifvol lifv;
77 struct lifdir lifd[LIF_NUMDIR];
78
79 int main(int, char **);
80 void bcddate(char *, char *);
81 char *lifname(char *);
82 int putfile(char *, int);
83 void usage(void);
84
85 /*
86 * Old Format:
87 * sector 0: LIF volume header (40 bytes)
88 * sector 1: <unused>
89 * sector 2: LIF directory (8 x 32 == 256 bytes)
90 * sector 3-: LIF file 0, LIF file 1, etc.
91 * where sectors are 256 bytes.
92 *
93 * New Format:
94 * sector 0: LIF volume header (40 bytes)
95 * sector 1: <unused>
96 * sector 2: LIF directory (8 x 32 == 256 bytes)
97 * sector 3: <unused>
98 * sector 4-31: disklabel (~300 bytes right now)
99 * sector 32-: LIF file 0, LIF file 1, etc.
100 */
101 int
102 main(int argc, char **argv)
103 {
104 char *n1, *n2, *n3;
105 int n, to;
106 int count;
107
108 --argc;
109 ++argv;
110 if (argc == 0)
111 usage();
112 if (!strcmp(argv[0], "-l")) {
113 argv++;
114 argc--;
115 if (argc == 0)
116 usage();
117 sscanf(argv[0], "0x%x", &loadpoint);
118 lpflag++;
119 argv++;
120 argc--;
121 }
122 if (!lpflag || argc == 0)
123 usage();
124 n1 = argv[0];
125 argv++;
126 argc--;
127 if (argc == 0)
128 usage();
129 if (argc > 1) {
130 n2 = argv[0];
131 argv++;
132 argc--;
133 if (argc > 1) {
134 n3 = argv[0];
135 argv++;
136 argc--;
137 } else
138 n3 = NULL;
139 } else
140 n2 = n3 = NULL;
141
142 to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
143 if (to < 0) {
144 perror("open");
145 exit(1);
146 }
147 /* clear possibly unused directory entries */
148 strncpy(lifd[1].dir_name, " ", 10);
149 lifd[1].dir_type = htobe16(-1);
150 lifd[1].dir_addr = htobe32(0);
151 lifd[1].dir_length = htobe32(0);
152 lifd[1].dir_flag = htobe16(0xFF);
153 lifd[1].dir_exec = htobe32(0);
154 lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
155 /* record volume info */
156 lifv.vol_id = htobe16(VOL_ID);
157 strncpy(lifv.vol_label, "BOOT43", 6);
158 lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
159 lifv.vol_oct = htobe16(VOL_OCT);
160 lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
161 lifv.vol_version = htobe16(1);
162 /* output bootfile one */
163 lseek(to, LIF_FILESTART, SEEK_SET);
164 count = putfile(n1, to);
165 n = btolifs(count);
166 strcpy(lifd[0].dir_name, lifname(n1));
167 lifd[0].dir_type = htobe16(DIR_TYPE);
168 lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
169 lifd[0].dir_length = htobe32(n);
170 bcddate(n1, lifd[0].dir_toc);
171 lifd[0].dir_flag = htobe16(DIR_FLAG);
172 lifd[0].dir_exec = htobe32(loadpoint);
173 lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
174 be32toh(lifd[0].dir_length));
175 /* if there is an optional second boot program, output it */
176 if (n2) {
177 lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
178 count = putfile(n2, to);
179 n = btolifs(count);
180 strcpy(lifd[1].dir_name, lifname(n2));
181 lifd[1].dir_type = htobe16(DIR_TYPE);
182 lifd[1].dir_addr = htobe32(lifv.vol_length);
183 lifd[1].dir_length = htobe32(n);
184 bcddate(n2, lifd[1].dir_toc);
185 lifd[1].dir_flag = htobe32(DIR_FLAG);
186 lifd[1].dir_exec = htobe32(loadpoint);
187 lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
188 be32toh(lifd[1].dir_length));
189 }
190 /* ditto for three */
191 if (n3) {
192 lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n),
193 SEEK_SET);
194 count = putfile(n3, to);
195 n = btolifs(count);
196 strcpy(lifd[2].dir_name, lifname(n3));
197 lifd[2].dir_type = htobe16(DIR_TYPE);
198 lifd[2].dir_addr = htobe32(lifv.vol_length);
199 lifd[2].dir_length = htobe32(n);
200 bcddate(n3, lifd[2].dir_toc);
201 lifd[2].dir_flag = htobe32(DIR_FLAG);
202 lifd[2].dir_exec = htobe32(loadpoint);
203 lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
204 be32toh(lifd[2].dir_length));
205 }
206 /* output volume/directory header info */
207 lseek(to, LIF_VOLSTART, SEEK_SET);
208 write(to, &lifv, LIF_VOLSIZE);
209 lseek(to, LIF_DIRSTART, SEEK_SET);
210 write(to, lifd, LIF_DIRSIZE);
211 exit(0);
212 }
213
214 int
215 putfile(from, to)
216 char *from;
217 int to;
218 {
219 int fd;
220 struct stat statb;
221 int nr;
222 void *bp;
223
224 if ((fd = open(from, 0)) < 0) {
225 printf("error: unable to open file %s\n", from);
226 exit(1);
227 }
228 fstat(fd, &statb);
229 ld.address = htobe32(loadpoint);
230 ld.count = htobe32(statb.st_size);
231 bp = malloc(statb.st_size);
232 if ((nr = read(fd, bp, statb.st_size)) < 0) {
233 printf("error: reading from file %s\n", from);
234 exit(1);
235 }
236 (void)close(fd);
237 write(to, &ld, sizeof(ld));
238 write(to, bp, statb.st_size);
239 free(bp);
240 return (statb.st_size + sizeof(ld));
241 }
242
243 void
244 usage(void)
245 {
246
247 fprintf(stderr,
248 "usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
249 exit(1);
250 }
251
252 char *
253 lifname(char *str)
254 {
255 static char lname[10] = "SYS_XXXXX";
256 char *cp;
257 int i;
258
259 if ((cp = strrchr(str, '/')) != NULL)
260 str = ++cp;
261 for (i = 4; i < 9; i++) {
262 if (islower(*str))
263 lname[i] = toupper(*str);
264 else if (isalnum(*str) || *str == '_')
265 lname[i] = *str;
266 else
267 break;
268 str++;
269 }
270 for ( ; i < 10; i++)
271 lname[i] = '\0';
272 return(lname);
273 }
274
275 void
276 bcddate(char *name, char *toc)
277 {
278 struct stat statb;
279 struct tm *tm;
280
281 stat(name, &statb);
282 tm = localtime(&statb.st_ctime);
283 *toc = ((tm->tm_mon+1) / 10) << 4;
284 *toc++ |= (tm->tm_mon+1) % 10;
285 *toc = (tm->tm_mday / 10) << 4;
286 *toc++ |= tm->tm_mday % 10;
287 *toc = (tm->tm_year / 10) << 4;
288 *toc++ |= tm->tm_year % 10;
289 *toc = (tm->tm_hour / 10) << 4;
290 *toc++ |= tm->tm_hour % 10;
291 *toc = (tm->tm_min / 10) << 4;
292 *toc++ |= tm->tm_min % 10;
293 *toc = (tm->tm_sec / 10) << 4;
294 *toc |= tm->tm_sec % 10;
295 }
296