Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / image / bootsector.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /**
23  * @file
24  *
25  * x86 bootsector image format
26  *
27  */
28
29 #include <errno.h>
30 #include <realmode.h>
31 #include <biosint.h>
32 #include <bootsector.h>
33 #include <ipxe/console.h>
34
35 /** Vector for storing original INT 18 handler
36  *
37  * We do not chain to this vector, so there is no need to place it in
38  * .text16.
39  */
40 static struct segoff int18_vector;
41
42 /** Vector for storing original INT 19 handler
43  *
44  * We do not chain to this vector, so there is no need to place it in
45  * .text16.
46  */
47 static struct segoff int19_vector;
48
49 /** Restart point for INT 18 or 19 */
50 extern void bootsector_exec_fail ( void );
51
52 /**
53  * Jump to preloaded bootsector
54  *
55  * @v segment           Real-mode segment
56  * @v offset            Real-mode offset
57  * @v drive             Drive number to pass to boot sector
58  * @ret rc              Return status code
59  */
60 int call_bootsector ( unsigned int segment, unsigned int offset,
61                       unsigned int drive ) {
62         int discard_b, discard_D, discard_d;
63
64         /* Reset console, since boot sector will probably use it */
65         console_reset();
66
67         DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
68
69         /* Hook INTs 18 and 19 to capture failure paths */
70         hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
71                               &int18_vector );
72         hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
73                               &int19_vector );
74
75         /* Boot the loaded sector
76          *
77          * We assume that the boot sector may completely destroy our
78          * real-mode stack, so we preserve everything we need in
79          * static storage.
80          */
81         __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
82                                            "popw %%cs:saved_retaddr\n\t"
83                                            /* Save stack pointer */
84                                            "movw %%ss, %%ax\n\t"
85                                            "movw %%ax, %%cs:saved_ss\n\t"
86                                            "movw %%sp, %%cs:saved_sp\n\t"
87                                            /* Save frame pointer (gcc bug) */
88                                            "movl %%ebp, %%cs:saved_ebp\n\t"
89                                            /* Prepare jump to boot sector */
90                                            "pushw %%bx\n\t"
91                                            "pushw %%di\n\t"
92                                            /* Clear all registers */
93                                            "xorl %%eax, %%eax\n\t"
94                                            "xorl %%ebx, %%ebx\n\t"
95                                            "xorl %%ecx, %%ecx\n\t"
96                                            /* %edx contains drive number */
97                                            "xorl %%esi, %%esi\n\t"
98                                            "xorl %%edi, %%edi\n\t"
99                                            "xorl %%ebp, %%ebp\n\t"
100                                            "movw %%ax, %%ds\n\t"
101                                            "movw %%ax, %%es\n\t"
102                                            "movw %%ax, %%fs\n\t"
103                                            "movw %%ax, %%gs\n\t"
104                                            /* Jump to boot sector */
105                                            "sti\n\t"
106                                            "lret\n\t"
107                                            /* Preserved variables */
108                                            "\nsaved_ebp: .long 0\n\t"
109                                            "\nsaved_ss: .word 0\n\t"
110                                            "\nsaved_sp: .word 0\n\t"
111                                            "\nsaved_retaddr: .word 0\n\t"
112                                            /* Boot failure return point */
113                                            "\nbootsector_exec_fail:\n\t"
114                                            /* Restore frame pointer (gcc bug) */
115                                            "movl %%cs:saved_ebp, %%ebp\n\t"
116                                            /* Restore stack pointer */
117                                            "movw %%cs:saved_ss, %%ax\n\t"
118                                            "movw %%ax, %%ss\n\t"
119                                            "movw %%cs:saved_sp, %%sp\n\t"
120                                            /* Return via saved address */
121                                            "jmp *%%cs:saved_retaddr\n\t" )
122                                : "=b" ( discard_b ), "=D" ( discard_D ),
123                                  "=d" ( discard_d )
124                                : "b" ( segment ), "D" ( offset ),
125                                  "d" ( drive )
126                                : "eax", "ecx", "esi" );
127
128         DBG ( "Booted disk returned via INT 18 or 19\n" );
129
130         /* Unhook INTs 18 and 19 */
131         unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
132                                 &int18_vector );
133         unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
134                                 &int19_vector );
135         
136         return -ECANCELED;
137 }