$NetBSD: patch-di,v 1.1 2008/03/03 16:45:27 hannken Exp $ --- libfsimage/common/fsimage_grub.c.orig 2008-01-31 11:06:54.000000000 +0100 +++ libfsimage/common/fsimage_grub.c 2008-03-01 18:59:27.000000000 +0100 @@ -204,6 +204,45 @@ int fsig_devread(fsi_file_t *ffi, unsigned int sector, unsigned int offset, unsigned int bufsize, char *buf) { +#ifdef __NetBSD__ + off_t off; + ssize_t ret; + int n, r; + char tmp[512]; + + off = ffi->ff_fsi->f_off + ((off_t)sector * 512) + offset; + + /* + * On NetBSD reads from a raw disk must be sector-aligned. + * Split the read into up to three parts to meet this requirement. + */ + + if ((n = (off & 511)) > 0) { + if ((r = 512 - n) > bufsize) + r = bufsize; + if ((ret = pread(ffi->ff_fsi->f_fd, tmp, 512, off - n)) < n + r) + return (0); + bcopy(tmp + n, buf, r); + buf += r; + bufsize -= r; + off += r; + } + + if ((n = (bufsize & ~511)) > 0) { + if ((ret = pread(ffi->ff_fsi->f_fd, buf, n, off)) < n) + return (0); + buf += n; + bufsize -= n; + off += n; + } + if (bufsize > 0) { + if ((ret = pread(ffi->ff_fsi->f_fd, tmp, 512, off)) < bufsize) + return (0); + bcopy(tmp, buf, bufsize); + } + + return (1); +#else /* __NetBSD__ */ uint64_t off = ffi->ff_fsi->f_off + ((uint64_t)sector * 512) + offset; ssize_t bytes_read = 0; @@ -220,6 +259,7 @@ fsig_devread(fsi_file_t *ffi, unsigned i } return (1); +#endif /* __NetBSD__ */ } int