mbr.S revision 1.17 1 /* $NetBSD: mbr.S,v 1.17 2008/01/25 21:40:58 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1999-2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * i386 master boot code
42 */
43
44 /* Compile options:
45 * BOOTSEL - bootselector code
46 * BOOT_EXTENDED - scan extended partition list (LBA reads)
47 * COM_PORT - do serial io to specified port number
48 * 0..3 => bios port, otherwise actual io_addr
49 * COM_BAUD - initialise serial port baud rate
50 *
51 * TERSE_ERROR - terse error messages
52 * NO_CHS - all reads are LBA
53 * NO_LBA_CHECK - no check if bios supports LBA reads
54 * NO_BANNER - do not output title line 'banner'
55 */
56
57 #ifdef BOOT_EXTENDED
58 #define NO_CHS 1
59 #define BOOTSEL 1
60 #endif
61
62 #ifdef COM_PORT
63 #if COM_PORT < 4
64 /* The first 4 items in the 40:xx segment are the serial port base addresses */
65 #define COM_PORT_VAL (0x400 + (COM_PORT * 2))
66 #else
67 #define COM_PORT_VAL $COM_PORT
68 #endif
69
70 #if !defined(COM_FREQ)
71 #define COM_FREQ 1843200
72 #endif
73 #endif
74
75 #ifdef BOOTSEL
76 #define TERSE_ERROR 1
77 #endif
78
79 #include <machine/asm.h>
80 #include <sys/bootblock.h>
81
82 #define BOOTADDR 0x7c00
83 #define LOADADDR 0x0600 /* address were are linked to */
84
85 #define TABENTRYSIZE (MBR_BS_PARTNAMESIZE + 1)
86 #define NAMETABSIZE (MBR_PART_COUNT * TABENTRYSIZE)
87
88 #ifdef COM_PORT
89 /* ASCII values for the keys */
90 #define KEY_ACTIVE '\m'
91 #define KEY_DISK1 'a'
92 #define KEY_PTN1 '1'
93 #else
94 /* Scan values for the various keys we use, as returned by the BIOS */
95 #define SCAN_ENTER 0x1c
96 #define SCAN_F1 0x3b
97 #define SCAN_1 0x2
98
99 #define KEY_ACTIVE SCAN_ENTER
100 #define KEY_DISK1 SCAN_F1
101 #define KEY_PTN1 SCAN_1
102 #endif
103
104 /*
105 * Minimum and maximum drive number that is considered to be valid.
106 */
107 #define MINDRV 0x80
108 #define MAXDRV 0x8f
109
110 #ifdef TERSE_ERROR
111 /*
112 * Error codes. Done this way to save space.
113 */
114 #define ERR_INVPART '1' /* Invalid partition table */
115 #define ERR_READ '2' /* Read error */
116 #define ERR_NOOS '3' /* Magic no. check failed for part. */
117 #define ERR_KEY '?' /* unknown key press */
118 #define ERR_NO_LBA 'L' /* sector above chs limit */
119
120 #define set_err(err) movb $err, %al
121
122 #else
123 #define set_err(err) mov $err, %ax
124 #endif
125
126 .text
127 .code16
128 /*
129 * Move ourselves out of the way first.
130 * (to the address we are linked at - 0x600)
131 * and zero our bss
132 */
133 ENTRY(start)
134 xor %ax, %ax
135 mov %ax, %ss
136 movw $BOOTADDR, %sp
137 mov %ax, %es
138 mov %ax, %ds
139 movw $mbr, %di
140 mov $mbr - LOADADDR + BOOTADDR, %si
141 push %ax /* zero for %cs of lret */
142 push %di
143 movw $(bss_start - mbr), %cx
144 rep
145 movsb /* relocate code */
146 mov $(bss_end - bss_start + 1)/2, %cx
147 rep
148 stosw /* zero bss */
149 lret /* Ensures %cs == 0 */
150
151 /*
152 * Sanity check the drive number passed by the BIOS. Some BIOSs may not
153 * do this and pass garbage.
154 */
155 mbr:
156 cmpb $MAXDRV, %dl /* relies on MINDRV being 0x80 */
157 jle 1f
158 movb $MINDRV, %dl /* garbage in, boot disk 0 */
159 1:
160 push %dx /* save drive number */
161 push %dx /* twice - for err_msg loop */
162
163 #if defined(COM_PORT) && defined(COM_BAUD)
164 mov $com_args, %si
165 mov $num_com_args, %cl /* %ch is zero from above */
166 mov COM_PORT_VAL, %dx
167 1: lodsw
168 add %ah, %dl
169 outb %dx
170 loop 1b
171 #endif
172
173 #ifndef NO_BANNER
174 mov $banner, %si
175 call message_crlf
176 #endif
177
178 /*
179 * Walk through the selector (name) table printing used entries.
180 *
181 * Register use:
182 * %ax temp
183 * %bx nametab[] boot seletor menu
184 * %ecx base of 'extended' partition
185 * %edx next extended partition
186 * %si message ptr (etc)
187 * %edi sector number of this partition
188 * %bp parttab[] mbr partition table
189 */
190 bootsel_menu:
191 movw $nametab, %bx
192 #ifdef BOOT_EXTENDED
193 xorl %ecx, %ecx /* base of extended partition */
194 next_extended:
195 xorl %edx, %edx /* for next extended partition */
196 #endif
197 lea parttab - nametab(%bx), %bp
198 next_ptn:
199 movb 4(%bp), %al /* partition type */
200 #ifdef NO_CHS
201 movl 8(%bp), %edi /* partition sector number */
202 #ifdef BOOT_EXTENDED
203 cmpb $MBR_PTYPE_EXT, %al /* Extended partition */
204 je 1f
205 cmpb $MBR_PTYPE_EXT_LBA, %al /* Extended LBA partition */
206 je 1f
207 cmpb $MBR_PTYPE_EXT_LNX, %al /* Linux extended partition */
208 jne 2f
209 1: movl %edi, %edx /* save next extended ptn */
210 jmp 4f
211 2:
212 #endif
213 addl lba_sector, %edi /* add in extended ptn base */
214 #endif
215 test %al, %al /* undefined partition */
216 je 4f
217 cmpb $0x80, (%bp) /* check for active partition */
218 jne 3f /* jump if not... */
219 #define ACTIVE (4 * ((KEY_ACTIVE - KEY_DISK1) & 0xff))
220 #ifdef NO_CHS
221 movl %edi, ptn_list + ACTIVE /* save location of active ptn */
222 #else
223 mov %bp, ptn_list + ACTIVE
224 #endif
225 #undef ENTER
226 3:
227 #ifdef BOOTSEL
228 cmpb $0, (%bx) /* check for prompt */
229 jz 4f
230 /* output menu item */
231 movw $prefix, %si
232 incb (%si)
233 call message /* menu number */
234 mov (%si), %si /* ':' << 8 | '1' + count */
235 shl $2, %si /* const + count * 4 */
236 #define CONST (4 * ((':' << 8) + '1' - ((KEY_PTN1 - KEY_DISK1) & 0xff)))
237 #ifdef NO_CHS
238 movl %edi, ptn_list - CONST(%si) /* sector to read */
239 #else
240 mov %bp, ptn_list - CONST(%si) /* partition info */
241 #endif
242 #undef CONST
243 mov %bx, %si
244 call message_crlf /* prompt */
245 #endif
246 4:
247 add $0x10, %bp
248 add $TABENTRYSIZE, %bx
249 cmpb $(nametab - start - 0x100) + 4 * TABENTRYSIZE, %bl
250 jne next_ptn
251
252 #ifdef BOOT_EXTENDED
253 /*
254 * Now check extended partition chain
255 */
256 testl %edx, %edx
257 je wait_key
258 testl %ecx, %ecx
259 jne 1f
260 xchg %ecx, %edx /* save base of ext ptn chain */
261 1: addl %ecx, %edx /* sector to read */
262 movl %edx, lba_sector
263 movw $lba_info, %si
264 movb $0x42, %ah
265 pop %dx /* recover drive # */
266 push %dx /* save drive */
267 int $0x13
268 movw $nametab - LOADADDR + BOOTADDR, %bx
269 jnc next_extended /* abort menu on read fail */
270 #endif
271
272 /*
273 * The non-bootsel code traverses this code path, it needs the
274 * correct keycode to select the active partition.
275 */
276
277 #ifndef BOOTSEL
278 mov $(KEY_ACTIVE - KEY_DISK1) & 0xff, %ax
279 #else
280 /*
281 * Get the initial time value for the timeout comparison. It is returned
282 * by int 1a in cx:dx. We do sums modulo 2^16 so it doesn't matter if
283 * the counter wraps (which it does every hour) - so we can safely
284 * ignore 'cx'.
285 *
286 * Loop around checking for a keypress until we have one, or timeout is
287 * reached.
288 */
289 wait_key:
290 xorb %ah, %ah
291 int $0x1a
292 mov %dx, %di /* start time to di */
293 3:
294 #ifdef COM_PORT
295 mov COM_PORT_VAL, %dx
296 push %dx
297 add $5, %dx
298 inb %dx
299 pop %dx
300 test $1, %al
301 jz 1f
302 inb %dx
303 jmp check_key
304 #else
305 movb $1, %ah /* looks to see if a */
306 int $0x16 /* key has been pressed */
307 jz 1f
308 get_key:
309 xorb %ah, %ah
310 int $0x16 /* 'read key', code ah, ascii al */
311 shr $8, %ax /* code in %al, %ah zero */
312 jmp check_key
313 #endif
314
315 1: xorb %ah, %ah
316 int $0x1a /* current time to cx:dx */
317 sub %di, %dx
318 cmpw timeout, %dx /* always wait for 1 tick... */
319 jbe 3b /* 0xffff means never timeout */
320 def_key:
321 mov defkey - 1, %ax /* timedout - get default key to %ah */
322
323 /*
324 * We have a keycode, see what it means.
325 * If we don't know we generate error '?' and go ask again
326 */
327 check_key:
328 /*
329 * F1-F10 -> boot disk 0-9. Check if the requested disk isn't above
330 * the number of disks actually in the system as stored in 0:0475 by
331 * the BIOS.
332 * If we trust loc 475, we needn't check the upper bound on the keystroke
333 * This is always sector 0, so always read using chs.
334 */
335 subb $KEY_DISK1, %al
336 cmpb 0x0475, %al
337 jae boot_ptn
338 addb $0x80, %al
339 pop %dx /* dump saved drive # */
340 push %ax /* replace with new */
341 #ifdef NO_CHS
342 xorl %ebp, %ebp /* read sector number 0 */
343 jmp boot_lba
344 #else
345 movw $chs_zero, %si /* chs read sector zero info */
346 jmp read_chs
347 #endif
348 #endif /* BOOTSEL */
349
350 /*
351 * Boot requested partition.
352 * Use keycode to index the table we generated when we scanned the mbr
353 * while generating the menu.
354 *
355 * We very carfully saved the values in the correct part of the table.
356 */
357
358 boot_ptn:
359 shl $2, %ax
360 movw %ax, %si
361 #ifdef NO_CHS
362 movl ptn_list(%si), %ebp
363 testl %ebp, %ebp
364 jnz boot_lba
365 #else
366 mov ptn_list(%si), %si
367 test %si, %si
368 jnz boot_si
369 #endif
370 #ifdef BOOTSEL
371 set_err(ERR_KEY)
372 #else
373 set_err(ERR_INVPART)
374 #endif
375 /* jmp err_msg */
376
377 /* Something went wrong...
378 * Output error code,
379 * reset disk subsystem - needed after read failure,
380 * and wait for user key
381 */
382 err_msg:
383 #ifdef TERSE_ERROR
384 movb %al, errcod
385 movw $errtxt, %si
386 call message
387 #else
388 push %ax
389 movw $errtxt, %si
390 call message
391 pop %si
392 call message_crlf
393 #endif
394 pop %dx /* drive we errored on */
395 xor %ax,%ax /* only need %ah = 0 */
396 int $0x13 /* reset disk subsystem */
397 #ifdef BOOTSEL
398 pop %dx /* original drive number */
399 push %dx
400 push %dx
401 #ifdef COM_PORT
402 jmp wait_key /* Read with timeout (again) */
403 #else
404 jmp get_key /* Blocking read */
405 #endif
406 #else
407 int $0x18 /* BIOS might ask for a key */
408 /* press and retry boot seq. */
409 1: sti
410 hlt
411 jmp 1b
412 #endif
413
414 #ifndef NO_CHS
415 /*
416 * Active partition pointed to by si.
417 * Read the first sector.
418 *
419 * We can either do a CHS (Cylinder Head Sector) or an LBA (Logical
420 * Block Address) read. Always doing the LBA one
421 * would be nice - unfortunately not all systems support it.
422 * Also some may contain a separate (eg SCSI) bios that doesn't
423 * support it even when the main bios does.
424 *
425 * There is also the additional problem that the CHS values may be wrong
426 * (eg if fdisk was run on a different system that used different BIOS
427 * geometry). We convert the CHS value to a LBA sector number using
428 * the geometry from the BIOS, if the number matches we do a CHS read.
429 */
430 boot_si:
431 movl 8(%si), %ebp /* get sector # */
432
433 testb $MBR_BS_READ_LBA, flags
434 jnz boot_lba /* fdisk forced LBA read */
435
436 pop %dx /* collect saved drive... */
437 push %dx /* ...number to dl */
438 movb $8, %ah
439 int $0x13 /* chs info */
440
441 /*
442 * Validate geometry, if the CHS sector number doesn't match the LBA one
443 * we'll do an LBA read.
444 * calc: (cylinder * number_of_heads + head) * number_of_sectors + sector
445 * and compare against LBA sector number.
446 * Take a slight 'flier' and assume we can just check 16bits (very likely
447 * to be true because the number of sectors per track is 63).
448 */
449 movw 2(%si), %ax /* cylinder + sector */
450 push %ax /* save for sector */
451 shr $6, %al
452 xchgb %al, %ah /* 10 bit cylinder number */
453 shr $8, %dx /* last head */
454 inc %dx /* number of heads */
455 mul %dx
456 mov 1(%si), %dl /* head we want */
457 add %dx, %ax
458 and $0x3f, %cx /* number of sectors */
459 mul %cx
460 pop %dx /* recover sector we want */
461 and $0x3f, %dx
462 add %dx, %ax
463 dec %ax
464
465 cmp %bp, %ax
466 je read_chs
467
468 #ifndef NO_LBA_CHECK
469 /*
470 * Determine whether we have int13-extensions, by calling int 13, function 41.
471 * Check for the magic number returned, and the disk packet capability.
472 */
473 movw $0x55aa, %bx
474 movb $0x41, %ah
475 pop %dx
476 push %dx
477 int $0x13
478 set_err(ERR_NO_LBA)
479 jc err_msg /* no int13 extensions */
480 cmpw $0xaa55, %bx
481 jnz err_msg
482 testb $1, %cl
483 jz err_msg
484 #endif /* NO_LBA_CHECK */
485 #endif /* NO_CHS */
486
487 /*
488 * Save sector number (passed in %ebp) into lba parameter block,
489 * read the sector and leap into it.
490 */
491 boot_lba:
492 movl %ebp, lba_sector /* save sector number */
493 movw $lba_info, %si
494 movb $0x42, %ah
495 pop %dx /* recover drive # */
496 do_read:
497 push %dx /* save drive */
498 int $0x13
499
500 set_err(ERR_READ)
501 jc err_msg
502
503 /*
504 * Check signature for valid bootcode
505 */
506 movb BOOTADDR, %al /* first byte non-zero */
507 test %al, %al
508 jz 1f
509 movw BOOTADDR + MBR_MAGIC_OFFSET, %ax
510 1: cmp $MBR_MAGIC, %ax
511 set_err(ERR_NOOS)
512 jnz err_msg
513
514 /* We pass the sector number through to the next stage boot.
515 * It doesn't have to use it (indeed no other mbr code will generate) it,
516 * but it does let us have a NetBSD pbr that can identify where it was
517 * read from! This lets us use this code to select between two
518 * NetBSD system on the same physical driver.
519 * (If we've read the mbr of a different disk, it gets a random number
520 * - but it wasn't expecting anything...)
521 */
522 movl %ebp, %esi
523 pop %dx /* recover drive # */
524 jmp start - LOADADDR + BOOTADDR
525
526
527 #ifndef NO_CHS
528 /*
529 * Sector below CHS limit
530 * Do a cylinder-head-sector read instead.
531 */
532 read_chs:
533 pop %dx /* recover drive # */
534 movb 1(%si), %dh /* head */
535 movw 2(%si), %cx /* ch=cyl, cl=sect */
536 movw $BOOTADDR, %bx /* es:bx is buffer */
537 movw $0x201, %ax /* command 2, 1 sector */
538 jmp do_read
539 #endif
540
541 /*
542 * Control block for int-13 LBA read.
543 * We need a xx, 00, 01, 00 somewhere to load chs for sector zero,
544 * by a complete fluke there is one here!
545 */
546 chs_zero:
547 lba_info:
548 .word 0x10 /* control block length */
549 .word 1 /* sector count */
550 .word BOOTADDR /* offset in segment */
551 .word 0 /* segment */
552 lba_sector:
553 .long 0x0000 /* sector # goes here... */
554 .long 0x0000
555
556 errtxt: .ascii "Error " /* runs into crlf if errcod set */
557 errcod: .byte 0
558 crlf: .asciz "\r\n"
559
560 #ifndef NO_BANNER
561 #ifdef BOOTSEL
562 #ifdef COM_PORT
563 banner: .asciz "a: disk"
564 #else
565 banner: .asciz "Fn: diskn"
566 #endif
567 #else
568 banner: .asciz "NetBSD MBR boot"
569 #endif
570 #endif
571
572 #ifdef BOOTSEL
573 prefix: .asciz "0: "
574 #endif
575
576 #ifndef TERSE_ERROR
577 ERR_INVPART: .asciz "No active partition"
578 ERR_READ: .asciz "Disk read error"
579 ERR_NOOS: .asciz "No operating system"
580 #ifndef NO_LBA_CHECK
581 ERR_NO_LBA: .asciz "Invalid CHS read"
582 #endif
583 #ifdef BOOTSEL
584 ERR_KEY: .asciz "bad key"
585 #endif
586 #endif
587
588 #if defined(COM_PORT) && defined(COM_BAUD)
589 #define COM_DIVISOR (((COM_FREQ / COM_BAUD) + 8) / 16)
590 com_args:
591 .byte 0x80 /* divisor latch enable */
592 .byte +3 /* io_port + 3 */
593 .byte COM_DIVISOR & 0xff
594 .byte -3 /* io_port */
595 .byte COM_DIVISOR >> 8 /* high baud */
596 .byte +1 /* io_port + 1 */
597 .byte 0x03 /* 8 bit no parity */
598 .byte +2 /* io_port + 3 */
599 num_com_args = (. - com_args)/2
600 #endif
601
602 /*
603 * I hate #including source files, but the stuff below has to be at
604 * the correct absolute address.
605 * Clearly this could be done with a linker script.
606 */
607
608 message_crlf:
609 call message
610 movw $crlf, %si
611 #ifdef COM_PORT
612 message:
613 pusha
614 message_1:
615 lodsb
616 test %al, %al
617 jz 3f
618 mov COM_PORT_VAL, %dx
619 outb %al, %dx
620 add $5, %dl
621 2: inb %dx
622 test $0x40, %al
623 jz 2b
624 jmp message_1
625 3: popa
626 ret
627 #else
628 #include <message.S>
629 #endif
630
631 #if 0
632 #include <dump_eax.S>
633 #endif
634
635 /*
636 * Stuff from here on is overwritten by fdisk - the offset must not change...
637 *
638 * Get amount of space to makefile can report it.
639 * (Unfortunately I can't seem to get the value reported when it is -ve)
640 */
641 mbr_space = defkey - .
642 . = start + MBR_BS_OFFSET
643 /*
644 * Default action, as a keyvalue we'd normally read from the BIOS.
645 */
646 defkey:
647 .byte KEY_ACTIVE /* ps/2 code */
648 #ifndef BOOTSEL_FLAGS
649 #define BOOTSEL_FLAGS 0
650 #endif
651 flags: .byte MBR_BS_NEWMBR | BOOTSEL_FLAGS
652 /*
653 * Timeout value. ~65536 ticks per hour, which is ~18.2 times per second.
654 * 0xffff means never timeout.
655 */
656 timeout:
657 .word 182 /* default to 10 seconds */
658 /*
659 * mbr_bootsel
660 */
661 nametab:
662 .fill MBR_PART_COUNT * (MBR_BS_PARTNAMESIZE + 1), 0x01, 0x00
663
664 /* space for mbr_dsn */
665 . = start + MBR_DSN_OFFSET
666 .long 0
667
668 /* mbr_bootsel_magic */
669 . = start + MBR_BS_MAGIC_OFFSET
670 .word MBR_BS_MAGIC
671
672 /*
673 * MBR partition table
674 */
675 . = start + MBR_PART_OFFSET
676 parttab:
677 .fill 0x40, 0x01, 0x00
678
679 . = start + MBR_MAGIC_OFFSET
680 .word MBR_MAGIC
681
682 /* zeroed data space */
683 bss_off = 0
684 bss_start = .
685 #define BSS(name, size) name = bss_start + bss_off; bss_off = bss_off + size
686 BSS(ptn_list, 256 * 4) /* long[]: boot sector numbers */
687 BSS(dump_eax_buff, 16)
688 BSS(bss_end, 0)
689