svn commit: r263653 - user/marcel/mkimg

Marcel Moolenaar marcel at FreeBSD.org
Sun Mar 23 01:10:08 UTC 2014


Author: marcel
Date: Sun Mar 23 01:10:05 2014
New Revision: 263653
URL: http://svnweb.freebsd.org/changeset/base/263653

Log:
  Revamp:
  1.  Make secsz globally visible.
  2.  Have all code use the global secsz and nparts, rather than passing
      them around.
  3.  Introduce lba_t as the type to use when talking about block addresses.
  4.  Work on LBAs instead of offsets. There's just too much division with
      the sector size and there's really no value to use by offsets other
      than that is what lseek() wants. For that we now have mkimg_seek().
  
  The bigger picture is that geometry (cylinders, heads, sectors/track) and
  also a possible physical sector size are to be globals that can be used
  from anyway. We really don't want to pass all that stuff as arguments to
  functions and then add __unused for most of them...

Modified:
  user/marcel/mkimg/apm.c
  user/marcel/mkimg/bsd.c
  user/marcel/mkimg/ebr.c
  user/marcel/mkimg/gpt.c
  user/marcel/mkimg/mbr.c
  user/marcel/mkimg/mkimg.c
  user/marcel/mkimg/mkimg.h
  user/marcel/mkimg/pc98.c
  user/marcel/mkimg/scheme.c
  user/marcel/mkimg/scheme.h
  user/marcel/mkimg/vtoc8.c

Modified: user/marcel/mkimg/apm.c
==============================================================================
--- user/marcel/mkimg/apm.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/apm.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -40,17 +40,16 @@ static struct mkimg_alias apm_aliases[] 
 };
 
 static u_int
-apm_metadata(u_int where, u_int parts, u_int secsz __unused)
+apm_metadata(u_int where)
 {
 	u_int secs;
 
-	secs = (where == SCHEME_META_IMG_START) ? parts + 1 : 0;
+	secs = (where == SCHEME_META_IMG_START) ? nparts + 1 : 0;
 	return (secs);
 }
 
 static int
-apm_write(int fd __unused, off_t imgsz __unused, u_int parts __unused,
-    u_int secsz __unused, void *bootcode __unused)
+apm_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
 {
 	return (ENOSYS);
 }

Modified: user/marcel/mkimg/bsd.c
==============================================================================
--- user/marcel/mkimg/bsd.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/bsd.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -40,7 +40,7 @@ static struct mkimg_alias bsd_aliases[] 
 };
 
 static u_int
-bsd_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
+bsd_metadata(u_int where)
 {
 	u_int secs;
 
@@ -49,8 +49,7 @@ bsd_metadata(u_int where, u_int parts __
 }
 
 static int
-bsd_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, 
-    u_int secsz __unused, void *bootcode __unused)
+bsd_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
 {
 	return (ENOSYS);
 }

Modified: user/marcel/mkimg/ebr.c
==============================================================================
--- user/marcel/mkimg/ebr.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/ebr.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -40,7 +40,7 @@ static struct mkimg_alias ebr_aliases[] 
 };
 
 static u_int
-ebr_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
+ebr_metadata(u_int where)
 {
 	u_int secs;
 
@@ -49,8 +49,7 @@ ebr_metadata(u_int where, u_int parts __
 }
 
 static int
-ebr_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, 
-    u_int secsz __unused, void *bootcode __unused)
+ebr_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
 {
 	return (ENOSYS);
 }

Modified: user/marcel/mkimg/gpt.c
==============================================================================
--- user/marcel/mkimg/gpt.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/gpt.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -124,46 +124,48 @@ crc32(const void *buf, size_t sz)
 }
 
 static u_int
-gpt_tblsz(u_int parts, u_int secsz)
+gpt_tblsz()
 {
 	u_int ents;
 
 	ents = secsz / sizeof(struct gpt_ent);
-	return ((parts + ents - 1) / ents);
+	return ((nparts + ents - 1) / ents);
 }
 
 static u_int
-gpt_metadata(u_int where, u_int parts, u_int secsz)
+gpt_metadata(u_int where)
 {
 	u_int secs;
 
 	if (where != SCHEME_META_IMG_START && where != SCHEME_META_IMG_END)
 		return (0);
 
-	secs = gpt_tblsz(parts, secsz);
+	secs = gpt_tblsz();
 	secs += (where == SCHEME_META_IMG_START) ? 2 : 1;
 	return (secs);
 }
 
 static int
