2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 * x86 bootsector image format
36 #include <bootsector.h>
37 #include <ipxe/console.h>
39 /** Vector for storing original INT 18 handler
41 * We do not chain to this vector, so there is no need to place it in
44 static struct segoff int18_vector;
46 /** Vector for storing original INT 19 handler
48 * We do not chain to this vector, so there is no need to place it in
51 static struct segoff int19_vector;
53 /** Restart point for INT 18 or 19 */
54 extern void bootsector_exec_fail ( void );
57 * Jump to preloaded bootsector
59 * @v segment Real-mode segment
60 * @v offset Real-mode offset
61 * @v drive Drive number to pass to boot sector
62 * @ret rc Return status code
64 int call_bootsector ( unsigned int segment, unsigned int offset,
65 unsigned int drive ) {
66 int discard_b, discard_D, discard_d;
68 /* Reset console, since boot sector will probably use it */
71 DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
73 /* Hook INTs 18 and 19 to capture failure paths */
74 hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
76 hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
79 /* Boot the loaded sector
81 * We assume that the boot sector may completely destroy our
82 * real-mode stack, so we preserve everything we need in
85 __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
86 "popw %%cs:saved_retaddr\n\t"
87 /* Save stack pointer */
89 "movw %%ax, %%cs:saved_ss\n\t"
90 "movw %%sp, %%cs:saved_sp\n\t"
91 /* Save frame pointer (gcc bug) */
92 "movl %%ebp, %%cs:saved_ebp\n\t"
93 /* Prepare jump to boot sector */
96 /* Clear all registers */
97 "xorl %%eax, %%eax\n\t"
98 "xorl %%ebx, %%ebx\n\t"
99 "xorl %%ecx, %%ecx\n\t"
100 /* %edx contains drive number */
101 "xorl %%esi, %%esi\n\t"
102 "xorl %%edi, %%edi\n\t"
103 "xorl %%ebp, %%ebp\n\t"
104 "movw %%ax, %%ds\n\t"
105 "movw %%ax, %%es\n\t"
106 "movw %%ax, %%fs\n\t"
107 "movw %%ax, %%gs\n\t"
108 /* Jump to boot sector */
111 /* Preserved variables */
112 "\nsaved_ebp: .long 0\n\t"
113 "\nsaved_ss: .word 0\n\t"
114 "\nsaved_sp: .word 0\n\t"
115 "\nsaved_retaddr: .word 0\n\t"
116 /* Boot failure return point */
117 "\nbootsector_exec_fail:\n\t"
118 /* Restore frame pointer (gcc bug) */
119 "movl %%cs:saved_ebp, %%ebp\n\t"
120 /* Restore stack pointer */
121 "movw %%cs:saved_ss, %%ax\n\t"
122 "movw %%ax, %%ss\n\t"
123 "movw %%cs:saved_sp, %%sp\n\t"
124 /* Return via saved address */
125 "jmp *%%cs:saved_retaddr\n\t" )
126 : "=b" ( discard_b ), "=D" ( discard_D ),
128 : "b" ( segment ), "D" ( offset ),
130 : "eax", "ecx", "esi" );
132 DBG ( "Booted disk returned via INT 18 or 19\n" );
134 /* Unhook INTs 18 and 19 */
135 unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
137 unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,