iPXE
Functions
com32.c File Reference

SYSLINUX COM32 image format. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <basemem.h>
#include <comboot.h>
#include <ipxe/uaccess.h>
#include <ipxe/image.h>
#include <ipxe/segment.h>
#include <ipxe/init.h>
#include <ipxe/io.h>
#include <ipxe/console.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 
static int com32_exec_loop (struct image *image)
 Execute COMBOOT image. More...
 
static int com32_identify (struct image *image)
 Check image name extension. More...
 
static int com32_load_image (struct image *image)
 Load COM32 image into memory. More...
 
static int com32_prepare_bounce_buffer (struct image *image)
 Prepare COM32 low memory bounce buffer. More...
 
static int com32_probe (struct image *image)
 Probe COM32 image. More...
 
static int com32_exec (struct image *image)
 Execute COMBOOT image. More...
 
struct image_type com32_image_type __image_type (PROBE_NORMAL)
 SYSLINUX COM32 image type. More...
 

Detailed Description

SYSLINUX COM32 image format.

Definition in file com32.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ com32_exec_loop()

static int com32_exec_loop ( struct image image)
static

Execute COMBOOT image.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 51 of file com32.c.

51  {
52  struct memory_map memmap;
53  unsigned int i;
54  int state;
55  uint32_t avail_mem_top;
56 
58 
59  switch ( state ) {
60  case 0: /* First time through; invoke COM32 program */
61 
62  /* Get memory map */
63  get_memmap ( &memmap );
64 
65  /* Find end of block covering COM32 image loading area */
66  for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
67  if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
68  (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
69  avail_mem_top = memmap.regions[i].end;
70  break;
71  }
72  }
73 
74  DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
75  image, avail_mem_top );
76 
77  assert ( avail_mem_top != 0 );
78 
79  /* Hook COMBOOT API interrupts */
81 
82  /* Unregister image, so that a "boot" command doesn't
83  * throw us into an execution loop. We never
84  * reregister ourselves; COMBOOT images expect to be
85  * removed on exit.
86  */
88 
90  /* Preserve registers */
91  "pushal\n\t"
92  /* Preserve stack pointer */
93  "subl $4, %k0\n\t"
94  "movl %%esp, (%k0)\n\t"
95  /* Switch to COM32 stack */
96  "movl %k0, %%esp\n\t"
97  /* Enable interrupts */
98  "sti\n\t"
99  /* Construct stack frame */
100  "pushl %k1\n\t"
101  "pushl %k2\n\t"
102  "pushl %k3\n\t"
103  "pushl %k4\n\t"
104  "pushl %k5\n\t"
105  "pushl %k6\n\t"
106  "pushl $6\n\t"
107  /* Call COM32 entry point */
108  "movl %k7, %k0\n\t"
109  "call *%k0\n\t"
110  /* Disable interrupts */
111  "cli\n\t"
112  /* Restore stack pointer */
113  "movl 28(%%esp), %%esp\n\t"
114  /* Restore registers */
115  "popal\n\t" )
116  :
117  : "r" ( avail_mem_top ),
120  "r" ( get_fbms() * 1024 - ( COM32_BOUNCE_SEG << 4 ) ),
121  "i" ( COM32_BOUNCE_SEG << 4 ),
123  "r" ( virt_to_phys ( image->cmdline ?
124  image->cmdline : "" ) ),
125  "i" ( COM32_START_PHYS )
126  : "memory" );
127  DBGC ( image, "COM32 %p: returned\n", image );
128  break;
129 
130  case COMBOOT_EXIT:
131  DBGC ( image, "COM32 %p: exited\n", image );
132  break;
133 
135  assert ( image->replacement );
136  DBGC ( image, "COM32 %p: exited to run kernel %s\n",
138  break;
139 
141  DBGC ( image, "COM32 %p: exited after executing command\n",
142  image );
143  break;
144 
145  default:
146  assert ( 0 );
147  break;
148  }
149 
152 
153  return 0;
154 }
#define PHYS_CODE(asm_code_str)
Definition: librm.h:281
void get_memmap(struct memory_map *memmap)
Get memory map.
static unsigned int get_fbms(void)
Read the BIOS free base memory counter.
Definition: basemem.h:21
uint8_t state
State.
Definition: eth_slow.h:47
#define COM32_BOUNCE_SEG
COM32 bounce buffer segment.
Definition: comboot.h:23
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define COM32_START_PHYS
Entry point address of COM32 images.
Definition: comboot.h:20
A memory map.
Definition: io.h:499
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
char * cmdline
Command line to pass to image.
Definition: image.h:39
#define COMBOOT_EXIT
Definition: comboot.h:119
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
rmjmp_buf comboot_return
Definition: comboot_call.c:82
void comboot_force_text_mode(void)
Set default text mode.
Definition: comboot_call.c:140
void unhook_comboot_interrupts()
Unhook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
Definition: comboot_call.c:692
size_t len
Length of raw file image.
Definition: image.h:43
void com32_cfarcall_wrapper()
__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")
unsigned int uint32_t
Definition: stdint.h:12
void com32_farcall_wrapper()
#define COMBOOT_EXIT_RUN_KERNEL
Definition: comboot.h:120
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:303
__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")
void hook_comboot_interrupts()
Hook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
Definition: comboot_call.c:659
#define COMBOOT_EXIT_COMMAND
Definition: comboot.h:121
#define rmsetjmp(_env)
Definition: rmsetjmp.h:17
struct image * replacement
Replacement image.
Definition: image.h:60
char * name
Name.
Definition: image.h:34
void com32_intcall_wrapper()

