[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fdisk diffs



The last response on testing fdisk diffs was abismal, so this time I'm posting
them straight to the list.  Please test these, and let me know how they work
(or not) out for you.

These largely fix some error handling, some of the 1023 cylinder limit handling,
and also get rid of the need to have /usr/mdec/mbr exist, which in turn might
make more space available on the boot floppy for other things.

--Toby.


Index: disk.c
===================================================================
RCS file: /cvs/src/sbin/fdisk/disk.c,v
retrieving revision 1.12
diff -u -r1.12 disk.c
--- disk.c	2001/05/21 22:47:53	1.12
+++ disk.c	2001/05/30 04:41:11
@@ -1,7 +1,7 @@
 /*	$OpenBSD: disk.c,v 1.12 2001/05/21 22:47:53 angelos Exp $	*/
 
 /*
- * Copyright (c) 1997 Tobias Weingartner
+ * Copyright (c) 1997, 2001 Tobias Weingartner
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -58,9 +58,9 @@
 	struct stat st;
 
 	fd = opendev(disk, mode, OPENDEV_PART, NULL);
-	if (fd < 0)
+	if (fd == -1)
 		err(1, "%s", disk);
-	if (fstat(fd, &st) < 0)
+	if (fstat(fd, &st) == -1)
 		err(1, "%s", disk);
 	if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
 		err(1, "%s is not a character device or a regular file", disk);
@@ -89,10 +89,10 @@
 	int fd;
 
 	/* Get label metrics */
-	if ((fd = DISK_open(name, O_RDONLY)) >= 0) {
+	if ((fd = DISK_open(name, O_RDONLY)) != -1) {
 		lm = malloc(sizeof(DISK_metrics));
 
-		if (ioctl(fd, DIOCGDINFO, &dl) < 0) {
+		if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
 			warn("DIOCGDINFO");
 			free(lm);
 			lm = NULL;
@@ -125,7 +125,7 @@
 	int mib[4], size, fd;
 	dev_t devno;
 
-	if ((fd = DISK_open(name, O_RDONLY)) < 0)
+	if ((fd = DISK_open(name, O_RDONLY)) == -1)
 		return (NULL);
 	fstat(fd, &st);
 	DISK_close(fd);
@@ -135,7 +135,7 @@
 	mib[1] = CPU_CHR2BLK;
 	mib[2] = st.st_rdev;
 	size = sizeof(devno);
-	if (sysctl(mib, 3, &devno, &size, NULL, 0) < 0) {
+	if (sysctl(mib, 3, &devno, &size, NULL, 0) == -1) {
 		warn("sysctl(machdep.chr2blk)");
 		return (NULL);
 	}
@@ -146,7 +146,7 @@
 	mib[2] = BIOS_DISKINFO;
 	mib[3] = devno;
 	size = sizeof(di);
-	if (sysctl(mib, 4, &di, &size, NULL, 0) < 0) {
+	if (sysctl(mib, 4, &di, &size, NULL, 0) == -1) {
 		warn("sysctl(machedep.bios.diskinfo)");
 		return (NULL);
 	}
@@ -195,22 +195,33 @@
 		return (0);
 	}
 
-	/* If we have BIOS geometry, use that */
+	/* Fixup bios metrics to include cylinders past 1023 boundary */
+	if(disk->label && disk->bios){
+		int cyls, secs;
+
+		cyls = disk->label->size / (disk->bios->heads * disk->bios->sectors);
+		secs = cyls * (disk->bios->heads * disk->bios->sectors);
+		if ((disk->label->size - secs) < 0)
+			errx(1, "BIOS fixup botch (%d sectors)", disk->label->size - secs);
+		disk->bios->cylinders = cyls;
+		disk->bios->size = secs;
+	}
+
+	/* If we have a (fixed) BIOS geometry, use that */
 	if (disk->bios) {
 		disk->real = disk->bios;
 		return (0);
 	}
 
-
 	/* If we have a label, use that */
-	if (!disk->real && disk->label)
+	if (disk->label) {
 		disk->real = disk->label;
+		return (0);
+	}
 
 	/* Can not get geometry, punt */
-	if (disk->real == NULL)
-		return (1);
-
-	return (0);
+	disk->real = NULL;
+	return (1);
 }
 
 int
Index: fdisk.c
===================================================================
RCS file: /cvs/src/sbin/fdisk/fdisk.c,v
retrieving revision 1.31
diff -u -r1.31 fdisk.c
--- fdisk.c	2000/01/08 04:51:16	1.31
+++ fdisk.c	2001/05/30 04:41:12
@@ -42,6 +42,9 @@
 #include "user.h"
 
 #define _PATH_MBR _PATH_BOOTDIR "mbr"
+static unsigned char builtin_mbr[] = {
+#include "mbrcode.h"
+};
 
 
 void
@@ -139,10 +142,14 @@
 		exit(USER_print_disk(&disk));
 
 	/* Parse mbr template, to pass on later */
-	if ((fd = open(mbrfile, O_RDONLY)) < 0)
-		err(1, "open mbr file");
-	MBR_read(fd, 0, mbr_buf);
-	close(fd);
+	if ((fd = open(mbrfile, O_RDONLY)) == -1) {
+		warn("Can not open MBR file");
+		warnx("using builtin MBR");
+		memcpy(mbr_buf, builtin_mbr, sizeof(mbr_buf));
+	} else {
+		MBR_read(fd, 0, mbr_buf);
+		close(fd);
+	}
 	MBR_parse(&disk, mbr_buf, 0, 0, &mbr);
 
 	/* Punt if no i or m */
Index: mbrcode.h
===================================================================
RCS file: mbrcode.h
diff -N mbrcode.h
--- /dev/null	Tue May 29 22:29:48 2001
+++ mbrcode.h	Tue May 29 22:41:12 2001
@@ -0,0 +1,97 @@
+/* $OpenBSD$ */
+/*
+ * Copyright (c) 2000 Tobias Weingartner
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *    This product includes software developed by Tobias Weingartner.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/* Largely generated by:
+ * hexdump -ve '8/1 "0x%02x, " "\n"' /usr/mdec/mbr
+ */
+0xfa, 0xea, 0x06, 0x00, 0xc0, 0x07, 0x8c, 0xc8,
+0x8e, 0xd8, 0x8e, 0xd0, 0xbc, 0xfc, 0xff, 0xfb,
+0xb0, 0x53, 0xe8, 0xe2, 0x00, 0xb8, 0xa0, 0x07,
+0x8e, 0xc0, 0x31, 0xf6, 0x31, 0xff, 0xb9, 0x00,
+0x02, 0xfc, 0xf2, 0xa4, 0xea, 0x29, 0x00, 0xa0,
+0x07, 0xb0, 0x52, 0xe8, 0xc9, 0x00, 0x1e, 0x07,
+0x0e, 0x1f, 0xf6, 0xc2, 0x80, 0x75, 0x0b, 0x66,
+0xbe, 0x13, 0x01, 0x00, 0x00, 0xe8, 0xab, 0x00,
+0xb2, 0x80, 0x66, 0xbe, 0xbe, 0x01, 0x00, 0x00,
+0x66, 0xb9, 0x04, 0x00, 0x00, 0x00, 0xb0, 0x4c,
+0xe8, 0xa4, 0x00, 0x8a, 0x44, 0x00, 0x3c, 0x80,
+0x74, 0x18, 0x66, 0x83, 0xc6, 0x10, 0xe2, 0xee,
+0x66, 0xbe, 0x3c, 0x01, 0x00, 0x00, 0xe8, 0x82,
+0x00, 0xfa, 0xf4, 0xb0, 0x2e, 0xe8, 0x87, 0x00,
+0xeb, 0xf7, 0xb0, 0x42, 0xe8, 0x80, 0x00, 0x8b,
+0x14, 0x8b, 0x4c, 0x02, 0x66, 0xb8, 0x01, 0x02,
+0x00, 0x00, 0x31, 0xdb, 0xcd, 0x13, 0x73, 0x13,
+0x80, 0xfa, 0x80, 0x75, 0xaa, 0x66, 0xbe, 0x2f,
+0x01, 0x00, 0x00, 0xe8, 0x55, 0x00, 0xe8, 0x33,
+0x00, 0xeb, 0xce, 0xb0, 0x43, 0xe8, 0x57, 0x00,
+0x66, 0x31, 0xc0, 0x66, 0xbb, 0xfe, 0x01, 0x00,
+0x00, 0x67, 0x8b, 0x03, 0x66, 0x3d, 0x55, 0xaa,
+0x00, 0x00, 0x74, 0x0b, 0x66, 0xbe, 0x52, 0x01,
+0x00, 0x00, 0xe8, 0x2e, 0x00, 0xeb, 0xaa, 0xb0,
+0x47, 0xe8, 0x33, 0x00, 0x66, 0xea, 0x00, 0x7c,
+0x00, 0x00, 0x00, 0x00, 0x50, 0x53, 0x66, 0xbb,
+0x03, 0x01, 0x00, 0x00, 0x50, 0x88, 0xe0, 0x66,
+0x83, 0xe0, 0x0f, 0xd7, 0xe8, 0x18, 0x00, 0x58,
+0x66, 0x83, 0xe0, 0x0f, 0xd7, 0xe8, 0x0f, 0x00,
+0x5b, 0x58, 0xc3, 0x50, 0xfc, 0xac, 0x84, 0xc0,
+0x74, 0x0f, 0xe8, 0x02, 0x00, 0xeb, 0xf6, 0x50,
+0x53, 0xb4, 0x0e, 0x31, 0xdb, 0x43, 0xcd, 0x10,
+0x5b, 0x58, 0xc3, 0x30, 0x31, 0x32, 0x33, 0x34,
+0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43,
+0x44, 0x45, 0x46, 0x4d, 0x42, 0x52, 0x20, 0x6f,
+0x6e, 0x20, 0x66, 0x6c, 0x6f, 0x70, 0x70, 0x79,
+0x20, 0x6f, 0x72, 0x20, 0x6f, 0x6c, 0x64, 0x20,
+0x42, 0x49, 0x4f, 0x53, 0x0d, 0x0a, 0x00, 0x52,
+0x65, 0x61, 0x64, 0x20, 0x65, 0x72, 0x72, 0x6f,
+0x72, 0x0d, 0x0a, 0x00, 0x4e, 0x6f, 0x20, 0x61,
+0x63, 0x74, 0x69, 0x76, 0x65, 0x20, 0x70, 0x61,
+0x72, 0x74, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x0d,
+0x0a, 0x00, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69,
+0x64, 0x20, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x74,
+0x75, 0x72, 0x65, 0x0d, 0x0a, 0x00, 0x90, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
+0x01, 0x00, 0xa6, 0xff, 0xff, 0xff, 0x00, 0x00,
+0x00, 0x00, 0xff, 0xff, 0xff, 0x7f, 0x55, 0xaa,
Index: part.c
===================================================================
RCS file: /cvs/src/sbin/fdisk/part.c,v
retrieving revision 1.21
diff -u -r1.21 part.c
--- part.c	2001/05/18 20:22:02	1.21
+++ part.c	2001/05/30 04:41:13
@@ -208,10 +208,10 @@
 
 	tmp.shead = partn->shead;
 	tmp.ssect = partn->ssect;
-	tmp.scyl = (partn->scyl > 1024)? 1023: partn->scyl; 
+	tmp.scyl = (partn->scyl > 1023)? 1023: partn->scyl; 
 	tmp.ehead = partn->ehead;
 	tmp.esect = partn->ssect;
-	tmp.ecyl = (partn->ecyl > 1024)? 1023: partn->ecyl; 
+	tmp.ecyl = (partn->ecyl > 1023)? 1023: partn->ecyl; 
 	if (!PRT_check_chs(partn) && PRT_check_chs(&tmp)) {
 		partn->shead = tmp.shead;
 		partn->ssect = tmp.ssect;



Visit your host, monkey.org