Replace *_get_leader() and *_get_trailer() with a single *_metadata().
This single function takes a where argument to indicate the kind of metadata to "size". This way we can also get rid of the "padding" field in the scheme structure. This should make it a little more understandable what's going on.
This commit is contained in:
parent
5f39eb99a0
commit
3f2295fee5
21
apm.c
21
apm.c
@ -40,28 +40,21 @@ static struct mkimg_alias apm_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
apm_get_leader(u_int parts)
|
||||
static u_int
|
||||
apm_metadata(u_int where, u_int parts, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (parts + 1);
|
||||
}
|
||||
|
||||
static off_t
|
||||
apm_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
return (0);
|
||||
secs = (where == SCHEME_META_IMG_START) ? parts + 1 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme apm_scheme = {
|
||||
.name = "apm",
|
||||
.description = "Apple Partition Map",
|
||||
.nparts = 4096,
|
||||
.padding = 0,
|
||||
.aliases = apm_aliases,
|
||||
.get_leader = apm_get_leader,
|
||||
.get_trailer = apm_get_trailer
|
||||
.metadata = apm_metadata,
|
||||
.nparts = 4096
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(apm_scheme);
|
||||
|
23
bsd.c
23
bsd.c
@ -40,28 +40,21 @@ static struct mkimg_alias bsd_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
bsd_get_leader(u_int parts __unused)
|
||||
static u_int
|
||||
bsd_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (16);
|
||||
}
|
||||
|
||||
static off_t
|
||||
bsd_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
return (0);
|
||||
secs = (where == SCHEME_META_IMG_START) ? 16 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme bsd_scheme = {
|
||||
.name = "bsd",
|
||||
.description = "GUID Partition Table",
|
||||
.nparts = 20,
|
||||
.padding = 0,
|
||||
.description = "BSD disk label",
|
||||
.aliases = bsd_aliases,
|
||||
.get_leader = bsd_get_leader,
|
||||
.get_trailer = bsd_get_trailer
|
||||
.metadata = bsd_metadata,
|
||||
.nparts = 20
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(bsd_scheme);
|
||||
|
25
ebr.c
25
ebr.c
@ -40,32 +40,21 @@ static struct mkimg_alias ebr_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
ebr_get_leader(u_int parts __unused)
|
||||
static u_int
|
||||
ebr_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
static off_t
|
||||
ebr_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
/*
|
||||
* Compensate for having reserved a sector for the EBR after
|
||||
* the last partition.
|
||||
*/
|
||||
return (-1);
|
||||
secs = (where == SCHEME_META_PART_BEFORE) ? 1 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme ebr_scheme = {
|
||||
.name = "ebr",
|
||||
.description = "Extended Boot Record",
|
||||
.nparts = 4096,
|
||||
.padding = 1, /* See ebr_get_trailer() above */
|
||||
.aliases = ebr_aliases,
|
||||
.get_leader = ebr_get_leader,
|
||||
.get_trailer = ebr_get_trailer
|
||||
.metadata = ebr_metadata,
|
||||
.nparts = 4096
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(ebr_scheme);
|
||||
|
24
gpt.c
24
gpt.c
@ -44,28 +44,26 @@ static struct mkimg_alias gpt_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
gpt_get_leader(u_int parts)
|
||||
static u_int
|
||||
gpt_metadata(u_int where, u_int parts, u_int secsz)
|
||||
{
|
||||
u_int ents, secs;
|
||||
|
||||
return (2 + (parts + 3) / 4);
|
||||
}
|
||||
if (where != SCHEME_META_IMG_START && where != SCHEME_META_IMG_START)
|
||||
return (0);
|
||||
|
||||
static off_t
|
||||
gpt_get_trailer(u_int parts)
|
||||
{
|
||||
|
||||
return (1 + (parts + 3) / 4);
|
||||
ents = secsz / sizeof(struct gpt_ent);
|
||||
secs = (parts + ents - 1) / ents;
|
||||
secs += (where == SCHEME_META_IMG_START) ? 2 : 1;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme gpt_scheme = {
|
||||
.name = "gpt",
|
||||
.description = "GUID Partition Table",
|
||||
.nparts = 4096,
|
||||
.padding = 0,
|
||||
.aliases = gpt_aliases,
|
||||
.get_leader = gpt_get_leader,
|
||||
.get_trailer = gpt_get_trailer
|
||||
.metadata = gpt_metadata,
|
||||
.nparts = 4096
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(gpt_scheme);
|
||||
|
21
mbr.c
21
mbr.c
@ -41,28 +41,21 @@ static struct mkimg_alias mbr_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
mbr_get_leader(u_int parts __unused)
|
||||
static u_int
|
||||
mbr_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
static off_t
|
||||
mbr_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
return (0);
|
||||
secs = (where == SCHEME_META_IMG_START) ? 1 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme mbr_scheme = {
|
||||
.name = "mbr",
|
||||
.description = "Master Boot Record",
|
||||
.nparts = NDOSPART,
|
||||
.padding = 0,
|
||||
.aliases = mbr_aliases,
|
||||
.get_leader = mbr_get_leader,
|
||||
.get_trailer = mbr_get_trailer
|
||||
.metadata = mbr_metadata,
|
||||
.nparts = NDOSPART
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(mbr_scheme);
|
||||
|
21
pc98.c
21
pc98.c
@ -41,28 +41,21 @@ static struct mkimg_alias pc98_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
pc98_get_leader(u_int parts __unused)
|
||||
static u_int
|
||||
pc98_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (2);
|
||||
}
|
||||
|
||||
static off_t
|
||||
pc98_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
return (0);
|
||||
secs = (where == SCHEME_META_IMG_START) ? 2 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme pc98_scheme = {
|
||||
.name = "pc98",
|
||||
.description = "PC-9800 disk partitions",
|
||||
.nparts = PC98_NPARTS,
|
||||
.padding = 0,
|
||||
.aliases = pc98_aliases,
|
||||
.get_leader = pc98_get_leader,
|
||||
.get_trailer = pc98_get_trailer
|
||||
.metadata = pc98_metadata,
|
||||
.nparts = PC98_NPARTS
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(pc98_scheme);
|
||||
|
24
scheme.c
24
scheme.c
@ -99,27 +99,35 @@ scheme_max_parts(void)
|
||||
off_t
|
||||
scheme_first_offset(u_int parts)
|
||||
{
|
||||
off_t off;
|
||||
u_int secs;
|
||||
|
||||
off = scheme->get_leader(parts);
|
||||
off *= secsz;
|
||||
return (off);
|
||||
secs = scheme->metadata(SCHEME_META_IMG_START, parts, secsz) +
|
||||
scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
||||
return (secs * secsz);
|
||||
}
|
||||
|
||||
off_t
|
||||
scheme_next_offset(off_t off, uint64_t sz)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
sz = (sz + secsz - 1) & ~(secsz - 1);
|
||||
sz += scheme->padding * secsz;
|
||||
secs = scheme->metadata(SCHEME_META_PART_AFTER, 0, secsz) +
|
||||
scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
||||
sz += (secs * secsz);
|
||||
return (off + sz);
|
||||
}
|
||||
|
||||
void
|
||||
scheme_write(int fd, off_t off)
|
||||
{
|
||||
off_t trailer;
|
||||
u_int secs;
|
||||
|
||||
trailer = scheme->get_trailer(nparts) * secsz;
|
||||
ftruncate(fd, off + trailer);
|
||||
/* Fixup offset: it has an extra metadata before the partition */
|
||||
secs = scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
|
||||
off -= (secs * secsz);
|
||||
|
||||
secs = scheme->metadata(SCHEME_META_IMG_END, nparts, secsz);
|
||||
off += (secs * secsz);
|
||||
ftruncate(fd, off);
|
||||
}
|
||||
|
18
scheme.h
18
scheme.h
@ -30,20 +30,22 @@
|
||||
#define _MKIMG_SCHEME_H_
|
||||
|
||||
struct mkimg_alias {
|
||||
const char *name;
|
||||
uintptr_t tp;
|
||||
const char *name;
|
||||
uintptr_t tp;
|
||||
#define ALIAS_PTR(p) (uintptr_t)(p)
|
||||
#define ALIAS_INT(i) (uintptr_t)(i)
|
||||
};
|
||||
|
||||
struct mkimg_scheme {
|
||||
const char *name;
|
||||
const char *description;
|
||||
int nparts;
|
||||
int padding;
|
||||
const char *name;
|
||||
const char *description;
|
||||
struct mkimg_alias *aliases;
|
||||
off_t (*get_leader)(u_int);
|
||||
off_t (*get_trailer)(u_int);
|
||||
u_int (*metadata)(u_int, u_int, u_int);
|
||||
#define SCHEME_META_IMG_START 1
|
||||
#define SCHEME_META_IMG_END 2
|
||||
#define SCHEME_META_PART_BEFORE 3
|
||||
#define SCHEME_META_PART_AFTER 4
|
||||
int nparts;
|
||||
};
|
||||
|
||||
SET_DECLARE(schemes, struct mkimg_scheme);
|
||||
|
21
vtoc8.c
21
vtoc8.c
@ -41,28 +41,21 @@ static struct mkimg_alias vtoc8_aliases[] = {
|
||||
{ NULL, 0 }
|
||||
};
|
||||
|
||||
static off_t
|
||||
vtoc8_get_leader(u_int parts __unused)
|
||||
static u_int
|
||||
vtoc8_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
|
||||
{
|
||||
u_int secs;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
static off_t
|
||||
vtoc8_get_trailer(u_int parts __unused)
|
||||
{
|
||||
|
||||
return (0);
|
||||
secs = (where == SCHEME_META_IMG_START) ? 1 : 0;
|
||||
return (secs);
|
||||
}
|
||||
|
||||
static struct mkimg_scheme vtoc8_scheme = {
|
||||
.name = "vtoc8",
|
||||
.description = "SMI VTOC8 disk labels",
|
||||
.nparts = VTOC8_NPARTS,
|
||||
.padding = 0,
|
||||
.aliases = vtoc8_aliases,
|
||||
.get_leader = vtoc8_get_leader,
|
||||
.get_trailer = vtoc8_get_trailer
|
||||
.metadata = vtoc8_metadata,
|
||||
.nparts = VTOC8_NPARTS
|
||||
};
|
||||
|
||||
SCHEME_DEFINE(vtoc8_scheme);
|
||||
|
Loading…
Reference in New Issue
Block a user