References __asm__(), __volatile__(), assert(), image::cmdline, COM32_BOUNCE_SEG, com32_cfarcall_wrapper(), com32_farcall_wrapper(), com32_intcall_wrapper(), COM32_START_PHYS, COMBOOT_EXIT, COMBOOT_EXIT_COMMAND, COMBOOT_EXIT_RUN_KERNEL, comboot_force_text_mode(), comboot_return, memory_map::count, DBGC, memory_region::end, get_fbms(), get_memmap(), hook_comboot_interrupts(), image::len, image::name, PHYS_CODE, memory_map::regions, image::replacement, rmsetjmp, memory_region::start, state, unhook_comboot_interrupts(), unregister_image(), and virt_to_phys().

Referenced by com32_exec().

◆ com32_identify()

static int com32_identify ( struct image image)
static

Check image name extension.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 162 of file com32.c.

162  {
163  const char *ext;
164  static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
165  uint8_t buf[5];
166 
167  if ( image->len >= 5 ) {
168  /* Check for magic number
169  * mov eax,21cd4cffh
170  * B8 FF 4C CD 21
171  */
172  copy_from_user ( buf, image->data, 0, sizeof(buf) );
173  if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
174  DBGC ( image, "COM32 %p: found magic number\n",
175  image );
176  return 0;
177  }
178  }
179 
180  /* Magic number not found; check filename extension */
181 
182  ext = strrchr( image->name, '.' );
183 
184  if ( ! ext ) {
185  DBGC ( image, "COM32 %p: no extension\n",
186  image );
187  return -ENOEXEC;
188  }
189 
190  ++ext;
191 
192  if ( strcasecmp( ext, "c32" ) ) {
193  DBGC ( image, "COM32 %p: unrecognized extension %s\n",
194  image, ext );
195  return -ENOEXEC;
196  }
197 
198  return 0;
199 }
userptr_t data
Raw file image.
Definition: image.h:41
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
#define ENOEXEC
Exec format error.
Definition: errno.h:519
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
uint32_t magic
Magic signature.
Definition: fdt.h:12
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
An executable image.
Definition: image.h:24
size_t len
Length of raw file image.
Definition: image.h:43
unsigned char uint8_t
Definition: stdint.h:10
uint16_t ext
Extended status.
Definition: ena.h:20
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
char * name
Name.
Definition: image.h:34

References copy_from_user(), image::data, DBGC, ENOEXEC, ext, image::len, magic, memcmp(), image::name, strcasecmp(), and strrchr().

Referenced by com32_probe().

◆ com32_load_image()

static int com32_load_image ( struct image image)
static

