xref: /csrg-svn/sys/pmax/stand/mkboottape.c (revision 56526)
153090Sbostic /*-
253090Sbostic  * Copyright (c) 1992 The Regents of the University of California.
353090Sbostic  * All rights reserved.
453090Sbostic  *
553090Sbostic  * This code is derived from software contributed to Berkeley by
653090Sbostic  * Ralph Campbell.
753090Sbostic  *
853090Sbostic  * %sccs.include.redist.c%
953090Sbostic  *
10*56526Sbostic  *	@(#)mkboottape.c	7.5 (Berkeley) 10/11/92
1153090Sbostic  */
1253090Sbostic 
1353090Sbostic #include <sys/param.h>
1453091Sbostic #include <sys/stat.h>
1553090Sbostic #include <sys/exec.h>
16*56526Sbostic 
1753091Sbostic #include <errno.h>
1853091Sbostic #include <stdio.h>
1953091Sbostic #include <stdlib.h>
2053091Sbostic #include <unistd.h>
2153091Sbostic #include <fcntl.h>
2253091Sbostic #include <string.h>
2353090Sbostic 
24*56526Sbostic #include <pmax/stand/dec_boot.h>
2553206Sralph 
2653091Sbostic void err __P((const char *, ...));
2753091Sbostic void usage __P((void));
2853090Sbostic 
2953206Sralph struct	Dec_DiskBoot decBootInfo;
3053206Sralph 
3153090Sbostic /*
3253090Sbostic  * This program takes a kernel and the name of the special device file that
3353090Sbostic  * has the mini-root file system stored on it and creates a boot tape.
3453090Sbostic  * The -b option makes a bootfile that can load the kernel and mini-root
3553090Sbostic  * over the network using the 'boot 6/tftp/filename -m' PROM command.
3653090Sbostic  *
3753090Sbostic  * usage: mkboottape [-b] tapedev vmunix minirootdev size
3853090Sbostic  */
3953091Sbostic int
4053090Sbostic main(argc, argv)
4153090Sbostic 	int argc;
4253090Sbostic 	char *argv[];
4353090Sbostic {
4453090Sbostic 	register int i, n;
4553090Sbostic 	ProcSectionHeader shdr;
4653090Sbostic 	struct exec aout;
4753090Sbostic 	long loadAddr;
4853090Sbostic 	long execAddr;
4953090Sbostic 	long textoff;
5053090Sbostic 	long length;
5153090Sbostic 	long rootsize;
5253091Sbostic 	int ifd, ofd, rfd;
5353091Sbostic 	int makebootfile;
5453091Sbostic 	int nsectors;
5553091Sbostic 	char block[DEV_BSIZE];
5653090Sbostic 
5753091Sbostic 	makebootfile = 0;
5853091Sbostic 	while ((i = getopt(argc, argv, "b")) != EOF)
5953091Sbostic 		switch(i) {
6053091Sbostic 		case 'b':
6153091Sbostic 			makebootfile = 1;
6253091Sbostic 			break;
6353091Sbostic 		case '?':
6453091Sbostic 		default:
6553091Sbostic 			usage();
6653091Sbostic 		}
6753091Sbostic 	argc -= optind;
6853091Sbostic 	argv += optind;
6953091Sbostic 
7053092Sbostic 	if (argc != 4)
7153090Sbostic 		usage();
7253091Sbostic 
7353090Sbostic 	if (makebootfile)
7453206Sralph 		ofd = open(argv[0], O_CREAT|O_TRUNC|O_WRONLY, DEFFILEMODE);
7553090Sbostic 	else
7653206Sralph 		ofd = open(argv[0], O_RDWR, 0);
7753091Sbostic 	if (ofd < 0)
7853206Sralph deverr:		err("%s: %s", argv[0], strerror(errno));
7953090Sbostic 
8053206Sralph 	if ((ifd = open(argv[1], O_RDONLY, 0)) < 0)
8153206Sralph bootferr:	err("%s: %s", argv[1], strerror(errno));
8253091Sbostic 
8353206Sralph 	if ((rfd = open(argv[2], O_RDONLY, 0)) < 0)
8453206Sralph rooterr:	err("%s: %s", argv[2], strerror(errno));
8553091Sbostic 
8653206Sralph 	rootsize = atoi(argv[3]);
8753091Sbostic 
8853090Sbostic 	/*
8953090Sbostic 	 * Check for exec header and skip to code segment.
9053090Sbostic 	 */
9153091Sbostic 	if (read(ifd, &aout, sizeof(aout)) != sizeof(aout) ||
9253091Sbostic 	    aout.ex_fhdr.magic != COFF_MAGIC || aout.a_magic != OMAGIC)
9353091Sbostic 		err("need impure text format (OMAGIC) file");
9453091Sbostic 
9553090Sbostic 	loadAddr = aout.ex_aout.codeStart;
9653090Sbostic 	execAddr = aout.a_entry;
9753090Sbostic 	length = aout.a_text + aout.a_data;
9853090Sbostic 	textoff = N_TXTOFF(aout);
9953091Sbostic 	(void)printf("Input file is COFF format\n");
10053091Sbostic 	(void)printf("load %x, start %x, len %d\n", loadAddr, execAddr, length);
10153090Sbostic 
10253090Sbostic 	/*
10353090Sbostic 	 * Compute size of boot program rounded to page size + mini-root size.
10453090Sbostic 	 */
10553090Sbostic 	nsectors = (((length + aout.a_bss + NBPG - 1) & ~(NBPG - 1)) >>
10653206Sralph 		DEV_BSHIFT) + rootsize;
10753090Sbostic 
10853090Sbostic 	if (makebootfile) {
10953090Sbostic 		/*
11053090Sbostic 		 * Write modified ECOFF header.
11153090Sbostic 		 */
11253090Sbostic 		aout.ex_fhdr.numSections = 1;
11353090Sbostic 		aout.ex_fhdr.numSyms = 0;
11453090Sbostic 		aout.ex_fhdr.symPtr = 0;
11553090Sbostic 		aout.a_text = nsectors << DEV_BSHIFT;
11653090Sbostic 		aout.a_data = 0;
11753090Sbostic 		aout.a_bss = 0;
11853090Sbostic 		aout.ex_aout.heapStart = aout.ex_aout.bssStart =
11953090Sbostic 			aout.ex_aout.codeStart + aout.a_text;
12053090Sbostic 		if (write(ofd, (char *)&aout, sizeof(aout)) != sizeof(aout))
12153090Sbostic 			goto deverr;
12253090Sbostic 		strncpy(shdr.name, ".text", sizeof(shdr.name));
12353090Sbostic 		shdr.physAddr = shdr.virtAddr = loadAddr;
12453090Sbostic 		shdr.size = aout.a_text;
12553090Sbostic 		shdr.sectionPtr = n = (sizeof(aout) + sizeof(shdr) + 15) & ~15;
12653090Sbostic 		shdr.relocPtr = 0;
12753090Sbostic 		shdr.lnnoPtr = 0;
12853090Sbostic 		shdr.numReloc = 0;
12953090Sbostic 		shdr.numLnno = 0;
13053090Sbostic 		shdr.flags = 0x20;
13153090Sbostic 		if (write(ofd, (char *)&shdr, sizeof(shdr)) != sizeof(shdr))
13253090Sbostic 			goto deverr;
13353090Sbostic 		n -= sizeof(aout) + sizeof(shdr);
13453090Sbostic 		if (write(ofd, block, n) != n)
13553090Sbostic 			goto deverr;
13653090Sbostic 	} else {
13753090Sbostic 		/*
13853090Sbostic 		 * Write the boot information block.
13953090Sbostic 		 */
14053090Sbostic 		decBootInfo.magic = DEC_BOOT_MAGIC;
14153090Sbostic 		decBootInfo.mode = 0;
14253090Sbostic 		decBootInfo.loadAddr = loadAddr;
14353090Sbostic 		decBootInfo.execAddr = execAddr;
14453090Sbostic 		decBootInfo.map[0].numBlocks = nsectors;
14553090Sbostic 		decBootInfo.map[0].startBlock = 1;
14653090Sbostic 		decBootInfo.map[1].numBlocks = 0;
14753090Sbostic 		if (write(ofd, (char *)&decBootInfo, sizeof(decBootInfo)) !=
14853090Sbostic 		    sizeof(decBootInfo))
14953090Sbostic 			goto deverr;
15053090Sbostic 	}
15153090Sbostic 	/* seek to start of text */
15253091Sbostic 	if (lseek(ifd, textoff, SEEK_SET) < 0)
15353090Sbostic 		goto bootferr;
15453090Sbostic 
15553090Sbostic 	/*
15653090Sbostic 	 * Write the remaining code to the correct place on the tape.
15753090Sbostic 	 */
15853091Sbostic 	for (i = length; i > 0; i -= n) {
15953090Sbostic 		n = DEV_BSIZE;
16053090Sbostic 		if (n > i)
16153090Sbostic 			n = i;
16253090Sbostic 		if (read(ifd, block, n) != n)
16353090Sbostic 			goto bootferr;
16453206Sralph 		if (write(ofd, block, DEV_BSIZE) != DEV_BSIZE)
16553090Sbostic 			goto deverr;
16653090Sbostic 	}
16753090Sbostic 
16853090Sbostic 	/*
16953090Sbostic 	 * Pad the boot file with zeros to the start of the mini-root.
17053090Sbostic 	 */
17153090Sbostic 	bzero(block, DEV_BSIZE);
17253206Sralph 	i = ((nsectors - rootsize) << DEV_BSHIFT) -
17353206Sralph 		((length + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1));
17453206Sralph 	n = DEV_BSIZE;
17553206Sralph 	for (; i > 0; i -= n) {
17653090Sbostic 		if (write(ofd, block, n) != n)
17753090Sbostic 			goto deverr;
17853090Sbostic 	}
17953090Sbostic 
18053090Sbostic 	/*
18153090Sbostic 	 * Write the mini-root to tape.
18253090Sbostic 	 */
18353091Sbostic 	for (i = rootsize; i > 0; i--) {
18453091Sbostic 		if (read(rfd, block, DEV_BSIZE) != DEV_BSIZE)
18553091Sbostic 			goto rooterr;
18653090Sbostic 		if (write(ofd, block, DEV_BSIZE) != DEV_BSIZE)
18753090Sbostic 			goto deverr;
18853090Sbostic 	}
18953090Sbostic 
19053091Sbostic 	(void)printf("mkboottape: wrote %d sectors\n", nsectors);
19153090Sbostic 	exit(0);
19253090Sbostic }
19353090Sbostic 
19453091Sbostic void
19553090Sbostic usage()
19653090Sbostic {
19753091Sbostic 	(void)fprintf(stderr,
19853091Sbostic 	    "usage: mkboottape [-b] tapedev vmunix minirootdev size\n");
19953090Sbostic 	exit(1);
20053090Sbostic }
20153091Sbostic 
20253091Sbostic #if __STDC__
20353091Sbostic #include <stdarg.h>
20453091Sbostic #else
20553091Sbostic #include <varargs.h>
20653091Sbostic #endif
20753091Sbostic 
20853091Sbostic void
20953091Sbostic #if __STDC__
21053091Sbostic err(const char *fmt, ...)
21153091Sbostic #else
21253091Sbostic err(fmt, va_alist)
21353091Sbostic 	char *fmt;
21453091Sbostic         va_dcl
21553091Sbostic #endif
21653091Sbostic {
21753091Sbostic 	va_list ap;
21853091Sbostic #if __STDC__
21953091Sbostic 	va_start(ap, fmt);
22053091Sbostic #else
22153091Sbostic 	va_start(ap);
22253091Sbostic #endif
22353091Sbostic 	(void)fprintf(stderr, "mkboottape: ");
22453091Sbostic 	(void)vfprintf(stderr, fmt, ap);
22553091Sbostic 	va_end(ap);
22653091Sbostic 	(void)fprintf(stderr, "\n");
22753091Sbostic 	exit(1);
22853091Sbostic 	/* NOTREACHED */
22953091Sbostic }
230