Allow bootstrapping makefs on older FreeBSD hosts and Linux/macOS

In order to do so we need to install the msdosfs headers to the bootstrap
sysroot and avoid includes of kernel headers that may not exist on every
host (e.g. sys/lockmgr.h). This change should allow bootstrapping of makefs
on FreeBSD 11+ as well as Linux and macOS.

We also have to avoid using the IO_SYNC macro since that may not be
available. In makefs it is only used to switch between calling
bwrite() and bdwrite() which both call the same function. Therefore we
can simply always call bwrite().

For our CheriBSD builds we always bootstrap makefs by setting
LOCAL_XTOOL_DIRS='lib/libnetbsd usr.sbin/makefs' and use the makefs binary
from the build tree to create a bootable disk image.

Reviewed By:	brooks
Differential Revision: https://reviews.freebsd.org/D23201
This commit is contained in:
Alex Richardson 2020-01-27 12:02:41 +00:00
parent 9ea85092d9
commit 162ae9c834
5 changed files with 71 additions and 16 deletions

View File

@ -31,10 +31,15 @@ static const char rcsid[] =
#endif /* not lint */
#include <sys/param.h>
#ifdef MAKEFS
/* In the makefs case we only want struct disklabel */
#include <sys/disk/bsd.h>
#else
#include <sys/fdcio.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/mount.h>
#endif
#include <sys/stat.h>
#include <sys/time.h>
@ -285,14 +290,18 @@ mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
if (!S_ISREG(sb.st_mode))
warnx("warning, %s is not a regular file", fname);
} else {
#ifndef MAKEFS
#ifdef MAKEFS
errx(1, "o.create_size must be set!");
#else
if (!S_ISCHR(sb.st_mode))
warnx("warning, %s is not a character device", fname);
#endif
}
#ifndef MAKEFS
if (!o.no_create)
if (check_mounted(fname, sb.st_mode) == -1)
goto done;
#endif
if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
warnx("cannot seek to %jd", (intmax_t)o.offset);
goto done;
@ -621,10 +630,12 @@ mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
bpb.bpbBigFATsecs) * bpb.bpbFATs;
memset(&si_sa, 0, sizeof(si_sa));
si_sa.sa_handler = infohandler;
#ifdef SIGINFO
if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
warn("sigaction SIGINFO");
goto done;
}
#endif
for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
if (got_siginfo) {
fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
@ -766,6 +777,11 @@ mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
static int
check_mounted(const char *fname, mode_t mode)
{
/*
* If getmntinfo() is not available (e.g. Linux) don't check. This should
* not be a problem since we will only be using makefs to create images.
*/
#if !defined(MAKEFS)
struct statfs *mp;
const char *s1, *s2;
size_t len;
@ -790,6 +806,7 @@ check_mounted(const char *fname, mode_t mode)
return -1;
}
}
#endif
return 0;
}
@ -811,6 +828,23 @@ getstdfmt(const char *fmt, struct bpb *bpb)
return 0;
}
static void
compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
{
struct stat st;
off_t ms;
if (fstat(fd, &st))
err(1, "cannot get disk size");
if (!S_ISREG(st.st_mode))
errx(1, "%s is not a regular file", fname);
ms = st.st_size;
lp->d_secsize = 512;
lp->d_nsectors = 63;
lp->d_ntracks = 255;
lp->d_secperunit = ms / lp->d_secsize;
}
/*
* Get disk slice, partition, and geometry information.
*/
@ -819,8 +853,10 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
struct bpb *bpb)
{
struct disklabel *lp, dlp;
off_t hs = 0;
#ifndef MAKEFS
off_t ms;
struct fd_type type;
off_t ms, hs = 0;
lp = NULL;
@ -832,16 +868,8 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
/* Maybe it's a floppy drive */
if (lp == NULL) {
if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
struct stat st;
if (fstat(fd, &st))
err(1, "cannot get disk size");
/* create a fake geometry for a file image */
ms = st.st_size;
dlp.d_secsize = 512;
dlp.d_nsectors = 63;
dlp.d_ntracks = 255;
dlp.d_secperunit = ms / dlp.d_secsize;
compute_geometry_from_file(fd, fname, &dlp);
lp = &dlp;
} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
dlp.d_secsize = 128 << type.secsize;
@ -881,6 +909,11 @@ getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
lp = &dlp;
}
#else
/* In the makefs case we only support image files: */
compute_geometry_from_file(fd, fname, &dlp);
lp = &dlp;
#endif
if (bpb->bpbBytesPerSec == 0) {
if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)

View File

@ -56,8 +56,10 @@
#if defined (_KERNEL) || defined(MAKEFS)
#include <sys/types.h>
#ifndef MAKEFS
#include <sys/lock.h>
#include <sys/lockmgr.h>
#endif
#include <sys/tree.h>
#ifdef MALLOC_DECLARE
@ -110,7 +112,9 @@ struct msdosfsmount {
void *pm_w2u; /* Unicode->Local iconv handle */
void *pm_u2d; /* Unicode->DOS iconv handle */
void *pm_d2u; /* DOS->Local iconv handle */
#ifndef MAKEFS
struct lock pm_fatlock; /* lockmgr protecting allocations */
#endif
};
/*

View File

@ -4,11 +4,16 @@
LIB= egacy
SRC=
INCSGROUPS= INCS SYSINCS CASPERINC
INCSGROUPS= INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
INCS=
SYSINCSDIR= ${INCLUDEDIR}/sys
CASPERINCDIR= ${INCLUDEDIR}/casper
# Also add ufs/ffs/msdosfs/disk headers to allow building makefs as a bootstrap tool
UFSINCSDIR= ${INCLUDEDIR}/ufs/ufs
FFSINCSDIR= ${INCLUDEDIR}/ufs/ffs
MSDOSFSINCSDIR= ${INCLUDEDIR}/fs/msdosfs
DISKINCSDIR= ${INCLUDEDIR}/sys/disk
BOOTSTRAPPING?= 0
@ -70,6 +75,19 @@ SRCS= dummy.c
SUBDIR= cross-build
.endif
# To allow bootstrapping makefs on FreeBSD 11 or non-FreeBSD systems:
UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dinode.h
UFSINCS+= ${SRCTOP}/sys/ufs/ufs/dir.h
FFSINCS+= ${SRCTOP}/sys/ufs/ffs/fs.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bootsect.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/bpb.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/denode.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/direntry.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/fat.h
MSDOSFSINCS+= ${SRCTOP}/sys/fs/msdosfs/msdosfsmount.h
DISKINCS+= ${SRCTOP}/sys/sys/disk/bsd.h
# Needed to build config (since it uses libnv)
SYSINCS+= ${SRCTOP}/sys/sys/nv.h ${SRCTOP}/sys/sys/cnv.h \
${SRCTOP}/sys/sys/dnv.h

View File

@ -54,7 +54,6 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <stdbool.h>
#include <stdio.h>
@ -288,10 +287,7 @@ detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
return (error);
}
memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
if (flags & IO_SYNC)
bwrite(bp);
else
bdwrite(bp);
}
}

View File

@ -103,7 +103,11 @@ msdosfs_times(struct denode *dep, const struct stat *st)
if (stampst.st_ino)
st = &stampst;
#ifdef HAVE_STRUCT_STAT_BIRTHTIME
unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
#else
unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
#endif
unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
}