diff --git a/usr.bin/mkimg/apm.c b/usr.bin/mkimg/apm.c index a9556564f5d5..36d6ab8d289c 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 052e6c16de5e..df9913076d94 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 33aacc27741d..372dbbd4c337 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 9657a555fafb..5385228b6fb5 100644 --- a/usr.bin/mkimg/gpt.c +++ b/usr.bin/mkimg/gpt.c @@ -165,19 +165,6 @@ gpt_metadata(u_int where) return (secs); } -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) { @@ -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 ba8476c3b42b..0f890576a6f9 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 79443426f281..e767471e502b 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 7262bdb5451d..3be345d7cd53 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 bce6c3cea6b0..7eb0a8e31265 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 e5385f6b7d16..231f0a296ad3 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); }