• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 FILE_LICENCE ( GPL2_OR_LATER );
20 
21 /**
22  * @file
23  *
24  * Executable image segments
25  *
26  */
27 
28 #include <errno.h>
29 #include <gpxe/uaccess.h>
30 #include <gpxe/memmap.h>
31 #include <gpxe/errortab.h>
32 #include <gpxe/segment.h>
33 
34 /**
35  * Prepare segment for loading
36  *
37  * @v segment		Segment start
38  * @v filesz		Size of the "allocated bytes" portion of the segment
39  * @v memsz		Size of the segment
40  * @ret rc		Return status code
41  */
prep_segment(userptr_t segment,size_t filesz,size_t memsz)42 int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
43 	struct memory_map memmap;
44 	physaddr_t start = user_to_phys ( segment, 0 );
45 	physaddr_t mid = user_to_phys ( segment, filesz );
46 	physaddr_t end = user_to_phys ( segment, memsz );
47 	unsigned int i;
48 
49 	DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
50 
51 	/* Sanity check */
52 	if ( filesz > memsz ) {
53 		DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
54 		return -EINVAL;
55 	}
56 
57 	/* Get a fresh memory map.  This allows us to automatically
58 	 * avoid treading on any regions that Etherboot is currently
59 	 * editing out of the memory map.
60 	 */
61 	get_memmap ( &memmap );
62 
63 	/* Look for a suitable memory region */
64 	for ( i = 0 ; i < memmap.count ; i++ ) {
65 		if ( ( start >= memmap.regions[i].start ) &&
66 		     ( end <= memmap.regions[i].end ) ) {
67 			/* Found valid region: zero bss and return */
68 			memset_user ( segment, filesz, 0, ( memsz - filesz ) );
69 			return 0;
70 		}
71 	}
72 
73 	/* No suitable memory region found */
74 	DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
75 	      start, mid, end );
76 	return -ERANGE;
77 }
78 
79 /**
80  * Segment-specific error messages
81  *
82  * This error happens sufficiently often to merit a user-friendly
83  * description.
84  */
85 struct errortab segment_errors[] __errortab = {
86 	{ ERANGE, "Requested memory not available" },
87 };
88