1 /*
2  * Copyright (c) 2021 - 2022, Intel Corporation.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *    * Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer
12  *      in the documentation and/or other materials provided with the
13  *      distribution.
14  *    * Neither the name of Intel Corporation nor the names of its
15  *      contributors may be used to endorse or promote products
16  *      derived from this software without specific prior written
17  *      permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <elf.h>
34 #include <stdint.h>
35 #include "stdlib.h"
36 #include "boot.h"
37 
38 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
39 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 
41 
42 /**
43  * @brief Load elf pointed by ehdr, linked at link_addr, to load_addr.
44  *
45  * @param[in] ehdr          A pointer to target ELF header.
46  * @param[in] load_addr     The address to which the image will be loaded.
47  * @param[in] link_addr     The address to which the image was linked.
48  *
49  * @return  0 on success, -1 on error.
50  */
elf_load(Elf32_Ehdr * ehdr,uint64_t load_addr,uint64_t link_addr)51 int elf_load(Elf32_Ehdr *ehdr, uint64_t load_addr, uint64_t link_addr)
52 {
53 	int i;
54 	uint64_t addr;
55 	Elf32_Phdr *phdr;
56 	Elf32_Phdr *pbase = (Elf32_Phdr *)((char *)ehdr + ehdr->e_phoff);
57 
58 	for (i = 0; i < ehdr->e_phnum; i++) {
59 		phdr = (Elf32_Phdr *)((char *)pbase + i * ehdr->e_phentsize);
60 		if ((phdr->p_type != PT_LOAD) || (phdr->p_memsz == 0) || (phdr->p_offset == 0)) {
61 			continue;
62 		}
63 
64 		if (phdr->p_filesz > phdr->p_memsz) {
65 			return -1;
66 		}
67 
68 		addr = (uint64_t)(load_addr + (phdr->p_paddr - link_addr));
69 		memcpy((char *)addr, (const char *)((char *)ehdr + phdr->p_offset), phdr->p_filesz);
70 
71 		if (phdr->p_memsz > phdr->p_filesz) {
72 			addr = (uint64_t)(load_addr + (phdr->p_paddr - link_addr + phdr->p_filesz));
73 			(void)memset((void *)addr, 0x0, (phdr->p_memsz - phdr->p_filesz));
74 		}
75 	}
76 
77 	return 0;
78 }
79 
80 /**
81  * @brief Calculate link address range of the elf image.
82  *
83  * @param[in] ehdr          A pointer to target ELF header.
84  * @param[out] link_addr_low      The lowest link address of all loadable section in ELF.
85  * @param[out] link_addr_high     The highest reachable link address (link address plus memory size of that section) of all
86  *                                loadable section in ELF.
87  *
88  * @return  0 on success, -1 on error.
89  */
elf_calc_link_addr_range(Elf32_Ehdr * ehdr,uint64_t * link_addr_low,uint64_t * link_addr_high)90 int elf_calc_link_addr_range(Elf32_Ehdr *ehdr, uint64_t *link_addr_low, uint64_t *link_addr_high)
91 {
92 	Elf32_Phdr *phdr;
93 	Elf32_Phdr *pbase = (Elf32_Phdr *)((char *)ehdr + ehdr->e_phoff);
94 
95 	int i;
96 	uint64_t ram_low = ~0;
97 	uint64_t ram_high = 0;
98 
99 	for (i = 0; i < ehdr->e_phnum; i++) {
100 		phdr = (Elf32_Phdr *)((char *)pbase + i * ehdr->e_phentsize);
101 		if (phdr->p_type != PT_LOAD)
102 			continue;
103 
104 		ram_low = MIN(ram_low, phdr->p_paddr);
105 		ram_high = MAX(ram_high, ALIGN_UP(phdr->p_paddr + phdr->p_memsz, phdr->p_align));
106 	}
107 
108 	*link_addr_low = ram_low;
109 	*link_addr_high = ram_high;
110 
111 	return 0;
112 }
113 
114 /**
115  * @brief Get entry point of ELF image.
116  *
117  * @param[in] ehdr A pointer to target ELF header.
118  *
119  * @return  Entry point address of ELF image.
120  */
elf_get_entry(Elf32_Ehdr * ehdr)121 uint32_t elf_get_entry(Elf32_Ehdr *ehdr)
122 {
123 	return ehdr->e_entry;
124 }
125 
126 /**
127  * @brief Validate ELF header.
128  *
129  * @param[in] ehdr A pointer to target ELF header.
130  *
131  * @return  0 on success (valid elf), -1 on error.
132  */
validate_elf_header(Elf32_Ehdr * ehdr)133 int validate_elf_header(Elf32_Ehdr *ehdr)
134 {
135 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
136 		ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
137 		ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
138 		ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
139 		ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
140 		Print(L"Image is not a valid arch-independent ELF\n");
141 		return -1;
142 	}
143 
144 	if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
145 		ehdr->e_machine != EM_386 ||
146 		ehdr->e_version != EV_CURRENT) {
147 		Print(L"Image is not a valid arch-dependent ELF\n");
148 		return -1;
149 	}
150 
151 	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
152 		Print(L"This ELF is not of the right type\n");
153 		return -1;
154 	}
155 
156 	return 0;
157 }
158