iPXE
Macros | Functions | Variables
relocate.c File Reference
#include <ipxe/io.h>
#include <registers.h>

Go to the source code of this file.

Macros

#define MAX_ADDR   (0xfff00000UL)
 
#define ALIGN   4096
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
__asmcall void relocate (struct i386_all_regs *ix86)
 Relocate iPXE. More...
 

Variables

char _textdata []
 
char _etextdata []
 

Macro Definition Documentation

◆ MAX_ADDR

#define MAX_ADDR   (0xfff00000UL)

Definition at line 23 of file relocate.c.

◆ ALIGN

#define ALIGN   4096

Definition at line 29 of file relocate.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ relocate()

__asmcall void relocate ( struct i386_all_regs ix86)

Relocate iPXE.

Parameters
ebpMaximum address to use for relocation
Return values
esiCurrent physical address
ediNew physical address
ecxLength to copy

This finds a suitable location for iPXE near the top of 32-bit address space, and returns the physical address of the new location to the prefix in edi.

Definition at line 43 of file relocate.c.

43  {
44  struct memory_map memmap;
45  uint32_t start, end, size, padded_size, max;
46  uint32_t new_start, new_end;
47  unsigned i;
48 
49  /* Get memory map and current location */
50  get_memmap ( &memmap );
53  size = ( end - start );
54  padded_size = ( size + ALIGN - 1 );
55 
56  DBG ( "Relocate: currently at [%x,%x)\n"
57  "...need %x bytes for %d-byte alignment\n",
58  start, end, padded_size, ALIGN );
59 
60  /* Determine maximum usable address */
61  max = MAX_ADDR;
62  if ( ix86->regs.ebp < max ) {
63  max = ix86->regs.ebp;
64  DBG ( "Limiting relocation to [0,%x)\n", max );
65  }
66 
67  /* Walk through the memory map and find the highest address
68  * below 4GB that iPXE will fit into.
69  */
70  new_end = end;
71  for ( i = 0 ; i < memmap.count ; i++ ) {
72  struct memory_region *region = &memmap.regions[i];
73  uint32_t r_start, r_end;
74 
75  DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
76 
77  /* Truncate block to maximum address. This will be
78  * less than 4GB, which means that we can get away
79  * with using just 32-bit arithmetic after this stage.
80  */
81  if ( region->start > max ) {
82  DBG ( "...starts after max=%x\n", max );
83  continue;
84  }
85  r_start = region->start;
86  if ( region->end > max ) {
87  DBG ( "...end truncated to max=%x\n", max );
88  r_end = max;
89  } else {
90  r_end = region->end;
91  }
92  DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
93 
94  /* If we have rounded down r_end below r_ start, skip
95  * this block.
96  */
97  if ( r_end < r_start ) {
98  DBG ( "...truncated to negative size\n" );
99  continue;
100  }
101 
102  /* Check that there is enough space to fit in iPXE */
103  if ( ( r_end - r_start ) < size ) {
104  DBG ( "...too small (need %x bytes)\n", size );
105  continue;
106  }
107 
108  /* If the start address of the iPXE we would
109  * place in this block is higher than the end address
110  * of the current highest block, use this block.
111  *
112  * Note that this avoids overlaps with the current
113  * iPXE, as well as choosing the highest of all viable
114  * blocks.
115  */
116  if ( ( r_end - size ) > new_end ) {
117  new_end = r_end;
118  DBG ( "...new best block found.\n" );
119  }
120  }
121 
122  /* Calculate new location of iPXE, and align it to the
123  * required alignemnt.
124  */
125  new_start = new_end - padded_size;
126  new_start += ( ( start - new_start ) & ( ALIGN - 1 ) );
127  new_end = new_start + size;
128 
129  DBG ( "Relocating from [%x,%x) to [%x,%x)\n",
130  start, end, new_start, new_end );
131 
132  /* Let prefix know what to copy */
133  ix86->regs.esi = start;
134  ix86->regs.edi = new_start;
135  ix86->regs.ecx = size;
136 }
void get_memmap(struct memory_map *memmap)
Get memory map.
uint32_t ebp
Definition: registers.h:73
#define max(x, y)
Definition: ath.h:39
#define MAX_ADDR
Definition: relocate.c:23
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
uint32_t start
Starting offset.
Definition: netvsc.h:12
uint32_t edi
Definition: registers.h:65
uint32_t esi
Definition: registers.h:69
struct i386_regs regs
Definition: registers.h:176
#define ALIGN
Definition: relocate.c:29
A usable memory region.
Definition: io.h:488
unsigned int uint32_t
Definition: stdint.h:12
uint32_t ecx
Definition: registers.h:101
char _etextdata[]
uint64_t start
Physical start address.
Definition: io.h:490
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
uint64_t end
Physical end address.
Definition: io.h:492
char _textdata[]

References _etextdata, _textdata, ALIGN, memory_map::count, DBG, i386_regs::ebp, i386_regs::ecx, i386_regs::edi, end, memory_region::end, i386_regs::esi, get_memmap(), max, MAX_ADDR, memory_map::regions, i386_all_regs::regs, size, start, memory_region::start, and virt_to_phys().

Variable Documentation

◆ _textdata

char _textdata[]

Referenced by relocate().

◆ _etextdata

char _etextdata[]

Referenced by relocate().