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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 * x86 bootsector image format
32 #include <bootsector.h>
33 #include <ipxe/console.h>
35 /** Vector for storing original INT 18 handler
37 * We do not chain to this vector, so there is no need to place it in
40 static struct segoff int18_vector;
42 /** Vector for storing original INT 19 handler
44 * We do not chain to this vector, so there is no need to place it in
47 static struct segoff int19_vector;
49 /** Restart point for INT 18 or 19 */
50 extern void bootsector_exec_fail ( void );
53 * Jump to preloaded bootsector
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
60 int call_bootsector ( unsigned int segment, unsigned int offset,
61 unsigned int drive ) {
62 int discard_b, discard_D, discard_d;
64 /* Reset console, since boot sector will probably use it */
67 DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
69 /* Hook INTs 18 and 19 to capture failure paths */
70 hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
72 hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
75 /* Boot the loaded sector
77 * We assume that the boot sector may completely destroy our
78 * real-mode stack, so we preserve everything we need in
81 __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
82 "popw %%cs:saved_retaddr\n\t"
83 /* Save stack pointer */
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 */
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 */
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 ),
124 : "b" ( segment ), "D" ( offset ),
126 : "eax", "ecx", "esi" );
128 DBG ( "Booted disk returned via INT 18 or 19\n" );
130 /* Unhook INTs 18 and 19 */
131 unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
133 unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,