MBR: always do setup for EBIOS, makes stack handling easier and saves jmp
[profile/ivi/syslinux.git] / mbr / mbr.S
1 /* -----------------------------------------------------------------------
2  *
3  *   Copyright 2007 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  * ----------------------------------------------------------------------- */
27
28         .code16
29         .text
30
31         .globl  bootsec
32 stack           = 0x7c00
33 driveno         = (stack-6)
34 sectors         = (stack-8)
35 secpercyl       = (stack-12)
36
37 BIOS_page = 0x462
38
39         /* gas/ld has issues with doing this as absolute addresses... */
40         .section ".bootsec", "a", @nobits
41         .globl  bootsec
42 bootsec:
43         .space  512
44
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
60
61         /* Copy down to 0:0x600 */
62         movw    $_start, %di
63         movw    $(512/2), %cx
64         rep; movsw
65
66         ljmpw   $0, $next
67
68 next:
69         /* Check to see if we have EBIOS */
70         pushw   %dx             /* drive number */
71         movw    $0x4100, %ax
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
82
83         /* We have EBIOS; patch in jump to skip over read_sector_cbios */
84         movw    $0xeb+((read_common-read_sector_cbios-2)<< 8), (read_sector_cbios)
85
86 1:
87         popw    %dx
88
89         /* Get (C)HS geometry */
90         movb    $0x08, %ah
91         int     $0x13
92         andw    $0x3f, %cx      /* Sector count */
93         pushw   %cx             /* Save sectors on the stack */
94         xorw    %ax, %ax
95         pushw   %ax             /* High word of sectors/cylinder */
96         movb    %dh, %al        /* dh = number of heads */
97         incw    %ax             /* From 0-based to count */
98         mulw    %cx             /* Heads*sectors -> sectors per cylinder */
99         pushw   %ax             /* Save sectors/cylinder on the stack */
100
101         xorl    %eax, %eax      /* Base */
102         cdq                     /* Root (%edx <- 0) */
103         call    scan_partition_table
104
105         /* If we get here, we have no OS */
106         jmp     missing_os
107
108 /*
109  * read_sector: read a single sector pointed to by %eax to 0x7c00.
110  * CF is set on error.  All registers saved.
111  */
112 read_sector:
113         pushal
114         xorl    %edx, %edx
115         movw    $bootsec, %bx
116         pushl   %edx    /* MSW of LBA */
117         pushl   %eax    /* LSW of LBA */
118         pushw   %es     /* Buffer segment */
119         pushw   %bx     /* Buffer offset */
120         pushw   $1      /* Sector count */
121         pushw   $16     /* Size of packet */
122         movw    %sp, %si
123         movb    $0x42, %ah
124
125         /* This chunk is skipped if we have ebios */
126 read_sector_cbios:
127         divl    (secpercyl)
128         shlb    $6, %ah
129         movb    %ah, %cl
130         movb    %al, %ch
131         xchgw   %dx, %ax
132         divb    (sectors)
133         movb    %al, %dh
134         orb     %ah, %cl
135         incw    %cx     /* Sectors are 1-based */
136         movw    $0x0201, %ax
137
138 read_common:
139         movb    (driveno), %dl
140         int     $0x13
141         addw    $16, %sp        /* Drop DAPA */
142         popal
143         ret
144
145 /*
146  * read_partition_table:
147  *      Read a partition table (pointed to by %eax), and copy
148  *      the partition table into the ptab buffer.
149  *      Clobbers %si, %di, and %cx, other registers preserved.
150  */
151 ptab    = _start+446
152
153 read_partition_table:
154         call    read_sector
155         jc      20f
156         movw    $bootsec+446, %si
157         movw    $ptab, %di
158         movw    $(16*4/2), %cx
159         rep ; movsw
160 20:
161         ret
162
163 /*
164  * scan_partition_table:
165  *      Scan a partition table currently loaded in the partition table
166  *      area.  Preserve all registers.
167  *
168  *      On entry:
169  *        %eax - base (location of this partition table)
170  *        %edx - root (offset from MBR, or 0 for MBR)
171  *
172  *      These get pushed into stack slots:
173  *        28(%bp) - %eax - base
174  *        20(%bp) - %edx - root
175  */
176
177 scan_partition_table:
178         pushal
179         movw    %sp, %bp
180
181         /* Search for active partitions */
182         movw    $ptab, %bx
183         movw    $4, %cx
184         xorw    %ax, %ax
185         push    %bx
186         push    %cx
187 5:
188         testb   $0x80, (%bx)
189         jz      6f
190         incw    %ax
191         movw    %bx, %si
192 6:
193         addw    $16, %bx
194         loopw   5b
195
196         decw    %ax             /* Number of active partitions found */
197         jz      boot
198         jns     too_many_active
199
200         /* No active partitions found, look for extended partitions */
201         popw    %bx             /* %bx <- ptab */
202         popw    %cx             /* %cx <- 4    */
203 7:
204         movb    4(%bx), %al
205         cmpb    $0x0f, %al      /* 0x0f = Win9x extended */
206         je      8f
207         andb    $~0x80, %al     /* 0x85 = Linux extended */
208         cmpb    $0x05, %al      /* 0x05 = MS-DOS extended */
209         jne     9f
210
211         /* It is an extended partition.  Read the extended partition and
212            try to scan it.  If the scan returns, re-load the current
213            partition table and resume scan. */
214 8:
215         movl    8(%bx), %eax            /* Partition table offset */
216         movl    20(%bp), %edx           /* "Root" */
217         addl    %edx, %eax              /* Compute location of new ptab */
218         andl    %edx, %edx              /* Is this the MBR? */
219         jnz     10f
220         movl    %eax, %edx              /* Offset -> root if this was MBR */
221 10:
222         call    read_partition_table
223         jc      11f
224         call    scan_partition_table
225 11:
226         /* This returned, so we need to reload the current partition table */
227         movl    28(%bp), %eax           /* "Base" */
228         call    read_partition_table
229
230         /* fall through */
231 9:
232         /* Not an extended partition */
233         addw    $16, %bx
234         loopw   7b
235
236         /* Nothing found, return */
237         popal
238         ret
239
240 /*
241  * boot: invoke the actual bootstrap. (%si) points to the partition
242  *       table entry, and 28(%bp) has the partition table base.
243  */
244 boot:
245         movl    8(%si), %eax
246         addl    28(%bp), %eax
247         movl    %eax, 8(%si)    /* Adjust in-memory partition table entry */
248         call    read_sector
249         jc      disk_error
250         cmpw    $0xaa55, (bootsec+510)
251         jne     missing_os              /* Not a valid boot sector */
252         movw    $driveno, %sp
253         popw    %dx             /* dl -> drive number */
254         popw    %di             /* es:di -> $PnP vector */
255         popw    %es
256         cli
257         jmp     bootsec
258
259 /*
260  * error messages
261  */
262 missing_os:
263         movw    $missing_os_msg, %si
264         jmp     error
265 disk_error:
266         movw    $disk_error_msg, %si
267         jmp     error
268 too_many_active:
269         movw    $too_many_active_msg, %si
270         /* jmp error */
271
272 error:
273 2:
274         lodsb
275         andb    %al, %al
276         jz      3f
277         movb    $0x0e, %ah
278         movb    (BIOS_page), %bh
279         movb    $0x07, %bl
280         int     $0x10
281         jmp     2b
282 3:
283         int     $0x18           /* Boot failure */
284         jmp     .               /* Die */
285
286 missing_os_msg:
287         .ascii  "Missing operating system."
288         .byte   0
289 disk_error_msg:
290         .ascii  "Operating system load error."
291         .byte   0
292 too_many_active_msg:
293         .ascii  "Multiple active partitions."
294         .byte   0