1 /* -----------------------------------------------------------------------
3 * Copyright 2007 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
47 pushw %es /* es:di -> $PnP header */
49 pushw %dx /* dl -> drive number */
54 /* Copy down to 0:0x600 */
62 /* Check to see if we have EBIOS */
63 pushw %dx /* drive number */
76 /* We have EBIOS; patch in a jump to read_sector_ebios */
77 movw $0xeb+((read_sector_ebios-read_sector_cbios+2)<< 8), (read_sector_cbios)
82 /* Get (C)HS geometry */
86 movb %dh, %al /* dh = number of heads */
87 incw %ax /* From 0-based to count */
88 pushw %ax /* Save heads on the stack */
89 andw $0x3f, %cx /* Sector count */
90 pushw %cx /* Save sectors on the stack */
95 call scan_partition_table
98 /* If we get here, we have no OS */
102 * read_sector: read a single sector pointed to by %eax to 0x7c00.
103 * CF is set on error. All registers clobbered.
114 /* dx = head, ax = cylinder */
133 * read_partition_table:
134 * Read a partition table (pointed to by %eax), and copy
135 * the partition table into the ptab buffer.
136 * Preserve registers.
140 read_partition_table:
144 movw $bootsec+446, %si
153 * scan_partition_table:
154 * Scan a partition table currently loaded in the partition table
155 * area. Preserve 16-bit registers.
158 * 18(%bp) - root (offset from MBR, or 0 for MBR)
159 * 22(%bp) - base (location of this partition table)
162 scan_partition_table:
166 /* Search for active partitions */
179 cmpw $1, %ax /* Number of active partitions found */
183 /* No active partitions found, look for extended partitions */
185 movb $4, %cl /* cx == 0 here */
188 cmpb $0x05, %al /* MS-DOS extended */
190 cmpb $0x0f, %al /* Win9x extended */
192 cmpb $0x85, %al /* Linux extended */
195 /* It is an extended partition. Read the extended partition and
196 try to scan it. If the scan returns, re-load the current
197 partition table and resume scan. */
199 movl 8(%di), %eax /* Partition table offset */
200 movl 18(%bp), %edx /* "Root" */
201 addl %edx, %eax /* Compute location of new ptab */
202 andl %edx, %edx /* Is this the MBR? */
204 movl %eax, %edx /* Offset -> root if this was MBR */
206 pushl %eax /* Push new base */
207 pushl %edx /* Push new root */
208 call read_partition_table
210 call scan_partition_table
213 /* This returned, so we need to reload the current partition table */
215 call read_partition_table
219 /* Not an extended partition */
223 /* Nothing found, return */
228 * boot: invoke the actual bootstrap. (%si) points to the partition
229 * table entry, and 22(%bp) has the partition table base.
237 cmpw $0xaa55, (bootsec+0x510)
238 je missing_os /* Not a valid boot sector */
240 popw %dx /* dl -> drive number */
241 popw %di /* es:di -> $PnP vector */
250 movw $missing_os_msg, %si
253 movw $disk_error_msg, %si
256 movw $too_many_active_msg, %si
265 movb (BIOS_page), %bh
270 int $0x18 /* Boot failure */
274 .ascii "Missing operating system."
277 .ascii "Failed to load operating system."
280 .ascii "Multiple active partititons."
285 .short 16 /* Size of packet */
286 .short 1 /* Sector count */
287 .short 0x7c00 /* Buffer offset */
288 .short 0 /* Buffer segment */
289 .long 0 /* LSW of LBA */
290 .long 0 /* MSW of LBA */