From 85841216d308c824f4b1a02819c83ea2d36565c6 Mon Sep 17 00:00:00 2001 From: jhb Date: Thu, 1 Nov 2007 20:06:12 +0000 Subject: Fix 'gpt boot' to work on disk devices and not just plain files. Writes to disk devices have to consist of a block of sectors. Thus, when writing gptboot to the boot partition, round the size of the gptboot file up to a sector boundary, pre-zero it, and write out the full buffer to disk. --- sbin/gpt/boot.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'sbin') diff --git a/sbin/gpt/boot.c b/sbin/gpt/boot.c index ff9d4a0..b4a151a 100644 --- a/sbin/gpt/boot.c +++ b/sbin/gpt/boot.c @@ -168,7 +168,6 @@ boot(int fd) warn("unable to open GPT boot loader"); return; } - /* Fourth step: find an existing boot partition or create one. */ if (gpt_find(&boot_uuid, &gptboot) != 0) @@ -191,8 +190,14 @@ boot(int fd) return; } - /* Fourth step, write out the gptboot binary to the boot partition. */ - buf = malloc(sb.st_size); + /* + * Fourth step, write out the gptboot binary to the boot partition. + * When writing to a disk device, the write must be sector aligned + * and not write to any partial sectors, so round up the buffer size + * to the next sector and zero it. + */ + bsize = (sb.st_size + secsz - 1) / secsz * secsz; + buf = calloc(1, bsize); nbytes = read(bfd, buf, sb.st_size); if (nbytes < 0) { warn("unable to read GPT boot loader"); @@ -209,12 +214,12 @@ boot(int fd) device_name); return; } - nbytes = write(fd, buf, sb.st_size); + nbytes = write(fd, buf, bsize); if (nbytes < 0) { warn("unable to write GPT boot loader"); return; } - if (nbytes != sb.st_size) { + if (nbytes != bsize) { warnx("short write of GPT boot loader"); return; } -- cgit v1.1