Add mkimg_chs() for those schemes that need the LBA broken down into

cylinder, head and track numbers. Return ~0U for these values when
mkimg wasn't given both -T and -H (i.e. no geometry) or the cylinder
would be larger than the provided maximum.

Use mkimgs_chs() for the EBR, MBR and PC98 schemes to fill in the
appropriate fields. Make sure to use a "rounded" size so that the
partition is always a multiple of the track size. We reserved the
room for it in the metadata callback so that's a valid thing to
do.

Bump the mkimg version number.
While doing that again: have mkimg.o depend on the Makefile so that
a version change triggers a rebuild as needed.
This commit is contained in:
Marcel Moolenaar 2014-10-03 20:48:11 +00:00
parent c36047bdb7
commit 2c83b36f45
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=272485
6 changed files with 58 additions and 21 deletions

View File

@ -6,7 +6,9 @@ PROG= mkimg
SRCS= format.c image.c mkimg.c scheme.c
MAN= mkimg.1
MKIMG_VERSION=20141001
MKIMG_VERSION=20141003
mkimg.o: Makefile
CFLAGS+=-DMKIMG_VERSION=${MKIMG_VERSION}
CFLAGS+=-DSPARSE_WRITE

View File

@ -58,12 +58,14 @@ ebr_metadata(u_int where, lba_t blk)
}
static void
ebr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
ebr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba)
{
u_int cyl, hd, sec;
*cyl = 0xff; /* XXX */
*hd = 0xff; /* XXX */
*sec = 0xff; /* XXX */
mkimg_chs(lba, 1023, &cyl, &hd, &sec);
*cylp = cyl;
*hdp = hd;
*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
}
static int
@ -72,7 +74,7 @@ ebr_write(lba_t imgsz __unused, void *bootcode __unused)
u_char *ebr;
struct dos_partition *dp;
struct part *part, *next;
lba_t block;
lba_t block, size;
int error;
ebr = malloc(secsz);
@ -84,24 +86,26 @@ ebr_write(lba_t imgsz __unused, void *bootcode __unused)
error = 0;
STAILQ_FOREACH_SAFE(part, &partlist, link, next) {
block = part->block - nsecs;
size = round_track(part->size);
dp = (void *)(ebr + DOSPARTOFF);
ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, nsecs);
dp->dp_typ = ALIAS_TYPE2INT(part->type);
ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
part->block + part->size - 1);
part->block + size - 1);
le32enc(&dp->dp_start, nsecs);
le32enc(&dp->dp_size, part->size);
le32enc(&dp->dp_size, size);
/* Add link entry */
if (next != NULL) {
size = round_track(next->size);
dp++;
ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
next->block - nsecs);
dp->dp_typ = DOSPTYP_EXT;
ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
next->block + next->size - 1);
next->block + size - 1);
le32enc(&dp->dp_start, next->block - nsecs);
le32enc(&dp->dp_size, next->size + nsecs);
le32enc(&dp->dp_size, size + nsecs);
}
error = image_write(block, ebr, 1);

View File

@ -59,12 +59,14 @@ mbr_metadata(u_int where, lba_t blk)
}
static void
mbr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
mbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba)
{
u_int cyl, hd, sec;
*cyl = 0xff; /* XXX */
*hd = 0xff; /* XXX */
*sec = 0xff; /* XXX */
mkimg_chs(lba, 1023, &cyl, &hd, &sec);
*cylp = cyl;
*hdp = hd;
*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
}
static int
@ -73,6 +75,7 @@ mbr_write(lba_t imgsz __unused, void *bootcode)
u_char *mbr;
struct dos_partition *dpbase, *dp;
struct part *part;
lba_t size;
int error;
mbr = malloc(secsz);
@ -86,15 +89,16 @@ mbr_write(lba_t imgsz __unused, void *bootcode)
le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC);
dpbase = (void *)(mbr + DOSPARTOFF);
STAILQ_FOREACH(part, &partlist, link) {
size = round_track(part->size);
dp = dpbase + part->index;
dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0;
mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
part->block);
dp->dp_typ = ALIAS_TYPE2INT(part->type);
mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
part->block + part->size - 1);
part->block + size - 1);
le32enc(&dp->dp_start, part->block);
le32enc(&dp->dp_size, part->size);
le32enc(&dp->dp_size, size);
}
error = image_write(0, mbr, 1);
free(mbr);

View File

@ -339,6 +339,27 @@ sparse_write(int fd, const void *ptr, size_t sz)
}
#endif /* SPARSE_WRITE */
void
mkimg_chs(lba_t lba, u_int maxcyl, u_int *cylp, u_int *hdp, u_int *secp)
{
u_int hd, sec;
*cylp = *hdp = *secp = ~0U;
if (nsecs == 1 || nheads == 1)
return;
sec = lba % nsecs + 1;
lba /= nsecs;
hd = lba % nheads;
lba /= nheads;
if (lba > maxcyl)
return;
*cylp = lba;
*hdp = hd;
*secp = sec;
}
void
mkimg_uuid(struct uuid *uuid)
{

View File

@ -87,6 +87,8 @@ round_track(lba_t n)
ssize_t sparse_write(int, const void *, size_t);
#endif
void mkimg_chs(lba_t, u_int, u_int *, u_int *, u_int *);
struct uuid;
void mkimg_uuid(struct uuid *);

View File

@ -68,12 +68,14 @@ pc98_metadata(u_int where, lba_t blk)
}
static void
pc98_chs(u_short *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
pc98_chs(u_short *cylp, u_char *hdp, u_char *secp, lba_t lba)
{
u_int cyl, hd, sec;
*cyl = 0xffff; /* XXX */
*hd = 0xff; /* XXX */
*sec = 0xff; /* XXX */
mkimg_chs(lba, 0xffff, &cyl, &hd, &sec);
le16enc(cylp, cyl);
*hdp = hd;
*secp = sec;
}
static int
@ -82,6 +84,7 @@ pc98_write(lba_t imgsz __unused, void *bootcode)
struct part *part;
struct pc98_partition *dpbase, *dp;
u_char *buf;
lba_t size;
int error, ptyp;
buf = malloc(PC98_BOOTCODESZ);
@ -95,6 +98,7 @@ pc98_write(lba_t imgsz __unused, void *bootcode)
le16enc(buf + PC98_MAGICOFS, PC98_MAGIC);
dpbase = (void *)(buf + secsz);
STAILQ_FOREACH(part, &partlist, link) {
size = round_track(part->size);
dp = dpbase + part->index;
ptyp = ALIAS_TYPE2INT(part->type);
dp->dp_mid = ptyp;
@ -102,7 +106,7 @@ pc98_write(lba_t imgsz __unused, void *bootcode)
pc98_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
part->block);
pc98_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
part->block + part->size - 1);
part->block + size - 1);
if (part->label != NULL)
memcpy(dp->dp_name, part->label, strlen(part->label));
}