Home | History | Annotate | Line # | Download | only in mkboot
mkboot.c revision 1.11.50.1
      1  1.11.50.1    martin /*	$NetBSD: mkboot.c,v 1.11.50.1 2024/06/22 10:57:11 martin 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.7       jmc #if HAVE_NBTOOL_CONFIG_H
     35        1.7       jmc #include "nbtool_config.h"
     36        1.7       jmc #endif
     37        1.7       jmc 
     38        1.2    simonb #include <sys/cdefs.h>
     39        1.2    simonb 
     40        1.1   thorpej #ifndef lint
     41  1.11.50.1    martin __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
     42  1.11.50.1    martin The Regents of the University of California.  All rights reserved.");
     43        1.1   thorpej #endif /* not lint */
     44        1.1   thorpej 
     45        1.1   thorpej #ifndef lint
     46        1.1   thorpej #ifdef notdef
     47        1.1   thorpej static char sccsid[] = "@(#)mkboot.c	7.2 (Berkeley) 12/16/90";
     48        1.1   thorpej #endif
     49  1.11.50.1    martin __RCSID("$NetBSD: mkboot.c,v 1.11.50.1 2024/06/22 10:57:11 martin Exp $");
     50        1.1   thorpej #endif /* not lint */
     51        1.1   thorpej 
     52        1.1   thorpej #include <sys/param.h>
     53        1.1   thorpej #include <sys/file.h>
     54        1.2    simonb #include <sys/stat.h>
     55  1.11.50.1    martin #if HAVE_NBTOOL_CONFIG_H
     56  1.11.50.1    martin #include "nbtool_config.h"
     57  1.11.50.1    martin #include "../../sys/sys/bootblock.h"
     58  1.11.50.1    martin #else
     59  1.11.50.1    martin #include <sys/bootblock.h>
     60        1.3  gmcgarry #include <sys/endian.h>
     61       1.10   gdamore #endif
     62        1.2    simonb 
     63        1.7       jmc #include <time.h>
     64        1.7       jmc 
     65        1.2    simonb #include <ctype.h>
     66  1.11.50.1    martin #include <err.h>
     67        1.2    simonb #include <stdio.h>
     68        1.2    simonb #include <stdlib.h>
     69        1.2    simonb #include <string.h>
     70        1.2    simonb #include <unistd.h>
     71        1.1   thorpej 
     72        1.1   thorpej #define LIF_NUMDIR	8
     73        1.1   thorpej 
     74        1.1   thorpej #define LIF_VOLSTART	0
     75  1.11.50.1    martin #define LIF_VOLSIZE	sizeof(struct hp300_lifvol)
     76        1.1   thorpej #define LIF_DIRSTART	512
     77  1.11.50.1    martin #define LIF_DIRSIZE	(LIF_NUMDIR * sizeof(struct hp300_lifdir))
     78        1.1   thorpej #define LIF_FILESTART	8192
     79        1.1   thorpej 
     80  1.11.50.1    martin #define btolifs(b)	(((b) + (HP300_SECTSIZE - 1)) / HP300_SECTSIZE)
     81  1.11.50.1    martin #define lifstob(s)	((s) * HP300_SECTSIZE)
     82        1.1   thorpej 
     83  1.11.50.1    martin #define bintobcd(bin)	((((bin) / 10) << 4) | ((bin) % 10))
     84  1.11.50.1    martin 
     85  1.11.50.1    martin static uint32_t loadpoint = ULONG_MAX;
     86  1.11.50.1    martin static struct hp300_load ld;
     87  1.11.50.1    martin static struct hp300_lifvol lifv;
     88  1.11.50.1    martin static struct hp300_lifdir lifd[LIF_NUMDIR];
     89  1.11.50.1    martin static time_t repro_epoch = 0;
     90        1.2    simonb 
     91        1.2    simonb int	 main(int, char **);
     92  1.11.50.1    martin 
     93  1.11.50.1    martin static void	 bcddate(char *, char *);
     94  1.11.50.1    martin static char	*lifname(char *);
     95  1.11.50.1    martin static size_t	 putfile(char *, int);
     96  1.11.50.1    martin static void	 usage(void);
     97  1.11.50.1    martin 
     98  1.11.50.1    martin #ifndef __CTASSERT
     99  1.11.50.1    martin #define	__CTASSERT(X)
    100  1.11.50.1    martin #endif
    101  1.11.50.1    martin 
    102  1.11.50.1    martin #define CLEAR(a, b, c)	\
    103  1.11.50.1    martin __CTASSERT(sizeof(b) - 1 == c); \
    104  1.11.50.1    martin memcpy((a), (b), (c))
    105        1.1   thorpej 
    106        1.1   thorpej /*
    107        1.1   thorpej  * Old Format:
    108        1.1   thorpej  *	sector 0:	LIF volume header (40 bytes)
    109        1.1   thorpej  *	sector 1:	<unused>
    110        1.1   thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
    111        1.1   thorpej  *	sector 3-:	LIF file 0, LIF file 1, etc.
    112        1.1   thorpej  * where sectors are 256 bytes.
    113        1.1   thorpej  *
    114        1.1   thorpej  * New Format:
    115        1.1   thorpej  *	sector 0:	LIF volume header (40 bytes)
    116        1.1   thorpej  *	sector 1:	<unused>
    117        1.1   thorpej  *	sector 2:	LIF directory (8 x 32 == 256 bytes)
    118        1.1   thorpej  *	sector 3:	<unused>
    119        1.1   thorpej  *	sector 4-31:	disklabel (~300 bytes right now)
    120        1.1   thorpej  *	sector 32-:	LIF file 0, LIF file 1, etc.
    121        1.1   thorpej  */
    122        1.2    simonb int
    123        1.2    simonb main(int argc, char **argv)
    124        1.1   thorpej {
    125  1.11.50.1    martin 	char *name1, *name2, *name3;
    126  1.11.50.1    martin 	int to, ch;
    127  1.11.50.1    martin 	uint32_t count, nsec;
    128  1.11.50.1    martin 
    129  1.11.50.1    martin 	while ((ch = getopt(argc, argv, "l:t:")) != -1)
    130  1.11.50.1    martin 		switch (ch) {
    131  1.11.50.1    martin 		case 'l':
    132  1.11.50.1    martin 			loadpoint = strtoul(optarg, NULL, 0);
    133  1.11.50.1    martin 			break;
    134  1.11.50.1    martin 		case 't':
    135  1.11.50.1    martin 			repro_epoch = (time_t)atoll(optarg);
    136  1.11.50.1    martin 			break;
    137  1.11.50.1    martin 		default:
    138        1.1   thorpej 			usage();
    139  1.11.50.1    martin 		}
    140  1.11.50.1    martin 
    141  1.11.50.1    martin 	argc -= optind;
    142  1.11.50.1    martin 	argv += optind;
    143  1.11.50.1    martin 	if (loadpoint == ULONG_MAX || argc == 0)
    144        1.1   thorpej 		usage();
    145  1.11.50.1    martin 	name1 = argv[0];
    146        1.2    simonb 	argv++;
    147        1.2    simonb 	argc--;
    148        1.2    simonb 	if (argc == 0)
    149        1.1   thorpej 		usage();
    150        1.2    simonb 	if (argc > 1) {
    151  1.11.50.1    martin 		name2 = argv[0];
    152        1.2    simonb 		argv++;
    153        1.2    simonb 		argc--;
    154        1.2    simonb 		if (argc > 1) {
    155  1.11.50.1    martin 			name3 = argv[0];
    156        1.2    simonb 			argv++;
    157        1.2    simonb 			argc--;
    158        1.1   thorpej 		} else
    159  1.11.50.1    martin 			name3 = NULL;
    160        1.1   thorpej 	} else
    161  1.11.50.1    martin 		name2 = name3 = NULL;
    162  1.11.50.1    martin 
    163  1.11.50.1    martin 	if ((to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1)
    164  1.11.50.1    martin 		err(1, "Can't open `%s'", argv[0]);
    165        1.3  gmcgarry 
    166        1.1   thorpej 	/* clear possibly unused directory entries */
    167  1.11.50.1    martin 	CLEAR(lifd[1].dir_name, "          ", sizeof(lifd[1].dir_name));
    168  1.11.50.1    martin 	lifd[1].dir_type = htobe16(0xFFFF);
    169        1.3  gmcgarry 	lifd[1].dir_addr = htobe32(0);
    170        1.3  gmcgarry 	lifd[1].dir_length = htobe32(0);
    171  1.11.50.1    martin 	lifd[1].dir_flag = htobe16(0x00FF);
    172        1.3  gmcgarry 	lifd[1].dir_exec = htobe32(0);
    173        1.1   thorpej 	lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
    174  1.11.50.1    martin 
    175        1.1   thorpej 	/* record volume info */
    176  1.11.50.1    martin 	lifv.vol_id = htobe16(HP300_VOL_ID);
    177  1.11.50.1    martin 	CLEAR(lifv.vol_label, "BOOT43", sizeof(lifv.vol_label));
    178        1.3  gmcgarry 	lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
    179  1.11.50.1    martin 	lifv.vol_oct = htobe16(HP300_VOL_OCT);
    180        1.3  gmcgarry 	lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
    181        1.3  gmcgarry 	lifv.vol_version = htobe16(1);
    182  1.11.50.1    martin 
    183        1.1   thorpej 	/* output bootfile one */
    184        1.2    simonb 	lseek(to, LIF_FILESTART, SEEK_SET);
    185  1.11.50.1    martin 	count = putfile(name1, to);
    186  1.11.50.1    martin 	nsec = btolifs(count);
    187  1.11.50.1    martin 	strcpy(lifd[0].dir_name, lifname(name1));
    188  1.11.50.1    martin 	lifd[0].dir_type = htobe16(HP300_DIR_TYPE);
    189        1.3  gmcgarry 	lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
    190  1.11.50.1    martin 	lifd[0].dir_length = htobe32(nsec);
    191  1.11.50.1    martin 	bcddate(name1, lifd[0].dir_toc);
    192  1.11.50.1    martin 	lifd[0].dir_flag = htobe16(HP300_DIR_FLAG);
    193        1.3  gmcgarry 	lifd[0].dir_exec = htobe32(loadpoint);
    194        1.3  gmcgarry 	lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
    195  1.11.50.1    martin 	    be32toh(lifd[0].dir_length));
    196  1.11.50.1    martin 
    197        1.1   thorpej 	/* if there is an optional second boot program, output it */
    198  1.11.50.1    martin 	if (name2 != NULL) {
    199  1.11.50.1    martin 		lseek(to, LIF_FILESTART + lifstob(nsec), SEEK_SET);
    200  1.11.50.1    martin 		count = putfile(name2, to);
    201  1.11.50.1    martin 		nsec = btolifs(count);
    202  1.11.50.1    martin 		strcpy(lifd[1].dir_name, lifname(name2));
    203  1.11.50.1    martin 		lifd[1].dir_type = htobe16(HP300_DIR_TYPE);
    204        1.3  gmcgarry 		lifd[1].dir_addr = htobe32(lifv.vol_length);
    205  1.11.50.1    martin 		lifd[1].dir_length = htobe32(nsec);
    206  1.11.50.1    martin 		bcddate(name2, lifd[1].dir_toc);
    207  1.11.50.1    martin 		lifd[1].dir_flag = htobe16(HP300_DIR_FLAG);
    208        1.3  gmcgarry 		lifd[1].dir_exec = htobe32(loadpoint);
    209        1.3  gmcgarry 		lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
    210  1.11.50.1    martin 		    be32toh(lifd[1].dir_length));
    211        1.1   thorpej 	}
    212  1.11.50.1    martin 
    213        1.1   thorpej 	/* ditto for three */
    214  1.11.50.1    martin 	if (name3 != NULL) {
    215  1.11.50.1    martin 		lseek(to, LIF_FILESTART + lifstob(lifd[0].dir_length + nsec),
    216  1.11.50.1    martin 		    SEEK_SET);
    217  1.11.50.1    martin 		count = putfile(name3, to);
    218  1.11.50.1    martin 		nsec = btolifs(count);
    219  1.11.50.1    martin 		strcpy(lifd[2].dir_name, lifname(name3));
    220  1.11.50.1    martin 		lifd[2].dir_type = htobe16(HP300_DIR_TYPE);
    221        1.3  gmcgarry 		lifd[2].dir_addr = htobe32(lifv.vol_length);
    222  1.11.50.1    martin 		lifd[2].dir_length = htobe32(nsec);
    223  1.11.50.1    martin 		bcddate(name3, lifd[2].dir_toc);
    224  1.11.50.1    martin 		lifd[2].dir_flag = htobe16(HP300_DIR_FLAG);
    225        1.3  gmcgarry 		lifd[2].dir_exec = htobe32(loadpoint);
    226        1.3  gmcgarry 		lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
    227  1.11.50.1    martin 		    be32toh(lifd[2].dir_length));
    228        1.1   thorpej 	}
    229  1.11.50.1    martin 
    230        1.1   thorpej 	/* output volume/directory header info */
    231        1.2    simonb 	lseek(to, LIF_VOLSTART, SEEK_SET);
    232        1.1   thorpej 	write(to, &lifv, LIF_VOLSIZE);
    233        1.2    simonb 	lseek(to, LIF_DIRSTART, SEEK_SET);
    234        1.1   thorpej 	write(to, lifd, LIF_DIRSIZE);
    235  1.11.50.1    martin 
    236  1.11.50.1    martin 	return EXIT_SUCCESS;
    237        1.1   thorpej }
    238        1.1   thorpej 
    239  1.11.50.1    martin static size_t
    240        1.8   tsutsui putfile(char *from, int to)
    241        1.1   thorpej {
    242        1.2    simonb 	int fd;
    243        1.3  gmcgarry 	struct stat statb;
    244        1.3  gmcgarry 	void *bp;
    245        1.3  gmcgarry 
    246  1.11.50.1    martin 	if ((fd = open(from, 0)) < 0)
    247  1.11.50.1    martin 		err(EXIT_FAILURE, "Unable to open file `%s'", from);
    248        1.3  gmcgarry 	fstat(fd, &statb);
    249        1.3  gmcgarry 	ld.address = htobe32(loadpoint);
    250        1.3  gmcgarry 	ld.count = htobe32(statb.st_size);
    251  1.11.50.1    martin 	if ((bp = malloc(statb.st_size)) == NULL)
    252  1.11.50.1    martin 		err(EXIT_FAILURE, "Can't allocate buffer");
    253  1.11.50.1    martin 	if (read(fd, bp, statb.st_size) < 0)
    254  1.11.50.1    martin 		err(EXIT_FAILURE, "Error reading from file `%s'", from);
    255        1.2    simonb 	(void)close(fd);
    256        1.1   thorpej 	write(to, &ld, sizeof(ld));
    257        1.3  gmcgarry 	write(to, bp, statb.st_size);
    258        1.3  gmcgarry 	free(bp);
    259  1.11.50.1    martin 
    260  1.11.50.1    martin 	return statb.st_size + sizeof(ld);
    261        1.1   thorpej }
    262        1.1   thorpej 
    263  1.11.50.1    martin static void
    264        1.2    simonb usage(void)
    265        1.1   thorpej {
    266        1.2    simonb 
    267  1.11.50.1    martin 	fprintf(stderr, "Usage:	%s -l <loadpoint> [-t <timestamp>] prog1 "
    268  1.11.50.1    martin 	    "[ prog2 ] outfile\n", getprogname());
    269  1.11.50.1    martin 	exit(EXIT_FAILURE);
    270        1.1   thorpej }
    271        1.1   thorpej 
    272  1.11.50.1    martin static char *
    273        1.2    simonb lifname(char *str)
    274        1.1   thorpej {
    275        1.1   thorpej 	static char lname[10] = "SYS_XXXXX";
    276        1.2    simonb 	char *cp;
    277        1.2    simonb 	int i;
    278        1.1   thorpej 
    279        1.2    simonb 	if ((cp = strrchr(str, '/')) != NULL)
    280        1.2    simonb 		str = ++cp;
    281        1.1   thorpej 	for (i = 4; i < 9; i++) {
    282  1.11.50.1    martin 		if (islower((unsigned char)*str))
    283  1.11.50.1    martin 			lname[i] = toupper((unsigned char)*str);
    284  1.11.50.1    martin 		else if (isalnum((unsigned char)*str) || *str == '_')
    285        1.1   thorpej 			lname[i] = *str;
    286        1.1   thorpej 		else
    287        1.1   thorpej 			break;
    288        1.1   thorpej 		str++;
    289        1.1   thorpej 	}
    290  1.11.50.1    martin 	for (; i < 10; i++)
    291        1.1   thorpej 		lname[i] = '\0';
    292  1.11.50.1    martin 
    293  1.11.50.1    martin 	return lname;
    294        1.1   thorpej }
    295        1.1   thorpej 
    296  1.11.50.1    martin static void
    297        1.2    simonb bcddate(char *name, char *toc)
    298        1.1   thorpej {
    299        1.1   thorpej 	struct stat statb;
    300        1.1   thorpej 	struct tm *tm;
    301        1.1   thorpej 
    302  1.11.50.1    martin 	if (repro_epoch != 0)
    303  1.11.50.1    martin 		tm = gmtime(&repro_epoch);
    304  1.11.50.1    martin 	else {
    305  1.11.50.1    martin 		stat(name, &statb);
    306  1.11.50.1    martin 		tm = localtime(&statb.st_ctime);
    307  1.11.50.1    martin 	}
    308  1.11.50.1    martin 	*toc++ = bintobcd(tm->tm_mon + 1);
    309  1.11.50.1    martin 	*toc++ = bintobcd(tm->tm_mday);
    310  1.11.50.1    martin 	*toc++ = bintobcd(tm->tm_year);
    311  1.11.50.1    martin 	*toc++ = bintobcd(tm->tm_hour);
    312  1.11.50.1    martin 	*toc++ = bintobcd(tm->tm_min);
    313  1.11.50.1    martin 	*toc   = bintobcd(tm->tm_sec);
    314        1.1   thorpej }
    315