From owner-freebsd-arch@FreeBSD.ORG Wed Nov 4 17:20:48 2009 Return-Path: Delivered-To: arch@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6DDF41065670; Wed, 4 Nov 2009 17:20:48 +0000 (UTC) (envelope-from imp@bsdimp.com) Received: from harmony.bsdimp.com (bsdimp.com [199.45.160.85]) by mx1.freebsd.org (Postfix) with ESMTP id 1F16B8FC0A; Wed, 4 Nov 2009 17:20:48 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by harmony.bsdimp.com (8.14.3/8.14.1) with ESMTP id nA4HBoGf049389; Wed, 4 Nov 2009 10:11:50 -0700 (MST) (envelope-from imp@bsdimp.com) Date: Wed, 04 Nov 2009 10:12:07 -0700 (MST) Message-Id: <20091104.101207.-1398301090.imp@bsdimp.com> To: arch@FreeBSD.org From: "M. Warner Losh" X-Mailer: Mew version 5.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Wed_Nov__4_10_12_07_2009_078)--" Content-Transfer-Encoding: 7bit Cc: marcel@FreeBSD.org Subject: fdisk behavior hack X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Nov 2009 17:20:48 -0000 ----Next_Part(Wed_Nov__4_10_12_07_2009_078)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Greetings, Enclosed please find a hack to the behavior of fdisk. On !x86, !ia64 platforms, you can't do a "fdisk -I da0" because there's not /boot/mbr. It is perfectly valid to make disks that don't have any boot code on them (say if you are an ARM or MIPS embedded machine). This patch changes things. Currently, for all patforms except ia64 we open the specified file (defaulting to /boot/mbr). If it doesn't exist or we can't read it, we die. Then we do a bunch of sanity checking on the MBR that was read in. On ia64 we just make a fake one and return (we don't use the -b argument at all!). I'd like to propose something simpler. I'd like to propose that for !i386 and !amd64 we allow the open and/or stat to fail. If the user specified the -b flag, the same thing as the !ia64 case will happen as today: they get an error and the program refuses to work. If no -b flag was specified, then we'll use the current ia64-only code to fake up a 'good enough' MBR and proceed. This will allow non-x86 platforms that install fdisk to still initialize a disk in the face of the missing template mbr file. Comments? Code Review? Warner ----Next_Part(Wed_Nov__4_10_12_07_2009_078)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="fdisk-20091104.diff" Index: fdisk.c =================================================================== --- fdisk.c (revision 198865) +++ fdisk.c (working copy) @@ -501,15 +501,37 @@ static void init_boot(void) { -#ifndef __ia64__ const char *fname; int fdesc, n; struct stat sb; + /* + * Open the mbr file and sanity check it. If we're not on a x86 box, + * then /boot/mbr doesn't exist. In that case, allow the user to + * continue with an MBR that just has the right signature, but no + * actual code. If it does exist, then we sanity check it on all + * platforms. If there's a problem with the -b argument, always + * whine. + */ fname = b_flag ? b_flag : "/boot/mbr"; if ((fdesc = open(fname, O_RDONLY)) == -1 || - fstat(fdesc, &sb) == -1) + fstat(fdesc, &sb) == -1) { +#if !defined(__i386__) && !defined(__amd64__) + if (b_flag) + err(1, "%s", fname); + warn("%s", fname); + if (mboot.bootinst != NULL) + free(mboot.bootinst); + mboot.bootinst_size = secsize; + if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) + errx(1, "unable to allocate boot block buffer"); + memset(mboot.bootinst, 0, mboot.bootinst_size); + le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); + return; +#else err(1, "%s", fname); +#endif + } if ((mboot.bootinst_size = sb.st_size) % secsize != 0) errx(1, "%s: length must be a multiple of sector size", fname); if (mboot.bootinst != NULL) @@ -521,15 +543,6 @@ err(1, "%s", fname); if (n != mboot.bootinst_size) errx(1, "%s: short read", fname); -#else - if (mboot.bootinst != NULL) - free(mboot.bootinst); - mboot.bootinst_size = secsize; - if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) - errx(1, "unable to allocate boot block buffer"); - memset(mboot.bootinst, 0, mboot.bootinst_size); - le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC); -#endif } ----Next_Part(Wed_Nov__4_10_12_07_2009_078)----