Allow building mkimg as cross-tool

For linux the mmap offset must also be page aligned, and we
need to disable macros like __FBSDID()

Change the linux osdep_uuidgen() to use more portable gettimeofday().

Reviewed by: marcel
This commit is contained in:
Simon J. Gerraty 2017-03-03 01:56:55 +00:00
parent 6d6e8a4a09
commit baf4abfc39
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=314577
6 changed files with 75 additions and 17 deletions

View file

@ -0,0 +1,27 @@
# $FreeBSD$
# allow building mkimg as a host tool.
MKIMG_SRC = ${SRCTOP}/usr.bin/mkimg
.PATH: ${MKIMG_SRC}
.if ${.MAKE.OS} == "Linux"
WARNS= 0
CFLAGS+= \
-D"__FBSDID(x)=" \
-D_XOPEN_SOURCE -D_GNU_SOURCE
.PATH: ${SRCTOP}/lib/libutil
SRCS+= expand_number.c
.endif
MK_STAGING=no
MK_TESTS= no
.include <${MKIMG_SRC}/Makefile>
.if ${.MAKE.OS} == "Linux"
LDADD+= -lbsd
.endif

View file

@ -0,0 +1,11 @@
# $FreeBSD$
# Autogenerated - do NOT edit!
DIRDEPS = \
.include <dirdeps.mk>
.if ${DEP_RELDIR} == ${_DEP_RELDIR}
# local dependencies - needed for -jN in clean tree
.endif

View file

@ -3,7 +3,7 @@
.include <src.opts.mk>
PROG= mkimg
SRCS= format.c image.c mkimg.c scheme.c uuid.c
SRCS+= format.c image.c mkimg.c scheme.c uuid.c
MAN= mkimg.1
MKIMG_VERSION=20161016

View file

@ -304,12 +304,20 @@ image_chunk_copyin(lba_t blk, void *buf, size_t sz, off_t ofs, int fd)
*/
static void *
image_file_map(int fd, off_t ofs, size_t sz)
image_file_map(int fd, off_t ofs, size_t sz, off_t *iofp)
{
void *ptr;
size_t unit;
int flags, prot;
off_t x;
/* On Linux anyway ofs must also be page aligned */
if ((x = (ofs % image_swap_pgsz)) != 0) {
ofs -= x;
sz += x;
*iofp = x;
} else
*iofp = 0;
unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
assert((unit & (unit - 1)) == 0);
@ -347,6 +355,7 @@ image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
size_t iosz;
ssize_t rdsz;
int error;
off_t iof;
/*
* This makes sure we're doing I/O in multiples of the page
@ -361,12 +370,12 @@ image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
swofs = image_swap_alloc(iosz);
if (swofs == -1LL)
return (errno);
buffer = image_file_map(image_swap_fd, swofs, iosz);
buffer = image_file_map(image_swap_fd, swofs, iosz, &iof);
if (buffer == NULL)
return (errno);
rdsz = read(fd, buffer, iosz);
rdsz = read(fd, &buffer[iof], iosz);
if (rdsz > 0)
error = image_chunk_copyin(blk, buffer, rdsz, swofs,
error = image_chunk_copyin(blk, &buffer[iof], rdsz, swofs,
image_swap_fd);
else if (rdsz < 0)
error = errno;
@ -389,8 +398,9 @@ image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
static int
image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep)
{
off_t cur, data, end, hole, pos;
void *buf;
off_t cur, data, end, hole, pos, iof;
void *mp;
char *buf;
uint64_t bytesize;
size_t iosz, sz;
int error;
@ -450,11 +460,12 @@ image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep)
sz = (pos - data > (off_t)iosz)
? iosz : (size_t)(pos - data);
buf = image_file_map(fd, data, sz);
if (buf != NULL) {
buf = mp = image_file_map(fd, data, sz, &iof);
if (mp != NULL) {
buf += iof;
error = image_chunk_copyin(blk, buf,
sz, data, fd);
image_file_unmap(buf, sz);
image_file_unmap(mp, sz);
} else
error = errno;
@ -564,19 +575,22 @@ image_copyout_zeroes(int fd, size_t count)
static int
image_copyout_file(int fd, size_t size, int ifd, off_t iofs)
{
void *buf;
void *mp;
char *buf;
size_t iosz, sz;
int error;
off_t iof;
iosz = secsz * image_swap_pgsz;
while (size > 0) {
sz = (size > iosz) ? iosz : size;
buf = image_file_map(ifd, iofs, sz);
buf = mp = image_file_map(ifd, iofs, sz, &iof);
if (buf == NULL)
return (errno);
buf += iof;
error = image_copyout_memory(fd, sz, buf);
image_file_unmap(buf, sz);
image_file_unmap(mp, sz);
if (error)
return (error);
size -= sz;

View file

@ -104,4 +104,10 @@ typedef struct mkimg_uuid mkimg_uuid_t;
void mkimg_uuid(mkimg_uuid_t *);
void mkimg_uuid_enc(void *, const mkimg_uuid_t *);
#ifdef __linux__
# if !defined(__unused)
# define __unused __attribute__ ((__unused__))
# endif
#endif
#endif /* _MKIMG_MKIMG_H_ */

View file

@ -66,16 +66,16 @@ osdep_uuidgen(mkimg_uuid_t *uuid)
static void
osdep_uuidgen(mkimg_uuid_t *uuid)
{
struct timespec tp;
struct timeval tv;
uint64_t time = 0x01B21DD213814000LL;
u_int i;
uint16_t seq;
if (clock_gettime(CLOCK_REALTIME, &tp) == -1)
if (gettimeofday(&tv, NULL) == -1)
abort();
time += (uint64_t)tp.tv_sec * 10000000LL;
time += tp.tv_nsec / 100;
time += (uint64_t)tv.tv_sec * 10000000LL;
time += tv.tv_usec * 10;
uuid->time_low = (uint32_t)time;
uuid->time_mid = (uint16_t)(time >> 32);