Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Mar 2014 22:45:06 +0000 (UTC)
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r263844 - user/marcel/mkimg
Message-ID:  <201403272245.s2RMj6Eo021537@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: marcel
Date: Thu Mar 27 22:45:05 2014
New Revision: 263844
URL: http://svnweb.freebsd.org/changeset/base/263844

Log:
  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.

Modified:
  user/marcel/mkimg/mkimg.c
  user/marcel/mkimg/mkimg.h
  user/marcel/mkimg/scheme.c
  user/marcel/mkimg/scheme.h

Modified: user/marcel/mkimg/mkimg.c
==============================================================================
--- user/marcel/mkimg/mkimg.c	Thu Mar 27 22:39:22 2014	(r263843)
+++ user/marcel/mkimg/mkimg.c	Thu Mar 27 22:45:05 2014	(r263844)
@@ -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) {
@@ -310,11 +314,18 @@ mkimg(int bfd)
 			break;
 		}
 		if (error)
-			errc(EX_IOERR, error, "partition %d", part->index+1);
+			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);

Modified: user/marcel/mkimg/mkimg.h
==============================================================================
--- user/marcel/mkimg/mkimg.h	Thu Mar 27 22:39:22 2014	(r263843)
+++ user/marcel/mkimg/mkimg.h	Thu Mar 27 22:45:05 2014	(r263844)
@@ -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_ */

Modified: user/marcel/mkimg/scheme.c
==============================================================================
--- user/marcel/mkimg/scheme.c	Thu Mar 27 22:39:22 2014	(r263843)
+++ user/marcel/mkimg/scheme.c	Thu Mar 27 22:45:05 2014	(r263844)
@@ -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);
 

Modified: user/marcel/mkimg/scheme.h
==============================================================================
--- user/marcel/mkimg/scheme.h	Thu Mar 27 22:39:22 2014	(r263843)
+++ user/marcel/mkimg/scheme.h	Thu Mar 27 22:45:05 2014	(r263844)
@@ -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_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201403272245.s2RMj6Eo021537>