Home | History | Annotate | Line # | Download | only in mkboot
mkboot.c revision 1.2
      1 /*	$NetBSD: mkboot.c,v 1.2 2001/01/02 04:14:34 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.2 2001/01/02 04:14:34 simonb Exp $");
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/file.h>
     55 #include <sys/stat.h>
     56 
     57 #include <ctype.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 
     63 #include "volhdr.h"
     64 #include "loadfile.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 int	entrypoint;
     80 struct	load ld;
     81 struct	lifvol lifv;
     82 struct	lifdir lifd[LIF_NUMDIR];
     83 char	buf[10240];
     84 
     85 int	 main(int, char **);
     86 void	 bcddate(char *, char *);
     87 char	*lifname(char *);
     88 void	 putfile(char *, int);
     89 void	 usage(void);
     90 
     91 /*
     92  * Old Format:
     93  *	sector 0:	LIF volume header (40 bytes)
     94  *	sector 1:	<unused>
     95  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
     96  *	sector 3-:	LIF file 0, LIF file 1, etc.
     97  * where sectors are 256 bytes.
     98  *
     99  * New Format:
    100  *	sector 0:	LIF volume header (40 bytes)
    101  *	sector 1:	<unused>
    102  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
    103  *	sector 3:	<unused>
    104  *	sector 4-31:	disklabel (~300 bytes right now)
    105  *	sector 32-:	LIF file 0, LIF file 1, etc.
    106  */
    107 int
    108 main(int argc, char **argv)
    109 {
    110 	char *n1, *n2, *n3;
    111 	int n, to;
    112 
    113 	--argc;
    114 	++argv;
    115 	if (argc == 0)
    116 		usage();
    117 	if (!strcmp(argv[0], "-l")) {
    118 		argv++;
    119 		argc--;
    120 		if (argc == 0)
    121 			usage();
    122 		sscanf(argv[0], "0x%x", &loadpoint);
    123 		lpflag++;
    124 		argv++;
    125 		argc--;
    126 	}
    127 	if (argc == 0)
    128 		usage();
    129 	n1 = argv[0];
    130 	argv++;
    131 	argc--;
    132 	if (argc == 0)
    133 		usage();
    134 	if (argc > 1) {
    135 		n2 = argv[0];
    136 		argv++;
    137 		argc--;
    138 		if (argc > 1) {
    139 			n3 = argv[0];
    140 			argv++;
    141 			argc--;
    142 		} else
    143 			n3 = NULL;
    144 	} else
    145 		n2 = n3 = NULL;
    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 = -1;
    154 	lifd[1].dir_addr = 0;
    155 	lifd[1].dir_length = 0;
    156 	lifd[1].dir_flag = 0xFF;
    157 	lifd[1].dir_exec = 0;
    158 	lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
    159 	/* record volume info */
    160 	lifv.vol_id = VOL_ID;
    161 	strncpy(lifv.vol_label, "BOOT43", 6);
    162 	lifv.vol_addr = btolifs(LIF_DIRSTART);
    163 	lifv.vol_oct = VOL_OCT;
    164 	lifv.vol_dirsize = btolifs(LIF_DIRSIZE);
    165 	lifv.vol_version = 1;
    166 	/* output bootfile one */
    167 	lseek(to, LIF_FILESTART, SEEK_SET);
    168 	putfile(n1, to);
    169 	n = btolifs(ld.count + sizeof(ld));
    170 	strcpy(lifd[0].dir_name, lifname(n1));
    171 	lifd[0].dir_type = DIR_TYPE;
    172 	lifd[0].dir_addr = btolifs(LIF_FILESTART);
    173 	lifd[0].dir_length = n;
    174 	bcddate(n1, lifd[0].dir_toc);
    175 	lifd[0].dir_flag = DIR_FLAG;
    176 	lifd[0].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    177 	lifv.vol_length = lifd[0].dir_addr + lifd[0].dir_length;
    178 	/* if there is an optional second boot program, output it */
    179 	if (n2) {
    180 		lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
    181 		putfile(n2, to);
    182 		n = btolifs(ld.count + sizeof(ld));
    183 		strcpy(lifd[1].dir_name, lifname(n2));
    184 		lifd[1].dir_type = DIR_TYPE;
    185 		lifd[1].dir_addr = lifv.vol_length;
    186 		lifd[1].dir_length = n;
    187 		bcddate(n2, lifd[1].dir_toc);
    188 		lifd[1].dir_flag = DIR_FLAG;
    189 		lifd[1].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    190 		lifv.vol_length = lifd[1].dir_addr + lifd[1].dir_length;
    191 	}
    192 	/* ditto for three */
    193 	if (n3) {
    194 		lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n), SEEK_SET);
    195 		putfile(n3, to);
    196 		n = btolifs(ld.count + sizeof(ld));
    197 		strcpy(lifd[2].dir_name, lifname(n3));
    198 		lifd[2].dir_type = DIR_TYPE;
    199 		lifd[2].dir_addr = lifv.vol_length;
    200 		lifd[2].dir_length = n;
    201 		bcddate(n3, lifd[2].dir_toc);
    202 		lifd[2].dir_flag = DIR_FLAG;
    203 		lifd[2].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    204 		lifv.vol_length = lifd[2].dir_addr + lifd[2].dir_length;
    205 	}
    206 	/* output volume/directory header info */
    207 	lseek(to, LIF_VOLSTART, SEEK_SET);
    208 	write(to, &lifv, LIF_VOLSIZE);
    209 	lseek(to, LIF_DIRSTART, SEEK_SET);
    210 	write(to, lifd, LIF_DIRSIZE);
    211 	exit(0);
    212 }
    213 
    214 void
    215 putfile(from, to)
    216 	char *from;
    217 	int to;
    218 {
    219 	int fd;
    220 	u_long bp;
    221 	u_long marks[MARK_MAX];
    222 
    223 	marks[MARK_START] = 0;
    224 	if ((fd = loadfile(from, marks, COUNT_TEXT|COUNT_DATA)) == -1)
    225 		exit(1);
    226 	(void)close(fd);
    227 
    228 	entrypoint = marks[MARK_ENTRY];
    229 	ld.address = lpflag ? loadpoint : entrypoint;
    230 	ld.count = marks[MARK_END] - marks[MARK_START];
    231 	bp = (u_long)malloc(ld.count);
    232 	marks[MARK_START] = bp - marks[MARK_START];
    233 
    234 	if ((fd = loadfile(from, marks, LOAD_TEXT|LOAD_DATA)) == -1)
    235 		exit(1);
    236 	(void)close(fd);
    237 
    238 	write(to, &ld, sizeof(ld));
    239 	write(to, (void *)bp, ld.count);
    240 }
    241 
    242 void
    243 usage(void)
    244 {
    245 
    246 	fprintf(stderr,
    247 		"usage:	 mkboot [-l loadpoint] prog1 [ prog2 ] outfile\n");
    248 	exit(1);
    249 }
    250 
    251 char *
    252 lifname(char *str)
    253 {
    254 	static char lname[10] = "SYS_XXXXX";
    255 	char *cp;
    256 	int i;
    257 
    258 	if ((cp = strrchr(str, '/')) != NULL)
    259 		str = ++cp;
    260 	for (i = 4; i < 9; i++) {
    261 		if (islower(*str))
    262 			lname[i] = toupper(*str);
    263 		else if (isalnum(*str) || *str == '_')
    264 			lname[i] = *str;
    265 		else
    266 			break;
    267 		str++;
    268 	}
    269 	for ( ; i < 10; i++)
    270 		lname[i] = '\0';
    271 	return(lname);
    272 }
    273 
    274 void
    275 bcddate(char *name, char *toc)
    276 {
    277 	struct stat statb;
    278 	struct tm *tm;
    279 
    280 	stat(name, &statb);
    281 	tm = localtime(&statb.st_ctime);
    282 	*toc = ((tm->tm_mon+1) / 10) << 4;
    283 	*toc++ |= (tm->tm_mon+1) % 10;
    284 	*toc = (tm->tm_mday / 10) << 4;
    285 	*toc++ |= tm->tm_mday % 10;
    286 	*toc = (tm->tm_year / 10) << 4;
    287 	*toc++ |= tm->tm_year % 10;
    288 	*toc = (tm->tm_hour / 10) << 4;
    289 	*toc++ |= tm->tm_hour % 10;
    290 	*toc = (tm->tm_min / 10) << 4;
    291 	*toc++ |= tm->tm_min % 10;
    292 	*toc = (tm->tm_sec / 10) << 4;
    293 	*toc |= tm->tm_sec % 10;
    294 }
    295