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 * ----------------------------------------------------------------------- */
36 secpercyl = (stack-12)
41 /* gas/ld has issues with doing this as absolute addresses... */
42 .section ".bootsec", "a", @nobits
56 pushw %es /* es:di -> $PnP header */
62 /* Copy down to 0:0x600 */
70 /* Escape for the user: if Ctrl is pressed, assume drive hd0 */
71 testb $0x04, BIOS_kbdflags /* Ctrl pressed */
75 pushw %dx /* dl -> drive number */
77 /* Check to see if we have EBIOS */
78 pushw %dx /* drive number */
79 movb $0x41, %ah /* %al == 0 already */
88 shrw %cx /* Bit 0 = fixed disk subset */
91 /* We have EBIOS; patch in the following code at
92 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
93 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
99 /* Get (C)HS geometry */
102 andw $0x3f, %cx /* Sector count */
103 pushw %cx /* Save sectors on the stack */
104 movzbw %dh, %ax /* dh = max head */
105 incw %ax /* From 0-based max to count */
106 mulw %cx /* Heads*sectors -> sectors per cylinder */
108 /* Save sectors/cylinder on the stack */
109 pushw %dx /* High word */
110 pushw %ax /* Low word */
112 xorl %eax, %eax /* Base */
113 cdq /* Root (%edx <- 0) */
114 call scan_partition_table
116 /* If we get here, we have no OS */
119 .ascii "Missing operating system.\r\n"
122 * read_sector: read a single sector pointed to by %eax to 0x7c00.
123 * CF is set on error. All registers saved.
129 pushl %edx /* MSW of LBA */
130 pushl %eax /* LSW of LBA */
131 pushw %es /* Buffer segment */
132 pushw %bx /* Buffer offset */
133 pushw $1 /* Sector count */
134 pushw $16 /* Size of packet */
137 /* This chunk is skipped if we have ebios */
138 /* Do not clobber %eax before this chunk! */
139 /* This also relies on %bx and %edx as set up above. */
149 incw %cx /* Sectors are 1-based */
155 leaw 16(%si), %sp /* Drop DAPA */
160 * read_partition_table:
161 * Read a partition table (pointed to by %eax), and copy
162 * the partition table into the ptab buffer.
164 * Clobbers %si, %di, and %cx, other registers preserved.
167 * On error, CF is set and ptab is overwritten with junk.
171 read_partition_table:
173 movw $bootsec+446, %si
180 * scan_partition_table:
181 * Scan a partition table currently loaded in the partition table
182 * area. Preserve all registers.
185 * %eax - base (location of this partition table)
186 * %edx - root (offset from MBR, or 0 for MBR)
188 * These get pushed into stack slots:
189 * 28(%bp) - %eax - base
190 * 20(%bp) - %edx - root
193 scan_partition_table:
197 /* Search for active partitions */
212 decw %ax /* Number of active partitions found */
216 /* No active partitions found, look for extended partitions */
217 popw %cx /* %cx <- 4 */
218 popw %bx /* %bx <- ptab */
221 cmpb $0x0f, %al /* 0x0f = Win9x extended */
223 andb $~0x80, %al /* 0x85 = Linux extended */
224 cmpb $0x05, %al /* 0x05 = MS-DOS extended */
227 /* It is an extended partition. Read the extended partition and
228 try to scan it. If the scan returns, re-load the current
229 partition table and resume scan. */
231 movl 8(%bx), %eax /* Partition table offset */
232 movl 20(%bp), %edx /* "Root" */
233 addl %edx, %eax /* Compute location of new ptab */
234 andl %edx, %edx /* Is this the MBR? */
236 movl %eax, %edx /* Offset -> root if this was MBR */
238 call read_partition_table
240 call scan_partition_table
242 /* This returned, so we need to reload the current partition table */
243 movl 28(%bp), %eax /* "Base" */
244 call read_partition_table
248 /* Not an extended partition */
252 /* Nothing found, return */
258 .ascii "Multiple active partitions.\r\n"
261 * boot: invoke the actual bootstrap. (%si) points to the partition
262 * 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 */