We probably shouldn't leave interrupts disabled when starting an OS.
[people/adir/gpxe.git] / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /**
20  * @file
21  *
22  * x86 bootsector image format
23  *
24  */
25
26 #include <errno.h>
27 #include <realmode.h>
28 #include <biosint.h>
29 #include <bootsector.h>
30
31 /** Vector for storing original INT 18 handler
32  *
33  * We do not chain to this vector, so there is no need to place it in
34  * .text16.
35  */
36 static struct segoff int18_vector;
37
38 /** Vector for storing original INT 19 handler
39  *
40  * We do not chain to this vector, so there is no need to place it in
41  * .text16.
42  */
43 static struct segoff int19_vector;
44
45 /** Restart point for INT 18 or 19 */
46 extern void bootsector_exec_fail ( void );
47
48 /**
49  * Jump to preloaded bootsector
50  *
51  * @v segment           Real-mode segment
52  * @v offset            Real-mode offset
53  * @v drive             Drive number to pass to boot sector
54  * @ret rc              Return status code
55  */
56 int call_bootsector ( unsigned int segment, unsigned int offset,
57                       unsigned int drive ) {
58         int discard_b, discard_D, discard_d;
59
60         DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
61
62         /* Hook INTs 18 and 19 to capture failure paths */
63         hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
64                               &int18_vector );
65         hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
66                               &int19_vector );
67
68         /* Boot the loaded sector
69          *
70          * We assume that the boot sector may completely destroy our
71          * real-mode stack, so we preserve everything we need in
72          * static storage.
73          */
74         __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
75                                            "popw %%cs:saved_retaddr\n\t"
76                                            /* Save stack pointer */
77                                            "movw %%ss, %%ax\n\t"
78                                            "movw %%ax, %%cs:saved_ss\n\t"
79                                            "movw %%sp, %%cs:saved_sp\n\t"
80                                            /* Jump to boot sector */
81                                            "pushw %%bx\n\t"
82                                            "pushw %%di\n\t"
83                                            "sti\n\t"
84                                            "lret\n\t"
85                                            /* Preserved variables */
86                                            "\nsaved_ss: .word 0\n\t"
87                                            "\nsaved_sp: .word 0\n\t"
88                                            "\nsaved_retaddr: .word 0\n\t"
89                                            /* Boot failure return point */
90                                            "\nbootsector_exec_fail:\n\t"
91                                            /* Restore stack pointer */
92                                            "movw %%cs:saved_ss, %%ax\n\t"
93                                            "movw %%ax, %%ss\n\t"
94                                            "movw %%cs:saved_sp, %%sp\n\t"
95                                            /* Return via saved address */
96                                            "jmp *%%cs:saved_retaddr\n\t" )
97                                : "=b" ( discard_b ), "=D" ( discard_D ),
98                                  "=d" ( discard_d )
99                                : "b" ( segment ), "D" ( offset ),
100                                  "d" ( drive )
101                                : "eax", "ecx", "esi", "ebp" );
102
103         DBG ( "Booted disk returned via INT 18 or 19\n" );
104
105         /* Unhook INTs 18 and 19 */
106         unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
107                                 &int18_vector );
108         unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
109                                 &int19_vector );
110         
111         return -ECANCELED;
112 }