Skip to content

Commit

Permalink
Update mkimg(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
laffer1 committed Nov 13, 2020
1 parent cb86cf4 commit 0aebfeb
Show file tree
Hide file tree
Showing 23 changed files with 644 additions and 242 deletions.
11 changes: 8 additions & 3 deletions usr.bin/mkimg/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
.include <src.opts.mk>

PROG= mkimg
SRCS= format.c image.c mkimg.c scheme.c
SRCS= format.c image.c mkimg.c scheme.c uuid.c
MAN= mkimg.1

MKIMG_VERSION=20150620
MKIMG_VERSION=20161016
mkimg.o: Makefile

CFLAGS+=-DMKIMG_VERSION=${MKIMG_VERSION}
CFLAGS+=-DSPARSE_WRITE
CFLAGS+=-I${.CURDIR:H:H}/sys

# List of formats to support
SRCS+= \
Expand All @@ -30,8 +31,12 @@ SRCS+= \

BINDIR?=/usr/bin

LIBADD+= util
LIBADD= util

WARNS?= 6

.if ${MK_TESTS} != "no"
SUBDIR+= tests
.endif

.include <bsd.prog.mk>
11 changes: 6 additions & 5 deletions usr.bin/mkimg/apm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/apm.c 272030 2014-09-23 16:05:23Z marcel $");
__FBSDID("$FreeBSD: stable/11/usr.bin/mkimg/apm.c 329059 2018-02-09 09:15:43Z manu $");

#include <sys/types.h>
#include <sys/apm.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/apm.h>

#include "endian.h"
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
Expand Down Expand Up @@ -92,7 +93,7 @@ apm_write(lba_t imgsz, void *bootcode __unused)
strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type));
strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name));

