1 /* -----------------------------------------------------------------------
3 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
38 secpercyl = (stack-12)
43 /* gas/ld has issues with doing this as absolute addresses... */
44 .section ".bootsec", "a", @nobits
52 .byte 0x33, 0xc0 /* xorw %ax, %ax */
58 pushw %es /* es:di -> $PnP header */
64 /* Copy down to 0:0x600 */
73 pushw %dx /* dl -> drive number */
75 /* Check to see if we have EBIOS */
76 pushw %dx /* drive number */
77 movb $0x41, %ah /* %al == 0 already */
86 shrw %cx /* Bit 0 = fixed disk subset */
89 /* We have EBIOS; patch in the following code at
90 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
91 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
97 /* Get (C)HS geometry */
100 andw $0x3f, %cx /* Sector count */
101 pushw %cx /* Save sectors on the stack */
102 movzbw %dh, %ax /* dh = max head */
103 incw %ax /* From 0-based max to count */
104 mulw %cx /* Heads*sectors -> sectors per cylinder */
106 /* Save sectors/cylinder on the stack */
107 pushw %dx /* High word */
108 pushw %ax /* Low word */
110 xorl %eax, %eax /* Base */
111 cdq /* Root (%edx <- 0) */
112 call scan_partition_table
114 /* If we get here, we have no OS */
117 .ascii "Missing operating system.\r\n"
120 * read_sector: read a single sector pointed to by %eax to 0x7c00.
121 * CF is set on error. All registers saved.
127 pushl %edx /* MSW of LBA */
128 pushl %eax /* LSW of LBA */
129 pushw %es /* Buffer segment */
130 pushw %bx /* Buffer offset */
131 pushw $1 /* Sector count */
132 pushw $16 /* Size of packet */
135 /* This chunk is skipped if we have ebios */
136 /* Do not clobber %eax before this chunk! */
137 /* This also relies on %bx and %edx as set up above. */
147 incw %cx /* Sectors are 1-based */
153 leaw 16(%si), %sp /* Drop DAPA */
158 * read_partition_table:
159 * Read a partition table (pointed to by %eax), and copy
160 * the partition table into the ptab buffer.
162 * Clobbers %si, %di, and %cx, other registers preserved.
165 * On error, CF is set and ptab is overwritten with junk.
169 read_partition_table:
171 movw $bootsec+446, %si
178 * scan_partition_table:
179 * Scan a partition table currently loaded in the partition table
180 * area. Preserve all registers.
183 * %eax - base (location of this partition table)
184 * %edx - root (offset from MBR, or 0 for MBR)
186 * These get pushed into stack slots:
187 * 28(%bp) - %eax - base
188 * 20(%bp) - %edx - root
191 scan_partition_table:
195 /* Search for active partitions */
210 decw %ax /* Number of active partitions found */
214 /* No active partitions found, look for extended partitions */
215 popw %cx /* %cx <- 4 */
216 popw %bx /* %bx <- ptab */
219 cmpb $0x0f, %al /* 0x0f = Win9x extended */
221 andb $~0x80, %al /* 0x85 = Linux extended */
222 cmpb $0x05, %al /* 0x05 = MS-DOS extended */
225 /* It is an extended partition. Read the extended partition and
226 try to scan it. If the scan returns, re-load the current
227 partition table and resume scan. */
229 movl 8(%bx), %eax /* Partition table offset */
230 movl 20(%bp), %edx /* "Root" */
231 addl %edx, %eax /* Compute location of new ptab */
232 andl %edx, %edx /* Is this the MBR? */
234 movl %eax, %edx /* Offset -> root if this was MBR */
236 call read_partition_table
238 call scan_partition_table
240 /* This returned, so we need to reload the current partition table */
241 movl 28(%bp), %eax /* "Base" */
242 call read_partition_table
246 /* Not an extended partition */
250 /* Nothing found, return */
256 .ascii "Multiple active partitions.\r\n"
259 * boot: invoke the actual bootstrap. (%si) points to the partition
260 * table entry, and 28(%bp) has the partition table base.
265 movl %eax, 8(%si) /* Adjust in-memory partition table entry */
268 cmpw $0xaa55, (bootsec+510)
269 jne missing_os /* Not a valid boot sector */
270 movw $driveno, %sp /* driveno == bootsec-6 */
271 popw %dx /* dl -> drive number */
272 popw %di /* es:di -> $PnP vector */
275 jmpw *%sp /* %sp == bootsec */
279 .ascii "Operating system load error.\r\n"
282 * Print error messages. This is invoked with "call", with the
283 * error message at the return address.
290 movb (BIOS_page), %bh
292 int $0x10 /* May destroy %bp */
293 cmpb $10, %al /* Newline? */
296 int $0x18 /* Boot failure */