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