Home | History | Annotate | Line # | Download | only in mkboot
mkboot.c revision 1.1.26.1
      1       1.1  thorpej /*	$NetBSD: mkboot.c,v 1.1.26.1 2001/01/05 17:34:14 bouyer Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*
      4       1.1  thorpej  * Copyright (c) 1990, 1993
      5       1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6       1.1  thorpej  *
      7       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8       1.1  thorpej  * modification, are permitted provided that the following conditions
      9       1.1  thorpej  * are met:
     10       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16       1.1  thorpej  *    must display the following acknowledgement:
     17       1.1  thorpej  *	This product includes software developed by the University of
     18       1.1  thorpej  *	California, Berkeley and its contributors.
     19       1.1  thorpej  * 4. Neither the name of the University nor the names of its contributors
     20       1.1  thorpej  *    may be used to endorse or promote products derived from this software
     21       1.1  thorpej  *    without specific prior written permission.
     22       1.1  thorpej  *
     23       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1  thorpej  * SUCH DAMAGE.
     34       1.1  thorpej  *
     35       1.1  thorpej  *	@(#)mkboot.c	8.1 (Berkeley) 7/15/93
     36       1.1  thorpej  */
     37       1.1  thorpej 
     38  1.1.26.1   bouyer #include <sys/cdefs.h>
     39  1.1.26.1   bouyer 
     40       1.1  thorpej #ifndef lint
     41  1.1.26.1   bouyer __COPYRIGHT(
     42       1.1  thorpej "@(#) Copyright (c) 1990, 1993\n\
     43  1.1.26.1   bouyer 	The Regents of the University of California.  All rights reserved.\n");
     44       1.1  thorpej #endif /* not lint */
     45       1.1  thorpej 
     46       1.1  thorpej #ifndef lint
     47       1.1  thorpej #ifdef notdef
     48       1.1  thorpej static char sccsid[] = "@(#)mkboot.c	7.2 (Berkeley) 12/16/90";
     49       1.1  thorpej #endif
     50  1.1.26.1   bouyer __RCSID("$NetBSD: mkboot.c,v 1.1.26.1 2001/01/05 17:34:14 bouyer Exp $");
     51       1.1  thorpej #endif /* not lint */
     52       1.1  thorpej 
     53       1.1  thorpej #include <sys/param.h>
     54       1.1  thorpej #include <sys/file.h>
     55  1.1.26.1   bouyer #include <sys/stat.h>
     56       1.1  thorpej 
     57       1.1  thorpej #include <ctype.h>
     58  1.1.26.1   bouyer #include <stdio.h>
     59  1.1.26.1   bouyer #include <stdlib.h>
     60  1.1.26.1   bouyer #include <string.h>
     61  1.1.26.1   bouyer #include <unistd.h>
     62  1.1.26.1   bouyer 
     63  1.1.26.1   bouyer #include "volhdr.h"
     64  1.1.26.1   bouyer #include "loadfile.h"
     65       1.1  thorpej 
     66       1.1  thorpej #define LIF_NUMDIR	8
     67       1.1  thorpej 
     68       1.1  thorpej #define LIF_VOLSTART	0
     69       1.1  thorpej #define LIF_VOLSIZE	sizeof(struct lifvol)
     70       1.1  thorpej #define LIF_DIRSTART	512
     71       1.1  thorpej #define LIF_DIRSIZE	(LIF_NUMDIR * sizeof(struct lifdir))
     72       1.1  thorpej #define LIF_FILESTART	8192
     73       1.1  thorpej 
     74       1.1  thorpej #define btolifs(b)	(((b) + (SECTSIZE - 1)) / SECTSIZE)
     75       1.1  thorpej #define lifstob(s)	((s) * SECTSIZE)
     76       1.1  thorpej 
     77  1.1.26.1   bouyer int	lpflag;
     78  1.1.26.1   bouyer int	loadpoint;
     79  1.1.26.1   bouyer int	entrypoint;
     80  1.1.26.1   bouyer struct	load ld;
     81  1.1.26.1   bouyer struct	lifvol lifv;
     82  1.1.26.1   bouyer struct	lifdir lifd[LIF_NUMDIR];
     83  1.1.26.1   bouyer char	buf[10240];
     84  1.1.26.1   bouyer 
     85  1.1.26.1   bouyer int	 main(int, char **);
     86  1.1.26.1   bouyer void	 bcddate(char *, char *);
     87  1.1.26.1   bouyer char	*lifname(char *);
     88  1.1.26.1   bouyer void	 putfile(char *, int);
     89  1.1.26.1   bouyer void	 usage(void);
     90       1.1  thorpej 
     91       1.1  thorpej /*
     92       1.1  thorpej  * Old Format:
     93       1.1  thorpej  *	sector 0:	LIF volume header (40 bytes)
     94       1.1  thorpej  *	sector 1:	<unused>
     95       1.1  thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
     96       1.1  thorpej  *	sector 3-:	LIF file 0, LIF file 1, etc.
     97       1.1  thorpej  * where sectors are 256 bytes.
     98       1.1  thorpej  *
     99       1.1  thorpej  * New Format:
    100       1.1  thorpej  *	sector 0:	LIF volume header (40 bytes)
    101       1.1  thorpej  *	sector 1:	<unused>
    102       1.1  thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
    103       1.1  thorpej  *	sector 3:	<unused>
    104       1.1  thorpej  *	sector 4-31:	disklabel (~300 bytes right now)
    105       1.1  thorpej  *	sector 32-:	LIF file 0, LIF file 1, etc.
    106       1.1  thorpej  */
    107  1.1.26.1   bouyer int
    108  1.1.26.1   bouyer main(int argc, char **argv)
    109       1.1  thorpej {
    110  1.1.26.1   bouyer 	char *n1, *n2, *n3;
    111  1.1.26.1   bouyer 	int n, to;
    112  1.1.26.1   bouyer 
    113  1.1.26.1   bouyer 	--argc;
    114  1.1.26.1   bouyer 	++argv;
    115  1.1.26.1   bouyer 	if (argc == 0)
    116       1.1  thorpej 		usage();
    117  1.1.26.1   bouyer 	if (!strcmp(argv[0], "-l")) {
    118  1.1.26.1   bouyer 		argv++;
    119  1.1.26.1   bouyer 		argc--;
    120  1.1.26.1   bouyer 		if (argc == 0)
    121       1.1  thorpej 			usage();
    122  1.1.26.1   bouyer 		sscanf(argv[0], "0x%x", &loadpoint);
    123       1.1  thorpej 		lpflag++;
    124  1.1.26.1   bouyer 		argv++;
    125  1.1.26.1   bouyer 		argc--;
    126       1.1  thorpej 	}
    127  1.1.26.1   bouyer 	if (argc == 0)
    128       1.1  thorpej 		usage();
    129  1.1.26.1   bouyer 	n1 = argv[0];
    130  1.1.26.1   bouyer 	argv++;
    131  1.1.26.1   bouyer 	argc--;
    132  1.1.26.1   bouyer 	if (argc == 0)
    133       1.1  thorpej 		usage();
    134  1.1.26.1   bouyer 	if (argc > 1) {
    135  1.1.26.1   bouyer 		n2 = argv[0];
    136  1.1.26.1   bouyer 		argv++;
    137  1.1.26.1   bouyer 		argc--;
    138  1.1.26.1   bouyer 		if (argc > 1) {
    139  1.1.26.1   bouyer 			n3 = argv[0];
    140  1.1.26.1   bouyer 			argv++;
    141  1.1.26.1   bouyer 			argc--;
    142       1.1  thorpej 		} else
    143  1.1.26.1   bouyer 			n3 = NULL;
    144       1.1  thorpej 	} else
    145  1.1.26.1   bouyer 		n2 = n3 = NULL;
    146  1.1.26.1   bouyer 	to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
    147       1.1  thorpej 	if (to < 0) {
    148       1.1  thorpej 		perror("open");
    149       1.1  thorpej 		exit(1);
    150       1.1  thorpej 	}
    151       1.1  thorpej 	/* clear possibly unused directory entries */
    152       1.1  thorpej 	strncpy(lifd[1].dir_name, "	     ", 10);
    153       1.1  thorpej 	lifd[1].dir_type = -1;
    154       1.1  thorpej 	lifd[1].dir_addr = 0;
    155       1.1  thorpej 	lifd[1].dir_length = 0;
    156       1.1  thorpej 	lifd[1].dir_flag = 0xFF;
    157       1.1  thorpej 	lifd[1].dir_exec = 0;
    158       1.1  thorpej 	lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
    159       1.1  thorpej 	/* record volume info */
    160       1.1  thorpej 	lifv.vol_id = VOL_ID;
    161       1.1  thorpej 	strncpy(lifv.vol_label, "BOOT43", 6);
    162       1.1  thorpej 	lifv.vol_addr = btolifs(LIF_DIRSTART);
    163       1.1  thorpej 	lifv.vol_oct = VOL_OCT;
    164       1.1  thorpej 	lifv.vol_dirsize = btolifs(LIF_DIRSIZE);
    165       1.1  thorpej 	lifv.vol_version = 1;
    166       1.1  thorpej 	/* output bootfile one */
    167  1.1.26.1   bouyer 	lseek(to, LIF_FILESTART, SEEK_SET);
    168  1.1.26.1   bouyer 	putfile(n1, to);
    169       1.1  thorpej 	n = btolifs(ld.count + sizeof(ld));
    170       1.1  thorpej 	strcpy(lifd[0].dir_name, lifname(n1));
    171       1.1  thorpej 	lifd[0].dir_type = DIR_TYPE;
    172       1.1  thorpej 	lifd[0].dir_addr = btolifs(LIF_FILESTART);
    173       1.1  thorpej 	lifd[0].dir_length = n;
    174  1.1.26.1   bouyer 	bcddate(n1, lifd[0].dir_toc);
    175       1.1  thorpej 	lifd[0].dir_flag = DIR_FLAG;
    176  1.1.26.1   bouyer 	lifd[0].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    177       1.1  thorpej 	lifv.vol_length = lifd[0].dir_addr + lifd[0].dir_length;
    178       1.1  thorpej 	/* if there is an optional second boot program, output it */
    179  1.1.26.1   bouyer 	if (n2) {
    180  1.1.26.1   bouyer 		lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
    181  1.1.26.1   bouyer 		putfile(n2, to);
    182       1.1  thorpej 		n = btolifs(ld.count + sizeof(ld));
    183       1.1  thorpej 		strcpy(lifd[1].dir_name, lifname(n2));
    184       1.1  thorpej 		lifd[1].dir_type = DIR_TYPE;
    185       1.1  thorpej 		lifd[1].dir_addr = lifv.vol_length;
    186       1.1  thorpej 		lifd[1].dir_length = n;
    187  1.1.26.1   bouyer 		bcddate(n2, lifd[1].dir_toc);
    188       1.1  thorpej 		lifd[1].dir_flag = DIR_FLAG;
    189  1.1.26.1   bouyer 		lifd[1].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    190       1.1  thorpej 		lifv.vol_length = lifd[1].dir_addr + lifd[1].dir_length;
    191       1.1  thorpej 	}
    192       1.1  thorpej 	/* ditto for three */
    193  1.1.26.1   bouyer 	if (n3) {
    194  1.1.26.1   bouyer 		lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n), SEEK_SET);
    195  1.1.26.1   bouyer 		putfile(n3, to);
    196       1.1  thorpej 		n = btolifs(ld.count + sizeof(ld));
    197       1.1  thorpej 		strcpy(lifd[2].dir_name, lifname(n3));
    198       1.1  thorpej 		lifd[2].dir_type = DIR_TYPE;
    199       1.1  thorpej 		lifd[2].dir_addr = lifv.vol_length;
    200       1.1  thorpej 		lifd[2].dir_length = n;
    201  1.1.26.1   bouyer 		bcddate(n3, lifd[2].dir_toc);
    202       1.1  thorpej 		lifd[2].dir_flag = DIR_FLAG;
    203  1.1.26.1   bouyer 		lifd[2].dir_exec = lpflag? loadpoint + entrypoint : entrypoint;
    204       1.1  thorpej 		lifv.vol_length = lifd[2].dir_addr + lifd[2].dir_length;
    205       1.1  thorpej 	}
    206       1.1  thorpej 	/* output volume/directory header info */
    207  1.1.26.1   bouyer 	lseek(to, LIF_VOLSTART, SEEK_SET);
    208       1.1  thorpej 	write(to, &lifv, LIF_VOLSIZE);
    209  1.1.26.1   bouyer 	lseek(to, LIF_DIRSTART, SEEK_SET);
    210       1.1  thorpej 	write(to, lifd, LIF_DIRSIZE);
    211       1.1  thorpej 	exit(0);
    212       1.1  thorpej }
    213       1.1  thorpej 
    214  1.1.26.1   bouyer void
    215       1.1  thorpej putfile(from, to)
    216  1.1.26.1   bouyer 	char *from;
    217  1.1.26.1   bouyer 	int to;
    218       1.1  thorpej {
    219  1.1.26.1   bouyer 	int fd;
    220  1.1.26.1   bouyer 	u_long bp;
    221  1.1.26.1   bouyer 	u_long marks[MARK_MAX];
    222       1.1  thorpej 
    223  1.1.26.1   bouyer 	marks[MARK_START] = 0;
    224  1.1.26.1   bouyer 	if ((fd = loadfile(from, marks, COUNT_TEXT|COUNT_DATA)) == -1)
    225       1.1  thorpej 		exit(1);
    226  1.1.26.1   bouyer 	(void)close(fd);
    227  1.1.26.1   bouyer 
    228  1.1.26.1   bouyer 	entrypoint = marks[MARK_ENTRY];
    229  1.1.26.1   bouyer 	ld.address = lpflag ? loadpoint : entrypoint;
    230  1.1.26.1   bouyer 	ld.count = marks[MARK_END] - marks[MARK_START];
    231  1.1.26.1   bouyer 	bp = (u_long)malloc(ld.count);
    232  1.1.26.1   bouyer 	marks[MARK_START] = bp - marks[MARK_START];
    233  1.1.26.1   bouyer 
    234  1.1.26.1   bouyer 	if ((fd = loadfile(from, marks, LOAD_TEXT|LOAD_DATA)) == -1)
    235       1.1  thorpej 		exit(1);
    236  1.1.26.1   bouyer 	(void)close(fd);
    237  1.1.26.1   bouyer 
    238       1.1  thorpej 	write(to, &ld, sizeof(ld));
    239  1.1.26.1   bouyer 	write(to, (void *)bp, ld.count);
    240       1.1  thorpej }
    241       1.1  thorpej 
    242  1.1.26.1   bouyer void
    243  1.1.26.1   bouyer usage(void)
    244       1.1  thorpej {
    245  1.1.26.1   bouyer 
    246       1.1  thorpej 	fprintf(stderr,
    247       1.1  thorpej 		"usage:	 mkboot [-l loadpoint] prog1 [ prog2 ] outfile\n");
    248       1.1  thorpej 	exit(1);
    249       1.1  thorpej }
    250       1.1  thorpej 
    251       1.1  thorpej char *
    252  1.1.26.1   bouyer lifname(char *str)
    253       1.1  thorpej {
    254       1.1  thorpej 	static char lname[10] = "SYS_XXXXX";
    255  1.1.26.1   bouyer 	char *cp;
    256  1.1.26.1   bouyer 	int i;
    257       1.1  thorpej 
    258  1.1.26.1   bouyer 	if ((cp = strrchr(str, '/')) != NULL)
    259  1.1.26.1   bouyer 		str = ++cp;
    260       1.1  thorpej 	for (i = 4; i < 9; i++) {
    261       1.1  thorpej 		if (islower(*str))
    262       1.1  thorpej 			lname[i] = toupper(*str);
    263       1.1  thorpej 		else if (isalnum(*str) || *str == '_')
    264       1.1  thorpej 			lname[i] = *str;
    265       1.1  thorpej 		else
    266       1.1  thorpej 			break;
    267       1.1  thorpej 		str++;
    268       1.1  thorpej 	}
    269       1.1  thorpej 	for ( ; i < 10; i++)
    270       1.1  thorpej 		lname[i] = '\0';
    271       1.1  thorpej 	return(lname);
    272       1.1  thorpej }
    273       1.1  thorpej 
    274  1.1.26.1   bouyer void
    275  1.1.26.1   bouyer bcddate(char *name, char *toc)
    276       1.1  thorpej {
    277       1.1  thorpej 	struct stat statb;
    278       1.1  thorpej 	struct tm *tm;
    279       1.1  thorpej 
    280  1.1.26.1   bouyer 	stat(name, &statb);
    281       1.1  thorpej 	tm = localtime(&statb.st_ctime);
    282       1.1  thorpej 	*toc = ((tm->tm_mon+1) / 10) << 4;
    283       1.1  thorpej 	*toc++ |= (tm->tm_mon+1) % 10;
    284       1.1  thorpej 	*toc = (tm->tm_mday / 10) << 4;
    285       1.1  thorpej 	*toc++ |= tm->tm_mday % 10;
    286       1.1  thorpej 	*toc = (tm->tm_year / 10) << 4;
    287       1.1  thorpej 	*toc++ |= tm->tm_year % 10;
    288       1.1  thorpej 	*toc = (tm->tm_hour / 10) << 4;
    289       1.1  thorpej 	*toc++ |= tm->tm_hour % 10;
    290       1.1  thorpej 	*toc = (tm->tm_min / 10) << 4;
    291       1.1  thorpej 	*toc++ |= tm->tm_min % 10;
    292       1.1  thorpej 	*toc = (tm->tm_sec / 10) << 4;
    293       1.1  thorpej 	*toc |= tm->tm_sec % 10;
    294       1.1  thorpej }
    295