2001-05-27 09:35:11 +04:00
|
|
|
/* $NetBSD: mkboot.c,v 1.3 2001/05/27 05:35:14 gmcgarry Exp $
|
1997-02-04 06:51:37 +03:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
/*
|
1994-01-26 05:38:16 +03:00
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-05-13 17:56:20 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-10-26 10:22:45 +03:00
|
|
|
* @(#)mkboot.c 8.1 (Berkeley) 7/15/93
|
1993-05-13 17:56:20 +04:00
|
|
|
*/
|
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
#ifndef lint
|
2001-01-02 07:14:33 +03:00
|
|
|
__COPYRIGHT(
|
1994-01-26 05:38:16 +03:00
|
|
|
"@(#) Copyright (c) 1990, 1993\n\
|
2001-01-02 07:14:33 +03:00
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-10-26 10:22:45 +03:00
|
|
|
#ifdef notdef
|
|
|
|
static char sccsid[] = "@(#)mkboot.c 7.2 (Berkeley) 12/16/90";
|
|
|
|
#endif
|
2001-05-27 09:35:11 +04:00
|
|
|
__RCSID("$NetBSD: mkboot.c,v 1.3 2001/05/27 05:35:14 gmcgarry Exp $");
|
1993-05-13 17:56:20 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-01-26 05:38:16 +03:00
|
|
|
#include <sys/param.h>
|
1993-05-13 17:56:20 +04:00
|
|
|
#include <sys/file.h>
|
2001-01-02 07:14:33 +03:00
|
|
|
#include <sys/stat.h>
|
2001-05-27 09:35:11 +04:00
|
|
|
#include <sys/endian.h>
|
1994-01-26 05:38:16 +03:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
#include <ctype.h>
|
2001-01-02 07:14:33 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "volhdr.h"
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-01-26 05:38:16 +03:00
|
|
|
#define LIF_NUMDIR 8
|
|
|
|
|
|
|
|
#define LIF_VOLSTART 0
|
|
|
|
#define LIF_VOLSIZE sizeof(struct lifvol)
|
|
|
|
#define LIF_DIRSTART 512
|
|
|
|
#define LIF_DIRSIZE (LIF_NUMDIR * sizeof(struct lifdir))
|
|
|
|
#define LIF_FILESTART 8192
|
|
|
|
|
|
|
|
#define btolifs(b) (((b) + (SECTSIZE - 1)) / SECTSIZE)
|
|
|
|
#define lifstob(s) ((s) * SECTSIZE)
|
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
int lpflag;
|
|
|
|
int loadpoint;
|
2001-05-27 09:35:11 +04:00
|
|
|
struct load ld;
|
2001-01-02 07:14:33 +03:00
|
|
|
struct lifvol lifv;
|
|
|
|
struct lifdir lifd[LIF_NUMDIR];
|
|
|
|
|
|
|
|
int main(int, char **);
|
|
|
|
void bcddate(char *, char *);
|
|
|
|
char *lifname(char *);
|
2001-05-27 09:35:11 +04:00
|
|
|
int putfile(char *, int);
|
2001-01-02 07:14:33 +03:00
|
|
|
void usage(void);
|
1993-05-13 17:56:20 +04:00
|
|
|
|
1994-01-26 05:38:16 +03:00
|
|
|
/*
|
|
|
|
* Old Format:
|
|
|
|
* sector 0: LIF volume header (40 bytes)
|
|
|
|
* sector 1: <unused>
|
|
|
|
* sector 2: LIF directory (8 x 32 == 256 bytes)
|
|
|
|
* sector 3-: LIF file 0, LIF file 1, etc.
|
|
|
|
* where sectors are 256 bytes.
|
|
|
|
*
|
|
|
|
* New Format:
|
|
|
|
* sector 0: LIF volume header (40 bytes)
|
|
|
|
* sector 1: <unused>
|
|
|
|
* sector 2: LIF directory (8 x 32 == 256 bytes)
|
|
|
|
* sector 3: <unused>
|
|
|
|
* sector 4-31: disklabel (~300 bytes right now)
|
|
|
|
* sector 32-: LIF file 0, LIF file 1, etc.
|
|
|
|
*/
|
2001-01-02 07:14:33 +03:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
2001-01-02 07:14:33 +03:00
|
|
|
char *n1, *n2, *n3;
|
|
|
|
int n, to;
|
2001-05-27 09:35:11 +04:00
|
|
|
int count;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
if (argc == 0)
|
1993-05-13 17:56:20 +04:00
|
|
|
usage();
|
2001-01-02 07:14:33 +03:00
|
|
|
if (!strcmp(argv[0], "-l")) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
if (argc == 0)
|
1993-05-13 17:56:20 +04:00
|
|
|
usage();
|
2001-01-02 07:14:33 +03:00
|
|
|
sscanf(argv[0], "0x%x", &loadpoint);
|
1993-05-13 17:56:20 +04:00
|
|
|
lpflag++;
|
2001-01-02 07:14:33 +03:00
|
|
|
argv++;
|
|
|
|
argc--;
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
2001-05-27 09:35:11 +04:00
|
|
|
if (!lpflag || argc == 0)
|
1993-05-13 17:56:20 +04:00
|
|
|
usage();
|
2001-01-02 07:14:33 +03:00
|
|
|
n1 = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
if (argc == 0)
|
1993-05-13 17:56:20 +04:00
|
|
|
usage();
|
2001-01-02 07:14:33 +03:00
|
|
|
if (argc > 1) {
|
|
|
|
n2 = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
if (argc > 1) {
|
|
|
|
n3 = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
1994-01-26 05:38:16 +03:00
|
|
|
} else
|
2001-01-02 07:14:33 +03:00
|
|
|
n3 = NULL;
|
1993-05-13 17:56:20 +04:00
|
|
|
} else
|
2001-01-02 07:14:33 +03:00
|
|
|
n2 = n3 = NULL;
|
2001-05-27 09:35:11 +04:00
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
to = open(argv[0], O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
1993-05-13 17:56:20 +04:00
|
|
|
if (to < 0) {
|
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* clear possibly unused directory entries */
|
2001-05-27 09:35:11 +04:00
|
|
|
strncpy(lifd[1].dir_name, " ", 10);
|
|
|
|
lifd[1].dir_type = htobe16(-1);
|
|
|
|
lifd[1].dir_addr = htobe32(0);
|
|
|
|
lifd[1].dir_length = htobe32(0);
|
|
|
|
lifd[1].dir_flag = htobe16(0xFF);
|
|
|
|
lifd[1].dir_exec = htobe32(0);
|
1993-05-13 17:56:20 +04:00
|
|
|
lifd[7] = lifd[6] = lifd[5] = lifd[4] = lifd[3] = lifd[2] = lifd[1];
|
|
|
|
/* record volume info */
|
2001-05-27 09:35:11 +04:00
|
|
|
lifv.vol_id = htobe16(VOL_ID);
|
1993-05-13 17:56:20 +04:00
|
|
|
strncpy(lifv.vol_label, "BOOT43", 6);
|
2001-05-27 09:35:11 +04:00
|
|
|
lifv.vol_addr = htobe32(btolifs(LIF_DIRSTART));
|
|
|
|
lifv.vol_oct = htobe16(VOL_OCT);
|
|
|
|
lifv.vol_dirsize = htobe32(btolifs(LIF_DIRSIZE));
|
|
|
|
lifv.vol_version = htobe16(1);
|
1993-05-13 17:56:20 +04:00
|
|
|
/* output bootfile one */
|
2001-01-02 07:14:33 +03:00
|
|
|
lseek(to, LIF_FILESTART, SEEK_SET);
|
2001-05-27 09:35:11 +04:00
|
|
|
count = putfile(n1, to);
|
|
|
|
n = btolifs(count);
|
1993-05-13 17:56:20 +04:00
|
|
|
strcpy(lifd[0].dir_name, lifname(n1));
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[0].dir_type = htobe16(DIR_TYPE);
|
|
|
|
lifd[0].dir_addr = htobe32(btolifs(LIF_FILESTART));
|
|
|
|
lifd[0].dir_length = htobe32(n);
|
2001-01-02 07:14:33 +03:00
|
|
|
bcddate(n1, lifd[0].dir_toc);
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[0].dir_flag = htobe16(DIR_FLAG);
|
|
|
|
lifd[0].dir_exec = htobe32(loadpoint);
|
|
|
|
lifv.vol_length = htobe32(be32toh(lifd[0].dir_addr) +
|
|
|
|
be32toh(lifd[0].dir_length));
|
1993-05-13 17:56:20 +04:00
|
|
|
/* if there is an optional second boot program, output it */
|
2001-01-02 07:14:33 +03:00
|
|
|
if (n2) {
|
|
|
|
lseek(to, LIF_FILESTART+lifstob(n), SEEK_SET);
|
2001-05-27 09:35:11 +04:00
|
|
|
count = putfile(n2, to);
|
|
|
|
n = btolifs(count);
|
1993-05-13 17:56:20 +04:00
|
|
|
strcpy(lifd[1].dir_name, lifname(n2));
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[1].dir_type = htobe16(DIR_TYPE);
|
|
|
|
lifd[1].dir_addr = htobe32(lifv.vol_length);
|
|
|
|
lifd[1].dir_length = htobe32(n);
|
2001-01-02 07:14:33 +03:00
|
|
|
bcddate(n2, lifd[1].dir_toc);
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[1].dir_flag = htobe32(DIR_FLAG);
|
|
|
|
lifd[1].dir_exec = htobe32(loadpoint);
|
|
|
|
lifv.vol_length = htobe32(be32toh(lifd[1].dir_addr) +
|
|
|
|
be32toh(lifd[1].dir_length));
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
1994-01-26 05:38:16 +03:00
|
|
|
/* ditto for three */
|
2001-01-02 07:14:33 +03:00
|
|
|
if (n3) {
|
2001-05-27 09:35:11 +04:00
|
|
|
lseek(to, LIF_FILESTART+lifstob(lifd[0].dir_length+n),
|
|
|
|
SEEK_SET);
|
|
|
|
count = putfile(n3, to);
|
|
|
|
n = btolifs(count);
|
1994-01-26 05:38:16 +03:00
|
|
|
strcpy(lifd[2].dir_name, lifname(n3));
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[2].dir_type = htobe16(DIR_TYPE);
|
|
|
|
lifd[2].dir_addr = htobe32(lifv.vol_length);
|
|
|
|
lifd[2].dir_length = htobe32(n);
|
2001-01-02 07:14:33 +03:00
|
|
|
bcddate(n3, lifd[2].dir_toc);
|
2001-05-27 09:35:11 +04:00
|
|
|
lifd[2].dir_flag = htobe32(DIR_FLAG);
|
|
|
|
lifd[2].dir_exec = htobe32(loadpoint);
|
|
|
|
lifv.vol_length = htobe32(be32toh(lifd[2].dir_addr) +
|
|
|
|
be32toh(lifd[2].dir_length));
|
1994-01-26 05:38:16 +03:00
|
|
|
}
|
1993-05-13 17:56:20 +04:00
|
|
|
/* output volume/directory header info */
|
2001-01-02 07:14:33 +03:00
|
|
|
lseek(to, LIF_VOLSTART, SEEK_SET);
|
1994-01-26 05:38:16 +03:00
|
|
|
write(to, &lifv, LIF_VOLSIZE);
|
2001-01-02 07:14:33 +03:00
|
|
|
lseek(to, LIF_DIRSTART, SEEK_SET);
|
1994-01-26 05:38:16 +03:00
|
|
|
write(to, lifd, LIF_DIRSIZE);
|
1993-05-13 17:56:20 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2001-05-27 09:35:11 +04:00
|
|
|
int
|
1993-05-13 17:56:20 +04:00
|
|
|
putfile(from, to)
|
2001-01-02 07:14:33 +03:00
|
|
|
char *from;
|
|
|
|
int to;
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
2001-01-02 07:14:33 +03:00
|
|
|
int fd;
|
2001-05-27 09:35:11 +04:00
|
|
|
struct stat statb;
|
|
|
|
int nr;
|
|
|
|
void *bp;
|
2001-01-02 07:14:33 +03:00
|
|
|
|
2001-05-27 09:35:11 +04:00
|
|
|
if ((fd = open(from, 0)) < 0) {
|
|
|
|
printf("error: unable to open file %s\n", from);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fstat(fd, &statb);
|
|
|
|
ld.address = htobe32(loadpoint);
|
|
|
|
ld.count = htobe32(statb.st_size);
|
|
|
|
bp = malloc(statb.st_size);
|
|
|
|
if ((nr = read(fd, bp, statb.st_size)) < 0) {
|
|
|
|
printf("error: reading from file %s\n", from);
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-01-02 07:14:33 +03:00
|
|
|
(void)close(fd);
|
1993-05-13 17:56:20 +04:00
|
|
|
write(to, &ld, sizeof(ld));
|
2001-05-27 09:35:11 +04:00
|
|
|
write(to, bp, statb.st_size);
|
|
|
|
free(bp);
|
|
|
|
return (statb.st_size + sizeof(ld));
|
1993-05-13 17:56:20 +04:00
|
|
|
}
|
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
void
|
|
|
|
usage(void)
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
2001-01-02 07:14:33 +03:00
|
|
|
|
1993-05-13 17:56:20 +04:00
|
|
|
fprintf(stderr,
|
2001-05-27 09:35:11 +04:00
|
|
|
"usage: mkboot -l loadpoint prog1 [ prog2 ] outfile\n");
|
1993-05-13 17:56:20 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-01-02 07:14:33 +03:00
|
|
|
lifname(char *str)
|
1993-05-13 17:56:20 +04:00
|
|
|
{
|
|
|
|
static char lname[10] = "SYS_XXXXX";
|
2001-01-02 07:14:33 +03:00
|
|
|
char *cp;
|
|
|
|
int i;
|
1993-05-13 17:56:20 +04:00
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
if ((cp = strrchr(str, '/')) != NULL)
|
|
|
|
str = ++cp;
|
1993-05-13 17:56:20 +04:00
|
|
|
for (i = 4; i < 9; i++) {
|
|
|
|
if (islower(*str))
|
|
|
|
lname[i] = toupper(*str);
|
|
|
|
else if (isalnum(*str) || *str == '_')
|
|
|
|
lname[i] = *str;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
for ( ; i < 10; i++)
|
|
|
|
lname[i] = '\0';
|
|
|
|
return(lname);
|
|
|
|
}
|
1994-01-26 05:38:16 +03:00
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
void
|
|
|
|
bcddate(char *name, char *toc)
|
1994-01-26 05:38:16 +03:00
|
|
|
{
|
|
|
|
struct stat statb;
|
|
|
|
struct tm *tm;
|
|
|
|
|
2001-01-02 07:14:33 +03:00
|
|
|
stat(name, &statb);
|
1994-01-26 05:38:16 +03:00
|
|
|
tm = localtime(&statb.st_ctime);
|
|
|
|
*toc = ((tm->tm_mon+1) / 10) << 4;
|
|
|
|
*toc++ |= (tm->tm_mon+1) % 10;
|
|
|
|
*toc = (tm->tm_mday / 10) << 4;
|
|
|
|
*toc++ |= tm->tm_mday % 10;
|
|
|
|
*toc = (tm->tm_year / 10) << 4;
|
|
|
|
*toc++ |= tm->tm_year % 10;
|
|
|
|
*toc = (tm->tm_hour / 10) << 4;
|
|
|
|
*toc++ |= tm->tm_hour % 10;
|
|
|
|
*toc = (tm->tm_min / 10) << 4;
|
|
|
|
*toc++ |= tm->tm_min % 10;
|
|
|
|
*toc = (tm->tm_sec / 10) << 4;
|
|
|
|
*toc |= tm->tm_sec % 10;
|
|
|
|
}
|