iPXE
Functions | Variables
bootsector.c File Reference

x86 bootsector image format More...

#include <errno.h>
#include <realmode.h>
#include <biosint.h>
#include <bootsector.h>
#include <ipxe/console.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
void bootsector_exec_fail (void)
 Restart point for INT 18 or 19. More...
 
int call_bootsector (unsigned int segment, unsigned int offset, unsigned int drive)
 Jump to preloaded bootsector. More...
 

Variables

static struct segoff int18_vector
 Vector for storing original INT 18 handler. More...
 
static struct segoff int19_vector
 Vector for storing original INT 19 handler. More...
 

Detailed Description

x86 bootsector image format

Definition in file bootsector.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ bootsector_exec_fail()

void bootsector_exec_fail ( void  )

Restart point for INT 18 or 19.

Referenced by call_bootsector().

◆ call_bootsector()

int call_bootsector ( unsigned int  segment,
unsigned int  offset,
unsigned int  drive 
)

Jump to preloaded bootsector.

Parameters
segmentReal-mode segment
offsetReal-mode offset
driveDrive number to pass to boot sector
Return values
rcReturn status code

Definition at line 64 of file bootsector.c.

65  {
66  int discard_b, discard_D, discard_d;
67 
68  /* Reset console, since boot sector will probably use it */
69  console_reset();
70 
71  DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
72 
73  /* Hook INTs 18 and 19 to capture failure paths */
75  &int18_vector );
77  &int19_vector );
78 
79  /* Boot the loaded sector
80  *
81  * We assume that the boot sector may completely destroy our
82  * real-mode stack, so we preserve everything we need in
83  * static storage.
84  */
85  __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
86  "popw %%cs:saved_retaddr\n\t"
87  /* Save stack pointer */
88  "movw %%ss, %%ax\n\t"
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 */
94  "pushw %%bx\n\t"
95  "pushw %%di\n\t"
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 */
109  "sti\n\t"
110  "lret\n\t"
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 ),
127  "=d" ( discard_d )
128  : "b" ( segment ), "D" ( offset ),
129  "d" ( drive )
130  : "eax", "ecx", "esi" );
131 
132  DBG ( "Booted disk returned via INT 18 or 19\n" );
133 
134  /* Unhook INTs 18 and 19 */
136  &int18_vector );
138  &int19_vector );
139 
140  return -ECANCELED;
141 }
uint16_t segment
Code segment.
Definition: librm.h:252
void bootsector_exec_fail(void)
Restart point for INT 18 or 19.
uint8_t drive
Drive number.
Definition: int13.h:16
unsigned long intptr_t
Definition: stdint.h:21
#define ECANCELED
Operation canceled.
Definition: errno.h:343
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:24
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:69
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static struct segoff int18_vector
Vector for storing original INT 18 handler.
Definition: bootsector.c:44
static void console_reset(void)
Reset console.
Definition: console.h:214
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
void * discard_D
Definition: bigint.h:31
static struct segoff int19_vector
Vector for storing original INT 19 handler.
Definition: bootsector.c:51
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226

References __asm__(), __volatile__(), bootsector_exec_fail(), console_reset(), DBG, discard_D, drive, ECANCELED, hook_bios_interrupt(), int18_vector, int19_vector, offset, REAL_CODE, segment, and unhook_bios_interrupt().

Referenced by int13_boot().

Variable Documentation

◆ int18_vector

struct segoff int18_vector
static

Vector for storing original INT 18 handler.

We do not chain to this vector, so there is no need to place it in .text16.

Definition at line 44 of file bootsector.c.

Referenced by call_bootsector().

◆ int19_vector

struct segoff int19_vector
static

Vector for storing original INT 19 handler.

We do not chain to this vector, so there is no need to place it in .text16.

Definition at line 51 of file bootsector.c.

Referenced by call_bootsector().