/*
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wayne Knowles
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/* Open the input file and check it out */
if ((fd = open(bootstrap, O_RDONLY)) == -1)
FATALIO("open %s", bootstrap);
if (fstat(fd, &bootstrapsb) == -1)
FATALIO("fstat %s", bootstrap);
if (!S_ISREG(bootstrapsb.st_mode))
FATAL("%s must be a regular file", bootstrap);
/* read the file into the buffer */
len = read(fd, boot_code, bootstrapsb.st_size);
if (len == -1)
FATALIO("read %s", bootstrap);
else if (len != bootstrapsb.st_size)
FATAL("read %s: short read", bootstrap);
(void)close(fd);
if ((vfd = open(disk, O_WRONLY)) == -1)
FATALIO("open %s", disk);
if (verbose)
fprintf(stderr, "%s: writing volume header\n", disk);
len = pwrite(vfd, vhp, sizeof(*vhp), MIPS_VHSECTOR*512); /* XXX */
if (len == -1)
FATALIO("write %s", disk);
if (len != sizeof(*vhp))
FATAL("write %s: short write", disk);
(void) close(vfd);
}
/*
* Compute checksum for MIPS disk volume header
*
* Mips volume header checksum is the 32bit 2's complement sum
* of the entire volume header structure
*/
int
mipsvh_cksum(struct mips_volheader *vhp)
{
int i, *ptr;
int cksum = 0;
ptr = (int *)vhp;
i = sizeof(*vhp) / sizeof(*ptr);
while (i--)
cksum += *ptr++;
return cksum;
}
/*
* Locate the volume directory slot that matches a filename
*
* If the file entry cannot be found and create is non-zero the next
* empty slot is returned, otherwise return NULL
*/
static struct mips_voldir *
voldir_findfile(struct mips_volheader *vhp, const char *file, int create)
/* create: return unused entry if not found */
{
struct mips_voldir *vdp = vhp->vh_voldir;
int i;
for (i=0; i<MIPS_NVOLDIR; i++, vdp++) {
if (strcmp(vdp->vd_name, file) == 0)
return vdp;
}
if (create) {
vdp = vhp->vh_voldir;
for (i=0; i<MIPS_NVOLDIR; i++, vdp++)
if (vdp->vd_len == 0)
return vdp;
}
return NULL;
}