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