From 8155d6763dd54122ebad3d46f4dcad0501fd7139 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Mon, 14 Sep 2009 17:54:11 -0700 Subject: [PATCH] fdisk - Use heads = 255 on file images When operating on a file image use max values for sectors and heads in order to match the case where the file might exceed the CHS limit. By convention the CHS fields are set to all 1's in this case. If our sectors and heads are not set at max values then the all 1's case will result in a geometry that overflows the originally specified sectors and heads which can brick the BIOS. --- sbin/fdisk/fdisk.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index 93454f0aa5..fbdad7d697 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -825,10 +825,17 @@ get_params(void) struct partinfo partinfo; /* disk parameters */ struct stat st; + /* + * NOTE: When faking up the CHS for a file image (e.g. for USB), + * we must use max values. If we do not then an overflowed + * cylinder count will, by convention, set the CHS fields to + * all 1's. The heads and sectors in the CHS fields will then + * exceed the basic geometry which can cause BIOSes to brick. + */ if (ioctl(fd, DIOCGPART, &partinfo) == -1) { if (p_flag && fstat(fd, &st) == 0 && st.st_size) { sectors = 63; - heads = 16; + heads = 255; cylsecs = heads * sectors; cyls = st.st_size / 512 / cylsecs; } else { -- 2.11.4.GIT