aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/mkimg
diff options
context:
space:
mode:
authorMarcel Moolenaar <marcel@FreeBSD.org>2014-09-19 23:16:02 +0000
committerMarcel Moolenaar <marcel@FreeBSD.org>2014-09-19 23:16:02 +0000
commit53fcdb2d1ad0790de866fbdd5043cbd652856a19 (patch)
treea2e3472004d6fccdacf404e079c2a6cd4d9134e5 /usr.bin/mkimg
parenta4b2ac79e43381fa4f3b9be9e4f8ac1b8a0fac17 (diff)
downloadsrc-53fcdb2d1ad0790de866fbdd5043cbd652856a19.tar.gz
src-53fcdb2d1ad0790de866fbdd5043cbd652856a19.zip
Fix partition alignment and image rounding when any of -P (block size),
-T (track size) or -H (number of heads) is given: o scheme_metadata() always rounded to the block size. This is not always valid (e.g. vtoc8 that must have partitions start at cylinder boundaries). o The bsd and vtoc8 schemes "resized" the image to make it match the geometry, but since the geometry is an approximation and the size of the image computed from cylinders * heads * sectors is always smaller than the original image size, the partition information ran out of bounds. The fix is to have scheme_metadata() simply pass it's arguments to the per-scheme metadata callback, so that schemes not only know where the metadata is to go, but also what the current block address is. It's now up to the per-scheme callback to reserve room for metadata and to make sure alignment and rounding is applied. The BSD scheme now has the most elaborate alignment and rounding. Just to make the point: partitions are aligned on block boundaries, but the image is rounded to the next cyclinder boundary. vtoc8 now properly has all partitions aligned (and rounded) to the cyclinder boundary. Obtained from: Juniper Networks, Inc. MFC after: 3 days
Notes
Notes: svn path=/head/; revision=271881
Diffstat (limited to 'usr.bin/mkimg')
-rw-r--r--usr.bin/mkimg/apm.c9
-rw-r--r--usr.bin/mkimg/bsd.c20
-rw-r--r--usr.bin/mkimg/ebr.c9
-rw-r--r--usr.bin/mkimg/gpt.c16
-rw-r--r--usr.bin/mkimg/mbr.c9
-rw-r--r--usr.bin/mkimg/mkimg.h15
-rw-r--r--usr.bin/mkimg/pc98.c11
-rw-r--r--usr.bin/mkimg/scheme.c4
-rw-r--r--usr.bin/mkimg/scheme.h2
-rw-r--r--usr.bin/mkimg/vtoc8.c13
10 files changed, 54 insertions, 54 deletions
diff --git a/usr.bin/mkimg/apm.c b/usr.bin/mkimg/apm.c
index 12add8e4cb8a..de92cc0e9b92 100644
--- a/usr.bin/mkimg/apm.c
+++ b/usr.bin/mkimg/apm.c
@@ -57,13 +57,12 @@ static struct mkimg_alias apm_aliases[] = {
{ ALIAS_NONE, 0 }
};
-static u_int
-apm_metadata(u_int where)
+static lba_t
+apm_metadata(u_int where, lba_t blk)
{
- u_int secs;
- secs = (where == SCHEME_META_IMG_START) ? nparts + 2 : 0;
- return (secs);
+ blk += (where == SCHEME_META_IMG_START) ? nparts + 2 : 0;
+ return (round_block(blk));
}
static int
diff --git a/usr.bin/mkimg/bsd.c b/usr.bin/mkimg/bsd.c
index c07ae14bc0f5..75e554f39b4f 100644
--- a/usr.bin/mkimg/bsd.c
+++ b/usr.bin/mkimg/bsd.c
@@ -52,13 +52,17 @@ static struct mkimg_alias bsd_aliases[] = {
{ ALIAS_NONE, 0 }
};
-static u_int
-bsd_metadata(u_int where)
+static lba_t
+bsd_metadata(u_int where, lba_t blk)
{
- u_int secs;
- secs = BBSIZE / secsz;
- return ((where == SCHEME_META_IMG_START) ? secs : 0);
+ if (where == SCHEME_META_IMG_START)
+ blk += BBSIZE / secsz;
+ else if (where == SCHEME_META_IMG_END)
+ blk = round_cylinder(blk);
+ else
+ blk = round_block(blk);
+ return (blk);
}
static int
@@ -83,12 +87,6 @@ bsd_write(lba_t imgsz, void *bootcode)
bsdparts = nparts + 1; /* Account for c partition */
if (bsdparts < MAXPARTITIONS)
bsdparts = MAXPARTITIONS;
- imgsz = (lba_t)ncyls * nheads * nsecs;
- error = image_set_size(imgsz);
- if (error) {
- free(buf);
- return (error);
- }
d = (void *)(buf + secsz);
le32enc(&d->d_magic, DISKMAGIC);
diff --git a/usr.bin/mkimg/ebr.c b/usr.bin/mkimg/ebr.c
index 77204d70e813..43601cc89227 100644
--- a/usr.bin/mkimg/ebr.c
+++ b/usr.bin/mkimg/ebr.c
@@ -49,13 +49,12 @@ static struct mkimg_alias ebr_aliases[] = {
{ ALIAS_NONE, 0 }
};
-static u_int
-ebr_metadata(u_int where)
+static lba_t
+ebr_metadata(u_int where, lba_t blk)
{
- u_int secs;
- secs = (where == SCHEME_META_PART_BEFORE) ? nsecs : 0;
- return (secs);
+ blk += (where == SCHEME_META_PART_BEFORE) ? 1 : 0;
+ return (round_track(blk));
}
static void
diff --git a/usr.bin/mkimg/gpt.c b/usr.bin/mkimg/gpt.c
index 959deb37bc0d..5773a6a612f7 100644
--- a/usr.bin/mkimg/gpt.c
+++ b/usr.bin/mkimg/gpt.c
@@ -153,17 +153,15 @@ gpt_tblsz(void)
return ((nparts + ents - 1) / ents);
}
-static u_int
-gpt_metadata(u_int where)
+static lba_t
+gpt_metadata(u_int where, lba_t blk)
{
- u_int secs;
-
- if (where != SCHEME_META_IMG_START && where != SCHEME_META_IMG_END)
- return (0);
- secs = gpt_tblsz();
- secs += (where == SCHEME_META_IMG_START) ? 2 : 1;
- return (secs);
+ if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) {
+ blk += gpt_tblsz();
+ blk += (where == SCHEME_META_IMG_START) ? 2 : 1;
+ }
+ return (round_block(blk));
}
static int
diff --git a/usr.bin/mkimg/mbr.c b/usr.bin/mkimg/mbr.c
index 4b0f24262dbb..e9c2aeffce07 100644
--- a/usr.bin/mkimg/mbr.c
+++ b/usr.bin/mkimg/mbr.c
@@ -50,13 +50,12 @@ static struct mkimg_alias mbr_aliases[] = {
{ ALIAS_NONE, 0 } /* Keep last! */
};
-static u_int
-mbr_metadata(u_int where)
+static lba_t
+mbr_metadata(u_int where, lba_t blk)
{
- u_int secs;
- secs = (where == SCHEME_META_IMG_START) ? nsecs : 0;
- return (secs);
+ blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
+ return (round_track(blk));
}
static void
diff --git a/usr.bin/mkimg/mkimg.h b/usr.bin/mkimg/mkimg.h
index 9558b863d36d..7a780343ee4f 100644
--- a/usr.bin/mkimg/mkimg.h
+++ b/usr.bin/mkimg/mkimg.h
@@ -66,6 +66,21 @@ round_block(lba_t n)
return ((n + b - 1) & ~(b - 1));
}
+static inline lba_t
+round_cylinder(lba_t n)
+{
+ u_int cyl = nsecs * nheads;
+ u_int r = n % cyl;
+ return ((r == 0) ? n : n + cyl - r);
+}
+
+static inline lba_t
+round_track(lba_t n)
+{
+ u_int r = n % nsecs;
+ return ((r == 0) ? n : n + nsecs - r);
+}
+
#if !defined(SPARSE_WRITE)
#define sparse_write write
#else
diff --git a/usr.bin/mkimg/pc98.c b/usr.bin/mkimg/pc98.c
index 24b9156bf09c..56acb1ec4b70 100644
--- a/usr.bin/mkimg/pc98.c
+++ b/usr.bin/mkimg/pc98.c
@@ -59,13 +59,12 @@ static struct mkimg_alias pc98_aliases[] = {
{ ALIAS_NONE, 0 }
};
-static u_int
-pc98_metadata(u_int where)
+static lba_t
+pc98_metadata(u_int where, lba_t blk)
{
- u_int secs;
-
- secs = PC98_BOOTCODESZ / secsz;
- return ((where == SCHEME_META_IMG_START) ? secs : 0);
+ if (where == SCHEME_META_IMG_START)
+ blk += PC98_BOOTCODESZ / secsz;
+ return (round_track(blk));
}
static void
diff --git a/usr.bin/mkimg/scheme.c b/usr.bin/mkimg/scheme.c
index 7546ae289f34..ff5e2014a7e5 100644
--- a/usr.bin/mkimg/scheme.c
+++ b/usr.bin/mkimg/scheme.c
@@ -171,10 +171,8 @@ scheme_max_secsz(void)
lba_t
scheme_metadata(u_int where, lba_t start)
{
- lba_t secs;
- secs = scheme->metadata(where);
- return (round_block(start + secs));
+ return (scheme->metadata(where, start));
}
int
diff --git a/usr.bin/mkimg/scheme.h b/usr.bin/mkimg/scheme.h
index 8224930d8d28..d594a1917d67 100644
--- a/usr.bin/mkimg/scheme.h
+++ b/usr.bin/mkimg/scheme.h
@@ -62,7 +62,7 @@ struct mkimg_scheme {
const char *name;
const char *description;
struct mkimg_alias *aliases;
- u_int (*metadata)(u_int);
+ lba_t (*metadata)(u_int, lba_t);
#define SCHEME_META_IMG_START 1
#define SCHEME_META_IMG_END 2
#define SCHEME_META_PART_BEFORE 3
diff --git a/usr.bin/mkimg/vtoc8.c b/usr.bin/mkimg/vtoc8.c
index 8eb7d24080bd..2f5cf0f706e3 100644
--- a/usr.bin/mkimg/vtoc8.c
+++ b/usr.bin/mkimg/vtoc8.c
@@ -53,13 +53,12 @@ static struct mkimg_alias vtoc8_aliases[] = {
{ ALIAS_NONE, 0 }
};
-static u_int
-vtoc8_metadata(u_int where)
+static lba_t
+vtoc8_metadata(u_int where, lba_t blk)
{
- u_int secs;
- secs = (where == SCHEME_META_IMG_START) ? nsecs * nheads : 0;
- return (secs);
+ blk += (where == SCHEME_META_IMG_START) ? 1 : 0;
+ return (round_cylinder(blk));
}
static int
@@ -87,10 +86,6 @@ vtoc8_write(lba_t imgsz, void *bootcode __unused)
be16enc(&vtoc8.nsecs, nsecs);
be16enc(&vtoc8.magic, VTOC_MAGIC);
- error = image_set_size(imgsz);
- if (error)
- return (error);
-
be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz);
STAILQ_FOREACH(part, &partlist, link) {
n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0);