]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/peloader.c
1.1.07 release
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / peloader.c
1 /*
2 * Copyright (C) 2012 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 (at your option) 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 /**
21 * @file
22 *
23 * PE image loader
24 *
25 */
26
27 #include <stdint.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "wimboot.h"
32 #include "peloader.h"
33
34 /**
35 * Load PE image into memory
36 *
37 * @v data PE image
38 * @v len Length of PE image
39 * @v pe Loaded PE structure to fill in
40 * @ret rc Return status code
41 */
42 int load_pe ( const void *data, size_t len, struct loaded_pe *pe ) {
43 const struct mz_header *mzhdr;
44 size_t pehdr_offset;
45 const struct pe_header *pehdr;
46 size_t opthdr_offset;
47 const struct pe_optional_header *opthdr;
48 size_t section_offset;
49 const struct coff_section *section;
50 char name[ sizeof ( section->name ) + 1 /* NUL */ ];
51 unsigned int i;
52 void *section_base;
53 size_t filesz;
54 size_t memsz;
55 void *end;
56 void *raw_base;
57
58 DBG2 ( "Loading PE executable...\n" );
59
60 /* Parse PE header */
61 mzhdr = data;
62 if ( mzhdr->magic != MZ_HEADER_MAGIC ) {
63 DBG ( "Bad MZ magic %04x\n", mzhdr->magic );
64 return -1;
65 }
66 pehdr_offset = mzhdr->lfanew;
67 if ( pehdr_offset > len ) {
68 DBG ( "PE header outside file\n" );
69 return -1;
70 }
71 pehdr = ( data + pehdr_offset );
72 if ( pehdr->magic != PE_HEADER_MAGIC ) {
73 DBG ( "Bad PE magic %08x\n", pehdr->magic );
74 return -1;
75 }
76 opthdr_offset = ( pehdr_offset + sizeof ( *pehdr ) );
77 opthdr = ( data + opthdr_offset );
78 pe->base = ( ( void * ) ( intptr_t ) ( opthdr->base ) );
79 section_offset = ( opthdr_offset + pehdr->coff.opthdr_len );
80 section = ( data + section_offset );
81
82 /* Load header into memory */
83 DBG2 ( "...headers to %p+%#x\n", pe->base, opthdr->header_len );
84 memcpy ( pe->base, data, opthdr->header_len );
85 end = ( pe->base + opthdr->header_len );
86
87 /* Load each section into memory */
88 for ( i = 0 ; i < pehdr->coff.num_sections ; i++, section++ ) {
89 memset ( name, 0, sizeof ( name ) );
90 memcpy ( name, section->name, sizeof ( section->name ) );
91 section_base = ( pe->base + section->virtual );
92 filesz = section->raw_len;
93 memsz = section->misc.virtual_len;
94 DBG2 ( "...from %#05x to %p+%#zx/%#zx (%s)\n",
95 section->raw, section_base, filesz, memsz, name );
96 memset ( section_base, 0, memsz );
97 memcpy ( section_base, ( data + section->raw ), filesz );
98 if ( end < ( section_base + memsz ) )
99 end = ( section_base + memsz );
100 }
101 pe->len = ( ( ( end - pe->base ) + opthdr->section_align - 1 )
102 & ~( opthdr->section_align - 1 ) );
103
104 /* Load copy of raw image into memory immediately after loaded
105 * sections. This seems to be used for verification of X.509
106 * signatures.
107 */
108 raw_base = ( pe->base + pe->len );
109 memcpy ( raw_base, data, len );
110 pe->len += len;
111 DBG2 ( "...raw copy to %p+%#zx\n", raw_base, len );
112
113 /* Extract entry point */
114 pe->entry = ( pe->base + opthdr->entry );
115 DBG2 ( "...entry point %p\n", pe->entry );
116
117 return 0;
118 }