iPXE
segment.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Executable image segments
30  *
31  */
32 
33 #include <errno.h>
34 #include <ipxe/uaccess.h>
35 #include <ipxe/io.h>
36 #include <ipxe/errortab.h>
37 #include <ipxe/segment.h>
38 
39 /**
40  * Segment-specific error messages
41  *
42  * This error happens sufficiently often to merit a user-friendly
43  * description.
44  */
45 #define ERANGE_SEGMENT __einfo_error ( EINFO_ERANGE_SEGMENT )
46 #define EINFO_ERANGE_SEGMENT \
47  __einfo_uniqify ( EINFO_ERANGE, 0x01, "Requested memory not available" )
48 struct errortab segment_errors[] __errortab = {
50 };
51 
52 /**
53  * Prepare segment for loading
54  *
55  * @v segment Segment start
56  * @v filesz Size of the "allocated bytes" portion of the segment
57  * @v memsz Size of the segment
58  * @ret rc Return status code
59  */
60 int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
61  struct memory_map memmap;
63  physaddr_t mid = user_to_phys ( segment, filesz );
64  physaddr_t end = user_to_phys ( segment, memsz );
65  unsigned int i;
66 
67  DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
68 
69  /* Sanity check */
70  if ( filesz > memsz ) {
71  DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
72  return -EINVAL;
73  }
74 
75  /* Get a fresh memory map. This allows us to automatically
76  * avoid treading on any regions that Etherboot is currently
77  * editing out of the memory map.
78  */
79  get_memmap ( &memmap );
80 
81  /* Look for a suitable memory region */
82  for ( i = 0 ; i < memmap.count ; i++ ) {
83  if ( ( start >= memmap.regions[i].start ) &&
84  ( end <= memmap.regions[i].end ) ) {
85  /* Found valid region: zero bss and return */
86  memset_user ( segment, filesz, 0, ( memsz - filesz ) );
87  return 0;
88  }
89  }
90 
91  /* No suitable memory region found */
92  DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
93  start, mid, end );
94  return -ERANGE_SEGMENT;
95 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
uint16_t segment
Code segment.
Definition: librm.h:252
iPXE I/O API
void get_memmap(struct memory_map *memmap)
Get memory map.
Error message tables.
unsigned int count
Number of used regions.
Definition: io.h:503
Error codes.
uint16_t mid
Middle 16 bits of address.
Definition: librm.h:258
#define __einfo_errortab(einfo)
Definition: errortab.h:23
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
A memory map.
Definition: io.h:499
Access to external ("user") memory.
uint32_t start
Starting offset.
Definition: netvsc.h:12
struct memory_region regions[MAX_MEMORY_REGIONS]
Memory regions.
Definition: io.h:501
Executable image segments.
void memset_user(userptr_t userptr, off_t offset, int c, size_t len)
Fill user buffer with a constant byte.
int prep_segment(userptr_t segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:60
#define ERANGE_SEGMENT
Segment-specific error messages.
Definition: segment.c:45
#define EINFO_ERANGE_SEGMENT
Definition: segment.c:46
unsigned long physaddr_t
Definition: stdint.h:20
uint64_t start
Physical start address.
Definition: io.h:490
uint32_t end
Ending offset.
Definition: netvsc.h:18
struct errortab segment_errors [] __errortab
Definition: segment.c:48
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
uint64_t end
Physical end address.
Definition: io.h:492
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33