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