mkboot.c revision 1.8 1 /* $NetBSD: mkboot.c,v 1.8 2005/02/20 13:59:27 tsutsui 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.8 2005/02/20 13:59:27 tsutsui 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(char *from, int to)
222 {
223 int fd;
224 struct stat statb;
225 int nr;
226 void *bp;
227
228 if ((fd = open(from, 0)) < 0) {
229 printf("error: unable to open file %s\n", from);
230 exit(1);
231 }
232 fstat(fd, &statb);
233 ld.address = htobe32(loadpoint);
234 ld.count = htobe32(statb.st_size);
235 bp = malloc(statb.st_size);
236 if ((nr = read(fd, bp, statb.st_size)) < 0) {
237 printf("error: reading from file %s\n", from);
238 exit(1);
239 }
240 (void)close(fd);
241 write(to, &ld, sizeof(ld));
242 write(to, bp, statb.st_size);
243 free(bp);
244 return (statb.st_size + sizeof(ld));
245 }
246
247 void
248 usage(void)
249 {
250
251 fprintf(stderr,
252 "usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
253 exit(1);
254 }
255
256 char *
257 lifname(char *str)
258 {
259 static char lname[10] = "SYS_XXXXX";
260 char *cp;
261 int i;
262
263 if ((cp = strrchr(str, '/')) != NULL)
264 str = ++cp;
265 for (i = 4; i < 9; i++) {
266 if (islower(*str))
267 lname[i] = toupper(*str);
268 else if (isalnum(*str) || *str == '_')
269 lname[i] = *str;
270 else
271 break;
272 str++;
273 }
274 for ( ; i < 10; i++)
275 lname[i] = '\0';
276 return(lname);
277 }
278
279 void
280 bcddate(char *name, char *toc)
281 {
282 struct stat statb;
283 struct tm *tm;
284
285 stat(name, &statb);
286 tm = localtime(&statb.st_ctime);
287 *toc = ((tm->tm_mon+1) / 10) << 4;
288 *toc++ |= (tm->tm_mon+1) % 10;
289 *toc = (tm->tm_mday / 10) << 4;
290 *toc++ |= tm->tm_mday % 10;
291 *toc = (tm->tm_year / 10) << 4;
292 *toc++ |= tm->tm_year % 10;
293 *toc = (tm->tm_hour / 10) << 4;
294 *toc++ |= tm->tm_hour % 10;
295 *toc = (tm->tm_min / 10) << 4;
296 *toc++ |= tm->tm_min % 10;
297 *toc = (tm->tm_sec / 10) << 4;
298 *toc |= tm->tm_sec % 10;
299 }
300