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 * ----------------------------------------------------------------------- */
29 #include <machine/asm.h>
30 #include <sys/bootblock.h>
34 testb $0x04, BIOS_kbdflags
39 #elif defined(FORCE_80)
54 /* Partition table header here */
55 phdr = stack /* Above the stack, overwritten by bootsect */
56 /* Partition table sector here */
57 /* To handle > 32K we need to play segment tricks... */
60 /* Where we put DS:SI */
61 dssi_out = start + 0x1be
66 /* gas/ld has issues with doing this as absolute addresses... */
67 .section ".bootsec", "a", @nobits
81 pushw %es /* 4(%bp) es:di -> $PnP header */
82 pushw %di /* 2(%bp) */
87 /* Copy down to 0:0x600 */
96 pushw %dx /* 0(%bp) = %dl -> drive number */
98 /* Check to see if we have EBIOS */
99 pushw %dx /* drive number */
100 movb $0x41, %ah /* %al == 0 already */
109 shrw %cx /* Bit 0 = fixed disk subset */
112 /* We have EBIOS; patch in the following code at
113 read_sector_cbios: movb $0x42, %ah ; jmp read_common */
114 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \
120 /* Get (C)HS geometry */
123 andw $0x3f, %cx /* Sector count */
124 movw %sp, %bp /* %bp -> frame pointer: LEAVE UNCHANGED */
125 pushw %cx /* -2(%bp) Save sectors on the stack */
126 movzbw %dh, %ax /* dh = max head */
127 incw %ax /* From 0-based max to count */
128 mulw %cx /* Heads*sectors -> sectors per cylinder */
130 /* Save sectors/cylinder on the stack */
131 pushw %dx /* -4(%bp) High word */
132 pushw %ax /* -6(%bp) Low word */
134 /* Load partition table header */
137 incw %ax /* %edx:%eax = 1 */
139 pushw %bx /* -8(%bp) phdr == bootsect */
142 /* Number of partition sectors */
143 /* We assume the partition table is 32K or less, and that
144 the sector size is 512. */
145 /* Note: phdr == 6(%bp) */
146 movw (80+6)(%bp),%cx /* NumberOfPartitionEntries */
147 movw (84+6)(%bp),%ax /* SizeOfPartitionEntry */
155 /* Starting LBA of partition array */
156 movl (72+6)(%bp),%eax
157 movl (76+6)(%bp),%edx
165 /* Find the boot partition */
166 xorw %si,%si /* Nothing found yet */
167 popw %di /* Partition table in memory */
168 popw %cx /* NumberOfPartitionEntries */
169 popw %ax /* SizeOfPartitionEntry */
172 /* If the PartitionTypeGUID is all zero, it's an empty slot */
192 .ascii "Missing OS\r\n"
196 .ascii "Multiple active partitions\r\n"
199 xchgw %ax,%cx /* Set up %cx for rep movsb further down */
204 /* 80 00 00 00 ee 00 00 00
205 - bootable partition, type EFI (EE), no CHS information */
213 call saturate_stosl /* Partition start */
220 call saturate_stosl /* Partition length */
222 movzwl %cx,%eax /* Length of GPT entry */
225 rep; movsb /* GPT entry follows MBR entry */
229 * boot: invoke the actual bootstrap. %ds:%si points to the
230 * partition information in memory. The top word on the stack
231 * is phdr == 0x7c00 == the address of the boot sector.
234 movl (32+20)(%si),%eax
235 movl (36+20)(%si),%edx
238 cmpw $0xaa55, -2(%bx)
239 jne missing_os /* Not a valid boot sector */
240 movw %bp, %sp /* driveno == bootsec-6 */
241 popw %dx /* dl -> drive number */
242 popw %di /* es:di -> $PnP vector */
244 movl $0x54504721,%eax /* !GPT magic number */
246 jmpw *%sp /* %sp == bootsec */
249 * Store the value in %eax to %di iff %edx == 0, otherwise store -1.
250 * Returns the value that was actually written in %eax.
260 * Increment %edx:%eax
268 * read_sector: read a single sector pointed to by %edx:%eax to
269 * %es:%bx. CF is set on error. All registers saved.
273 pushl %edx /* MSW of LBA */
274 pushl %eax /* LSW of LBA */
275 pushw %es /* Buffer segment */
276 pushw %bx /* Buffer offset */
277 pushw $1 /* Sector count */
278 pushw $16 /* Size of packet */
281 /* This chunk is skipped if we have ebios */
282 /* Do not clobber %es:%bx or %edx:%eax before this chunk! */
284 divl -6(%bp) /* secpercyl */
289 divb -2(%bp) /* sectors */
292 incw %cx /* Sectors are 1-based */
296 movb (%bp), %dl /* driveno */
298 leaw 16(%si), %sp /* Drop DAPA */
301 addb $2, %bh /* bx += 512: point to the next buffer */
306 .ascii "Disk error on boot\r\n"
309 * Print error messages. This is invoked with "call", with the
310 * error message at the return address.
317 movb (BIOS_page), %bh
319 int $0x10 /* May destroy %bp */
320 cmpb $10, %al /* Newline? */
323 int $0x18 /* Boot failure */