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 * ----------------------------------------------------------------------- */
35 secpercyl = (stack-12)
39 /* gas/ld has issues with doing this as absolute addresses... */
40 .section ".bootsec", "a", @nobits
54 pushw %es /* es:di -> $PnP header */
56 pushw %dx /* dl -> drive number */
61 /* Copy down to 0:0x600 */
69 /* Check to see if we have EBIOS */
70 pushw %dx /* drive number */
83 /* We have EBIOS; patch in a jump to read_sector_ebios */
84 movw $0xeb+((read_sector_ebios-read_sector_cbios-2)<< 8), (read_sector_cbios)
89 /* Get (C)HS geometry */
92 andw $0x3f, %cx /* Sector count */
93 pushw %cx /* Save sectors on the stack */
95 pushw %ax /* High word of sectors/cylinder */
96 movb %dh, %al /* dh = number of heads */
97 incw %ax /* From 0-based to count */
98 mulw %cx /* Heads*sectors -> sectors per cylinder */
99 pushw %ax /* Save sectors/cylinder on the stack */
101 xorl %eax, %eax /* Base */
102 xorl %edx, %edx /* Root: %edx <- 0 */
103 call scan_partition_table
105 /* If we get here, we have no OS */
109 * read_sector: read a single sector pointed to by %eax to 0x7c00.
110 * CF is set on error. All registers clobbered.
138 * read_partition_table:
139 * Read a partition table (pointed to by %eax), and copy
140 * the partition table into the ptab buffer.
141 * Preserve registers.
145 read_partition_table:
149 movw $bootsec+446, %si
158 * scan_partition_table:
159 * Scan a partition table currently loaded in the partition table
160 * area. Preserve all registers.
163 * %eax - base (location of this partition table)
164 * %edx - root (offset from MBR, or 0 for MBR)
166 * These get pushed into stack slots:
167 * 28(%bp) - %eax - base
168 * 20(%bp) - %edx - root
171 scan_partition_table:
175 /* Search for active partitions */
188 cmpw $1, %ax /* Number of active partitions found */
192 /* No active partitions found, look for extended partitions */
194 movb $4, %cl /* cx == 0 here */
197 cmpb $0x05, %al /* MS-DOS extended */
199 cmpb $0x0f, %al /* Win9x extended */
201 cmpb $0x85, %al /* Linux extended */
204 /* It is an extended partition. Read the extended partition and
205 try to scan it. If the scan returns, re-load the current
206 partition table and resume scan. */
208 movl 8(%di), %eax /* Partition table offset */
209 movl 20(%bp), %edx /* "Root" */
210 addl %edx, %eax /* Compute location of new ptab */
211 andl %edx, %edx /* Is this the MBR? */
213 movl %eax, %edx /* Offset -> root if this was MBR */
215 call read_partition_table
217 call scan_partition_table
219 /* This returned, so we need to reload the current partition table */
221 call read_partition_table
225 /* Not an extended partition */
229 /* Nothing found, return */
234 * boot: invoke the actual bootstrap. (%si) points to the partition
235 * table entry, and 22(%bp) has the partition table base.
245 cmpw $0xaa55, (bootsec+510)
246 jne missing_os /* Not a valid boot sector */
248 popw %dx /* dl -> drive number */
249 popw %di /* es:di -> $PnP vector */
258 movw $missing_os_msg, %si
261 movw $disk_error_msg, %si
264 movw $too_many_active_msg, %si
273 movb (BIOS_page), %bh
278 int $0x18 /* Boot failure */
282 .ascii "Missing operating system."
285 .ascii "Operating system load error."
288 .ascii "Multiple active partitions."
293 .short 16 /* Size of packet */
294 .short 1 /* Sector count */
295 .short 0x7c00 /* Buffer offset */
296 .short 0 /* Buffer segment */
297 .long 0 /* LSW of LBA */
298 .long 0 /* MSW of LBA */