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