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