1 /* $NetBSD: mbr.S,v 1.23 2010/12/08 21:56:42 jakllsch Exp $ */
4 * Copyright (c) 1999-2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden, based on an earlier work by Wolfgang Solfrank.
9 * Major surgery performed by David Laight.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * i386 master boot code
38 * BOOTSEL - bootselector code
39 * BOOT_EXTENDED - scan extended partition list (LBA reads)
40 * COM_PORT - do serial io to specified port number
41 * 0..3 => bios port, otherwise actual io_addr
42 * COM_BAUD - initialise serial port baud rate
44 * TERSE_ERROR - terse error messages
45 * NO_CHS - all reads are LBA
46 * NO_LBA_CHECK - no check if bios supports LBA reads
47 * NO_BANNER - do not output title line 'banner'
57 /* The first 4 items in the 40:xx segment are the serial port base addresses */
58 #define COM_PORT_VAL (0x400 + (COM_PORT * 2))
60 #define COM_PORT_VAL $COM_PORT
63 #if !defined(COM_FREQ)
64 #define COM_FREQ 1843200
74 #include <machine/asm.h>
75 #include <sys/bootblock.h>
77 #define BOOTADDR 0x7c00 /* where we get loaded to */
79 #define TABENTRYSIZE (MBR_BS_PARTNAMESIZE + 1)
80 #define NAMETABSIZE (MBR_PART_COUNT * TABENTRYSIZE)
83 /* ASCII values for the keys */
84 #define KEY_ACTIVE '\r'
88 /* Scan values for the various keys we use, as returned by the BIOS */
89 #define SCAN_ENTER 0x1c
93 #define KEY_ACTIVE SCAN_ENTER
94 #define KEY_DISK1 SCAN_F1
95 #define KEY_PTN1 SCAN_1
99 * Minimum and maximum drive number that is considered to be valid.
106 * Error codes. Done this way to save space.
108 #define ERR_INVPART '1' /* Invalid partition table */
109 #define ERR_READ '2' /* Read error */
110 #define ERR_NOOS '3' /* Magic no. check failed for part. */
111 #define ERR_KEY '?' /* unknown key press */
112 #define ERR_NO_LBA 'L' /* sector above chs limit */
114 #define set_err(err) movb $err, %al
117 #define set_err(err) mov $err, %ax
123 * Move ourselves out of the way first.
124 * (to the address we are linked at)
134 mov $BOOTADDR + (mbr - start), %si
135 push %ax /* zero for %cs of lret */
137 movw $(bss_start - mbr), %cx
139 movsb /* relocate code */
140 mov $(bss_end - bss_start + 511)/512, %ch
143 lret /* Ensures %cs == 0 */
146 * Sanity check the drive number passed by the BIOS. Some BIOSs may not
147 * do this and pass garbage.
150 cmpb $MAXDRV, %dl /* relies on MINDRV being 0x80 */
152 movb $MINDRV, %dl /* garbage in, boot disk 0 */
154 push %dx /* save drive number */
155 push %dx /* twice - for err_msg loop */
157 #if defined(COM_BAUD)
159 mov $num_com_args, %cl /* %ch is zero from above */
160 mov COM_PORT_VAL, %dx
173 * Walk through the selector (name) table printing used entries.
177 * %bx nametab[] boot seletor menu
178 * %ecx base of 'extended' partition
179 * %edx next extended partition
180 * %si message ptr (etc)
181 * %edi sector number of this partition
182 * %bp parttab[] mbr partition table
187 xorl %ecx, %ecx /* base of extended partition */
189 xorl %edx, %edx /* for next extended partition */
191 lea parttab - nametab(%bx), %bp
193 movb 4(%bp), %al /* partition type */
195 movl 8(%bp), %edi /* partition sector number */
197 cmpb $MBR_PTYPE_EXT, %al /* Extended partition */
199 cmpb $MBR_PTYPE_EXT_LBA, %al /* Extended LBA partition */
201 cmpb $MBR_PTYPE_EXT_LNX, %al /* Linux extended partition */
203 1: movl %edi, %edx /* save next extended ptn */
207 addl lba_sector, %edi /* add in extended ptn base */
209 test %al, %al /* undefined partition */
211 cmpb $0x80, (%bp) /* check for active partition */
212 jne 3f /* jump if not... */
213 #define ACTIVE (4 * ((KEY_ACTIVE - KEY_DISK1) & 0xff))
215 movl %edi, ptn_list + ACTIVE /* save location of active ptn */
217 mov %bp, ptn_list + ACTIVE
222 cmpb $0, (%bx) /* check for prompt */
224 /* output menu item */
227 call message /* menu number */
228 mov (%si), %si /* ':' << 8 | '1' + count */
229 shl $2, %si /* const + count * 4 */
230 #define CONST (4 * ((':' << 8) + '1' - ((KEY_PTN1 - KEY_DISK1) & 0xff)))
232 movl %edi, ptn_list - CONST(%si) /* sector to read */
234 mov %bp, ptn_list - CONST(%si) /* partition info */
238 call message_crlf /* prompt */
242 add $TABENTRYSIZE, %bx
243 cmpb $(nametab - start - 0x100) + 4 * TABENTRYSIZE, %bl
248 * Now check extended partition chain
254 xchg %ecx, %edx /* save base of ext ptn chain */
255 1: addl %ecx, %edx /* sector to read */
256 movl %edx, lba_sector
259 pop %dx /* recover drive # */
260 push %dx /* save drive */
262 movw $BOOTADDR + (nametab - start), %bx
263 jnc next_extended /* abort menu on read fail */
267 * The non-bootsel code traverses this code path, it needs the
268 * correct keycode to select the active partition.
272 mov $(KEY_ACTIVE - KEY_DISK1) & 0xff, %ax
275 * Get the initial time value for the timeout comparison. It is returned
276 * by int 1a in cx:dx. We do sums modulo 2^16 so it doesn't matter if
277 * the counter wraps (which it does every hour) - so we can safely
280 * Loop around checking for a keypress until we have one, or timeout is
286 mov %dx, %di /* start time to di */
289 mov COM_PORT_VAL, %dx
299 movb $1, %ah /* looks to see if a */
300 int $0x16 /* key has been pressed */
304 int $0x16 /* 'read key', code ah, ascii al */
305 shr $8, %ax /* code in %al, %ah zero */
310 int $0x1a /* current time to cx:dx */
312 cmpw timeout, %dx /* always wait for 1 tick... */
313 jbe 3b /* 0xffff means never timeout */
315 mov defkey, %al /* timedout - we need %ah to still be zero! */
318 * We have a keycode, see what it means.
319 * If we don't know we generate error '?' and go ask again
323 * F1-F10 -> boot disk 0-9. Check if the requested disk isn't above
324 * the number of disks actually in the system as stored in 0:0475 by
326 * If we trust loc 475, we needn't check the upper bound on the keystroke
327 * This is always sector 0, so always read using chs.
333 pop %dx /* dump saved drive # */
334 push %ax /* replace with new */
336 xorl %ebp, %ebp /* read sector number 0 */
339 movw $chs_zero, %si /* chs read sector zero info */
345 * Boot requested partition.
346 * Use keycode to index the table we generated when we scanned the mbr
347 * while generating the menu.
349 * We very carfully saved the values in the correct part of the table.
356 movl ptn_list(%si), %ebp
360 mov ptn_list(%si), %si
371 /* Something went wrong...
373 * reset disk subsystem - needed after read failure,
374 * and wait for user key
388 pop %dx /* drive we errored on */
389 xor %ax,%ax /* only need %ah = 0 */
390 int $0x13 /* reset disk subsystem */
392 pop %dx /* original drive number */
396 jmp wait_key /* Read with timeout (again) */
398 jmp get_key /* Blocking read */
401 int $0x18 /* BIOS might ask for a key */
402 /* press and retry boot seq. */
410 * Active partition pointed to by si.
411 * Read the first sector.
413 * We can either do a CHS (Cylinder Head Sector) or an LBA (Logical
414 * Block Address) read. Always doing the LBA one
415 * would be nice - unfortunately not all systems support it.
416 * Also some may contain a separate (eg SCSI) bios that doesn't
417 * support it even when the main bios does.
419 * There is also the additional problem that the CHS values may be wrong
420 * (eg if fdisk was run on a different system that used different BIOS
421 * geometry). We convert the CHS value to a LBA sector number using
422 * the geometry from the BIOS, if the number matches we do a CHS read.
425 movl 8(%si), %ebp /* get sector # */
427 testb $MBR_BS_READ_LBA, flags
428 jnz boot_lba /* fdisk forced LBA read */
430 pop %dx /* collect saved drive... */
431 push %dx /* ...number to dl */
433 int $0x13 /* chs info */
436 * Validate geometry, if the CHS sector number doesn't match the LBA one
437 * we'll do an LBA read.
438 * calc: (cylinder * number_of_heads + head) * number_of_sectors + sector
439 * and compare against LBA sector number.
440 * Take a slight 'flier' and assume we can just check 16bits (very likely
441 * to be true because the number of sectors per track is 63).
443 movw 2(%si), %ax /* cylinder + sector */
444 push %ax /* save for sector */
446 xchgb %al, %ah /* 10 bit cylinder number */
447 shr $8, %dx /* last head */
448 inc %dx /* number of heads */
450 mov 1(%si), %dl /* head we want */
452 and $0x3f, %cx /* number of sectors */
454 pop %dx /* recover sector we want */
464 * Determine whether we have int13-extensions, by calling int 13, function 41.
465 * Check for the magic number returned, and the disk packet capability.
473 jc err_msg /* no int13 extensions */
478 #endif /* NO_LBA_CHECK */
482 * Save sector number (passed in %ebp) into lba parameter block,
483 * read the sector and leap into it.
486 movl %ebp, lba_sector /* save sector number */
489 pop %dx /* recover drive # */
491 push %dx /* save drive */
498 * Check signature for valid bootcode
500 movb BOOTADDR, %al /* first byte non-zero */
503 movw BOOTADDR + MBR_MAGIC_OFFSET, %ax
504 1: cmp $MBR_MAGIC, %ax
508 /* We pass the sector number through to the next stage boot.
509 * It doesn't have to use it (indeed no other mbr code will generate) it,
510 * but it does let us have a NetBSD pbr that can identify where it was
511 * read from! This lets us use this code to select between two
512 * NetBSD system on the same physical driver.
513 * (If we've read the mbr of a different disk, it gets a random number
514 * - but it wasn't expecting anything...)
517 pop %dx /* recover drive # */
519 /* There some bug in MINIX tools, which causes to
520 * generate wrong code for "jmp BOOTADDR" (relocation issue).
522 jmp start - LOADADDR + BOOTADDR
525 #endif /* defined(__minix) */
530 * Sector below CHS limit
531 * Do a cylinder-head-sector read instead.
534 pop %dx /* recover drive # */
535 movb 1(%si), %dh /* head */
536 movw 2(%si), %cx /* ch=cyl, cl=sect */
537 movw $BOOTADDR, %bx /* es:bx is buffer */
538 movw $0x201, %ax /* command 2, 1 sector */
543 * Control block for int-13 LBA read.
544 * We need a xx, 00, 01, 00 somewhere to load chs for sector zero,
545 * by a complete fluke there is one here!
549 .word 0x10 /* control block length */
550 .word 1 /* sector count */
551 .word BOOTADDR /* offset in segment */
552 .word 0 /* segment */
554 .long 0x0000 /* sector # goes here... */
557 errtxt: .ascii "Error " /* runs into crlf if errcod set */
564 banner: .asciz "a: disk"
566 banner: .asciz "Fn: diskn"
569 banner: .asciz "NetBSD MBR boot"
578 ERR_INVPART: .asciz "No active partition"
579 ERR_READ: .asciz "Disk read error"
580 ERR_NOOS: .asciz "No operating system"
582 ERR_NO_LBA: .asciz "Invalid CHS read"
585 ERR_KEY: .asciz "bad key"
589 #if defined(COM_BAUD)
590 #define COM_DIVISOR (((COM_FREQ / COM_BAUD) + 8) / 16)
592 .byte 0x80 /* divisor latch enable */
593 .byte +3 /* io_port + 3 */
594 .byte COM_DIVISOR & 0xff
595 .byte -3 /* io_port */
596 .byte COM_DIVISOR >> 8 /* high baud */
597 .byte +1 /* io_port + 1 */
598 .byte 0x03 /* 8 bit no parity */
599 .byte +2 /* io_port + 3 */
600 num_com_args = (. - com_args)/2
604 * I hate #including source files, but the stuff below has to be at
605 * the correct absolute address.
606 * Clearly this could be done with a linker script.
614 #include <dump_eax.S>
618 * Stuff from here on is overwritten by fdisk - the offset must not change...
620 * Get amount of space to makefile can report it.
621 * (Unfortunately I can't seem to get the value reported when it is -ve)
623 mbr_space = defkey - .
624 . = start + MBR_BS_OFFSET
626 * Default action, as a keyvalue we'd normally read from the BIOS.
629 .byte KEY_ACTIVE /* ps/2 code */
630 #ifndef BOOTSEL_FLAGS
631 #define BOOTSEL_FLAGS 0
633 flags: .byte MBR_BS_NEWMBR | BOOTSEL_FLAGS
635 * Timeout value. ~65536 ticks per hour, which is ~18.2 times per second.
636 * 0xffff means never timeout.
639 .word 182 /* default to 10 seconds */
644 .fill MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1), 0x01, 0x00
646 /* space for mbr_dsn */
647 . = start + MBR_DSN_OFFSET
650 /* mbr_bootsel_magic */
651 . = start + MBR_BS_MAGIC_OFFSET
655 * MBR partition table
657 . = start + MBR_PART_OFFSET
659 .fill 0x40, 0x01, 0x00
661 . = start + MBR_MAGIC_OFFSET
664 /* zeroed data space */
667 #define BSS(name, size) name = bss_start + bss_off; bss_off = bss_off + size
668 BSS(ptn_list, 256 * 4) /* long[]: boot sector numbers */
669 BSS(dump_eax_buff, 16)