diff options
author | jhb <jhb@FreeBSD.org> | 2007-11-01 20:06:12 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2007-11-01 20:06:12 +0000 |
commit | 85841216d308c824f4b1a02819c83ea2d36565c6 (patch) | |
tree | 6285a9d8e2f838098e01308dd01c79587242dae2 | |
parent | c2e7fe96311240114be15c33991eb8f81b4bc6f8 (diff) | |
download | FreeBSD-src-85841216d308c824f4b1a02819c83ea2d36565c6.zip FreeBSD-src-85841216d308c824f4b1a02819c83ea2d36565c6.tar.gz |
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.
-rw-r--r-- | sbin/gpt/boot.c | 15 |
1 files changed, 10 insertions, 5 deletions
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; } |