Home | History | Annotate | Line # | Download | only in mkboot
mkboot.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: mkboot.c,v 1.1 1997/02/04 03:53:04 thorpej 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  thorpej #ifndef lint
     39  1.1  thorpej static char copyright[] =
     40  1.1  thorpej "@(#) Copyright (c) 1990, 1993\n\
     41  1.1  thorpej 	The Regents of the University of California.  All rights reserved.\n";
     42  1.1  thorpej #endif /* not lint */
     43  1.1  thorpej 
     44  1.1  thorpej #ifndef lint
     45  1.1  thorpej #ifdef notdef
     46  1.1  thorpej static char sccsid[] = "@(#)mkboot.c	7.2 (Berkeley) 12/16/90";
     47  1.1  thorpej #endif
     48  1.1  thorpej static char rcsid[] = "$NetBSD: mkboot.c,v 1.1 1997/02/04 03:53:04 thorpej Exp $";
     49  1.1  thorpej #endif /* not lint */
     50  1.1  thorpej 
     51  1.1  thorpej #include <sys/param.h>
     52  1.1  thorpej #include <sys/file.h>
     53  1.1  thorpej #include <a.out.h>
     54  1.1  thorpej 
     55  1.1  thorpej #include "volhdr.h"
     56  1.1  thorpej 
     57  1.1  thorpej #include <stdio.h>
     58  1.1  thorpej #include <ctype.h>
     59  1.1  thorpej 
     60  1.1  thorpej #define LIF_NUMDIR	8
     61  1.1  thorpej 
     62  1.1  thorpej #define LIF_VOLSTART	0
     63  1.1  thorpej #define LIF_VOLSIZE	sizeof(struct lifvol)
     64  1.1  thorpej #define LIF_DIRSTART	512
     65  1.1  thorpej #define LIF_DIRSIZE	(LIF_NUMDIR * sizeof(struct lifdir))
     66  1.1  thorpej #define LIF_FILESTART	8192
     67  1.1  thorpej 
     68  1.1  thorpej #define btolifs(b)	(((b) + (SECTSIZE - 1)) / SECTSIZE)
     69  1.1  thorpej #define lifstob(s)	((s) * SECTSIZE)
     70  1.1  thorpej 
     71  1.1  thorpej int lpflag;
     72  1.1  thorpej int loadpoint;
     73  1.1  thorpej struct load ld;
     74  1.1  thorpej struct lifvol lifv;
     75  1.1  thorpej struct lifdir lifd[LIF_NUMDIR];
     76  1.1  thorpej struct exec ex;
     77  1.1  thorpej char buf[10240];
     78  1.1  thorpej 
     79  1.1  thorpej /*
     80  1.1  thorpej  * Old Format:
     81  1.1  thorpej  *	sector 0:	LIF volume header (40 bytes)
     82  1.1  thorpej  *	sector 1:	<unused>
     83  1.1  thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
     84  1.1  thorpej  *	sector 3-:	LIF file 0, LIF file 1, etc.
     85  1.1  thorpej  * where sectors are 256 bytes.
     86  1.1  thorpej  *
     87  1.1  thorpej  * New Format:
     88  1.1  thorpej  *	sector 0:	LIF volume header (40 bytes)
     89  1.1  thorpej  *	sector 1:	<unused>
     90  1.1  thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
     91  1.1  thorpej  *	sector 3:	<unused>
     92  1.1  thorpej  *	sector 4-31:	disklabel (~300 bytes right now)
     93  1.1  thorpej  *	sector 32-:	LIF file 0, LIF file 1, etc.
     94  1.1  thorpej  */
     95  1.1  thorpej main(argc, argv)
     96  1.1  thorpej 	char **argv;
     97  1.1  thorpej {
     98  1.1  thorpej 	int ac;
     99  1.1  thorpej 	char **av;
    100  1.1  thorpej 	int from1, from2, from3, to;
    101  1.1  thorpej 	register int n;
    102  1.1  thorpej 	char *n1, *n2, *n3, *lifname();
    103  1.1  thorpej 
    104  1.1  thorpej 	ac = --argc;
    105  1.1  thorpej 	av = ++argv;
    106  1.1  thorpej 	if (ac == 0)
    107  1.1  thorpej 		usage();
    108  1.1  thorpej 	if (!strcmp(av[0], "-l")) {
    109  1.1  thorpej 		av++;
    110  1.1  thorpej 		ac--;
    111  1.1  thorpej 		if (ac == 0)
    112  1.1  thorpej 			usage();
    113  1.1  thorpej 		sscanf(av[0], "0x%x", &loadpoint);
    114  1.1  thorpej 		lpflag++;
    115  1.1  thorpej 		av++;
    116  1.1  thorpej 		ac--;
    117  1.1  thorpej 	}
    118  1.1  thorpej 	if (ac == 0)
    119  1.1  thorpej 		usage();
    120  1.1  thorpej 	from1 = open(av[0], O_RDONLY, 0);
    121  1.1  thorpej 	if (from1 < 0) {
    122  1.1  thorpej 		perror("open");
    123  1.1  thorpej 		exit(1);
    124  1.1  thorpej 	}
    125  1.1  thorpej 	n1 = av[0];
    126  1.1  thorpej 	av++;
    127  1.1  thorpej 	ac--;
    128  1.1  thorpej 	if (ac == 0)
    129  1.1  thorpej 		usage();
    130  1.1  thorpej 	if (ac > 1) {
    131  1.1  thorpej 		from2 = open(av[0], O_RDONLY, 0);
    132  1.1  thorpej 		if (from2 < 0) {
    133  1.1  thorpej 			perror("open");
    134  1.1  thorpej 			exit(1);
    135  1.1  thorpej 		}
    136  1.1  thorpej 		n2 = av[0];
    137  1.1  thorpej 		av++;
    138  1.1  thorpej 		ac--;
    139  1.1  thorpej 		if (ac > 1) {
    140  1.1  thorpej 			from3 = open(av[0], O_RDONLY, 0);
    141  1.1  thorpej 			if (from3 < 0) {
    142  1.1  thorpej 				perror("open");
    143  1.1  thorpej 				exit(1);
    144  1.1  thorpej 			}
    145  1.1  thorpej 			n3 = av[0];
    146  1.1  thorpej 			av++;
    147  1.1  thorpej 			ac--;
    148  1.1  thorpej 		} else
    149  1.1  thorpej 			from3 = -1;
    150  1.1  thorpej 	} else
    151  1.1  thorpej 		from2 = from3 = -1;
    152  1.1  thorpej 	to = open(av[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
    153  1.1  thorpej 	if (to < 0) {
    154  1.1  thorpej 		perror("open");
    155  1.1  thorpej 		exit(1);
    156  1.1  thorpej 	}
    157  1.1  thorpej 	/* clear possibly unused directory entries */
    158  1.1  thorpej 	strncpy(lifd[1].dir_name, "	     ", 10);
    159  1.1  thorpej 	lifd[1].dir_type = -1;
    160  1.1  thorpej 	lifd[1].dir_addr = 0;
    161  1.1  thorpej 	lifd[1].dir_length = 0;
    162  1.1  thorpej 	lifd[1].dir_flag = 0xFF;
    163  1.1  thorpej 	lifd[1].dir_exec = 0;
    164  1.1  thorpej 	lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
    165  1.1  thorpej 	/* record volume info */
    166  1.1  thorpej 	lifv.vol_id = VOL_ID;
    167  1.1  thorpej 	strncpy(lifv.vol_label, "BOOT43", 6);
    168  1.1  thorpej 	lifv.vol_addr = btolifs(LIF_DIRSTART);
    169  1.1  thorpej 	lifv.vol_oct = VOL_OCT;
    170  1.1  thorpej 	lifv.vol_dirsize = btolifs(LIF_DIRSIZE);
    171  1.1  thorpej 	lifv.vol_version = 1;
    172  1.1  thorpej 	/* output bootfile one */
    173  1.1  thorpej 	lseek(to, LIF_FILESTART, 0);
    174  1.1  thorpej 	putfile(from1, to);
    175  1.1  thorpej 	n = btolifs(ld.count + sizeof(ld));
    176  1.1  thorpej 	strcpy(lifd[0].dir_name, lifname(n1));
    177  1.1  thorpej 	lifd[0].dir_type = DIR_TYPE;
    178  1.1  thorpej 	lifd[0].dir_addr = btolifs(LIF_FILESTART);
    179  1.1  thorpej 	lifd[0].dir_length = n;
    180  1.1  thorpej 	bcddate(from1, lifd[0].dir_toc);
    181  1.1  thorpej 	lifd[0].dir_flag = DIR_FLAG;
    182  1.1  thorpej 	lifd[0].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
    183  1.1  thorpej 	lifv.vol_length = lifd[0].dir_addr + lifd[0].dir_length;
    184  1.1  thorpej 	/* if there is an optional second boot program, output it */
    185  1.1  thorpej 	if (from2 >= 0) {
    186  1.1  thorpej 		lseek(to, LIF_FILESTART+lifstob(n), 0);
    187  1.1  thorpej 		putfile(from2, to);
    188  1.1  thorpej 		n = btolifs(ld.count + sizeof(ld));
    189  1.1  thorpej 		strcpy(lifd[1].dir_name, lifname(n2));
    190  1.1  thorpej 		lifd[1].dir_type = DIR_TYPE;
    191  1.1  thorpej 		lifd[1].dir_addr = lifv.vol_length;
    192  1.1  thorpej 		lifd[1].dir_length = n;
    193  1.1  thorpej 		bcddate(from2, lifd[1].dir_toc);
    194  1.1  thorpej 		lifd[1].dir_flag = DIR_FLAG;
    195  1.1  thorpej 		lifd[1].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
    196  1.1  thorpej 		lifv.vol_length = lifd[1].dir_addr + lifd[1].dir_length;
    197  1.1  thorpej 	}
    198  1.1  thorpej 	/* ditto for three */
    199  1.1  thorpej 	if (from3 >= 0) {
    200  1.1  thorpej 		lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n), 0);
    201  1.1  thorpej 		putfile(from3, to);
    202  1.1  thorpej 		n = btolifs(ld.count + sizeof(ld));
    203  1.1  thorpej 		strcpy(lifd[2].dir_name, lifname(n3));
    204  1.1  thorpej 		lifd[2].dir_type = DIR_TYPE;
    205  1.1  thorpej 		lifd[2].dir_addr = lifv.vol_length;
    206  1.1  thorpej 		lifd[2].dir_length = n;
    207  1.1  thorpej 		bcddate(from3, lifd[2].dir_toc);
    208  1.1  thorpej 		lifd[2].dir_flag = DIR_FLAG;
    209  1.1  thorpej 		lifd[2].dir_exec = lpflag? loadpoint + ex.a_entry : ex.a_entry;
    210  1.1  thorpej 		lifv.vol_length = lifd[2].dir_addr + lifd[2].dir_length;
    211  1.1  thorpej 	}
    212  1.1  thorpej 	/* output volume/directory header info */
    213  1.1  thorpej 	lseek(to, LIF_VOLSTART, 0);
    214  1.1  thorpej 	write(to, &lifv, LIF_VOLSIZE);
    215  1.1  thorpej 	lseek(to, LIF_DIRSTART, 0);
    216  1.1  thorpej 	write(to, lifd, LIF_DIRSIZE);
    217  1.1  thorpej 	exit(0);
    218  1.1  thorpej }
    219  1.1  thorpej 
    220  1.1  thorpej putfile(from, to)
    221  1.1  thorpej {
    222  1.1  thorpej 	register int n, tcnt, dcnt;
    223  1.1  thorpej 
    224  1.1  thorpej 	n = read(from, &ex, sizeof(ex));
    225  1.1  thorpej 	if (n != sizeof(ex)) {
    226  1.1  thorpej 		fprintf(stderr, "error reading file header\n");
    227  1.1  thorpej 		exit(1);
    228  1.1  thorpej 	}
    229  1.1  thorpej 	if (N_GETMAGIC(ex) == OMAGIC) {
    230  1.1  thorpej 		tcnt = ex.a_text;
    231  1.1  thorpej 		dcnt = ex.a_data;
    232  1.1  thorpej 	}
    233  1.1  thorpej 	else if (N_GETMAGIC(ex) == NMAGIC) {
    234  1.1  thorpej 		tcnt = (ex.a_text + PGOFSET) & ~PGOFSET;
    235  1.1  thorpej 		dcnt = ex.a_data;
    236  1.1  thorpej 	}
    237  1.1  thorpej 	else {
    238  1.1  thorpej 		fprintf(stderr, "bad magic number\n");
    239  1.1  thorpej 		exit(1);
    240  1.1  thorpej 	}
    241  1.1  thorpej 	ld.address = lpflag ? loadpoint : ex.a_entry;
    242  1.1  thorpej 	ld.count = tcnt + dcnt;
    243  1.1  thorpej 	write(to, &ld, sizeof(ld));
    244  1.1  thorpej 	while (tcnt) {
    245  1.1  thorpej 		n = sizeof(buf);
    246  1.1  thorpej 		if (n > tcnt)
    247  1.1  thorpej 			n = tcnt;
    248  1.1  thorpej 		n = read(from, buf, n);
    249  1.1  thorpej 		if (n < 0) {
    250  1.1  thorpej 			perror("read");
    251  1.1  thorpej 			exit(1);
    252  1.1  thorpej 		}
    253  1.1  thorpej 		if (n == 0) {
    254  1.1  thorpej 			fprintf(stderr, "short read\n");
    255  1.1  thorpej 			exit(1);
    256  1.1  thorpej 		}
    257  1.1  thorpej 		if (write(to, buf, n) < 0) {
    258  1.1  thorpej 			perror("write");
    259  1.1  thorpej 			exit(1);
    260  1.1  thorpej 		}
    261  1.1  thorpej 		tcnt -= n;
    262  1.1  thorpej 	}
    263  1.1  thorpej 	while (dcnt) {
    264  1.1  thorpej 		n = sizeof(buf);
    265  1.1  thorpej 		if (n > dcnt)
    266  1.1  thorpej 			n = dcnt;
    267  1.1  thorpej 		n = read(from, buf, n);
    268  1.1  thorpej 		if (n < 0) {
    269  1.1  thorpej 			perror("read");
    270  1.1  thorpej 			exit(1);
    271  1.1  thorpej 		}
    272  1.1  thorpej 		if (n == 0) {
    273  1.1  thorpej 			fprintf(stderr, "short read\n");
    274  1.1  thorpej 			exit(1);
    275  1.1  thorpej 		}
    276  1.1  thorpej 		if (write(to, buf, n) < 0) {
    277  1.1  thorpej 			perror("write");
    278  1.1  thorpej 			exit(1);
    279  1.1  thorpej 		}
    280  1.1  thorpej 		dcnt -= n;
    281  1.1  thorpej 	}
    282  1.1  thorpej }
    283  1.1  thorpej 
    284  1.1  thorpej usage()
    285  1.1  thorpej {
    286  1.1  thorpej 	fprintf(stderr,
    287  1.1  thorpej 		"usage:	 mkboot [-l loadpoint] prog1 [ prog2 ] outfile\n");
    288  1.1  thorpej 	exit(1);
    289  1.1  thorpej }
    290  1.1  thorpej 
    291  1.1  thorpej char *
    292  1.1  thorpej lifname(str)
    293  1.1  thorpej  char *str;
    294  1.1  thorpej {
    295  1.1  thorpej 	static char lname[10] = "SYS_XXXXX";
    296  1.1  thorpej 	register int i;
    297  1.1  thorpej 
    298  1.1  thorpej 	for (i = 4; i < 9; i++) {
    299  1.1  thorpej 		if (islower(*str))
    300  1.1  thorpej 			lname[i] = toupper(*str);
    301  1.1  thorpej 		else if (isalnum(*str) || *str == '_')
    302  1.1  thorpej 			lname[i] = *str;
    303  1.1  thorpej 		else
    304  1.1  thorpej 			break;
    305  1.1  thorpej 		str++;
    306  1.1  thorpej 	}
    307  1.1  thorpej 	for ( ; i < 10; i++)
    308  1.1  thorpej 		lname[i] = '\0';
    309  1.1  thorpej 	return(lname);
    310  1.1  thorpej }
    311  1.1  thorpej 
    312  1.1  thorpej #include <sys/stat.h>
    313  1.1  thorpej #include <time.h>	/* XXX */
    314  1.1  thorpej 
    315  1.1  thorpej bcddate(fd, toc)
    316  1.1  thorpej 	int fd;
    317  1.1  thorpej 	char *toc;
    318  1.1  thorpej {
    319  1.1  thorpej 	struct stat statb;
    320  1.1  thorpej 	struct tm *tm;
    321  1.1  thorpej 
    322  1.1  thorpej 	fstat(fd, &statb);
    323  1.1  thorpej 	tm = localtime(&statb.st_ctime);
    324  1.1  thorpej 	*toc = ((tm->tm_mon+1) / 10) << 4;
    325  1.1  thorpej 	*toc++ |= (tm->tm_mon+1) % 10;
    326  1.1  thorpej 	*toc = (tm->tm_mday / 10) << 4;
    327  1.1  thorpej 	*toc++ |= tm->tm_mday % 10;
    328  1.1  thorpej 	*toc = (tm->tm_year / 10) << 4;
    329  1.1  thorpej 	*toc++ |= tm->tm_year % 10;
    330  1.1  thorpej 	*toc = (tm->tm_hour / 10) << 4;
    331  1.1  thorpej 	*toc++ |= tm->tm_hour % 10;
    332  1.1  thorpej 	*toc = (tm->tm_min / 10) << 4;
    333  1.1  thorpej 	*toc++ |= tm->tm_min % 10;
    334  1.1  thorpej 	*toc = (tm->tm_sec / 10) << 4;
    335  1.1  thorpej 	*toc |= tm->tm_sec % 10;
    336  1.1  thorpej }
    337