summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkrw <krw@openbsd.org>2015-03-29 21:16:39 +0000
committerkrw <krw@openbsd.org>2015-03-29 21:16:39 +0000
commite6bc9b7aa550ea6b3c68013e08f96c97d3e898ab (patch)
tree96670be9fce1b5752fbbaf9926511678c8f06318
parentEscape punctuation characters that have a different meaning in -Tpdf. (diff)
downloadwireguard-openbsd-e6bc9b7aa550ea6b3c68013e08f96c97d3e898ab.tar.xz
wireguard-openbsd-e6bc9b7aa550ea6b3c68013e08f96c97d3e898ab.zip
Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more than MBR reading/writing. No intentional functional change.
-rw-r--r--sbin/fdisk/mbr.c71
-rw-r--r--sbin/fdisk/mbr.h4
-rw-r--r--sbin/fdisk/misc.c64
-rw-r--r--sbin/fdisk/misc.h4
4 files changed, 75 insertions, 68 deletions
diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c
index de7db1c36b3..87f201e02f3 100644
--- a/sbin/fdisk/mbr.c
+++ b/sbin/fdisk/mbr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbr.c,v 1.49 2015/03/29 19:11:23 krw Exp $ */
+/* $OpenBSD: mbr.c,v 1.50 2015/03/29 21:16:39 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -173,7 +173,7 @@ MBR_read(int fd, off_t where, struct dos_mbr *dos_mbr)
{
char *secbuf;
- secbuf = MBR_readsector(fd, where);
+ secbuf = readsector(fd, where);
if (secbuf == NULL)
return (-1);
@@ -188,7 +188,7 @@ MBR_write(int fd, off_t where, struct dos_mbr *dos_mbr)
{
char *secbuf;
- secbuf = MBR_readsector(fd, where);
+ secbuf = readsector(fd, where);
if (secbuf == NULL)
return (-1);
@@ -197,7 +197,7 @@ MBR_write(int fd, off_t where, struct dos_mbr *dos_mbr)
* write the sector back to "disk".
*/
memcpy(secbuf, dos_mbr, sizeof(*dos_mbr));
- MBR_writesector(fd, secbuf, where);
+ writesector(fd, secbuf, where);
ioctl(fd, DIOCRLDINFO, 0);
free(secbuf);
@@ -230,61 +230,6 @@ MBR_pcopy(struct mbr *mbr)
}
/*
- * Read the sector at 'where' into a sector sized buf and return the latter.
- */
-char *
-MBR_readsector(int fd, off_t where)
-{
- const int secsize = unit_types[SECTORS].conversion;
- char *secbuf;
- ssize_t len;
- off_t off;
-
- where *= secsize;
- off = lseek(fd, where, SEEK_SET);
- if (off != where)
- return (NULL);
-
- secbuf = calloc(1, secsize);
- if (secbuf == NULL)
- return (NULL);
-
- len = read(fd, secbuf, secsize);
- if (len == -1 || len != secsize) {
- free(secbuf);
- return (NULL);
- }
-
- return (secbuf);
-}
-
-/*
- * Write the sector sized 'secbuf' to the sector at 'where'.
- */
-int
-MBR_writesector(int fd, char *secbuf, off_t where)
-{
- const int secsize = unit_types[SECTORS].conversion;
- ssize_t len;
- off_t off;
-
- len = -1;
-
- where *= secsize;
- off = lseek(fd, where, SEEK_SET);
- if (off == where)
- len = write(fd, secbuf, secsize);
-
- if (len == -1 || len != secsize) {
- /* short read or write */
- errno = EIO;
- return (-1);
- }
-
- return (0);
-}
-
-/*
* If *dos_mbr has a 0xee or 0xef partition, nothing needs to happen. If no
* such partition is present but the first or last sector on the disk has a
* GPT, zero the GPT to ensure the MBR takes priority and fewer BIOSes get
@@ -305,25 +250,25 @@ MBR_zapgpt(int fd, struct dos_mbr *dos_mbr, uint64_t lastsec)
(dos_parts[i].dp_typ == DOSPTYP_EFISYS))
return;
- secbuf = MBR_readsector(fd, GPTSECTOR);
+ secbuf = readsector(fd, GPTSECTOR);
if (secbuf == NULL)
return;
memcpy(&sig, secbuf, sizeof(sig));
if (letoh64(sig) == GPTSIGNATURE) {
memset(secbuf, 0, sizeof(sig));
- MBR_writesector(fd, secbuf, GPTSECTOR);
+ writesector(fd, secbuf, GPTSECTOR);
}
free(secbuf);
- secbuf = MBR_readsector(fd, lastsec);
+ secbuf = readsector(fd, lastsec);
if (secbuf == NULL)
return;
memcpy(&sig, secbuf, sizeof(sig));
if (letoh64(sig) == GPTSIGNATURE) {
memset(secbuf, 0, sizeof(sig));
- MBR_writesector(fd, secbuf, lastsec);
+ writesector(fd, secbuf, lastsec);
}
free(secbuf);
}
diff --git a/sbin/fdisk/mbr.h b/sbin/fdisk/mbr.h
index a73568f640e..0ad1c727b89 100644
--- a/sbin/fdisk/mbr.h
+++ b/sbin/fdisk/mbr.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbr.h,v 1.21 2015/03/18 14:46:59 krw Exp $ */
+/* $OpenBSD: mbr.h,v 1.22 2015/03/29 21:16:39 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -37,8 +37,6 @@ void MBR_init(struct mbr *);
int MBR_read(int, off_t, struct dos_mbr *);
int MBR_write(int, off_t, struct dos_mbr *);
void MBR_pcopy(struct mbr *);
-char *MBR_readsector(int, off_t);
-int MBR_writesector(int, char *, off_t);
void MBR_zapgpt(int, struct dos_mbr *, uint64_t);
#endif /* _MBR_H */
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c
index d80427f38e3..cc851825768 100644
--- a/sbin/fdisk/misc.c
+++ b/sbin/fdisk/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.49 2015/03/28 13:29:16 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.50 2015/03/29 21:16:39 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <err.h>
#include <errno.h>
@@ -364,3 +365,64 @@ crc32(const u_char *buf, const u_int32_t size)
return ~crc;
}
+
+/*
+ * Read the sector at 'where' from the file descriptor 'fd' into newly
+ * calloc'd memory. Return a pointer to the memory if it contains the
+ * requested data, or NULL if it does not.
+ *
+ * The caller must free() the memory it gets.
+ */
+char *
+readsector(int fd, off_t where)
+{
+ const int secsize = unit_types[SECTORS].conversion;
+ char *secbuf;
+ ssize_t len;
+ off_t off;
+
+ where *= secsize;
+ off = lseek(fd, where, SEEK_SET);
+ if (off != where)
+ return (NULL);
+
+ secbuf = calloc(1, secsize);
+ if (secbuf == NULL)
+ return (NULL);
+
+ len = read(fd, secbuf, secsize);
+ if (len == -1 || len != secsize) {
+ free(secbuf);
+ return (NULL);
+ }
+
+ return (secbuf);
+}
+
+/*
+ * Write the sector-sized 'secbuf' to the sector 'where' on the file
+ * descriptor 'fd'. Return 0 if the write works. Return -1 and set
+ * errno if the write fails.
+ */
+int
+writesector(int fd, char *secbuf, off_t where)
+{
+ const int secsize = unit_types[SECTORS].conversion;
+ ssize_t len;
+ off_t off;
+
+ len = -1;
+
+ where *= secsize;
+ off = lseek(fd, where, SEEK_SET);
+ if (off == where)
+ len = write(fd, secbuf, secsize);
+
+ if (len == -1 || len != secsize) {
+ /* short read or write */
+ errno = EIO;
+ return (-1);
+ }
+
+ return (0);
+}
diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h
index ee5b07048c4..e451e50d9b8 100644
--- a/sbin/fdisk/misc.h
+++ b/sbin/fdisk/misc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.h,v 1.28 2015/03/28 13:29:16 krw Exp $ */
+/* $OpenBSD: misc.h,v 1.29 2015/03/29 21:16:39 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -36,5 +36,7 @@ char *ask_string(const char *, const char *);
int ask_yn(const char *);
u_int64_t getuint64(char *, u_int64_t, u_int64_t);
u_int32_t crc32(const u_char *, const u_int32_t);
+char *readsector(int, off_t);
+int writesector(int, char *, off_t);
#endif /* _MISC_H */