No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / stand / mkboot / mkboot.c
blob176cd2a22b3cbdda94da56d678d2101268a98660
1 /* $NetBSD: mkboot.c,v 1.9.16.1 2006/06/18 05:52:36 gdamore Exp $
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
31 * @(#)mkboot.c 8.1 (Berkeley) 7/15/93
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
38 #include <sys/cdefs.h>
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 */
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.9.16.1 2006/06/18 05:52:36 gdamore Exp $");
51 #endif /* not lint */
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
60 #include <time.h>
62 #include <ctype.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
68 #include "volhdr.h"
70 #define LIF_NUMDIR 8
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
78 #define btolifs(b) (((b) + (SECTSIZE - 1)) / SECTSIZE)
79 #define lifstob(s) ((s) * SECTSIZE)
81 int lpflag;
82 int loadpoint;
83 struct load ld;
84 struct lifvol lifv;
85 struct lifdir lifd[LIF_NUMDIR];
87 int main(int, char **);
88 void bcddate(char *, char *);
89 char *lifname(char *);
90 int putfile(char *, int);
91 void usage(void);
94 * Old Format:
95 * sector 0: LIF volume header (40 bytes)
96 * sector 1: <unused>
97 * sector 2: LIF directory (8 x 32 == 256 bytes)
98 * sector 3-: LIF file 0, LIF file 1, etc.
99 * where sectors are 256 bytes.
101 * New Format:
102 * sector 0: LIF volume header (40 bytes)
103 * sector 1: <unused>
104 * sector 2: LIF directory (8 x 32 == 256 bytes)
105 * sector 3: <unused>
106 * sector 4-31: disklabel (~300 bytes right now)
107 * sector 32-: LIF file 0, LIF file 1, etc.
110 main(int argc, char **argv)
112 char *n1, *n2, *n3;
113 int n, to;
114 int count;
116 --argc;
117 ++argv;
118 if (argc == 0)
119 usage();
120 if (!strcmp(argv[0], "-l")) {
121 argv++;
122 argc--;
123 if (argc == 0)
124 usage();
125 sscanf(argv[0], "0x%x", &loadpoint);
126 lpflag++;
127 argv++;
128 argc--;
130 if (!lpflag || argc == 0)
131 usage();
132 n1 = argv[0];
133 argv++;
134 argc--;
135 if (argc == 0)
136 usage();
137 if (argc > 1) {
138 n2 = argv[0];
139 argv++;
140 argc--;
141 if (argc > 1) {
142 n3 = argv[0];
143 argv++;
144 argc--;
145 } else
146 n3 = NULL;
147 } else
148 n2 = n3 = NULL;
150 to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
151 if (to < 0) {
152 perror("open");
153 exit(1);
155 /* clear possibly unused directory entries */
156 strncpy(lifd[1].dir_name, " ", 10);
157 lifd[1].dir_type = htobe16(-1);
158 lifd[1].dir_addr = htobe32(0);
159 lifd[1].dir_length = htobe32(0);
160 lifd[1].dir_flag = htobe16(0xFF);
161 lifd[1].dir_exec = htobe32(0);
162 lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
163 /* record volume info */
164 lifv.vol_id = htobe16(VOL_ID);
165 strncpy(lifv.vol_label, "BOOT43", 6);
166 lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
167 lifv.vol_oct = htobe16(VOL_OCT);
168 lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
169 lifv.vol_version = htobe16(1);
170 /* output bootfile one */
171 lseek(to, LIF_FILESTART, SEEK_SET);
172 count = putfile(n1, to);
173 n = btolifs(count);
174 strcpy(lifd[0].dir_name, lifname(n1));
175 lifd[0].dir_type = htobe16(DIR_TYPE);
176 lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
177 lifd[0].dir_length = htobe32(n);
178 bcddate(n1, lifd[0].dir_toc);
179 lifd[0].dir_flag = htobe16(DIR_FLAG);
180 lifd[0].dir_exec = htobe32(loadpoint);
181 lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
182 be32toh(lifd[0].dir_length));
183 /* if there is an optional second boot program, output it */
184 if (n2) {
185 lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
186 count = putfile(n2, to);
187 n = btolifs(count);
188 strcpy(lifd[1].dir_name, lifname(n2));
189 lifd[1].dir_type = htobe16(DIR_TYPE);
190 lifd[1].dir_addr = htobe32(lifv.vol_length);
191 lifd[1].dir_length = htobe32(n);
192 bcddate(n2, lifd[1].dir_toc);
193 lifd[1].dir_flag = htobe16(DIR_FLAG);
194 lifd[1].dir_exec = htobe32(loadpoint);
195 lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
196 be32toh(lifd[1].dir_length));
198 /* ditto for three */
199 if (n3) {
200 lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n),
201 SEEK_SET);
202 count = putfile(n3, to);
203 n = btolifs(count);
204 strcpy(lifd[2].dir_name, lifname(n3));
205 lifd[2].dir_type = htobe16(DIR_TYPE);
206 lifd[2].dir_addr = htobe32(lifv.vol_length);
207 lifd[2].dir_length = htobe32(n);
208 bcddate(n3, lifd[2].dir_toc);
209 lifd[2].dir_flag = htobe16(DIR_FLAG);
210 lifd[2].dir_exec = htobe32(loadpoint);
211 lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
212 be32toh(lifd[2].dir_length));
214 /* output volume/directory header info */
215 lseek(to, LIF_VOLSTART, SEEK_SET);
216 write(to, &lifv, LIF_VOLSIZE);
217 lseek(to, LIF_DIRSTART, SEEK_SET);
218 write(to, lifd, LIF_DIRSIZE);
219 exit(0);
223 putfile(char *from, int to)
225 int fd;
226 struct stat statb;
227 int nr;
228 void *bp;
230 if ((fd = open(from, 0)) < 0) {
231 printf("error: unable to open file %s\n", from);
232 exit(1);
234 fstat(fd, &statb);
235 ld.address = htobe32(loadpoint);
236 ld.count = htobe32(statb.st_size);
237 bp = malloc(statb.st_size);
238 if ((nr = read(fd, bp, statb.st_size)) < 0) {
239 printf("error: reading from file %s\n", from);
240 exit(1);
242 (void)close(fd);
243 write(to, &ld, sizeof(ld));
244 write(to, bp, statb.st_size);
245 free(bp);
246 return (statb.st_size + sizeof(ld));
249 void
250 usage(void)
253 fprintf(stderr,
254 "usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
255 exit(1);
258 char *
259 lifname(char *str)
261 static char lname[10] = "SYS_XXXXX";
262 char *cp;
263 int i;
265 if ((cp = strrchr(str, '/')) != NULL)
266 str = ++cp;
267 for (i = 4; i < 9; i++) {
268 if (islower(*str))
269 lname[i] = toupper(*str);
270 else if (isalnum(*str) || *str == '_')
271 lname[i] = *str;
272 else
273 break;
274 str++;
276 for ( ; i < 10; i++)
277 lname[i] = '\0';
278 return(lname);
281 void
282 bcddate(char *name, char *toc)
284 struct stat statb;
285 struct tm *tm;
287 stat(name, &statb);
288 tm = localtime(&statb.st_ctime);
289 *toc = ((tm->tm_mon+1) / 10) << 4;
290 *toc++ |= (tm->tm_mon+1) % 10;
291 *toc = (tm->tm_mday / 10) << 4;
292 *toc++ |= tm->tm_mday % 10;
293 *toc = (tm->tm_year / 10) << 4;
294 *toc++ |= tm->tm_year % 10;
295 *toc = (tm->tm_hour / 10) << 4;
296 *toc++ |= tm->tm_hour % 10;
297 *toc = (tm->tm_min / 10) << 4;
298 *toc++ |= tm->tm_min % 10;
299 *toc = (tm->tm_sec / 10) << 4;
300 *toc |= tm->tm_sec % 10;