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 */
80 shrw %cx /* Bit 0 = fixed disk subset */
83 /* We have EBIOS; patch in the following code at
84 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
85 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
91 /* Get (C)HS geometry */
94 andw $0x3f, %cx /* Sector count */
95 pushw %cx /* Save sectors on the stack */
96 movzbw %dh, %ax /* dh = max head */
97 incw %ax /* From 0-based max to count */
98 mulw %cx /* Heads*sectors -> sectors per cylinder */
100 /* Save sectors/cylinder on the stack */
101 pushw %dx /* High word */
102 pushw %ax /* Low word */
104 xorl %eax, %eax /* Base */
105 cdq /* Root (%edx <- 0) */
106 call scan_partition_table
108 /* If we get here, we have no OS */
111 .ascii "Missing operating system.\r\n"
115 * read_sector: read a single sector pointed to by %eax to 0x7c00.
116 * CF is set on error. All registers saved.
122 pushl %edx /* MSW of LBA */
123 pushl %eax /* LSW of LBA */
124 pushw %es /* Buffer segment */
125 pushw %bx /* Buffer offset */
126 pushw $1 /* Sector count */
127 pushw $16 /* Size of packet */
130 /* This chunk is skipped if we have ebios */
131 /* Do not clobber %eax before this chunk! */
132 /* This also relies on %bx and %edx as set up above. */
142 incw %cx /* Sectors are 1-based */
148 addw $16, %sp /* Drop DAPA */
153 * read_partition_table:
154 * Read a partition table (pointed to by %eax), and copy
155 * the partition table into the ptab buffer.
157 * Clobbers %si, %di, and %cx, other registers preserved.
160 * On error, CF is set and ptab is overwritten with junk.
164 read_partition_table:
166 movw $bootsec+446, %si
173 * scan_partition_table:
174 * Scan a partition table currently loaded in the partition table
175 * area. Preserve all registers.
178 * %eax - base (location of this partition table)
179 * %edx - root (offset from MBR, or 0 for MBR)
181 * These get pushed into stack slots:
182 * 28(%bp) - %eax - base
183 * 20(%bp) - %edx - root
186 scan_partition_table:
190 /* Search for active partitions */
205 decw %ax /* Number of active partitions found */
209 /* No active partitions found, look for extended partitions */
210 popw %bx /* %bx <- ptab */
211 popw %cx /* %cx <- 4 */
214 cmpb $0x0f, %al /* 0x0f = Win9x extended */
216 andb $~0x80, %al /* 0x85 = Linux extended */
217 cmpb $0x05, %al /* 0x05 = MS-DOS extended */
220 /* It is an extended partition. Read the extended partition and
221 try to scan it. If the scan returns, re-load the current
222 partition table and resume scan. */
224 movl 8(%bx), %eax /* Partition table offset */
225 movl 20(%bp), %edx /* "Root" */
226 addl %edx, %eax /* Compute location of new ptab */
227 andl %edx, %edx /* Is this the MBR? */
229 movl %eax, %edx /* Offset -> root if this was MBR */
231 call read_partition_table
233 call scan_partition_table
235 /* This returned, so we need to reload the current partition table */
236 movl 28(%bp), %eax /* "Base" */
237 call read_partition_table
241 /* Not an extended partition */
245 /* Nothing found, return */
251 .ascii "Multiple active partitions.\r\n"
255 * boot: invoke the actual bootstrap. (%si) points to the partition
256 * table entry, and 28(%bp) has the partition table base.
261 movl %eax, 8(%si) /* Adjust in-memory partition table entry */
264 cmpw $0xaa55, (bootsec+510)
265 jne missing_os /* Not a valid boot sector */
266 movw $driveno, %sp /* driveno == bootsec-6 */
267 popw %dx /* dl -> drive number */
268 popw %di /* es:di -> $PnP vector */
271 jmpw *%sp /* %sp == bootsec */
275 .ascii "Operating system load error.\r\n"
279 * Print error messages. This is invoked with "call", with the
280 * error message at the return address.
289 movb (BIOS_page), %bh
294 int $0x18 /* Boot failure */