Load COM32 image into memory.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 207 of file com32.c.

207  {
208  size_t filesz, memsz;
210  int rc;
211 
212  filesz = image->len;
213  memsz = filesz;
215  if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
216  DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
217  image, strerror ( rc ) );
218  return rc;
219  }
220 
221  /* Copy image to segment */
222  memcpy_user ( buffer, 0, image->data, 0, filesz );
223 
224  return 0;
225 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
#define DBGC(...)
Definition: compiler.h:505
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition: netvsc.h:16
An executable image.
Definition: image.h:24
#define COM32_START_PHYS
Entry point address of COM32 images.
Definition: comboot.h:20
int prep_segment(userptr_t segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:60
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:43
void memcpy_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers.
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References buffer, COM32_START_PHYS, image::data, DBGC, image::len, memcpy_user(), phys_to_user(), prep_segment(), rc, and strerror().

Referenced by com32_exec().

◆ com32_prepare_bounce_buffer()

static int com32_prepare_bounce_buffer ( struct image image)
static

Prepare COM32 low memory bounce buffer.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 232 of file com32.c.

232  {
233  unsigned int seg;
234  userptr_t seg_userptr;
235  size_t filesz, memsz;
236  int rc;
237 
239  seg_userptr = real_to_user ( seg, 0 );
240 
241  /* Ensure the entire 64k segment is free */
242  memsz = 0xFFFF;
243  filesz = 0;
244 
245  /* Prepare, verify, and load the real-mode segment */
246  if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
247  DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
248  image, strerror ( rc ) );
249  return rc;
250  }
251 
252  return 0;
253 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define COM32_BOUNCE_SEG
COM32 bounce buffer segment.
Definition: comboot.h:23
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
int prep_segment(userptr_t segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:60
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static __always_inline userptr_t real_to_user(unsigned int segment, unsigned int offset)
Convert segment:offset address to user buffer.
Definition: realmode.h:75
struct golan_mkey_seg seg
Definition: CIB_PRM.h:28
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References COM32_BOUNCE_SEG, DBGC, prep_segment(), rc, real_to_user(), seg, and strerror().

Referenced by com32_exec().

◆ com32_probe()

static int com32_probe ( struct image image)
static

Probe COM32 image.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 261 of file com32.c.

261  {
262  int rc;
263 
264  DBGC ( image, "COM32 %p: name '%s'\n", image, image->name );
265 
266  /* Check if this is a COMBOOT image */
267  if ( ( rc = com32_identify ( image ) ) != 0 ) {
268  return rc;
269  }
270 
271  return 0;
272 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
char * name
Name.
Definition: image.h:34
static int com32_identify(struct image *image)
Check image name extension.
Definition: com32.c:162

References com32_identify(), DBGC, image::name, and rc.

◆ com32_exec()

static int com32_exec ( struct image image)
static

Execute COMBOOT image.

Parameters
imageCOM32 image
Return values
rcReturn status code

Definition at line 280 of file com32.c.

280  {
281  int rc;
282 
283  /* Load image */
284  if ( ( rc = com32_load_image ( image ) ) != 0 ) {
285  return rc;
286  }
287 
288  /* Prepare bounce buffer segment */
289  if ( ( rc = com32_prepare_bounce_buffer ( image ) ) != 0 ) {
290  return rc;
291  }
292 
293  /* Reset console */
294  console_reset();
295 
296  return com32_exec_loop ( image );
297 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int com32_exec_loop(struct image *image)
Execute COMBOOT image.
Definition: com32.c:51
static int com32_prepare_bounce_buffer(struct image *image)
Prepare COM32 low memory bounce buffer.
Definition: com32.c:232
An executable image.
Definition: image.h:24
static void console_reset(void)
Reset console.
Definition: console.h:214
static int com32_load_image(struct image *image)
Load COM32 image into memory.
Definition: com32.c:207

References com32_exec_loop(), com32_load_image(), com32_prepare_bounce_buffer(), console_reset(), and rc.

◆ __image_type()

struct image_type com32_image_type __image_type ( PROBE_NORMAL  )

SYSLINUX COM32 image type.