1 /* $NetBSD: mkboot.c,v 1.9.16.1 2006/06/18 05:52:36 gdamore Exp $
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
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
31 * @(#)mkboot.c 8.1 (Berkeley) 7/15/93
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
42 "@(#) Copyright (c) 1990, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90";
50 __RCSID("$NetBSD: mkboot.c,v 1.9.16.1 2006/06/18 05:52:36 gdamore Exp $");
53 #include <sys/param.h>
56 #ifndef HAVE_NBTOOL_CONFIG_H
57 #include <sys/endian.h>
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)
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);
95 * sector 0: LIF volume header (40 bytes)
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.
102 * sector 0: LIF volume header (40 bytes)
104 * sector 2: LIF directory (8 x 32 == 256 bytes)
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
)
120 if (!strcmp(argv
[0], "-l")) {
125 sscanf(argv
[0], "0x%x", &loadpoint
);
130 if (!lpflag
|| argc
== 0)
150 to
= open(argv
[0], O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
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
);
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 */
185 lseek(to
, LIF_FILESTART
+lifstob(n
), SEEK_SET
);
186 count
= putfile(n2
, to
);
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 */
200 lseek(to
, LIF_FILESTART
+lifstob(lifd
[0].dir_length
+n
),
202 count
= putfile(n3
, to
);
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
);
223 putfile(char *from
, int to
)
230 if ((fd
= open(from
, 0)) < 0) {
231 printf("error: unable to open file %s\n", from
);
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
);
243 write(to
, &ld
, sizeof(ld
));
244 write(to
, bp
, statb
.st_size
);
246 return (statb
.st_size
+ sizeof(ld
));
254 "usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
261 static char lname
[10] = "SYS_XXXXX";
265 if ((cp
= strrchr(str
, '/')) != NULL
)
267 for (i
= 4; i
< 9; i
++) {
269 lname
[i
] = toupper(*str
);
270 else if (isalnum(*str
) || *str
== '_')
282 bcddate(char *name
, char *toc
)
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;