menu: quiet warnings in drain_keyboard()
[syslinux.git] / mbr / mbr.S
blob722d90dbf8f05b2abcea503f85e63359e53c6af0
1 /* -----------------------------------------------------------------------
2  *
3  *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4  *
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
12  *   conditions:
13  *
14  *   The above copyright notice and this permission notice shall
15  *   be included in all copies or substantial portions of the Software.
16  *
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.
25  *
26  * ----------------------------------------------------------------------- */
28         .code16
29         .text
31         .globl  bootsec
32 stack           = 0x7c00
33 driveno         = (stack-6)
34 sectors         = (stack-8)
35 secpercyl       = (stack-12)
37 BIOS_page = 0x462
39         /* gas/ld has issues with doing this as absolute addresses... */
40         .section ".bootsec", "a", @nobits
41         .globl  bootsec
42 bootsec:
43         .space  512
45         .text
46         .globl  _start
47 _start:
48         cli
49         xorw    %ax, %ax
50         movw    %ax, %ds
51         movw    %ax, %ss
52         movw    $stack, %sp
53         movw    %sp, %si
54         pushw   %es             /* es:di -> $PnP header */
55         pushw   %di
56         pushw   %dx             /* dl -> drive number */
57         movw    %ax, %es
58         sti
59         cld
61         /* Copy down to 0:0x600 */
62         movw    $_start, %di
63         movw    $(512/2), %cx
64         rep; movsw
66         ljmpw   $0, $next
68 next:
69         /* Check to see if we have EBIOS */
70         pushw   %dx             /* drive number */
71         movb    $0x41, %ah      /* %al == 0 already */
72         movw    $0x55aa, %bx
73         xorw    %cx, %cx
74         xorb    %dh, %dh
75         stc
76         int     $0x13
77         jc      1f
78         cmpw    $0xaa55, %bx
79         jne     1f
80         shrw    %cx             /* Bit 0 = fixed disk subset */
81         jnc     1f
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), \
86                 (read_sector_cbios)
89         popw    %dx
91         /* Get (C)HS geometry */
92         movb    $0x08, %ah
93         int     $0x13
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 */
109 missing_os:
110         call    error
111         .ascii  "Missing operating system.\r\n"
114  * read_sector: read a single sector pointed to by %eax to 0x7c00.
115  * CF is set on error.  All registers saved.
116  */
117 read_sector:
118         pushal
119         xorl    %edx, %edx
120         movw    $bootsec, %bx
121         pushl   %edx    /* MSW of LBA */
122         pushl   %eax    /* LSW of LBA */
123         pushw   %es     /* Buffer segment */
124         pushw   %bx     /* Buffer offset */
125         pushw   $1      /* Sector count */
126         pushw   $16     /* Size of packet */
127         movw    %sp, %si
129         /* This chunk is skipped if we have ebios */
130         /* Do not clobber %eax before this chunk! */
131         /* This also relies on %bx and %edx as set up above. */
132 read_sector_cbios:
133         divl    (secpercyl)
134         shlb    $6, %ah
135         movb    %ah, %cl
136         movb    %al, %ch
137         xchgw   %dx, %ax
138         divb    (sectors)
139         movb    %al, %dh
140         orb     %ah, %cl
141         incw    %cx     /* Sectors are 1-based */
142         movw    $0x0201, %ax
144 read_common:
145         movb    (driveno), %dl
146         int     $0x13
147         leaw    16(%si), %sp    /* Drop DAPA */
148         popal
149         ret
152  * read_partition_table:
153  *      Read a partition table (pointed to by %eax), and copy
154  *      the partition table into the ptab buffer.
156  *      Clobbers %si, %di, and %cx, other registers preserved.
157  *      %cx = 0 on exit.
159  *      On error, CF is set and ptab is overwritten with junk.
160  */
161 ptab    = _start+446
163 read_partition_table:
164         call    read_sector
165         movw    $bootsec+446, %si
166         movw    $ptab, %di
167         movw    $(16*4/2), %cx
168         rep ; movsw
169         ret
172  * scan_partition_table:
173  *      Scan a partition table currently loaded in the partition table
174  *      area.  Preserve all registers.
176  *      On entry:
177  *        %eax - base (location of this partition table)
178  *        %edx - root (offset from MBR, or 0 for MBR)
180  *      These get pushed into stack slots:
181  *        28(%bp) - %eax - base
182  *        20(%bp) - %edx - root
183  */
185 scan_partition_table:
186         pushal
187         movw    %sp, %bp
189         /* Search for active partitions */
190         movw    $ptab, %bx
191         movw    $4, %cx
192         xorw    %ax, %ax
193         push    %bx
194         push    %cx
196         testb   $0x80, (%bx)
197         jz      6f
198         incw    %ax
199         movw    %bx, %si
201         addw    $16, %bx
202         loopw   5b
204         decw    %ax             /* Number of active partitions found */
205         jz      boot
206         jns     too_many_active
208         /* No active partitions found, look for extended partitions */
209         popw    %cx             /* %cx <- 4    */
210         popw    %bx             /* %bx <- ptab */
212         movb    4(%bx), %al
213         cmpb    $0x0f, %al      /* 0x0f = Win9x extended */
214         je      8f
215         andb    $~0x80, %al     /* 0x85 = Linux extended */
216         cmpb    $0x05, %al      /* 0x05 = MS-DOS extended */
217         jne     9f
219         /* It is an extended partition.  Read the extended partition and
220            try to scan it.  If the scan returns, re-load the current
221            partition table and resume scan. */
223         movl    8(%bx), %eax            /* Partition table offset */
224         movl    20(%bp), %edx           /* "Root" */
225         addl    %edx, %eax              /* Compute location of new ptab */
226         andl    %edx, %edx              /* Is this the MBR? */
227         jnz     10f
228         movl    %eax, %edx              /* Offset -> root if this was MBR */
230         call    read_partition_table
231         jc      11f
232         call    scan_partition_table
234         /* This returned, so we need to reload the current partition table */
235         movl    28(%bp), %eax           /* "Base" */
236         call    read_partition_table
238         /* fall through */
240         /* Not an extended partition */
241         addw    $16, %bx
242         loopw   7b
244         /* Nothing found, return */
245         popal
246         ret
248 too_many_active:
249         call    error
250         .ascii  "Multiple active partitions.\r\n"
253  * boot: invoke the actual bootstrap. (%si) points to the partition
254  *       table entry, and 28(%bp) has the partition table base.
255  */
256 boot:
257         movl    8(%si), %eax
258         addl    28(%bp), %eax
259         movl    %eax, 8(%si)    /* Adjust in-memory partition table entry */
260         call    read_sector
261         jc      disk_error
262         cmpw    $0xaa55, (bootsec+510)
263         jne     missing_os              /* Not a valid boot sector */
264         movw    $driveno, %sp   /* driveno == bootsec-6 */
265         popw    %dx             /* dl -> drive number */
266         popw    %di             /* es:di -> $PnP vector */
267         popw    %es
268         cli
269         jmpw    *%sp            /* %sp == bootsec */
271 disk_error:
272         call    error
273         .ascii  "Operating system load error.\r\n"
276  * Print error messages.  This is invoked with "call", with the
277  * error message at the return address.
278  */
279 error:
280         popw    %si
282         lodsb
283         movb    $0x0e, %ah
284         movb    (BIOS_page), %bh
285         movb    $0x07, %bl
286         int     $0x10           /* May destroy %bp */
287         cmpb    $10, %al        /* Newline? */
288         jne     2b
290         int     $0x18           /* Boot failure */
291 die:
292         hlt
293         jmp     die