[sundance] Add reset completion check
[gpxe.git] / src / image / segment.c
blob9bd60f9c47992f9261084dac52dfe415bde08872
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
19 /**
20 * @file
22 * Executable image segments
26 #include <errno.h>
27 #include <gpxe/uaccess.h>
28 #include <gpxe/memmap.h>
29 #include <gpxe/segment.h>
31 /**
32 * Prepare segment for loading
34 * @v segment Segment start
35 * @v filesz Size of the "allocated bytes" portion of the segment
36 * @v memsz Size of the segment
37 * @ret rc Return status code
39 int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
40 struct memory_map memmap;
41 physaddr_t start = user_to_phys ( segment, 0 );
42 physaddr_t mid = user_to_phys ( segment, filesz );
43 physaddr_t end = user_to_phys ( segment, memsz );
44 unsigned int i;
46 DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
48 /* Sanity check */
49 if ( filesz > memsz ) {
50 DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
51 return -EINVAL;
54 /* Get a fresh memory map. This allows us to automatically
55 * avoid treading on any regions that Etherboot is currently
56 * editing out of the memory map.
58 get_memmap ( &memmap );
60 /* Look for a suitable memory region */
61 for ( i = 0 ; i < memmap.count ; i++ ) {
62 if ( ( start >= memmap.regions[i].start ) &&
63 ( end <= memmap.regions[i].end ) ) {
64 /* Found valid region: zero bss and return */
65 memset_user ( segment, filesz, 0, ( memsz - filesz ) );
66 return 0;
70 /* No suitable memory region found */
71 DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
72 start, mid, end );
73 return -ERANGE;