xref: /netbsd/sys/arch/hp300/stand/mkboot/mkboot.c (revision bf9ec67e)
1 /*	$NetBSD: mkboot.c,v 1.4 2002/03/05 00:38:41 simonb 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)mkboot.c	8.1 (Berkeley) 7/15/93
36  */
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.4 2002/03/05 00:38:41 simonb Exp $");
51 #endif /* not lint */
52 
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/endian.h>
57 
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "volhdr.h"
65 
66 #define LIF_NUMDIR	8
67 
68 #define LIF_VOLSTART	0
69 #define LIF_VOLSIZE	sizeof(struct lifvol)
70 #define LIF_DIRSTART	512
71 #define LIF_DIRSIZE	(LIF_NUMDIR * sizeof(struct lifdir))
72 #define LIF_FILESTART	8192
73 
74 #define btolifs(b)	(((b) + (SECTSIZE - 1)) / SECTSIZE)
75 #define lifstob(s)	((s) * SECTSIZE)
76 
77 int	lpflag;
78 int	loadpoint;
79 struct  load ld;
80 struct	lifvol lifv;
81 struct	lifdir lifd[LIF_NUMDIR];
82 
83 int	 main(int, char **);
84 void	 bcddate(char *, char *);
85 char	*lifname(char *);
86 int	 putfile(char *, int);
87 void	 usage(void);
88 
89 /*
90  * Old Format:
91  *	sector 0:	LIF volume header (40 bytes)
92  *	sector 1:	<unused>
93  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
94  *	sector 3-:	LIF file 0, LIF file 1, etc.
95  * where sectors are 256 bytes.
96  *
97  * New Format:
98  *	sector 0:	LIF volume header (40 bytes)
99  *	sector 1:	<unused>
100  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
101  *	sector 3:	<unused>
102  *	sector 4-31:	disklabel (~300 bytes right now)
103  *	sector 32-:	LIF file 0, LIF file 1, etc.
104  */
105 int
106 main(int argc, char **argv)
107 {
108 	char *n1, *n2, *n3;
109 	int n, to;
110 	int count;
111 
112 	--argc;
113 	++argv;
114 	if (argc == 0)
115 		usage();
116 	if (!strcmp(argv[0], "-l")) {
117 		argv++;
118 		argc--;
119 		if (argc == 0)
120 			usage();
121 		sscanf(argv[0], "0x%x", &loadpoint);
122 		lpflag++;
123 		argv++;
124 		argc--;
125 	}
126 	if (!lpflag || argc == 0)
127 		usage();
128 	n1 = argv[0];
129 	argv++;
130 	argc--;
131 	if (argc == 0)
132 		usage();
133 	if (argc > 1) {
134 		n2 = argv[0];
135 		argv++;
136 		argc--;
137 		if (argc > 1) {
138 			n3 = argv[0];
139 			argv++;
140 			argc--;
141 		} else
142 			n3 = NULL;
143 	} else
144 		n2 = n3 = NULL;
145 
146 	to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
147 	if (to < 0) {
148 		perror("open");
149 		exit(1);
150 	}
151 	/* clear possibly unused directory entries */
152 	strncpy(lifd[1].dir_name, "          ", 10);
153 	lifd[1].dir_type = htobe16(-1);
154 	lifd[1].dir_addr = htobe32(0);
155 	lifd[1].dir_length = htobe32(0);
156 	lifd[1].dir_flag = htobe16(0xFF);
157 	lifd[1].dir_exec = htobe32(0);
158 	lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
159 	/* record volume info */
160 	lifv.vol_id = htobe16(VOL_ID);
161 	strncpy(lifv.vol_label, "BOOT43", 6);
162 	lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
163 	lifv.vol_oct = htobe16(VOL_OCT);
164 	lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
165 	lifv.vol_version = htobe16(1);
166 	/* output bootfile one */
167 	lseek(to, LIF_FILESTART, SEEK_SET);
168 	count = putfile(n1, to);
169 	n = btolifs(count);
170 	strcpy(lifd[0].dir_name, lifname(n1));
171 	lifd[0].dir_type = htobe16(DIR_TYPE);
172 	lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
173 	lifd[0].dir_length = htobe32(n);
174 	bcddate(n1, lifd[0].dir_toc);
175 	lifd[0].dir_flag = htobe16(DIR_FLAG);
176 	lifd[0].dir_exec = htobe32(loadpoint);
177 	lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
178 				  be32toh(lifd[0].dir_length));
179 	/* if there is an optional second boot program, output it */
180 	if (n2) {
181 		lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
182 		count = putfile(n2, to);
183 		n = btolifs(count);
184 		strcpy(lifd[1].dir_name, lifname(n2));
185 		lifd[1].dir_type = htobe16(DIR_TYPE);
186 		lifd[1].dir_addr = htobe32(lifv.vol_length);
187 		lifd[1].dir_length = htobe32(n);
188 		bcddate(n2, lifd[1].dir_toc);
189 		lifd[1].dir_flag = htobe32(DIR_FLAG);
190 		lifd[1].dir_exec = htobe32(loadpoint);
191 		lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
192 					  be32toh(lifd[1].dir_length));
193 	}
194 	/* ditto for three */
195 	if (n3) {
196 		lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n),
197 		      SEEK_SET);
198 		count = putfile(n3, to);
199 		n = btolifs(count);
200 		strcpy(lifd[2].dir_name, lifname(n3));
201 		lifd[2].dir_type = htobe16(DIR_TYPE);
202 		lifd[2].dir_addr = htobe32(lifv.vol_length);
203 		lifd[2].dir_length = htobe32(n);
204 		bcddate(n3, lifd[2].dir_toc);
205 		lifd[2].dir_flag = htobe32(DIR_FLAG);
206 		lifd[2].dir_exec = htobe32(loadpoint);
207 		lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
208 					  be32toh(lifd[2].dir_length));
209 	}
210 	/* output volume/directory header info */
211 	lseek(to, LIF_VOLSTART, SEEK_SET);
212 	write(to, &lifv, LIF_VOLSIZE);
213 	lseek(to, LIF_DIRSTART, SEEK_SET);
214 	write(to, lifd, LIF_DIRSIZE);
215 	exit(0);
216 }
217 
218 int
219 putfile(from, to)
220 	char *from;
221 	int to;
222 {
223 	int fd;
224 	struct stat statb;
225 	int nr;
226 	void *bp;
227 
228 	if ((fd = open(from, 0)) < 0) {
229 		printf("error: unable to open file %s\n", from);
230 		exit(1);
231 	}
232 	fstat(fd, &statb);
233 	ld.address = htobe32(loadpoint);
234 	ld.count = htobe32(statb.st_size);
235 	bp = malloc(statb.st_size);
236 	if ((nr = read(fd, bp, statb.st_size)) < 0) {
237 		printf("error: reading from file %s\n", from);
238 		exit(1);
239 	}
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,
252 		"usage:	 mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
253 	exit(1);
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(*str))
267 			lname[i] = toupper(*str);
268 		else if (isalnum(*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 	stat(name, &statb);
286 	tm = localtime(&statb.st_ctime);
287 	*toc = ((tm->tm_mon+1) / 10) << 4;
288 	*toc++ |= (tm->tm_mon+1) % 10;
289 	*toc = (tm->tm_mday / 10) << 4;
290 	*toc++ |= tm->tm_mday % 10;
291 	*toc = (tm->tm_year / 10) << 4;
292 	*toc++ |= tm->tm_year % 10;
293 	*toc = (tm->tm_hour / 10) << 4;
294 	*toc++ |= tm->tm_hour % 10;
295 	*toc = (tm->tm_min / 10) << 4;
296 	*toc++ |= tm->tm_min % 10;
297 	*toc = (tm->tm_sec / 10) << 4;
298 	*toc |= tm->tm_sec % 10;
299 }
300