iPXE
segment.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * Executable image segments
00030  *
00031  */
00032 
00033 #include <errno.h>
00034 #include <ipxe/uaccess.h>
00035 #include <ipxe/io.h>
00036 #include <ipxe/errortab.h>
00037 #include <ipxe/segment.h>
00038 
00039 /**
00040  * Segment-specific error messages
00041  *
00042  * This error happens sufficiently often to merit a user-friendly
00043  * description.
00044  */
00045 #define ERANGE_SEGMENT __einfo_error ( EINFO_ERANGE_SEGMENT )
00046 #define EINFO_ERANGE_SEGMENT \
00047         __einfo_uniqify ( EINFO_ERANGE, 0x01, "Requested memory not available" )
00048 struct errortab segment_errors[] __errortab = {
00049         __einfo_errortab ( EINFO_ERANGE_SEGMENT ),
00050 };
00051 
00052 /**
00053  * Prepare segment for loading
00054  *
00055  * @v segment           Segment start
00056  * @v filesz            Size of the "allocated bytes" portion of the segment
00057  * @v memsz             Size of the segment
00058  * @ret rc              Return status code
00059  */
00060 int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
00061         struct memory_map memmap;
00062         physaddr_t start = user_to_phys ( segment, 0 );
00063         physaddr_t mid = user_to_phys ( segment, filesz );
00064         physaddr_t end = user_to_phys ( segment, memsz );
00065         unsigned int i;
00066 
00067         DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
00068 
00069         /* Sanity check */
00070         if ( filesz > memsz ) {
00071                 DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
00072                 return -EINVAL;
00073         }
00074 
00075         /* Get a fresh memory map.  This allows us to automatically
00076          * avoid treading on any regions that Etherboot is currently
00077          * editing out of the memory map.
00078          */
00079         get_memmap ( &memmap );
00080 
00081         /* Look for a suitable memory region */
00082         for ( i = 0 ; i < memmap.count ; i++ ) {
00083                 if ( ( start >= memmap.regions[i].start ) &&
00084                      ( end <= memmap.regions[i].end ) ) {
00085                         /* Found valid region: zero bss and return */
00086                         memset_user ( segment, filesz, 0, ( memsz - filesz ) );
00087                         return 0;
00088                 }
00089         }
00090 
00091         /* No suitable memory region found */
00092         DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
00093               start, mid, end );
00094         return -ERANGE_SEGMENT;
00095 }