1 /* -----------------------------------------------------------------------
3 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 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 * ----------------------------------------------------------------------- */
37 /* Partition table header here */
38 phdr = stack /* Above the stack, overwritten by bootsect */
39 /* Partition table sector here */
40 /* To handle > 32K we need to play segment tricks... */
43 /* Where we put DS:SI */
44 dssi_out = _start + 0x1be
49 /* gas/ld has issues with doing this as absolute addresses... */
50 .section ".bootsec", "a", @nobits
64 pushw %es /* 4(%bp) es:di -> $PnP header */
65 pushw %di /* 2(%bp) */
70 /* Copy down to 0:0x600 */
79 pushw %dx /* 0(%bp) = %dl -> drive number */
81 /* Check to see if we have EBIOS */
82 pushw %dx /* drive number */
83 movb $0x41, %ah /* %al == 0 already */
92 shrw %cx /* Bit 0 = fixed disk subset */
95 /* We have EBIOS; patch in the following code at
96 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
97 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
103 /* Get (C)HS geometry */
106 andw $0x3f, %cx /* Sector count */
107 movw %sp, %bp /* %bp -> frame pointer: LEAVE UNCHANGED */
108 pushw %cx /* -2(%bp) Save sectors on the stack */
109 movzbw %dh, %ax /* dh = max head */
110 incw %ax /* From 0-based max to count */
111 mulw %cx /* Heads*sectors -> sectors per cylinder */
113 /* Save sectors/cylinder on the stack */
114 pushw %dx /* -4(%bp) High word */
115 pushw %ax /* -6(%bp) Low word */
117 /* Load partition table header */
120 incw %ax /* %edx:%eax = 1 */
122 pushw %bx /* -8(%bp) phdr == bootsect */
125 /* Number of partition sectors */
126 /* We assume the partition table is 32K or less, and that
127 the sector size is 512. */
128 /* Note: phdr == 6(%bp) */
129 movw (80+6)(%bp),%cx /* NumberOfPartitionEntries */
130 movw (84+6)(%bp),%ax /* SizeOfPartitionEntry */
138 /* Starting LBA of partition array */
139 movl (72+6)(%bp),%eax
140 movl (76+6)(%bp),%edx
148 /* Find the boot partition */
149 xorw %si,%si /* Nothing found yet */
150 popw %di /* Partition table in memory */
151 popw %cx /* NumberOfPartitionEntries */
152 popw %ax /* SizeOfPartitionEntry */
169 .ascii "Missing OS\r\n"
173 .ascii "Multiple active partitions\r\n"
176 xchgw %ax,%cx /* Set up %cx for rep movsb further down */
181 /* 80 00 00 00 ee 00 00 00
182 - bootable partition, type EFI (EE), no CHS information */
190 call saturate_stosl /* Partition start */
197 call saturate_stosl /* Partition length */
199 movzwl %cx,%eax /* Length of GPT entry */
202 rep; movsb /* GPT entry follows MBR entry */
206 * boot: invoke the actual bootstrap. %ds:%si points to the
207 * partition information in memory. The top word on the stack
208 * is phdr == 0x7c00 == the address of the boot sector.
211 movl (32+20)(%si),%eax
212 movl (36+20)(%si),%edx
215 cmpw $0xaa55, -2(%bx)
216 jne missing_os /* Not a valid boot sector */
217 movw %bp, %sp /* driveno == bootsec-6 */
218 popw %dx /* dl -> drive number */
219 popw %di /* es:di -> $PnP vector */
221 movl $0x54504721,%eax /* !GPT magic number */
223 jmpw *%sp /* %sp == bootsec */
226 * Store the value in %eax to %di iff %edx == 0, otherwise store -1.
227 * Returns the value that was actually written in %eax.
237 * Increment %edx:%eax
245 * read_sector: read a single sector pointed to by %edx:%eax to
246 * %es:%bx. CF is set on error. All registers saved.
250 pushl %edx /* MSW of LBA */
251 pushl %eax /* LSW of LBA */
252 pushw %es /* Buffer segment */
253 pushw %bx /* Buffer offset */
254 pushw $1 /* Sector count */
255 pushw $16 /* Size of packet */
258 /* This chunk is skipped if we have ebios */
259 /* Do not clobber %es:%bx or %edx:%eax before this chunk! */
261 divl -6(%bp) /* secpercyl */
266 divb -2(%bp) /* sectors */
269 incw %cx /* Sectors are 1-based */
273 movb (%bp), %dl /* driveno */
275 leaw 16(%si), %sp /* Drop DAPA */
278 addb $2, %bh /* bx += 512: point to the next buffer */
283 .ascii "Disk error on boot\r\n"
286 * Print error messages. This is invoked with "call", with the
287 * error message at the return address.
294 movb (BIOS_page), %bh
296 int $0x10 /* May destroy %bp */
297 cmpb $10, %al /* Newline? */
300 int $0x18 /* Boot failure */