STAILQ_FOREACH(part, &partlist, link) {
TAILQ_FOREACH(part, &partlist, link) {
ent = (void *)(buf + (part->index + 2) * secsz);
be16enc(&ent->ent_sig, APM_ENT_SIG);
be32enc(&ent->ent_pmblkcnt, nparts + 1);
Expand Down
11 changes: 6 additions & 5 deletions usr.bin/mkimg/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/bsd.c 272030 2014-09-23 16:05:23Z marcel $");
__FBSDID("$FreeBSD: stable/11/usr.bin/mkimg/bsd.c 329059 2018-02-09 09:15:43Z manu $");

#include <sys/types.h>
#include <sys/disklabel.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/disklabel.h>

#include "endian.h"
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
Expand Down Expand Up @@ -104,7 +105,7 @@ bsd_write(lba_t imgsz, void *bootcode)

dp = &d->d_partitions[RAW_PART];
le32enc(&dp->p_size, imgsz);
STAILQ_FOREACH(part, &partlist, link) {
TAILQ_FOREACH(part, &partlist, link) {
n = part->index + ((part->index >= RAW_PART) ? 1 : 0);
dp = &d->d_partitions[n];
le32enc(&dp->p_size, part->size);
Expand Down
12 changes: 7 additions & 5 deletions usr.bin/mkimg/ebr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/ebr.c 292341 2015-12-16 16:44:56Z emaste $");
__FBSDID("$FreeBSD: stable/11/usr.bin/mkimg/ebr.c 329059 2018-02-09 09:15:43Z manu $");

#include <sys/types.h>
#include <sys/diskmbr.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/diskmbr.h>

#include "endian.h"
#include "image.h"
#include "mkimg.h"
#include "scheme.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ ebr_write(lba_t imgsz __unused, void *bootcode __unused)
le16enc(ebr + DOSMAGICOFFSET, DOSMAGIC);

error = 0;
STAILQ_FOREACH_SAFE(part, &partlist, link, next) {
TAILQ_FOREACH(part, &partlist, link) {
block = part->block - nsecs;
size = round_track(part->size);
dp = (void *)(ebr + DOSPARTOFF);
Expand All @@ -101,6 +102,7 @@ ebr_write(lba_t imgsz __unused, void *bootcode __unused)
le32enc(&dp->dp_size, size);

/* Add link entry */
next = TAILQ_NEXT(part, link);
if (next != NULL) {
size = round_track(next->size);
dp++;
Expand Down
106 changes: 106 additions & 0 deletions usr.bin/mkimg/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*-
* Copyright (c) 2002 Thomas Moestl <[email protected]>
* All rights reserved.
*
* 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 AUTHOR 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 AUTHOR 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.
*
* $FreeBSD: stable/11/usr.bin/mkimg/endian.h 329059 2018-02-09 09:15:43Z manu $
*/

#ifndef _MKIMG_ENDIAN_H_
#define _MKIMG_ENDIAN_H_

static __inline uint16_t
be16dec(const void *pp)
{
uint8_t const *p = (uint8_t const *)pp;

return ((p[0] << 8) | p[1]);
}

static __inline void
be16enc(void *pp, uint16_t u)
{
uint8_t *p = (uint8_t *)pp;

p[0] = (u >> 8) & 0xff;
p[1] = u & 0xff;
}

static __inline void
be32enc(void *pp, uint32_t u)
{
uint8_t *p = (uint8_t *)pp;

p[0] = (u >> 24) & 0xff;
p[1] = (u >> 16) & 0xff;
p[2] = (u >> 8) & 0xff;
p[3] = u & 0xff;
}

static __inline void
be64enc(void *pp, uint64_t u)
{
uint8_t *p = (uint8_t *)pp;

be32enc(p, (uint32_t)(u >> 32));
be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
}

static __inline uint16_t
le16dec(const void *pp)
{
uint8_t const *p = (uint8_t const *)pp;

return ((p[1] << 8) | p[0]);
}

static __inline void
le16enc(void *pp, uint16_t u)
{
uint8_t *p = (uint8_t *)pp;

p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
}

static __inline void
le32enc(void *pp, uint32_t u)
{
uint8_t *p = (uint8_t *)pp;

p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
p[2] = (u >> 16) & 0xff;
p[3] = (u >> 24) & 0xff;
}

static __inline void
le64enc(void *pp, uint64_t u)
{
uint8_t *p = (uint8_t *)pp;

le32enc(p, (uint32_t)(u & 0xffffffffU));
le32enc(p + 4, (uint32_t)(u >> 32));
}

#endif /* _MKIMG_ENDIAN_H_ */
27 changes: 20 additions & 7 deletions usr.bin/mkimg/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/format.c 284773 2015-06-24 18:40:34Z marcel $");
__FBSDID("$FreeBSD: stable/11/usr.bin/mkimg/format.c 329059 2018-02-09 09:15:43Z manu $");

#include <sys/types.h>
#include <sys/linker_set.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
Expand All @@ -43,8 +40,24 @@ __FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/format.c 284773 2015-06-24 18:40:34Z
#include "format.h"
#include "mkimg.h"

static struct mkimg_format *first;
static struct mkimg_format *format;

struct mkimg_format *
format_iterate(struct mkimg_format *f)
{

return ((f == NULL) ? first : f->next);
}

void
format_register(struct mkimg_format *f)
{

f->next = first;
first = f;
}

int
format_resize(lba_t end)
{
Expand All @@ -57,10 +70,10 @@ format_resize(lba_t end)
int
format_select(const char *spec)
{
struct mkimg_format *f, **iter;
struct mkimg_format *f;

SET_FOREACH(iter, formats) {
f = *iter;
f = NULL;
while ((f = format_iterate(f)) != NULL) {
if (strcasecmp(spec, f->name) == 0) {
format = f;
return (0);
Expand Down
15 changes: 9 additions & 6 deletions usr.bin/mkimg/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,30 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: stable/10/usr.bin/mkimg/format.h 268161 2014-07-02 14:54:41Z marcel $
* $FreeBSD: stable/11/usr.bin/mkimg/format.h 329059 2018-02-09 09:15:43Z manu $
*/

#ifndef _MKIMG_FORMAT_H_
#define _MKIMG_FORMAT_H_

#include <sys/linker_set.h>

struct mkimg_format {
struct mkimg_format *next;
const char *name;
const char *description;
int (*resize)(lba_t);
int (*write)(int);
};

SET_DECLARE(formats, struct mkimg_format);
#define FORMAT_DEFINE(nm) DATA_SET(formats, nm)
#define FORMAT_DEFINE(nm) \
static void format_register_##nm(void) __attribute__((constructor)); \
static void format_register_##nm(void) { format_register(&nm); }

int format_resize(lba_t);
struct mkimg_format *format_iterate(struct mkimg_format *);
void format_register(struct mkimg_format *);
int format_select(const char *);
struct mkimg_format *format_selected(void);

int format_resize(lba_t);
int format_write(int);

#endif /* _MKIMG_FORMAT_H_ */
Loading

0 comments on commit 0aebfeb

Please sign in to comment.