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