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
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 /* Scan the primary partition table */
198 /* Is it a primary partition table? */
210 popw %cx /* %cx <- 4 */
211 popw %si /* %si <- ptab */
213 /* No primary partitions found, look for extended/logical partitions */
217 jz 12f /* Not a valid partition */
218 cmpb $0x0f, %al /* 0x0f = Win9x extended */
220 andb $~0x80, %al /* 0x85 = Linux extended */
221 cmpb $0x05, %al /* 0x05 = MS-DOS extended */
224 /* It is an extended partition. Read the extended partition and
225 try to scan it. If the scan returns, re-load the current
226 partition table and resume scan. */
228 movl 8(%si), %eax /* Partition table offset */
229 addl %edx, %eax /* Compute location of new ptab */
230 andl %edx, %edx /* Is this the MBR? */
232 movl %eax, %edx /* Offset -> root if this was MBR */
234 call read_partition_table
236 call scan_partition_table
238 /* This returned, so we need to reload the current partition table */
239 movl 28(%bp), %eax /* "Base" */
240 call read_partition_table
244 /* Not an extended partition */
245 andl %edx, %edx /* Are we inside an extended part? */
247 /* If so, this is a logical partition */
254 /* Nothing found, return */
259 * boot: invoke the actual bootstrap. (%si) points to the partition
260 * table entry, and 28(%bp) has the partition table base.
267 movl %eax, 8(%si) /* Adjust in-memory partition table entry */
270 cmpw $0xaa55, (bootsec+510)
271 jne missing_os /* Not a valid boot sector */
272 movw $driveno, %sp /* driveno == bootsec-6 */
273 popw %dx /* dl -> drive number */
274 popw %di /* es:di -> $PnP vector */
277 jmpw *%sp /* %sp == bootsec */
281 .ascii "Operating system load error.\r\n"
284 * Print error messages. This is invoked with "call", with the
285 * error message at the return address.
292 movb (BIOS_page), %bh
294 int $0x10 /* May destroy %bp */
295 cmpb $10, %al /* Newline? */
298 int $0x18 /* Boot failure */
303 /* Location of the partition configuration byte */
304 partition = _start + 439