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