mkboot.c revision 1.7 1 /* $NetBSD: mkboot.c,v 1.7 2004/11/28 11:14:41 jmc 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 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39
40 #ifndef lint
41 __COPYRIGHT(
42 "@(#) Copyright (c) 1990, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #ifdef notdef
48 static char sccsid[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90";
49 #endif
50 __RCSID("$NetBSD: mkboot.c,v 1.7 2004/11/28 11:14:41 jmc Exp $");
51 #endif /* not lint */
52
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/endian.h>
57
58 #include <time.h>
59
60 #include <ctype.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "volhdr.h"
67
68 #define LIF_NUMDIR 8
69
70 #define LIF_VOLSTART 0
71 #define LIF_VOLSIZE sizeof(struct lifvol)
72 #define LIF_DIRSTART 512
73 #define LIF_DIRSIZE (LIF_NUMDIR * sizeof(struct lifdir))
74 #define LIF_FILESTART 8192
75
76 #define btolifs(b) (((b) + (SECTSIZE - 1)) / SECTSIZE)
77 #define lifstob(s) ((s) * SECTSIZE)
78
79 int lpflag;
80 int loadpoint;
81 struct load ld;
82 struct lifvol lifv;
83 struct lifdir lifd[LIF_NUMDIR];
84
85 int main(int, char **);
86 void bcddate(char *, char *);
87 char *lifname(char *);
88 int putfile(char *, int);
89 void usage(void);
90
91 /*
92 * Old Format:
93 * sector 0: LIF volume header (40 bytes)
94 * sector 1: <unused>
95 * sector 2: LIF directory (8 x 32 == 256 bytes)
96 * sector 3-: LIF file 0, LIF file 1, etc.
97 * where sectors are 256 bytes.
98 *
99 * New Format:
100 * sector 0: LIF volume header (40 bytes)
101 * sector 1: <unused>
102 * sector 2: LIF directory (8 x 32 == 256 bytes)
103 * sector 3: <unused>
104 * sector 4-31: disklabel (~300 bytes right now)
105 * sector 32-: LIF file 0, LIF file 1, etc.
106 */
107 int
108 main(int argc, char **argv)
109 {
110 char *n1, *n2, *n3;
111 int n, to;
112 int count;
113
114 --argc;
115 ++argv;
116 if (argc == 0)
117 usage();
118 if (!strcmp(argv[0], "-l")) {
119 argv++;
120 argc--;
121 if (argc == 0)
122 usage();
123 sscanf(argv[0], "0x%x", &loadpoint);
124 lpflag++;
125 argv++;
126 argc--;
127 }
128 if (!lpflag || argc == 0)
129 usage();
130 n1 = argv[0];
131 argv++;
132 argc--;
133 if (argc == 0)
134 usage();
135 if (argc > 1) {
136 n2 = argv[0];
137 argv++;
138 argc--;
139 if (argc > 1) {
140 n3 = argv[0];
141 argv++;
142 argc--;
143 } else
144 n3 = NULL;
145 } else
146 n2 = n3 = NULL;
147
148 to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
149 if (to < 0) {
150 perror("open");
151 exit(1);
152 }
153 /* clear possibly unused directory entries */
154 strncpy(lifd[1].dir_name, " ", 10);
155 lifd[1].dir_type = htobe16(-1);
156 lifd[1].dir_addr = htobe32(0);
157 lifd[1].dir_length = htobe32(0);
158 lifd[1].dir_flag = htobe16(0xFF);
159 lifd[1].dir_exec = htobe32(0);
160 lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
161 /* record volume info */
162 lifv.vol_id = htobe16(VOL_ID);
163 strncpy(lifv.vol_label, "BOOT43", 6);
164 lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
165 lifv.vol_oct = htobe16(VOL_OCT);
166 lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
167 lifv.vol_version = htobe16(1);
168 /* output bootfile one */
169 lseek(to, LIF_FILESTART, SEEK_SET);
170 count = putfile(n1, to);
171 n = btolifs(count);
172 strcpy(lifd[0].dir_name, lifname(n1));
173 lifd[0].dir_type = htobe16(DIR_TYPE);
174 lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
175 lifd[0].dir_length = htobe32(n);
176 bcddate(n1, lifd[0].dir_toc);
177 lifd[0].dir_flag = htobe16(DIR_FLAG);
178 lifd[0].dir_exec = htobe32(loadpoint);
179 lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
180 be32toh(lifd[0].dir_length));
181 /* if there is an optional second boot program, output it */
182 if (n2) {
183 lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
184 count = putfile(n2, to);
185 n = btolifs(count);
186 strcpy(lifd[1].dir_name, lifname(n2));
187 lifd[1].dir_type = htobe16(DIR_TYPE);
188 lifd[1].dir_addr = htobe32(lifv.vol_length);
189 lifd[1].dir_length = htobe32(n);
190 bcddate(n2, lifd[1].dir_toc);
191 lifd[1].dir_flag = htobe16(DIR_FLAG);
192 lifd[1].dir_exec = htobe32(loadpoint);
193 lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
194 be32toh(lifd[1].dir_length));
195 }
196 /* ditto for three */
197 if (n3) {
198 lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n),
199 SEEK_SET);
200 count = putfile(n3, to);
201 n = btolifs(count);
202 strcpy(lifd[2].dir_name, lifname(n3));
203 lifd[2].dir_type = htobe16(DIR_TYPE);
204 lifd[2].dir_addr = htobe32(lifv.vol_length);
205 lifd[2].dir_length = htobe32(n);
206 bcddate(n3, lifd[2].dir_toc);
207 lifd[2].dir_flag = htobe16(DIR_FLAG);
208 lifd[2].dir_exec = htobe32(loadpoint);
209 lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
210 be32toh(lifd[2].dir_length));
211 }
212 /* output volume/directory header info */
213 lseek(to, LIF_VOLSTART, SEEK_SET);
214 write(to, &lifv, LIF_VOLSIZE);
215 lseek(to, LIF_DIRSTART, SEEK_SET);
216 write(to, lifd, LIF_DIRSIZE);
217 exit(0);
218 }
219
220 int
221 putfile(from, to)
222 char *from;
223 int to;
224 {
225 int fd;
226 struct stat statb;
227 int nr;
228 void *bp;
229
230 if ((fd = open(from, 0)) < 0) {
231 printf("error: unable to open file %s\n", from);
232 exit(1);
233 }
234 fstat(fd, &statb);
235 ld.address = htobe32(loadpoint);
236 ld.count = htobe32(statb.st_size);
237 bp = malloc(statb.st_size);
238 if ((nr = read(fd, bp, statb.st_size)) < 0) {
239 printf("error: reading from file %s\n", from);
240 exit(1);
241 }
242 (void)close(fd);
243 write(to, &ld, sizeof(ld));
244 write(to, bp, statb.st_size);
245 free(bp);
246 return (statb.st_size + sizeof(ld));
247 }
248
249 void
250 usage(void)
251 {
252
253 fprintf(stderr,
254 "usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
255 exit(1);
256 }
257
258 char *
259 lifname(char *str)
260 {
261 static char lname[10] = "SYS_XXXXX";
262 char *cp;
263 int i;
264
265 if ((cp = strrchr(str, '/')) != NULL)
266 str = ++cp;
267 for (i = 4; i < 9; i++) {
268 if (islower(*str))
269 lname[i] = toupper(*str);
270 else if (isalnum(*str) || *str == '_')
271 lname[i] = *str;
272 else
273 break;
274 str++;
275 }
276 for ( ; i < 10; i++)
277 lname[i] = '\0';
278 return(lname);
279 }
280
281 void
282 bcddate(char *name, char *toc)
283 {
284 struct stat statb;
285 struct tm *tm;
286
287 stat(name, &statb);
288 tm = localtime(&statb.st_ctime);
289 *toc = ((tm->tm_mon+1) / 10) << 4;
290 *toc++ |= (tm->tm_mon+1) % 10;
291 *toc = (tm->tm_mday / 10) << 4;
292 *toc++ |= tm->tm_mday % 10;
293 *toc = (tm->tm_year / 10) << 4;
294 *toc++ |= tm->tm_year % 10;
295 *toc = (tm->tm_hour / 10) << 4;
296 *toc++ |= tm->tm_hour % 10;
297 *toc = (tm->tm_min / 10) << 4;
298 *toc++ |= tm->tm_min % 10;
299 *toc = (tm->tm_sec / 10) << 4;
300 *toc |= tm->tm_sec % 10;
301 }
302