Subject: Re: port-shark/35576 - Make sysinst clear MBR
To: David Laight <david@l8s.co.uk>
From: Julio M. Merino Vidal <jmmv84@gmail.com>
List: netbsd-bugs
Date: 03/11/2007 21:17:46
On 11/03/2007, at 21:10, David Laight wrote:

> On Sun, Mar 11, 2007 at 08:05:06PM +0000, Julio M. Merino Vidal wrote:
>> The following reply was made to PR port-shark/35576; it has been  
>> noted by GNATS.
>>
>>  Hi,
>>
>>  The attached patch for sysinst asks the user if he wants to clear  
>> the
>>  MBR and does so.  It also tells him how to configure the firmware to
>>  boot from the on-disk kernel.
>>
>>  But... I feel the question is useless.  Is it there any reason  
>> not to
>>  clear the MBR?  If there is not, I'll change the code to
>>  unconditionally clear it and only add a message at the end of the
>>  installation to tell the user what to do on the firmware.
>
> What do you mean by 'clear the mbr' ?
> I suspect you want the mbr editor ?

No, the first sector has to be cleared, as in "filled with zeros".  See:

http://www.netbsd.org/Ports/shark/faq.html#gen_shark_disk_boot

>>   
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0ARCS=20file:=20=
>>  /cvsroot/src/distrib/utils/sysinst/arch/shark/ 
>> md.c,v=0Aretrieving=20=
>>  revision=201.19=0Adiff=20-u=20-p=20-u=20-r1.19=20md.c=0A---=20=
>>  arch/shark/md.c=095=20Apr=202006=2016:55:07=20-0000=091.19=0A+++=20=
>
> Unreadable....

Ew, I wonder why...

Repasting without any of the translations, just the english messages  
to avoid encoding problems:

Index: arch/shark/md.c
===================================================================
RCS file: /cvsroot/src/distrib/utils/sysinst/arch/shark/md.c,v
retrieving revision 1.19
diff -u -p -u -r1.19 md.c
--- arch/shark/md.c	5 Apr 2006 16:55:07 -0000	1.19
+++ arch/shark/md.c	11 Mar 2007 20:00:06 -0000
@@ -53,6 +53,31 @@
#include "msg_defs.h"
#include "menu_defs.h"
+/*
+ * Clears the disk's first sector by writing all zeros over it.
+ * Returns 0 on success, -1 on failure.  Leaves the disk's path in
+ * the output diskpath buffer for further usage in error messages.
+ */
+static int
+clear_mbr(const char *disk, char *diskpath, size_t diskpathlen)
+{
+	int fd;
+	char sector[512];
+
+	fd = opendisk(disk, O_WRONLY, diskpath, diskpathlen, 0);
+	if (fd < 0)
+		return -1;
+
+	memset(sector, 0, sizeof(sector));
+	if (pwrite(fd, &sector, sizeof(sector), 0) < 0) {
+		close(fd);
+		return -1;
+	}
+
+	close(fd);
+	return 0;
+}
+
int
md_get_info(void)
{
@@ -109,6 +134,18 @@ md_pre_disklabel(void)
int
md_post_disklabel(void)
{
+
+	msg_display(MSG_clearmbr);
+	process_menu(MENU_yesno, NULL);
+	if (yesno) {
+		char diskpath[MAXPATHLEN];
+
+		if (clear_mbr(diskdev, diskpath, sizeof(diskpath)) == -1) {
+			msg_display(MSG_badclearmbr, diskpath);
+			process_menu(MENU_ok, NULL);
+		}
+	}
+
	return 0;
}
Index: arch/shark/msg.md.en
===================================================================
RCS file: /cvsroot/src/distrib/utils/sysinst/arch/shark/msg.md.en,v
retrieving revision 1.6
diff -u -p -u -r1.6 msg.md.en
--- arch/shark/msg.md.en	12 Jun 2003 11:20:13 -0000	1.6
+++ arch/shark/msg.md.en	11 Mar 2007 20:00:06 -0000
@@ -77,3 +77,28 @@ message arm32fspart
message set_kernel_1
{Kernel (GENERIC)}
+
+message clearmbr
+{The DNARD's firmware can load a NetBSD kernel straight from a FFS  
partition
+on the local hard disk.  However, it will not recognize the disk as  
bootable
+if it has an i386-like MBR on the first sector.
+
+Furthermore, to automatically boot the system from the disk you will  
need to
+manually run the following command from OpenFirmare's shell, unless  
it is
+already configured appropriately:
+
+setenv boot-device disk:\\netbsd.aout
+
+Do you want me to clear the first sector to ensure the disk is  
bootable?
+Note that the firmware's configuration will be left untouched.
+
+}
+
+message badclearmbr
+{Failed to clear the disk's first sector.  If the firmware cannot  
see the
+disk, try to run the following command manually from the installer's  
shell
+utility:
+
+dd if=/dev/zero of=%s bs=512 count=1
+
+}

-- 
Julio M. Merino Vidal <jmmv84@gmail.com>