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