2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1998 Robert Nordier
35 * All rights reserved.
37 * Redistribution and use in source and binary forms are freely
38 * permitted provided that the above copyright notice and this
39 * paragraph and the following disclaimer are duplicated in all
42 * This software is provided "AS IS" and without any express or
43 * implied warranties, including, without limitation, the implied
44 * warranties of merchantability and fitness for a particular
48 * $FreeBSD: src/sys/boot/i386/boot0/boot0.s,v 1.26 2003/06/01 20:41:04 obrien Exp $
49 * $DragonFly: src/sys/boot/pc32/boot0/boot0.S,v 1.9 2008/07/06 17:17:01 mneumann Exp $
52 #include "../bootasm.h"
55 /* ... using a serial console on COM1. */
59 * A 512-byte boot manager.
61 .set PRT_OFF,0x1be # Partition table
63 .set TBL0SZ,0x3 # Table 0 size
64 .set TBL1SZ,0xb # Table 1 size
66 .set MAGIC,0xaa55 # Magic: bootable
67 .set B0MAGIC,0xbb66 # Identification
69 .set KEY_ENTER,0x1c # Enter key scan code
70 .set KEY_F1,0x3b # F1 key scan code
71 .set KEY_1,0x02 # #1 key scan code
73 .set ASCII_BEL,0x07 # ASCII code for <BEL>
74 .set ASCII_CR,0x0D # ASCII code for <CR>
77 * Addresses in the sector of embedded data values. Accessed
78 * with negative offsets from the end of the relocated sector
81 * Note that %ebp is the base of our variable space and
82 * points at the end of the sector (base + 0x200). The
83 * fake partition and menu option is thus stored in the
84 * memory just after the boot0 sector.
86 .set _NXTDRV,-0x48 # Next drive
87 .set _OPT,-0x47 # Default option
88 .set _SETDRV,-0x46 # Drive to force
89 .set _FLAGS,-0x45 # Flags
90 .set _TICKS,-0x44 # Timeout ticks
91 .set _FAKE,0x0 # Fake partition entry
92 .set _MNUOPT,0xc # Menu options
94 .globl start # Entry point
95 .code16 # This runs in real mode
98 * Initialise segments and registers to known values.
99 * segments start at 0. The stack is immediately below the
100 * address we were loaded to.
102 start: cld # String ops inc
104 movw %ax,%es # Address
106 movw %ax,%ss # Set up
107 movw $MEM_BIOS_LADDR,%sp # stack
110 * Copy this code to the address it was linked for
112 movw %sp,%si # Source
113 movw $start,%di # Destination
114 movw $0x100,%cx # Word count
119 * Set address for variable space beyond code, and clear it.
120 * Notice that this is also used to point to the values
121 * embedded in the block, by using negative offsets.
123 movw %di,%bp # Address variables
124 movb $0x8,%cl # Words to clear
129 * Relocate to the new copy of the code. Do not make
130 * assumptions with regard to a relative-PC near jump
133 incb -0xe(%di) # Sector number
134 pushw $main # Jump to relocated code
138 #if defined(SIO) && COMSPEED != 0
140 * Initialize the serial port.
141 * bioscom preserves the driver number in DX.
143 movw $COMSPEED,%ax # defined by Makefile
147 * Check what flags were loaded with us, specifically, Use a
148 * predefined Drive. If what the bios gives us is bad, use
149 * the '0' in the block instead, as well.
151 testb $0x20,_FLAGS(%bp) # Set number drive?
153 testb %dl,%dl # Drive number valid?
154 js main.2 # Possibly (0x80 set)
156 main.1: movb _SETDRV(%bp),%dl # Drive number to use
159 * Whatever we decided to use, now store it into the fake
160 * partition entry that lives in the data space above us.
162 main.2: movb %dl,_FAKE(%bp) # Save drive number
163 callw putn # To new line
164 pushw %dx # Save drive number
167 * Start out with a pointer to the 4th byte of the first
168 * table entry so that after 4 iterations it's beyond the
169 * end of the sector. and beyond a 256 byte boundary and
170 * has overflowed 8 bits (see next comment). (remember
171 * that the table starts 2 bytes earlier than you would
172 * expect as the bootable flag is after it in the block)
174 movw $(partbl+0x4),%bx # Partition table (+4)
175 xorw %dx,%dx # Item number
178 * Loop around on the partition table, printing values until
179 * we pass a 256 byte boundary. The end of loop test is at
182 main.3: movb %ch,-0x4(%bx) # Zero active flag (ch == 0)
183 btw %dx,_FLAGS(%bp) # Entry enabled?
187 * If any of the entries in the table are the same as the
188 * 'type' in the slice table entry, then this is an empty
189 * or non bootable partition. Skip it.
191 movb (%bx),%al # Load type
192 movw $tables,%di # Lookup tables
193 movb $TBL0SZ,%cl # Number of entries
199 * Now scan the table of known types
201 movb $TBL1SZ,%cl # Number of entries
207 * If it matches get the matching element in the
208 * next array. if it doesn't, we are already
209 * pointing at its first element which points to a "?".
211 addw $TBL1SZ,%di # Adjust
212 main.4: movb (%di),%cl # Partition
213 addw %cx,%di # description
214 callw putx # Display it
215 main.5: incw %dx # Next item
216 addb $0x10,%bl # Next entry
217 jnc main.3 # Till done
220 * Passed a 256 byte boundary..
222 * Add one to the drive number and check it is valid,
224 popw %ax # Drive number
225 subb $0x80-0x1,%al # Does next
226 cmpb BDA_NHRDRV,%al # drive exist? (from BIOS?)
230 * If not then if there is only one drive,
231 * Don't display drive as an option.
233 decw %ax # Already drive 0?
237 * If it was illegal or we cycled through them,
238 * then go back to drive 0.
240 xorb %al,%al # Drive 0
243 * Whatever drive we selected, make it an ascii digit and
244 * save it back to the "next drive" location in the loaded
245 * block in case we want to save it for next time. This also
246 * is part of the printed drive string so add 0x80 to indicate
249 main.6: addb $'0'|0x80,%al # Save next
250 movb %al,_NXTDRV(%bp) # drive number
251 movw $drive,%di # Display
255 * Now that we've printed the drive (if we needed to),
256 * display a prompt. Get ready for the input by noting the
259 main.7: movw $prompt,%si # Display
260 callw putstr # prompt
261 movb _OPT(%bp),%dl # Display
264 xorb %ah,%ah # BIOS: Get
265 int $0x1a # system time
266 movw %dx,%di # Ticks when
267 addw _TICKS(%bp),%di # timeout
270 * Busy loop, looking for keystrokes but
271 * keeping one eye on the time.
275 movb $0x1,%ah # BIOS: Check
276 int $0x16 # for keypress
277 jnz main.11 # Have one
279 movb $0x03,%ah # BIOS: Read COM
281 testb $0x01,%ah # Check line status
282 jnz main.11 # (bit 1 indicates input)
284 xorb %ah,%ah # BIOS: Get
285 int $0x1a # system time
286 cmpw %di,%dx # Timeout?
290 * If timed out or defaulting, come here.
292 main.9: movb _OPT(%bp),%al # Load default
293 jmp main.12 # Join common code
296 * User's last try was bad, beep in displeasure.
297 * Since nothing was printed, just continue on as if the
298 * user hadn't done anything. This gives the effect of the
299 * user getting a beep for all bad keystrokes but no action
300 * until either the timeout occurs or the user hits a good
303 main.10: movb $ASCII_BEL,%al # Signal
311 xorb %ah,%ah # BIOS: Get
313 movb %ah,%al # Scan code
315 movb $0x02,%ah # BIOS: Receive
320 * If it's CR act as if timed out.
323 cmpb $KEY_ENTER,%al # Enter pressed?
325 cmpb $ASCII_CR,%al # Enter pressed?
330 * Otherwise check if legal
334 subb $KEY_F1,%al # Less F1 scan code
335 cmpb $0x4,%al # F1..F5?
337 subb $(KEY_1 - KEY_F1),%al # Less #1 scan code
339 subb $'1',%al # Less '1' ascii character
341 cmpb $0x4,%al # #1..#5?
345 * We have a selection. But if it's a bad selection go back
346 * to complain. The bits in MNUOPT were set when the options
347 * were printed. Anything not printed is not an option.
349 main.12: cbtw # Option
350 btw %ax,_MNUOPT(%bp) # enabled?
354 * Save the info in the original tables
355 * for rewriting to the disk.
357 movb %al,_OPT(%bp) # Save option
358 lea _FAKE(%bp),%si # Partition for write
359 movb (%si),%dl # Drive number
360 movw %si,%bx # Partition for read
361 cmpb $0x4,%al # F5 pressed?
364 shlb $0x4,%al # Point to
365 addw $partbl,%ax # selected
366 xchgw %bx,%ax # partition
367 movb $0x80,(%bx) # Flag active
370 * If not asked to do a write-back (flags 0x40) don't do one.
372 main.13: pushw %bx # Save
373 testb $0x40,_FLAGS(%bp) # No updates?
375 movw $start,%bx # Data to write
376 movb $0x3,%ah # Write sector
377 callw intx13 # to disk
378 main.14: popw %si # Restore
382 * If going to next drive, replace drive with selected one.
383 * Remember to un-ascii it. Hey 0x80 is already set, cool!
385 jne main.15 # If not F5
386 movb _NXTDRV(%bp),%dl # Next drive
387 subb $'0',%dl # number
390 * load selected bootsector to the MEM_BIOS_LADDR location
391 * in RAM. If it fails to read or isn't marked bootable,
392 * treat it as a bad selection.
393 * XXX what does %si carry?
395 main.15: movw $MEM_BIOS_LADDR,%bx # Address for read
396 movb $0x2,%ah # Read sector
397 callw intx13 # from disk
398 jc main.10 # If error
399 cmpw $MAGIC,0x1fe(%bx) # Bootable?
402 movw $crlf,%si # Leave some
405 jmp *%bx # Invoke bootstrap
412 movb $'F',%al # Display
415 movb $'1',%al # Prepare
417 jmp putstr.1 # Display the rest
420 * Display the option and note that it is a valid option.
421 * That last point is a bit tricky..
423 putx: btsw %dx,_MNUOPT(%bp) # Enable menu option
424 movw $item,%si # Display
426 movw %di,%si # Display the rest
428 puts: callw putstr # Display string
430 putn: movw $crlf,%si # To next line
432 putstr: lodsb # Get byte
433 testb $0x80,%al # End of string?
435 putstr.1: callw putchr # Display char
436 jmp putstr # Continue
437 putstr.2: andb $~0x80,%al # Clear MSB
442 movw $0x7,%bx # Page:attribute
443 movb $0xe,%ah # BIOS: Display
444 int $0x10 # character
449 movb $0x01,%ah # BIOS: Send
452 xorw %dx,%dx # Use COM1
453 int $0x14 # Character
460 * One-sector disk I/O routine
462 * Setup for both the packet and non-packet interfaces
463 * then select one or the other.
465 * Don't trust the BIOS to keep registers intact across
466 * the call, use pusha/popa.
468 intx13: movb 0x1(%si),%dh # (nonpkt) head
469 movw 0x2(%si),%cx # (nonpkt) cylinder:sector
470 movb $0x1,%al # (nonpkt) Sector count
471 pusha # save: do not trust the bios
472 pushl $0x0 # (pkt) LBA address
473 pushl 0x8(%si) # (pkt)
474 pushw %es # (pkt) xfer buffer address
476 pushw $1 # (pkt) Block count
477 pushw $16 # (pkt) Packet size
478 testb $0x80,_FLAGS(%bp) # Use packet interface?
480 movw %sp,%si # Yes, set packet pointer
481 decw %ax # Verify off
482 orb $0x40,%ah # Set pkt mode in command
483 intx13.1: int $0x13 # BIOS: Disk I/O
484 # WARNING: RETAIN CARRY AFTER BIOS CALL
486 lea 16(%si),%sp # cleanup the stack
487 popa # Restore registers
494 item: .ascii " "; .byte ' '|0x80
495 prompt: .ascii "\nDefault:"; .byte ' '|0x80
496 crlf: .ascii "\r"; .byte '\n'|0x80
499 * Partition type tables
504 * These entries identify invalid or NON BOOT types and
510 * These values indicate bootable types we know the names of.
512 .byte 0x1, 0x6, 0x7, 0xb, 0xc, 0xe, 0x83
513 .byte 0x9f, 0xa5, 0xa6, 0xa9
516 * These are offsets that match the known names above and
517 * point to the strings that will be printed.
519 .byte os_misc-. # Unknown
522 .byte os_dos-. # Windows
523 .byte os_dos-. # Windows
524 .byte os_dos-. # Windows
525 .byte os_dos-. # Windows
526 .byte os_linux-. # Linux
527 .byte os_bsd-. # BSD/OS
528 .byte os_dfbsd-. # DragonFly/FreeBSD
529 .byte os_bsd-. # OpenBSD
530 .byte os_bsd-. # NetBSD
533 * And here are the strings themselves. 0x80 or'd into a
534 * byte indicates the end of the string. (not so great for
537 os_misc: .ascii "?"; .byte '?'|0x80
538 os_dos: .ascii "DO"; .byte 'S'|0x80
539 os_linux: .ascii "Linu"; .byte 'x'|0x80
540 os_dfbsd: .ascii "DF/F"
541 os_bsd: .ascii "BS"; .byte 'D'|0x80
543 .org PRT_OFF-0xe,0x90
545 .word B0MAGIC # Magic number
548 * These values are sometimes changed before writing back
549 * to the drive. Be especially careful that nxtdrv: must
550 * come after drive:, as it is part of the same string.
552 drive: .ascii "Drive "
553 nxtdrv: .byte 0x0 # Next drive number
554 opt: .byte 0x0 # Option
555 setdrv: .byte 0x80 # Drive to force
556 flags: .byte FLAGS # Flags
557 ticks: .word TICKS # Delay
560 * here is the 64 byte partition table that fdisk would
563 partbl: .fill 0x40,0x1,0x0 # Partition table
564 .word MAGIC # Magic number