diff options
author | marcel <marcel@FreeBSD.org> | 2014-05-06 21:54:05 +0000 |
---|---|---|
committer | marcel <marcel@FreeBSD.org> | 2014-05-06 21:54:05 +0000 |
commit | f10e28b41fb321b32c2b40e964d002b0cd1cc51a (patch) | |
tree | 5adf257aebb562c20d42698029d2d87f82498a9a | |
parent | c4870b40524e45f4f10ae5da0babdeaa4f1e6959 (diff) | |
download | FreeBSD-src-f10e28b41fb321b32c2b40e964d002b0cd1cc51a.zip FreeBSD-src-f10e28b41fb321b32c2b40e964d002b0cd1cc51a.tar.gz |
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.
-rw-r--r-- | usr.bin/mkimg/apm.c | 8 | ||||
-rw-r--r-- | usr.bin/mkimg/bsd.c | 6 | ||||
-rw-r--r-- | usr.bin/mkimg/ebr.c | 6 | ||||
-rw-r--r-- | usr.bin/mkimg/gpt.c | 21 | ||||
-rw-r--r-- | usr.bin/mkimg/mbr.c | 6 | ||||
-rw-r--r-- | usr.bin/mkimg/mkimg.c | 15 | ||||
-rw-r--r-- | usr.bin/mkimg/mkimg.h | 2 | ||||
-rw-r--r-- | usr.bin/mkimg/pc98.c | 6 | ||||
-rw-r--r-- | usr.bin/mkimg/vtoc8.c | 6 |
9 files changed, 25 insertions, 51 deletions
diff --git a/usr.bin/mkimg/apm.c b/usr.bin/mkimg/apm.c index a955656..36d6ab8 100644 --- a/usr.bin/mkimg/apm.c +++ b/usr.bin/mkimg/apm.c @@ -69,7 +69,6 @@ apm_write(int fd, lba_t imgsz, void *bootcode __unused) 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 *bootcode __unused) 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); } diff --git a/usr.bin/mkimg/bsd.c b/usr.bin/mkimg/bsd.c index 052e6c1..df99130 100644 --- a/usr.bin/mkimg/bsd.c +++ b/usr.bin/mkimg/bsd.c @@ -111,11 +111,7 @@ bsd_write(int fd, lba_t imgsz, void *bootcode) 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); } diff --git a/usr.bin/mkimg/ebr.c b/usr.bin/mkimg/ebr.c index 33aacc2..372dbbd 100644 --- a/usr.bin/mkimg/ebr.c +++ b/usr.bin/mkimg/ebr.c @@ -104,11 +104,7 @@ ebr_write(int fd, lba_t imgsz __unused, void *bootcode __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; diff --git a/usr.bin/mkimg/gpt.c b/usr.bin/mkimg/gpt.c index 9657a55..5385228 100644 --- a/usr.bin/mkimg/gpt.c +++ b/usr.bin/mkimg/gpt.c @@ -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 *bootcode) 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 *hdr, uint64_t self, uint64_t alt, 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 *bootcode) 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; diff --git a/usr.bin/mkimg/mbr.c b/usr.bin/mkimg/mbr.c index ba8476c..0f89057 100644 --- a/usr.bin/mkimg/mbr.c +++ b/usr.bin/mkimg/mbr.c @@ -96,11 +96,7 @@ mbr_write(int fd, lba_t imgsz __unused, void *bootcode) 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); } diff --git a/usr.bin/mkimg/mkimg.c b/usr.bin/mkimg/mkimg.c index 7944342..e767471 100644 --- a/usr.bin/mkimg/mkimg.c +++ b/usr.bin/mkimg/mkimg.c @@ -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) { diff --git a/usr.bin/mkimg/mkimg.h b/usr.bin/mkimg/mkimg.h index 7262bdb..3be345d 100644 --- a/usr.bin/mkimg/mkimg.h +++ b/usr.bin/mkimg/mkimg.h @@ -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_ */ diff --git a/usr.bin/mkimg/pc98.c b/usr.bin/mkimg/pc98.c index bce6c3c..7eb0a8e 100644 --- a/usr.bin/mkimg/pc98.c +++ b/usr.bin/mkimg/pc98.c @@ -106,11 +106,7 @@ pc98_write(int fd, lba_t imgsz __unused, void *bootcode) 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); } diff --git a/usr.bin/mkimg/vtoc8.c b/usr.bin/mkimg/vtoc8.c index e5385f6..231f0a2 100644 --- a/usr.bin/mkimg/vtoc8.c +++ b/usr.bin/mkimg/vtoc8.c @@ -103,11 +103,7 @@ vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused) 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); } |