-gpt_filewrite(int fd, off_t ofs, void *buf, ssize_t bufsz)
+gpt_filewrite(int fd, lba_t blk, void *buf, ssize_t bufsz)
 {
+	int error;
 
-	if (lseek(fd, ofs, SEEK_SET) != ofs)
-		return (errno);
-	if (write(fd, buf, bufsz) != bufsz)
-		return (errno);
-	return (0);
+	error = mkimg_seek(fd, blk);
+	if (error == 0) {
+		if (write(fd, buf, bufsz) != bufsz)
+			error = errno;
+	}
+	return (error);
 }
 
 static int
-gpt_write_pmbr(int fd, off_t nblocks, u_int secsz, void *bootcode)
+gpt_write_pmbr(int fd, lba_t blks, void *bootcode)
 {
 	u_char *pmbr;
 	uint32_t secs;
 	int error;
 
-	secs = (nblocks > UINT32_MAX) ? UINT32_MAX : nblocks;
+	secs = (blks > UINT32_MAX) ? UINT32_MAX : (uint32_t)blks;
 
 	pmbr = malloc(secsz);
 	if (pmbr == NULL)
@@ -187,12 +189,11 @@ gpt_write_pmbr(int fd, off_t nblocks, u_
 }
 
 static struct gpt_ent *
-gpt_mktbl(u_int tblsz, u_int secsz)
+gpt_mktbl(u_int tblsz)
 {
 	uuid_t uuid;
 	struct gpt_ent *tbl, *ent;
 	struct part *part;
-	uint64_t limit;
 	int c, idx;
 
 	tbl = calloc(tblsz, secsz);
@@ -204,9 +205,8 @@ gpt_mktbl(u_int tblsz, u_int secsz)
 		uuid_enc_le(&ent->ent_type, ALIAS_TYPE2PTR(part->type));
 		uuidgen(&uuid, 1);
 		uuid_enc_le(&ent->ent_uuid, &uuid);
-		le64enc(&ent->ent_lba_start, part->offset / secsz);
-		limit = (part->offset + part->size) / secsz;
-		le64enc(&ent->ent_lba_end, limit - 1);
+		le64enc(&ent->ent_lba_start, part->block);
+		le64enc(&ent->ent_lba_end, part->block + part->size - 1);
 		if (part->label != NULL) {
 			idx = 0;
 			while ((c = part->label[idx]) != '\0') {
@@ -220,7 +220,7 @@ gpt_mktbl(u_int tblsz, u_int secsz)
 
 static int
 gpt_write_hdr(int fd, struct gpt_hdr *hdr, uint64_t self, uint64_t alt,
-    uint64_t tbl, u_int secsz)
+    uint64_t tbl)
 {
 	uint32_t crc;
 
@@ -230,37 +230,33 @@ gpt_write_hdr(int fd, struct gpt_hdr *hd
 	hdr->hdr_crc_self = 0;
 	crc = crc32(hdr, offsetof(struct gpt_hdr, padding));
 	le64enc(&hdr->hdr_crc_self, crc);
-	return (gpt_filewrite(fd, self * secsz, hdr, secsz));
+	return (gpt_filewrite(fd, self, hdr, secsz));
 }
 
 static int
-gpt_write(int fd, off_t imgsz, u_int parts, u_int secsz, void *bootcode)
+gpt_write(int fd, lba_t imgsz, void *bootcode)
 {
 	uuid_t uuid;
 	struct gpt_ent *tbl;
 	struct gpt_hdr *hdr;
-	off_t nblocks;
 	uint32_t crc;
 	u_int tblsz;
 	int error;
 
-	nblocks = imgsz / secsz;
-
 	/* PMBR */
-	error = gpt_write_pmbr(fd, nblocks, secsz, bootcode);
+	error = gpt_write_pmbr(fd, imgsz, bootcode);
 	if (error)
 		return (error);
 
 	/* GPT table(s) */
-	tblsz = gpt_tblsz(parts, secsz);
-	tbl = gpt_mktbl(tblsz, secsz);
+	tblsz = gpt_tblsz();
+	tbl = gpt_mktbl(tblsz);
 	if (tbl == NULL)
 		return (errno);
-	error = gpt_filewrite(fd, 2 * secsz, tbl, tblsz * secsz);
+	error = gpt_filewrite(fd, 2, tbl, tblsz * secsz);
 	if (error)
 		goto out;
-	error = gpt_filewrite(fd, imgsz - (tblsz + 1) * secsz, tbl,
-	    tblsz * secsz);
+	error = gpt_filewrite(fd, imgsz - (tblsz + 1), tbl, tblsz * secsz);
 	if (error)
 		goto out;
 
@@ -275,17 +271,16 @@ gpt_write(int fd, off_t imgsz, u_int par
 	le32enc(&hdr->hdr_revision, GPT_HDR_REVISION);
 	le32enc(&hdr->hdr_size, offsetof(struct gpt_hdr, padding));
 	le64enc(&hdr->hdr_lba_start, 2 + tblsz);
-	le64enc(&hdr->hdr_lba_end, nblocks - tblsz - 2);
+	le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2);
 	uuidgen(&uuid, 1);
 	uuid_enc_le(&hdr->hdr_uuid, &uuid);
-	le32enc(&hdr->hdr_entries, parts);
+	le32enc(&hdr->hdr_entries, nparts);
 	le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent));
-	crc = crc32(tbl, parts * sizeof(struct gpt_ent));
+	crc = crc32(tbl, nparts * sizeof(struct gpt_ent));
 	le32enc(&hdr->hdr_crc_table, crc);
-	error = gpt_write_hdr(fd, hdr, 1, nblocks - 1, 2, secsz);
+	error = gpt_write_hdr(fd, hdr, 1, imgsz - 1, 2);
 	if (!error)
-		error = gpt_write_hdr(fd, hdr, nblocks - 1, 1,
-		    nblocks - tblsz - 1, secsz);
+		error = gpt_write_hdr(fd, hdr, imgsz - 1, 1, imgsz - tblsz - 1);
 	free(hdr);
 
  out:

Modified: user/marcel/mkimg/mbr.c
==============================================================================
--- user/marcel/mkimg/mbr.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/mbr.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -44,7 +44,7 @@ static struct mkimg_alias mbr_aliases[] 
 };
 
 static u_int
-mbr_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
+mbr_metadata(u_int where)
 {
 	u_int secs;
 
@@ -53,12 +53,12 @@ mbr_metadata(u_int where, u_int parts __
 }
 
 static int
-mbr_write(int fd, off_t imgsz __unused, u_int parts __unused, u_int secsz,
-    void *bootcode)
+mbr_write(int fd, lba_t imgsz __unused, void *bootcode)
 {
 	u_char *mbr;
 	struct dos_partition *dpbase, *dp;
 	struct part *part;
+	int error;
 
 	mbr = malloc(secsz);
 	if (mbr == NULL)
@@ -75,15 +75,16 @@ mbr_write(int fd, off_t imgsz __unused, 
 		dp->dp_shd = dp->dp_ssect = dp->dp_scyl = 0xff;	/* XXX */
 		dp->dp_typ = ALIAS_TYPE2INT(part->type);
 		dp->dp_ehd = dp->dp_esect = dp->dp_ecyl = 0xff;	/* XXX */
-		le32enc(&dp[part->index].dp_start, part->offset / secsz);
-		le32enc(&dp[part->index].dp_size, part->size / secsz);
+		le32enc(&dp[part->index].dp_start, part->block);
+		le32enc(&dp[part->index].dp_size, part->size);
 	}
-	if (lseek(fd, 0, SEEK_SET) != 0 || write(fd, mbr, secsz) != secsz) {
-		free(mbr);
-		return (errno);
+	error = mkimg_seek(fd, 0);
+	if (error == 0) {
+		if (write(fd, mbr, secsz) != secsz)
+			error = errno;
 	}
 	free(mbr);
-	return (0);
+	return (error);
 }
 
 static struct mkimg_scheme mbr_scheme = {

Modified: user/marcel/mkimg/mkimg.c
==============================================================================
--- user/marcel/mkimg/mkimg.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/mkimg.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -49,6 +49,8 @@ __FBSDID("$FreeBSD$");
 struct partlisthead partlist = STAILQ_HEAD_INITIALIZER(partlist);
 u_int nparts = 0;
 
+u_int secsz = 512;
+
 static int bcfd = -1;
 static int outfd = 0;
 static int tmpfd = -1;
@@ -221,13 +223,24 @@ fdcopy(int src, int dst, uint64_t *count
 	return (errno);
 }
 
+int
+mkimg_seek(int fd, lba_t blk)
+{
+	off_t off;
+
+	off = blk * secsz;
+	if (lseek(fd, off, SEEK_SET) != off)
+		return (errno);
+	return (0);
+}
+
 static void
 mkimg(int bfd)
 {
 	FILE *fp;
 	struct part *part;
-	off_t offset;
-	uint64_t size;
+	lba_t block;
+	off_t bytesize;
 	int error, fd;
 
 	if (nparts > scheme_max_parts())
@@ -245,22 +258,19 @@ mkimg(int bfd)
 			errc(EX_DATAERR, error, "partition %d", part->index+1);
 	}
 
-	offset = scheme_first_offset(nparts);
+	block = scheme_first_block();
 	STAILQ_FOREACH(part, &partlist, link) {
-		part->offset = offset;
-		lseek(tmpfd, offset, SEEK_SET);
-		/* XXX check error */
-
-		error = 0;
+		part->block = block;
+		error = mkimg_seek(tmpfd, block);
 		switch (part->kind) {
 		case PART_KIND_SIZE:
-			if (expand_number(part->contents, &size) == -1)
+			if (expand_number(part->contents, &bytesize) == -1)
 				error = errno;
 			break;
 		case PART_KIND_FILE:
 			fd = open(part->contents, O_RDONLY, 0);
 			if (fd != -1) {
-				error = fdcopy(fd, tmpfd, &size);
+				error = fdcopy(fd, tmpfd, &bytesize);
 				close(fd);
 			} else
 				error = errno;
@@ -268,7 +278,7 @@ mkimg(int bfd)
 		case PART_KIND_PIPE:
 			fp = popen(part->contents, "r");
 			if (fp != NULL) {
-				error = fdcopy(fileno(fp), tmpfd, &size);
+				error = fdcopy(fileno(fp), tmpfd, &bytesize);
 				pclose(fp);
 			} else
 				error = errno;
@@ -276,12 +286,11 @@ mkimg(int bfd)
 		}
 		if (error)
 			errc(EX_IOERR, error, "partition %d", part->index+1);
-		size = scheme_round(size);
-		part->size = size;
-		offset = scheme_next_offset(offset, size);
+		part->size = (bytesize + secsz - 1) / secsz;
+		block = scheme_next_block(part->block, part->size);
 	}
 
-	error = (scheme_write(tmpfd, offset));
+	error = (scheme_write(tmpfd, block));
 }
 
 int

Modified: user/marcel/mkimg/mkimg.h
==============================================================================
--- user/marcel/mkimg/mkimg.h	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/mkimg.h	Sun Mar 23 01:10:05 2014	(r263653)
@@ -31,6 +31,8 @@
 
 #include <sys/queue.h>
 
+typedef int64_t	lba_t;
+
 struct part {
 	STAILQ_ENTRY(part) link;
 	char	*alias;		/* Partition type alias. */
@@ -42,12 +44,16 @@ struct part {
 #define	PART_KIND_SIZE	3
 	u_int	index;		/* Partition index (0-based). */
 	uintptr_t type;		/* Scheme-specific partition type. */
-	off_t	offset;		/* Byte-offset of partition in image. */
-	off_t	size;		/* Size in bytes of partition. */
+	lba_t	block;		/* Block-offset of partition in image. */
+	lba_t	size;		/* Size in blocks of partition. */
 	char	*label;		/* Partition label. */
 };
 
 extern STAILQ_HEAD(partlisthead, part) partlist;
 extern u_int nparts;
 
+extern u_int secsz;
+
+int mkimg_seek(int fd, lba_t blk);
+
 #endif /* _MKIMG_MKIMG_H_ */

Modified: user/marcel/mkimg/pc98.c
==============================================================================
--- user/marcel/mkimg/pc98.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/pc98.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -40,7 +40,7 @@ static struct mkimg_alias pc98_aliases[]
 };
 
 static u_int
-pc98_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
+pc98_metadata(u_int where)
 {
 	u_int secs;
 
@@ -49,8 +49,7 @@ pc98_metadata(u_int where, u_int parts _
 }
 
 static int
-pc98_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, 
-    u_int secsz __unused, void *bootcode __unused)
+pc98_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
 {
 	return (ENOSYS);
 }

Modified: user/marcel/mkimg/scheme.c
==============================================================================
--- user/marcel/mkimg/scheme.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/scheme.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -58,7 +58,6 @@ static struct {
 };
 
 static struct mkimg_scheme *scheme;
-static u_int secsz = 512;
 static void *bootcode;
 
 static enum alias
@@ -132,10 +131,6 @@ scheme_check_part(struct part *p)
 	struct mkimg_alias *iter;
 	enum alias alias;
 
-	warnx("part(%s): index=%u, type=`%s', offset=%ju, size=%ju",
-	    scheme->name, p->index, p->alias, (uintmax_t)p->offset,
-	    (uintmax_t)p->size);
-
 	/* Check the partition type alias */
 	alias = scheme_parse_alias(p->alias);
 	if (alias == ALIAS_NONE)
@@ -167,50 +162,37 @@ scheme_max_parts(void)
 	return (scheme->nparts);
 }
 
-uint64_t
-scheme_round(uint64_t sz)
-{
-
-	sz = (sz + secsz - 1) & ~(secsz - 1);
-	return (sz);
-}
-
-off_t
-scheme_first_offset(u_int parts)
+lba_t
+scheme_first_block(void)
 {
-	u_int secs;
+	lba_t blks;
 
-	secs = scheme->metadata(SCHEME_META_IMG_START, parts, secsz) +
-	    scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
-	return (secs * secsz);
+	blks = scheme->metadata(SCHEME_META_IMG_START) +
+	    scheme->metadata(SCHEME_META_PART_BEFORE);
+	return (blks);
 }
 
-off_t
-scheme_next_offset(off_t off, uint64_t sz)
+lba_t
+scheme_next_block(lba_t start, lba_t size)
 {
-	u_int secs;
+	lba_t blks;
 
-	secs = scheme->metadata(SCHEME_META_PART_AFTER, 0, secsz) +
-	    scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz);
-	sz += (secs * secsz);
-	return (off + sz);
+	blks = scheme->metadata(SCHEME_META_PART_AFTER) +
+	    scheme->metadata(SCHEME_META_PART_BEFORE);
+	return (start + size + blks);
 }
 
 int
-scheme_write(int fd, off_t off)
+scheme_write(int fd, lba_t end)
 {
-	u_int secs;
 	int error;
 
-	/* 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);
-	if (ftruncate(fd, off) == -1)
+	/* Fixup block: it has an extra metadata before the partition */
+	end -= scheme->metadata(SCHEME_META_PART_BEFORE);
+	end += scheme->metadata(SCHEME_META_IMG_END);
+	if (ftruncate(fd, end * secsz) == -1)
 		return (errno);
 
-	error = scheme->write(fd, off, nparts, secsz, bootcode);
+	error = scheme->write(fd, end, bootcode);
 	return (error);
 }

Modified: user/marcel/mkimg/scheme.h
==============================================================================
--- user/marcel/mkimg/scheme.h	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/scheme.h	Sun Mar 23 01:10:05 2014	(r263653)
@@ -60,12 +60,12 @@ struct mkimg_scheme {
 	const char	*name;
 	const char	*description;
 	struct mkimg_alias *aliases;
-	u_int		(*metadata)(u_int, u_int, u_int);
+	u_int		(*metadata)(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		(*write)(int, off_t, u_int, u_int, void *);
+	int		(*write)(int, lba_t, void *);
 	u_int		nparts;
 	u_int		labellen;
 	u_int		bootcode;
@@ -80,9 +80,8 @@ struct mkimg_scheme *scheme_selected(voi
 int scheme_bootcode(int fd);
 int scheme_check_part(struct part *);
 u_int scheme_max_parts(void);
-uint64_t scheme_round(uint64_t);
-off_t scheme_first_offset(u_int);
-off_t scheme_next_offset(off_t, uint64_t);
-int scheme_write(int, off_t);
+lba_t scheme_first_block(void);
+lba_t scheme_next_block(lba_t, lba_t);
+int scheme_write(int, lba_t);
 
 #endif /* _MKIMG_SCHEME_H_ */

Modified: user/marcel/mkimg/vtoc8.c
==============================================================================
--- user/marcel/mkimg/vtoc8.c	Sat Mar 22 23:34:35 2014	(r263652)
+++ user/marcel/mkimg/vtoc8.c	Sun Mar 23 01:10:05 2014	(r263653)
@@ -40,7 +40,7 @@ static struct mkimg_alias vtoc8_aliases[
 };
 
 static u_int
-vtoc8_metadata(u_int where, u_int parts __unused, u_int secsz __unused)
+vtoc8_metadata(u_int where)
 {
 	u_int secs;
 
@@ -49,8 +49,7 @@ vtoc8_metadata(u_int where, u_int parts 
 }
 
 static int
-vtoc8_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, 
-    u_int secsz __unused, void *bootcode __unused)
+vtoc8_write(int fd __unused, lba_t imgsz __unused, void *bootcode __unused)
 {
 	return (ENOSYS);
 }


More information about the svn-src-user mailing list