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.
This commit is contained in:
Marcel Moolenaar 2014-05-06 21:54:05 +00:00
parent 4d51df7409
commit 789a10b106
9 changed files with 25 additions and 51 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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)
{

View File

@ -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_ */

View File

@ -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);
}

View File

@ -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);
}