a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is free software; you can redistribute it and/or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * modify it under the terms of the GNU General Public License as
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * published by the Free Software Foundation; either version 2 of the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * License, or any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is distributed in the hope that it will be useful, but
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncFILE_LICENCE ( GPL2_OR_LATER );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Executable image segments
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <errno.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/uaccess.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/io.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/errortab.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/segment.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Segment-specific error messages
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This error happens sufficiently often to merit a user-friendly
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * description.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#define ERANGE_SEGMENT __einfo_error ( EINFO_ERANGE_SEGMENT )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#define EINFO_ERANGE_SEGMENT \
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync __einfo_uniqify ( EINFO_ERANGE, 0x01, "Requested memory not available" )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstruct errortab segment_errors[] __errortab = {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync __einfo_errortab ( EINFO_ERANGE_SEGMENT ),
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync};
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Prepare segment for loading
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v segment Segment start
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v filesz Size of the "allocated bytes" portion of the segment
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v memsz Size of the segment
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncint prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct memory_map memmap;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync physaddr_t start = user_to_phys ( segment, 0 );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync physaddr_t mid = user_to_phys ( segment, filesz );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync physaddr_t end = user_to_phys ( segment, memsz );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync unsigned int i;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Sanity check */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( filesz > memsz ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return -EINVAL;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Get a fresh memory map. This allows us to automatically
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * avoid treading on any regions that Etherboot is currently
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * editing out of the memory map.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync get_memmap ( &memmap );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Look for a suitable memory region */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync for ( i = 0 ; i < memmap.count ; i++ ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( start >= memmap.regions[i].start ) &&
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( end <= memmap.regions[i].end ) ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Found valid region: zero bss and return */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync memset_user ( segment, filesz, 0, ( memsz - filesz ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* No suitable memory region found */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync start, mid, end );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return -ERANGE_SEGMENT;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}