|  | @@ -0,0 +1,110 @@
 | 
		
	
		
			
			|  | 1 | +/*
 | 
		
	
		
			
			|  | 2 | + * Copyright (C) 2008 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
		
	
		
			
			|  | 17 | + */
 | 
		
	
		
			
			|  | 18 | +
 | 
		
	
		
			
			|  | 19 | +#include <errno.h>
 | 
		
	
		
			
			|  | 20 | +#include <elf.h>
 | 
		
	
		
			
			|  | 21 | +#include <gpxe/image.h>
 | 
		
	
		
			
			|  | 22 | +#include <gpxe/elf.h>
 | 
		
	
		
			
			|  | 23 | +#include <gpxe/features.h>
 | 
		
	
		
			
			|  | 24 | +#include <gpxe/init.h>
 | 
		
	
		
			
			|  | 25 | +
 | 
		
	
		
			
			|  | 26 | +/**
 | 
		
	
		
			
			|  | 27 | + * @file
 | 
		
	
		
			
			|  | 28 | + *
 | 
		
	
		
			
			|  | 29 | + * ELF bootable image
 | 
		
	
		
			
			|  | 30 | + *
 | 
		
	
		
			
			|  | 31 | + */
 | 
		
	
		
			
			|  | 32 | +
 | 
		
	
		
			
			|  | 33 | +FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );
 | 
		
	
		
			
			|  | 34 | +
 | 
		
	
		
			
			|  | 35 | +struct image_type elfboot_image_type __image_type ( PROBE_NORMAL );
 | 
		
	
		
			
			|  | 36 | +
 | 
		
	
		
			
			|  | 37 | +/**
 | 
		
	
		
			
			|  | 38 | + * Execute ELF image
 | 
		
	
		
			
			|  | 39 | + *
 | 
		
	
		
			
			|  | 40 | + * @v image		ELF image
 | 
		
	
		
			
			|  | 41 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 42 | + */
 | 
		
	
		
			
			|  | 43 | +static int elfboot_exec ( struct image *image ) {
 | 
		
	
		
			
			|  | 44 | +	physaddr_t entry = image->priv.phys;
 | 
		
	
		
			
			|  | 45 | +
 | 
		
	
		
			
			|  | 46 | +	/* An ELF image has no callback interface, so we need to shut
 | 
		
	
		
			
			|  | 47 | +	 * down before invoking it.
 | 
		
	
		
			
			|  | 48 | +	 */
 | 
		
	
		
			
			|  | 49 | +	shutdown();
 | 
		
	
		
			
			|  | 50 | +
 | 
		
	
		
			
			|  | 51 | +	/* Jump to OS with flat physical addressing */
 | 
		
	
		
			
			|  | 52 | +	__asm__ __volatile__ ( PHYS_CODE ( "call *%%edi\n\t" )
 | 
		
	
		
			
			|  | 53 | +			       : : "D" ( entry )
 | 
		
	
		
			
			|  | 54 | +			       : "eax", "ebx", "ecx", "edx", "esi", "ebp",
 | 
		
	
		
			
			|  | 55 | +			         "memory" );
 | 
		
	
		
			
			|  | 56 | +
 | 
		
	
		
			
			|  | 57 | +	DBGC ( image, "ELF %p returned\n", image );
 | 
		
	
		
			
			|  | 58 | +
 | 
		
	
		
			
			|  | 59 | +	/* It isn't safe to continue after calling shutdown() */
 | 
		
	
		
			
			|  | 60 | +	while ( 1 ) {}
 | 
		
	
		
			
			|  | 61 | +
 | 
		
	
		
			
			|  | 62 | +	return -ECANCELED;  /* -EIMPOSSIBLE, anyone? */
 | 
		
	
		
			
			|  | 63 | +}
 | 
		
	
		
			
			|  | 64 | +
 | 
		
	
		
			
			|  | 65 | +/**
 | 
		
	
		
			
			|  | 66 | + * Load ELF image into memory
 | 
		
	
		
			
			|  | 67 | + *
 | 
		
	
		
			
			|  | 68 | + * @v image		ELF file
 | 
		
	
		
			
			|  | 69 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 70 | + */
 | 
		
	
		
			
			|  | 71 | +static int elfboot_load ( struct image *image ) {
 | 
		
	
		
			
			|  | 72 | +	Elf32_Ehdr ehdr;
 | 
		
	
		
			
			|  | 73 | +	static const uint8_t e_ident[] = {
 | 
		
	
		
			
			|  | 74 | +		[EI_MAG0]	= ELFMAG0,
 | 
		
	
		
			
			|  | 75 | +		[EI_MAG1]	= ELFMAG1,
 | 
		
	
		
			
			|  | 76 | +		[EI_MAG2]	= ELFMAG2,
 | 
		
	
		
			
			|  | 77 | +		[EI_MAG3]	= ELFMAG3,
 | 
		
	
		
			
			|  | 78 | +		[EI_CLASS]	= ELFCLASS32,
 | 
		
	
		
			
			|  | 79 | +		[EI_DATA]	= ELFDATA2LSB,
 | 
		
	
		
			
			|  | 80 | +		[EI_VERSION]	= EV_CURRENT,
 | 
		
	
		
			
			|  | 81 | +	};
 | 
		
	
		
			
			|  | 82 | +	int rc;
 | 
		
	
		
			
			|  | 83 | +
 | 
		
	
		
			
			|  | 84 | +	/* Read ELF header */
 | 
		
	
		
			
			|  | 85 | +	copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
 | 
		
	
		
			
			|  | 86 | +	if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
 | 
		
	
		
			
			|  | 87 | +		DBG ( "Invalid ELF identifier\n" );
 | 
		
	
		
			
			|  | 88 | +		return -ENOEXEC;
 | 
		
	
		
			
			|  | 89 | +	}
 | 
		
	
		
			
			|  | 90 | +
 | 
		
	
		
			
			|  | 91 | +	/* This is an ELF image, valid or otherwise */
 | 
		
	
		
			
			|  | 92 | +	if ( ! image->type )
 | 
		
	
		
			
			|  | 93 | +		image->type = &elfboot_image_type;
 | 
		
	
		
			
			|  | 94 | +
 | 
		
	
		
			
			|  | 95 | +	/* Load the image using core ELF support */
 | 
		
	
		
			
			|  | 96 | +	if ( ( rc = elf_load ( image ) ) != 0 ) {
 | 
		
	
		
			
			|  | 97 | +		DBGC ( image, "ELF %p could not load: %s\n",
 | 
		
	
		
			
			|  | 98 | +		       image, strerror ( rc ) );
 | 
		
	
		
			
			|  | 99 | +		return rc;
 | 
		
	
		
			
			|  | 100 | +	}
 | 
		
	
		
			
			|  | 101 | +
 | 
		
	
		
			
			|  | 102 | +	return 0;
 | 
		
	
		
			
			|  | 103 | +}
 | 
		
	
		
			
			|  | 104 | +
 | 
		
	
		
			
			|  | 105 | +/** ELF image type */
 | 
		
	
		
			
			|  | 106 | +struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
 | 
		
	
		
			
			|  | 107 | +	.name = "ELF",
 | 
		
	
		
			
			|  | 108 | +	.load = elfboot_load,
 | 
		
	
		
			
			|  | 109 | +	.exec = elfboot_exec,
 | 
		
	
		
			
			|  | 110 | +};
 |