1. When calculating block addresses, round to the physical block

size.
2.  Replace scheme_first_block() & scheme_next_block() with
    scheme_metadata(). When we round to block sizes, we can't
    reliably fixup any miscalculations.
3.  In scheme_write, calculate ncyls (number of cyclinders), based
    on the total size, sectors/track and number of heads.
4.  Add verbosity when constructing the partitions. This includes
    the starting block address and size in bytes and blocks.
5.  Add verbosity about the sectors/track and number of heads.
This commit is contained in:
marcel 2014-03-27 22:45:05 +00:00
parent 5d04a4dffc
commit bfd31b94a3
4 changed files with 35 additions and 23 deletions

20
mkimg.c
View File

@ -283,8 +283,12 @@ mkimg(int bfd)
errc(EX_DATAERR, error, "partition %d", part->index+1);
}
block = scheme_first_block();
block = scheme_metadata(SCHEME_META_IMG_START, 0);
STAILQ_FOREACH(part, &partlist, link) {
block = scheme_metadata(SCHEME_META_PART_BEFORE, block);
if (verbose)
printf("partition %d: starting block %llu ... ",
part->index + 1, (long long)block);
part->block = block;
error = mkimg_seek(tmpfd, block);
switch (part->kind) {
@ -312,9 +316,16 @@ mkimg(int bfd)
if (error)
errc(EX_IOERR, error, "partition %d", part->index + 1);
part->size = (bytesize + secsz - 1) / secsz;
block = scheme_next_block(part->block, part->size);
if (verbose) {
bytesize = part->size * secsz;
printf("size %llu bytes (%llu blocks)\n",
(long long)bytesize, (long long)part->size);
}
block = scheme_metadata(SCHEME_META_PART_AFTER,
part->block + part->size);
}
block = scheme_metadata(SCHEME_META_IMG_END, block);
error = (scheme_write(tmpfd, block));
}
@ -421,10 +432,15 @@ main(int argc, char *argv[])
if (verbose) {
printf("Logical sector size: %u\n", secsz);
printf("Physical block size: %u\n", blksz);
printf("Sectors per track: %u\n", nsecs);
printf("Number of heads: %u\n", nheads);
}
mkimg(bcfd);
if (verbose)
printf("Number of cylinders: %u\n", ncyls);
if (tmpfd != outfd) {
if (lseek(tmpfd, 0, SEEK_SET) == 0)
error = fdcopy(tmpfd, outfd, NULL);

View File

@ -60,6 +60,13 @@ extern u_int nsecs;
extern u_int secsz; /* Logical block size. */
extern u_int blksz; /* Physical block size. */
static inline lba_t
round_block(lba_t n)
{
lba_t b = blksz / secsz;
return ((n + b - 1) & ~(b - 1));
}
int mkimg_seek(int fd, lba_t blk);
#endif /* _MKIMG_MKIMG_H_ */

View File

@ -172,33 +172,23 @@ scheme_max_secsz(void)
}
lba_t
scheme_first_block(void)
scheme_metadata(u_int where, lba_t start)
{
lba_t blks;
lba_t secs;
blks = scheme->metadata(SCHEME_META_IMG_START) +
scheme->metadata(SCHEME_META_PART_BEFORE);
return (blks);
}
lba_t
scheme_next_block(lba_t start, lba_t size)
{
lba_t blks;
blks = scheme->metadata(SCHEME_META_PART_AFTER) +
scheme->metadata(SCHEME_META_PART_BEFORE);
return (start + size + blks);
secs = scheme->metadata(where);
return (round_block(start + secs));
}
int
scheme_write(int fd, lba_t end)
{
u_int cylsz;
int error;
/* Fixup block: it has an extra metadata before the partition */
end -= scheme->metadata(SCHEME_META_PART_BEFORE);
end += scheme->metadata(SCHEME_META_IMG_END);
cylsz = nsecs * nheads;
ncyls = end + cylsz - 1 / cylsz;
if (ftruncate(fd, end * secsz) == -1)
return (errno);

View File

@ -84,8 +84,7 @@ int scheme_bootcode(int fd);
int scheme_check_part(struct part *);
u_int scheme_max_parts(void);
u_int scheme_max_secsz(void);
lba_t scheme_first_block(void);
lba_t scheme_next_block(lba_t, lba_t);
lba_t scheme_metadata(u_int, lba_t);
int scheme_write(int, lba_t);
#endif /* _MKIMG_SCHEME_H_ */