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