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