svn commit: r265468 - head/usr.bin/mkimg

Marcel Moolenaar marcel at FreeBSD.org
Tue May 6 21:54:07 UTC 2014


Author: marcel
Date: Tue May  6 21:54:05 2014
New Revision: 265468
URL: http://svnweb.freebsd.org/changeset/base/265468

Log:
  Add mkimg_write() which combines lseek(2) and write(2) and uses
  sector granularity for both offset and length. Have all schemes
  use mkimg_write() instead of mkimg_seek() followed by write(2).
  
  Now that schemes don't use lseek(2) nor write(2) directly, it's
  easier to support output formats other than raw disks.

Modified:
  head/usr.bin/mkimg/apm.c
  head/usr.bin/mkimg/bsd.c
  head/usr.bin/mkimg/ebr.c
  head/usr.bin/mkimg/gpt.c
  head/usr.bin/mkimg/mbr.c
  head/usr.bin/mkimg/mkimg.c
  head/usr.bin/mkimg/mkimg.h
  head/usr.bin/mkimg/pc98.c
  head/usr.bin/mkimg/vtoc8.c

Modified: head/usr.bin/mkimg/apm.c
==============================================================================
--- head/usr.bin/mkimg/apm.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/apm.c	Tue May  6 21:54:05 2014	(r265468)
@@ -69,7 +69,6 @@ apm_write(int fd, lba_t imgsz, void *boo
 	struct apm_ddr *ddr;
 	struct apm_ent *ent;
 	struct part *part;
-	ssize_t nbytes;
 	int error;
 
 	buf = calloc(nparts + 2, secsz);
@@ -100,12 +99,7 @@ apm_write(int fd, lba_t imgsz, void *boo
 			strcpy(ent->ent_name, part->label);
 	}
 
-	error = mkimg_seek(fd, 0);
-	if (error == 0) {
-		nbytes = (nparts + 2) * secsz;
-		if (write(fd, buf, nbytes) != nbytes)
-			error = errno;
-	}
+	error = mkimg_write(fd, 0, buf, nparts + 2);
 	free(buf);
 	return (error);
 }

Modified: head/usr.bin/mkimg/bsd.c
==============================================================================
--- head/usr.bin/mkimg/bsd.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/bsd.c	Tue May  6 21:54:05 2014	(r265468)
@@ -111,11 +111,7 @@ bsd_write(int fd, lba_t imgsz, void *boo
 		checksum ^= le16dec(p);
 	le16enc(&d->d_checksum, checksum);
 
-	error = mkimg_seek(fd, 0);
-	if (error == 0) {
-		if (write(fd, buf, BBSIZE) != BBSIZE)
-			error = errno;
-	}
+	error = mkimg_write(fd, 0, buf, BBSIZE / secsz);
 	free(buf);
 	return (error);
 }

Modified: head/usr.bin/mkimg/ebr.c
==============================================================================
--- head/usr.bin/mkimg/ebr.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/ebr.c	Tue May  6 21:54:05 2014	(r265468)
@@ -104,11 +104,7 @@ ebr_write(int fd, lba_t imgsz __unused, 
 			le32enc(&dp->dp_size, next->size + nsecs);
 		}
 
-		error = mkimg_seek(fd, block);
-		if (error == 0) {
-			if (write(fd, ebr, secsz) != (ssize_t)secsz)
-				error = errno;
-		}
+		error = mkimg_write(fd, block, ebr, 1);
 		if (error)
 			break;
 

Modified: head/usr.bin/mkimg/gpt.c
==============================================================================
--- head/usr.bin/mkimg/gpt.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/gpt.c	Tue May  6 21:54:05 2014	(r265468)
@@ -166,19 +166,6 @@ gpt_metadata(u_int where)
 }
 
 static int
-gpt_filewrite(int fd, lba_t blk, void *buf, ssize_t bufsz)
-{
-	int error;
-
-	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, lba_t blks, void *bootcode)
 {
 	u_char *pmbr;
@@ -203,7 +190,7 @@ gpt_write_pmbr(int fd, lba_t blks, void 
 	le32enc(pmbr + DOSPARTOFF + 8, 1);
 	le32enc(pmbr + DOSPARTOFF + 12, secs);
 	le16enc(pmbr + DOSMAGICOFFSET, DOSMAGIC);
-	error = gpt_filewrite(fd, 0, pmbr, secsz);
+	error = mkimg_write(fd, 0, pmbr, 1);
 	free(pmbr);
 	return (error);
 }
@@ -250,7 +237,7 @@ 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, hdr, secsz));
+	return (mkimg_write(fd, self, hdr, 1));
 }
 
 static int
