Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <limits.h>

#include "genimage.h"

Expand Down Expand Up @@ -279,7 +280,18 @@ static char *abspath(const char *path)
if (*path == '/')
return strdup(path);

#ifdef __GLIBC__
/* Use GNU extension on Linux */
xasprintf(&p, "%s/%s", get_current_dir_name(), path);
#else
/* Use POSIX standard for macOS/BSD */
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
return NULL;
}
xasprintf(&p, "%s/%s", cwd, path);
#endif

return p;
}
Expand Down
4 changes: 4 additions & 0 deletions genimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <confuse.h>
#include "list.h"

#ifndef __GLIBC__
#define strdupa(s) strdup(s)
#endif

struct image_handler;

struct image *image_get(const char *filename);
Expand Down
8 changes: 8 additions & 0 deletions image-android-sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
*/

#include <confuse.h>
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htole16(x) OSSwapHostToLittleInt16(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#else
#include <endian.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
Expand Down
10 changes: 10 additions & 0 deletions image-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htole16(x) OSSwapHostToLittleInt16(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#else
#include <endian.h>
#endif
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
Expand Down
50 changes: 50 additions & 0 deletions image-mdraid.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@
#include <string.h>
#include <time.h>
#include <errno.h>

#ifdef __linux__
#include <linux/raid/md_p.h>
#endif

#include "genimage.h"

#ifdef __linux__

#define DATA_OFFSET_SECTORS (2048)
#define DATA_OFFSET_BYTES (DATA_OFFSET_SECTORS * 512)
#define BITMAP_SECTORS_MAX 256
Expand Down Expand Up @@ -486,3 +491,48 @@ struct image_handler mdraid_handler = {
.generate = mdraid_generate,
.opts = mdraid_opts,
};

#else /* !__linux__ */

/* Stub implementation for non-Linux systems */
static int mdraid_parse(struct image *image, cfg_t *cfg)
{
image_error(image, "MDRAID is only supported on Linux\n");
return -1;
}

static int mdraid_generate(struct image *image)
{
image_error(image, "MDRAID is only supported on Linux\n");
return -1;
}

static int mdraid_setup(struct image *image, cfg_t *cfg)
{
image_error(image, "MDRAID is only supported on Linux\n");
return -1;
}

static cfg_opt_t mdraid_opts[] = {
CFG_STR("label", "any:42", CFGF_NONE),
CFG_INT("level", 1, CFGF_NONE),
CFG_INT("devices", 1, CFGF_NONE),
CFG_INT("role", -1, CFGF_NONE),
CFG_INT("timestamp", -1, CFGF_NONE),
CFG_STR("raid-uuid", NULL, CFGF_NONE),
CFG_STR("disk-uuid", NULL, CFGF_NONE),
CFG_STR("image", NULL, CFGF_NONE),
CFG_STR("parent", NULL, CFGF_NONE),
CFG_END()
};

struct image_handler mdraid_handler = {
.type = "mdraid",
.no_rootpath = cfg_true,
.parse = mdraid_parse,
.setup = mdraid_setup,
.generate = mdraid_generate,
.opts = mdraid_opts,
};

#endif /* __linux__ */
12 changes: 12 additions & 0 deletions image-verity.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/sendfile.h>
#endif
#include <sys/stat.h>

#include "genimage.h"
Expand Down Expand Up @@ -337,13 +339,23 @@ static int verity_generate(struct image *image)
return -errno;

if (image->size && image->size < (unsigned long)sb.st_size) {
#ifdef __APPLE__
image_error(image,
"Specified image size (%llu) is too small, generated %lld bytes\n",
image->size, sb.st_size);
#else
image_error(image,
"Specified image size (%llu) is too small, generated %ld bytes\n",
image->size, sb.st_size);
#endif
return -E2BIG;
}

#ifdef __APPLE__
image_debug(image, "generated %lld bytes\n", sb.st_size);
#else
image_debug(image, "generated %ld bytes\n", sb.st_size);
#endif
image->size = sb.st_size;
return 0;
}
Expand Down
17 changes: 16 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,11 @@ int parse_holes(struct image *image, cfg_t *cfg)
image->holes = xzalloc(image->n_holes * sizeof(*image->holes));
for (i = 0; i < image->n_holes; i++) {
const char *s = cfg_getnstr(cfg, "holes", i);
char *start, *end;
int len;

#ifdef __GLIBC__
/* Use GNU extension %m for automatic memory allocation */
char *start, *end;
if (sscanf(s, " ( %m[0-9skKMG] ; %m[0-9skKMG] ) %n", &start, &end, &len) != 2 ||
len != (int)strlen(s)) {
image_error(image, "invalid hole specification '%s', use '(<start>;<end>)'\n",
Expand All @@ -966,6 +968,19 @@ int parse_holes(struct image *image, cfg_t *cfg)
image->holes[i].end = strtoul_suffix(end, NULL, NULL);
free(start);
free(end);
#else
/* Use fixed-size buffers for non-glibc systems (macOS/BSD) */
char start[64], end[64];
if (sscanf(s, " ( %63[0-9skKMG] ; %63[0-9skKMG] ) %n", start, end, &len) != 2 ||
len != (int)strlen(s)) {
image_error(image, "invalid hole specification '%s', use '(<start>;<end>)'\n",
s);
return -EINVAL;
}

image->holes[i].start = strtoul_suffix(start, NULL, NULL);
image->holes[i].end = strtoul_suffix(end, NULL, NULL);
#endif
image_debug(image, "added hole (%llu, %llu)\n", image->holes[i].start, image->holes[i].end);
}
return 0;
Expand Down