|
|
1.1 ! root 1: /* ! 2: * Copyright (C) 2007 Michael Brown <[email protected]>. ! 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 <ipxe/uaccess.h> ! 30: #include <ipxe/io.h> ! 31: #include <ipxe/errortab.h> ! 32: #include <ipxe/segment.h> ! 33: ! 34: /** ! 35: * Segment-specific error messages ! 36: * ! 37: * This error happens sufficiently often to merit a user-friendly ! 38: * description. ! 39: */ ! 40: #define ERANGE_SEGMENT __einfo_error ( EINFO_ERANGE_SEGMENT ) ! 41: #define EINFO_ERANGE_SEGMENT \ ! 42: __einfo_uniqify ( EINFO_ERANGE, 0x01, "Requested memory not available" ) ! 43: struct errortab segment_errors[] __errortab = { ! 44: __einfo_errortab ( EINFO_ERANGE_SEGMENT ), ! 45: }; ! 46: ! 47: /** ! 48: * Prepare segment for loading ! 49: * ! 50: * @v segment Segment start ! 51: * @v filesz Size of the "allocated bytes" portion of the segment ! 52: * @v memsz Size of the segment ! 53: * @ret rc Return status code ! 54: */ ! 55: int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) { ! 56: struct memory_map memmap; ! 57: physaddr_t start = user_to_phys ( segment, 0 ); ! 58: physaddr_t mid = user_to_phys ( segment, filesz ); ! 59: physaddr_t end = user_to_phys ( segment, memsz ); ! 60: unsigned int i; ! 61: ! 62: DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end ); ! 63: ! 64: /* Sanity check */ ! 65: if ( filesz > memsz ) { ! 66: DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end ); ! 67: return -EINVAL; ! 68: } ! 69: ! 70: /* Get a fresh memory map. This allows us to automatically ! 71: * avoid treading on any regions that Etherboot is currently ! 72: * editing out of the memory map. ! 73: */ ! 74: get_memmap ( &memmap ); ! 75: ! 76: /* Look for a suitable memory region */ ! 77: for ( i = 0 ; i < memmap.count ; i++ ) { ! 78: if ( ( start >= memmap.regions[i].start ) && ! 79: ( end <= memmap.regions[i].end ) ) { ! 80: /* Found valid region: zero bss and return */ ! 81: memset_user ( segment, filesz, 0, ( memsz - filesz ) ); ! 82: return 0; ! 83: } ! 84: } ! 85: ! 86: /* No suitable memory region found */ ! 87: DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n", ! 88: start, mid, end ); ! 89: return -ERANGE_SEGMENT; ! 90: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.