@@ -273,10 +260,10 @@ gpt_write(int fd, lba_t imgsz, void *boo
 	tbl = gpt_mktbl(tblsz);
 	if (tbl == NULL)
 		return (errno);
-	error = gpt_filewrite(fd, 2, tbl, tblsz * secsz);
+	error = mkimg_write(fd, 2, tbl, tblsz);
 	if (error)
 		goto out;
-	error = gpt_filewrite(fd, imgsz - (tblsz + 1), tbl, tblsz * secsz);
+	error = mkimg_write(fd, imgsz - (tblsz + 1), tbl, tblsz);
 	if (error)
 		goto out;
 

Modified: head/usr.bin/mkimg/mbr.c
==============================================================================
--- head/usr.bin/mkimg/mbr.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/mbr.c	Tue May  6 21:54:05 2014	(r265468)
@@ -96,11 +96,7 @@ mbr_write(int fd, lba_t imgsz __unused, 
 		le32enc(&dp->dp_start, part->block);
 		le32enc(&dp->dp_size, part->size);
 	}
-	error = mkimg_seek(fd, 0);
-	if (error == 0) {
-		if (write(fd, mbr, secsz) != (ssize_t)secsz)
-			error = errno;
-	}
+	error = mkimg_write(fd, 0, mbr, 1);
 	free(mbr);
 	return (error);
 }

Modified: head/usr.bin/mkimg/mkimg.c
==============================================================================
--- head/usr.bin/mkimg/mkimg.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/mkimg.c	Tue May  6 21:54:05 2014	(r265468)
@@ -302,7 +302,7 @@ fdcopy(int src, int dst, uint64_t *count
 	return (errno);
 }
 
-int
+static int
 mkimg_seek(int fd, lba_t blk)
 {
 	off_t off;
@@ -313,6 +313,19 @@ mkimg_seek(int fd, lba_t blk)
 	return (0);
 }
 
+int
+mkimg_write(int fd, lba_t blk, void *buf, ssize_t len)
+{
+
+	blk *= secsz;
+	if (lseek(fd, blk, SEEK_SET) != blk)
+		return (errno);
+	len *= secsz;
+	if (write(fd, buf, len) != len)
+		return (errno);
+	return (0);
+}
+
 static void
 mkimg(int bfd)
 {

Modified: head/usr.bin/mkimg/mkimg.h
==============================================================================
--- head/usr.bin/mkimg/mkimg.h	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/mkimg.h	Tue May  6 21:54:05 2014	(r265468)
@@ -67,6 +67,6 @@ round_block(lba_t n)
 	return ((n + b - 1) & ~(b - 1));
 }
 
-int mkimg_seek(int fd, lba_t blk);
+int mkimg_write(int fd, lba_t blk, void *buf, ssize_t len);
 
 #endif /* _MKIMG_MKIMG_H_ */

Modified: head/usr.bin/mkimg/pc98.c
==============================================================================
--- head/usr.bin/mkimg/pc98.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/pc98.c	Tue May  6 21:54:05 2014	(r265468)
@@ -106,11 +106,7 @@ pc98_write(int fd, lba_t imgsz __unused,
 		if (part->label != NULL)
 			memcpy(dp->dp_name, part->label, strlen(part->label));
 	}
-	error = mkimg_seek(fd, 0);
-	if (error == 0) {
-		if (write(fd, buf, PC98_BOOTCODESZ) != PC98_BOOTCODESZ)
-			error = errno;
-	}
+	error = mkimg_write(fd, 0, buf, PC98_BOOTCODESZ / secsz);
 	free(buf);
 	return (error);
 }

Modified: head/usr.bin/mkimg/vtoc8.c
==============================================================================
--- head/usr.bin/mkimg/vtoc8.c	Tue May  6 21:51:16 2014	(r265467)
+++ head/usr.bin/mkimg/vtoc8.c	Tue May  6 21:54:05 2014	(r265468)
@@ -103,11 +103,7 @@ vtoc8_write(int fd, lba_t imgsz, void *b
 		sum ^= be16dec(p + ofs);
 	be16enc(&vtoc8.cksum, sum);
 
-	error = mkimg_seek(fd, 0);
-	if (error == 0) {
-		if (write(fd, &vtoc8, sizeof(vtoc8)) != sizeof(vtoc8))
-			error = errno;
-	}
+	error = mkimg_write(fd, 0, &vtoc8, 1);
 	return (error);
 }
 


More information about the svn-src-head mailing list