MBR: shave another ~4 bytes through an ugly error message hack
[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 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)
87
88 1:
89         popw    %dx
90
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         xorw    %ax, %ax
97         pushw   %ax             /* High word of sectors/cylinder */
98         movb    %dh, %al        /* dh = number of heads */
99         incw    %ax             /* From 0-based to count */
100         mulw    %cx             /* Heads*sectors -> sectors per cylinder */
101         pushw   %ax             /* Save sectors/cylinder on the stack */
102
103         xorl    %eax, %eax      /* Base */
104         cdq                     /* Root (%edx <- 0) */
105         call    scan_partition_table
106
107         /* If we get here, we have no OS */
108 missing_os:
109         call    error
110         .ascii  "Missing operating system.\r\n"
111         .byte   0
112
113 /*
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
128
129         /* This chunk is skipped if we have ebios */
130         /* Do not clobber %eax before this chunk! */
131 read_sector_cbios:
132         divl    (secpercyl)
133         shlb    $6, %ah
134         movb    %ah, %cl
135         movb    %al, %ch
136         xchgw   %dx, %ax
137         divb    (sectors)
138         movb    %al, %dh
139         orb     %ah, %cl
140         incw    %cx     /* Sectors are 1-based */
141         movw    $0x0201, %ax
142
143 read_common:
144         movb    (driveno), %dl
145         int     $0x13
146         addw    $16, %sp        /* Drop DAPA */
147         popal
148         ret
149
150 /*
151  * read_partition_table:
152  *      Read a partition table (pointed to by %eax), and copy
153  *      the partition table into the ptab buffer.
154  *
155  *      Clobbers %si, %di, and %cx, other registers preserved.
156  *      %cx = 0 on exit.
157  *
158  *      On error, CF is set and ptab is overwritten with junk.
159  */
160 ptab    = _start+446
161
162 read_partition_table:
163         call    read_sector
164         movw    $bootsec+446, %si
165         movw    $ptab, %di
166         movw    $(16*4/2), %cx
167         rep ; movsw
168         ret
169
170 /*
171  * scan_partition_table:
172  *      Scan a partition table currently loaded in the partition table
173  *      area.  Preserve all registers.
174  *
175  *      On entry:
176  *        %eax - base (location of this partition table)
177  *        %edx - root (offset from MBR, or 0 for MBR)
178  *
179  *      These get pushed into stack slots:
180  *        28(%bp) - %eax - base
181  *        20(%bp) - %edx - root
182  */
183
184 scan_partition_table:
185         pushal
186         movw    %sp, %bp
187
188         /* Search for active partitions */
189         movw    $ptab, %bx
190         movw    $4, %cx
191         xorw    %ax, %ax
192         push    %bx
193         push    %cx
194 5:
195         testb   $0x80, (%bx)
196         jz      6f
197         incw    %ax
198         movw    %bx, %si
199 6:
200         addw    $16, %bx
201         loopw   5b
202
203         decw    %ax             /* Number of active partitions found */
204         jz      boot
205         jns     too_many_active
206
207         /* No active partitions found, look for extended partitions */
208         popw    %bx             /* %bx <- ptab */
209         popw    %cx             /* %cx <- 4    */
210 7:
211         movb    4(%bx), %al
212         cmpb    $0x0f, %al      /* 0x0f = Win9x extended */
213         je      8f
214         andb    $~0x80, %al     /* 0x85 = Linux extended */
215         cmpb    $0x05, %al      /* 0x05 = MS-DOS extended */
216         jne     9f
217
218         /* It is an extended partition.  Read the extended partition and
219            try to scan it.  If the scan returns, re-load the current
220            partition table and resume scan. */
221 8:
222         movl    8(%bx), %eax            /* Partition table offset */
223         movl    20(%bp), %edx           /* "Root" */
224         addl    %edx, %eax              /* Compute location of new ptab */
225         andl    %edx, %edx              /* Is this the MBR? */
226         jnz     10f
227         movl    %eax, %edx              /* Offset -> root if this was MBR */
228 10:
229         call    read_partition_table
230         jc      11f
231         call    scan_partition_table
232 11:
233         /* This returned, so we need to reload the current partition table */
234         movl    28(%bp), %eax           /* "Base" */
235         call    read_partition_table
236
237         /* fall through */
238 9:
239         /* Not an extended partition */
240         addw    $16, %bx
241         loopw   7b
242
243         /* Nothing found, return */
244         popal
245         ret
246
247 too_many_active:
248         call    error
249         .ascii  "Multiple active partitions.\r\n"
250         .byte   0
251
252 /*
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
265         popw    %dx             /* dl -> drive number */
266         popw    %di             /* es:di -> $PnP vector */
267         popw    %es
268         cli
269         jmp     bootsec
270
271 disk_error:
272         call    error
273         .ascii  "Operating system load error.\r\n"
274         .byte   0
275
276 /*
277  * Print error messages.  This is invoked with "call", with the
278  * error message at the return address.
279  */
280 error:
281         popw    %si
282 2:
283         lodsb
284         andb    %al, %al
285         jz      3f
286         movb    $0x0e, %ah
287         movb    (BIOS_page), %bh
288         movb    $0x07, %bl
289         int     $0x10
290         jmp     2b
291 3:
292         int     $0x18           /* Boot failure */
293 die:
294         hlt
295         jmp     die