[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
New MBR code, LBA capable
Hi,
I've just coded an LBR capable Master Boot Record replacement
because I was bored.
Features:
- Capable of booting via CHS
- Capable of booting via LBA
- Can act as a boot manager
- Boot manager can boot from floppy
- print a dot every time it tries to boot
Tests it ran:
- boot LILO in PBR (CHS)
- boot MS-DOS on FLOPPY
- boot OpenBSD biosboot on PBR (CHS)
- boot NTLDR on PBR (CHS)
- boot simple test code (LBA)
The code has succeeded all tests.
An LBA capable biosboot will follow in a few days, when I get the time.
If you like this code, include it into the base system, because it also
solves the problem "OpenBSD has no boot manager".
I don't object a s/MirBSD/OpenBSD/ throughout the code, obviously.
The fdisk diff contains one diff to make fdisk on boot floppies smaller
and one diff to integrate the new code (with -DBOOTMANAGER defined) into
the binary.
Feedback welcome. (Except about .intel_syntax)
HTH&HAND,
//Thorsten
--
Willst Du wegen dummer User immer 'Ja, ich will' nach einem rm an /dev/tty
eingeben müssen? -- Bodo Eggert in de.alt.sysadmin.recovery
Index: sys/arch/i386/stand/mbr/Makefile
===================================================================
RCS file: /lcvs/src/sys/arch/i386/stand/mbr/Makefile,v
retrieving revision 1.1.1.2
retrieving revision 1.4
diff -u -r1.1.1.2 -r1.4
--- sys/arch/i386/stand/mbr/Makefile 24 Apr 2003 09:05:17 -0000 1.1.1.2
+++ sys/arch/i386/stand/mbr/Makefile 3 May 2003 11:53:09 -0000 1.4
@@ -1,8 +1,8 @@
+# $MirBSD: Makefile,v 1.4 2003/05/03 11:53:31 tg Exp $
# $OpenBSD: Makefile,v 1.14 2003/04/17 03:43:19 drahn Exp $
-#
PROG= mbr
-SRCS= mbr.S
+SRCS= mbrldr.S
AFLAGS+=-I${.CURDIR} -I${.CURDIR}/../../.. #-Wa,-a
LD=ld
LDFLAGS=-nostdlib -Ttext 0 -x -N -s -Bstatic -e start
@@ -14,9 +14,12 @@
SADIR=${.CURDIR}/..
S= ${.CURDIR}/../../../..
-# Uncomment this to make mbr talk to a serial port.
+# Uncomment this to make mbr.S talk to a serial port.
#CPPFLAGS+=-DSERIAL=0
+# Uncomment this to make mbrldr.S a bootmanager
+CPPFLAGS+=-DBOOTMANAGER
+
${PROG}: $(OBJS) $(DPADD)
$(LD) $(LDFLAGS) -o $(PROG) $(OBJS) $(LDADD)
@size $(PROG)
@@ -27,3 +30,9 @@
fi
.include <bsd.prog.mk>
+
+mbrldr.E: mbrldr.S
+ ${CC} ${AFLAGS} ${CPPFLAGS} ${.CURDIR}/mbrldr.S -E -o mbrldr.E
+
+disasm: ${PROG}
+ ndisasm -b 16 ${PROG} >${.CURDIR}/mbr~disasm
Index: sys/arch/i386/stand/mbr/mbrldr.S
===================================================================
RCS file: sys/arch/i386/stand/mbr/mbrldr.S
diff -N sys/arch/i386/stand/mbr/mbrldr.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/arch/i386/stand/mbr/mbrldr.S 3 May 2003 11:53:09 -0000 1.2
@@ -0,0 +1,181 @@
+/* $MirBSD: mbrldr.S,v 1.2 2003/05/03 11:53:31 tg Stab $ */
+/*-
+ * Copyright (c) 1982-2003 by Thorsten "mirabile" Glaser <x86@ePost.de>
+ *
+ * Anyone who obtained a copy of this work is hereby permitted to freely use,
+ * distribute, modify, merge, sublicence, give away or sell it as long as the
+ * authors are given due credit and the following notice is retained:
+ *
+ * This work is provided "as is", with no explicit or implicit warranty what-
+ * soever. Use it only at your own risk. In no event may an author or contri-
+ * butor be held liable for any damage, directly or indirectly, that origina-
+ * ted through or is caused by creation or modification of this work.
+ */
+
+ .file "mbrldr.S"
+ .intel_syntax noprefix
+
+ .text
+ .code16
+ .globl start
+start: xor eax,eax
+ push eax
+ popfd
+ mov ax,0x07A0
+ mov ss,ax
+ mov esp,0x1000
+ mov ds,ax
+ mov es,ax
+ mov si,0x0200 /* 7A0:0200 = 0:7C00 */
+ mov di,0x0000 /* 7A0:0000 = 0:7A00 */
+ mov cx,0x0100
+ rep movsw
+ jmp 0x07A0,offset doit
+ /* we are relocated now */
+doit: /*
+ * a) Output "Hello" Text
+ * b) if BOOTMANAGER
+ * Output "Choose" Text
+ * Handle input (0,1,2,3,RET,ESC)
+ * else
+ * Fake input RET
+ * endif
+ * c) load sector (RET=0x80-Partition, ESC=Floppy, 0-3=Partition)
+ * d) jump to sector
+ */
+ mov si,offset tHelo
+ call _otxt
+#ifdef BOOTMANAGER
+ mov si,offset tBmgr
+ call _otxt
+ilp1: xor ah,ah
+ int 0x16
+ cmp al,13
+ je ilok
+ cmp al,27
+ je ilok
+ sub al,0x30
+ cmp al,3
+ ja ilp1
+ilok: /* al=13,27,0,1,2,3 */
+#else
+ mov al,13
+#endif
+ cmp al,27
+ je floppy
+ cmp al,13
+ jne partok
+ xor al,al
+ mov bx,offset ptbl
+cklp: cmp byte ptr [bx],0x80
+ je partok
+ inc ax
+ add bl,0x10 /* sizeof ptbl-ent */
+ cmp al,4
+ jb cklp
+bootfl: mov si,offset tFail
+ call _otxt
+ xor ah,ah
+ int 0x16
+ jmp 0xF000,0xFFF0
+_otxt: lodsb
+ or al,al
+ je raus
+ mov ah,0x0E
+ mov bx,7
+ int 0x10
+ jmp _otxt
+raus: ret
+partok: mov bx,offset ptbl
+ shl al,4 /* log_2 sizeof ptbl-ent */
+ add bl,al /* can't overflow */
+ push bx
+ /* Test for LBA support */
+ mov ah,0x41
+ mov bx,0x55AA
+ mov dl,0x80
+ int 0x13
+ jc l_chs
+ cmp bx,0xAA55
+ jne l_chs
+ and cl,1
+ je l_chs
+l_lba: pop bx
+ /* Load the boot structure (LBA) */
+ mov si,offset lbapbl
+ mov eax,[bx+8]
+ mov [blknum],eax
+ mov ax,[bx+2]
+ mov [savecx],ax
+ mov dx,[bx]
+ mov dl,0x80
+ mov [savedx],dx
+ mov ah,0x42
+ jmp boot_loop
+l_chs: pop bx
+ /* Load the boot structure (CHS) */
+ mov cx,[bx+2]
+ mov dx,[bx]
+ mov dl,0x80
+ jmp boot_chs
+floppy: mov cx,1
+ xor dx,dx
+boot_chs:
+ mov bx,offset bsec
+ mov ax,0x0201
+ jmp boot_loop
+tHelo: .asciz "MirBSD mbr v1.00\r\n"
+#ifdef BOOTMANAGER
+tBmgr: .ascii "Choose partition to boot:\r\n"
+ .ascii "0,1,2,3: by number\r\n"
+ .ascii "RETURN: active\r\n"
+ .asciz "ESCAPE: floppy drive\r\n"
+#endif
+tFail: .asciz "Boot failed. Press a key to reboot.\r\n"
+boot_loop:
+ xor bp,bp
+blp1: pusha
+ mov ax,0x0E2E
+ mov bx,7
+ int 0x10
+ popa
+ pusha
+ int 0x13
+ popa
+ jc blp2
+ jmp 0,0x7C00 /* offset flat bsec */
+blp2: pusha
+ xor ax,ax
+ int 0x13
+ popa
+ inc bp
+ cmp bp,1 /* first time it failed? */
+ jne blp3
+ xor di,di
+ cmp [savedx],di /* if so, check if we were LBA */
+ je blp3
+ mov cx,[savecx] /* if so, fall back to CHS */
+ mov dx,[savedx]
+ mov bx,offset bsec
+ mov ax,0x0201
+blp3: cmp bp,16 /* 16 retries should be enough for everyone */
+ jb blp1
+ jmp bootfl /* failed */
+lbapbl: /* Parameter block for LBA */
+ .word 0x0010 /* length, reserved */
+ .word 0x0001 /* num. of sectors */
+ .long 0x00007C00 /* transfer buffer */
+blknum: .long 0,0 /* LBA block number */
+savecx: .word 0
+savedx: .word 0
+
+ /* Trailer: partition table, signature */
+ . = 0x01B8
+ .long 0 /* some NT stuff */
+ .word 0 /* space */
+ptbl: .long 0,0,0,0
+ .long 0,0,0,0
+ .long 0,0,0,0
+ .long 0x00010080,0xFFFFFFA6,0,0x7FFFFFFF
+ .word 0xAA55 /* signature */
+bsec: /* 0x0200 is where the new boot sector is loaded */
Index: sbin/fdisk/Makefile
===================================================================
RCS file: /lcvs/src/sbin/fdisk/Makefile,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- sbin/fdisk/Makefile 22 Mar 2003 17:48:01 -0000 1.1.1.1
+++ sbin/fdisk/Makefile 23 Mar 2003 21:51:38 -0000 1.2
@@ -48,7 +48,7 @@
manual.c: fdisk.cat8
(echo 'char manpage[] = "\\'; \
sed -e 's/[\\"]/\\&/g' -e 's/$$/\\n\\/' fdisk.cat8; \
- echo '";' ) > manual.c
+ echo '";' ) | col -b > manual.c
.endif
.else
Index: sbin/fdisk/mbrcode.h
===================================================================
RCS file: /lcvs/src/sbin/fdisk/mbrcode.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- sbin/fdisk/mbrcode.h 22 Mar 2003 17:48:01 -0000 1.1.1.1
+++ sbin/fdisk/mbrcode.h 3 May 2003 11:55:05 -0000 1.2
@@ -1,89 +1,60 @@
-/* $OpenBSD: mbrcode.h,v 1.1 2001/06/23 01:56:02 kjell Exp $ */
-/*
- * 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
+ * Thanks to Toby Weingartner for the hint.
*/
-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,
+0x66, 0x31, 0xc0, 0x66, 0x50, 0x66, 0x9d, 0xb8,
+0xa0, 0x07, 0x8e, 0xd0, 0x66, 0xbc, 0x00, 0x10,
+0x00, 0x00, 0x8e, 0xd8, 0x8e, 0xc0, 0xbe, 0x00,
+0x02, 0xbf, 0x00, 0x00, 0xb9, 0x00, 0x01, 0xf3,
+0xa5, 0xea, 0x26, 0x00, 0xa0, 0x07, 0xbe, 0xd2,
+0x00, 0xe8, 0x41, 0x00, 0xbe, 0xe5, 0x00, 0xe8,
+0x3b, 0x00, 0x30, 0xe4, 0xcd, 0x16, 0x3c, 0x0d,
+0x74, 0x0a, 0x3c, 0x1b, 0x74, 0x06, 0x2c, 0x30,
+0x3c, 0x03, 0x77, 0xee, 0x3c, 0x1b, 0x74, 0x7c,
+0x3c, 0x0d, 0x75, 0x30, 0x30, 0xc0, 0xbb, 0xbe,
+0x01, 0x80, 0x3f, 0x80, 0x74, 0x26, 0x40, 0x80,
+0xc3, 0x10, 0x3c, 0x04, 0x72, 0xf3, 0xbe, 0x3b,
+0x01, 0xe8, 0x09, 0x00, 0x30, 0xe4, 0xcd, 0x16,
+0xea, 0xf0, 0xff, 0x00, 0xf0, 0xac, 0x08, 0xc0,
+0x74, 0x09, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd,
+0x10, 0xeb, 0xf2, 0xc3, 0xbb, 0xbe, 0x01, 0xc0,
+0xe0, 0x04, 0x00, 0xc3, 0x53, 0xb4, 0x41, 0xbb,
+0xaa, 0x55, 0xb2, 0x80, 0xcd, 0x13, 0x72, 0x2a,
+0x81, 0xfb, 0x55, 0xaa, 0x75, 0x24, 0x80, 0xe1,
+0x01, 0x74, 0x1f, 0x5b, 0xbe, 0xa2, 0x01, 0x66,
+0x8b, 0x47, 0x08, 0x66, 0xa3, 0xaa, 0x01, 0x8b,
+0x47, 0x02, 0xa3, 0xb2, 0x01, 0x8b, 0x17, 0xb2,
+0x80, 0x89, 0x16, 0xb4, 0x01, 0xb4, 0x42, 0xe9,
+0xa7, 0x00, 0x5b, 0x8b, 0x4f, 0x02, 0x8b, 0x17,
+0xb2, 0x80, 0xeb, 0x05, 0xb9, 0x01, 0x00, 0x31,
+0xd2, 0xbb, 0x00, 0x02, 0xb8, 0x01, 0x02, 0xe9,
+0x8f, 0x00, 0x4d, 0x69, 0x72, 0x42, 0x53, 0x44,
+0x20, 0x6d, 0x62, 0x72, 0x20, 0x76, 0x31, 0x2e,
+0x30, 0x30, 0x0d, 0x0a, 0x00, 0x43, 0x68, 0x6f,
+0x6f, 0x73, 0x65, 0x20, 0x70, 0x61, 0x72, 0x74,
+0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74, 0x6f,
+0x20, 0x62, 0x6f, 0x6f, 0x74, 0x3a, 0x0d, 0x0a,
+0x30, 0x2c, 0x31, 0x2c, 0x32, 0x2c, 0x33, 0x3a,
+0x20, 0x62, 0x79, 0x20, 0x6e, 0x75, 0x6d, 0x62,
+0x65, 0x72, 0x0d, 0x0a, 0x52, 0x45, 0x54, 0x55,
+0x52, 0x4e, 0x3a, 0x20, 0x61, 0x63, 0x74, 0x69,
+0x76, 0x65, 0x0d, 0x0a, 0x45, 0x53, 0x43, 0x41,
+0x50, 0x45, 0x3a, 0x20, 0x66, 0x6c, 0x6f, 0x70,
+0x70, 0x79, 0x20, 0x64, 0x72, 0x69, 0x76, 0x65,
+0x0d, 0x0a, 0x00, 0x42, 0x6f, 0x6f, 0x74, 0x20,
+0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x2e, 0x20,
+0x50, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x20,
+0x6b, 0x65, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x72,
+0x65, 0x62, 0x6f, 0x6f, 0x74, 0x2e, 0x0d, 0x0a,
+0x00, 0x31, 0xed, 0x60, 0xb8, 0x2e, 0x0e, 0xbb,
+0x07, 0x00, 0xcd, 0x10, 0x61, 0x60, 0xcd, 0x13,
+0x61, 0x72, 0x05, 0xea, 0x00, 0x7c, 0x00, 0x00,
+0x60, 0x31, 0xc0, 0xcd, 0x13, 0x61, 0x45, 0x83,
+0xfd, 0x01, 0x75, 0x16, 0x31, 0xff, 0x39, 0x3e,
+0xb4, 0x01, 0x74, 0x0e, 0x8b, 0x0e, 0xb2, 0x01,
+0x8b, 0x16, 0xb4, 0x01, 0xbb, 0x00, 0x02, 0xb8,
+0x01, 0x02, 0x83, 0xfd, 0x10, 0x72, 0xc4, 0xe9,
+0xbc, 0xfe, 0x10, 0x00, 0x01, 0x00, 0x00, 0x7c,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,