blob: 302d50637d9aa375eed1cfd51bf5d29a50a05a23 [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Peter Stuge45ae92ff2009-04-14 19:48:32 +00002 * cbfs-mkpayload
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00003 *
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
Patrick Georgib7b56dd82009-09-14 13:29:27 +00005 * 2009 coresystems GmbH
6 * written by Patrick Georgi <patrick.georgi@coresystems.de>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000025
26#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000027#include "cbfs.h"
Hung-Te Lin4505ceb2013-01-28 22:40:10 +080028#include "elf.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000029
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080030int parse_elf_to_payload(const struct buffer *input,
31 struct buffer *output, comp_algo algo)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000032{
33 Elf32_Phdr *phdr;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080034 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000035 Elf32_Shdr *shdr;
36 char *header;
37 char *strtab;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000038 int headers;
39 int segments = 1;
40 int isize = 0, osize = 0;
41 int doffset = 0;
Peter Stuge1d862de2009-04-14 00:08:34 +000042 struct cbfs_payload_segment *segs;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000043 int i;
44
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080045 if(!iself((unsigned char *)input->data)){
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080046 ERROR("The payload file is not in ELF format!\n");
David Hendricks90ca3b62012-11-16 14:48:22 -080047 return -1;
Cristi Magherusan19a99c62009-09-25 22:21:47 +000048 }
49
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080050 // The tool may work in architecture-independent way.
51 if (arch != CBFS_ARCHITECTURE_UNKNOWN &&
52 !((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
David Hendricks90ca3b62012-11-16 14:48:22 -080053 !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080054 ERROR("The payload file has the wrong architecture\n");
David Hendricks90ca3b62012-11-16 14:48:22 -080055 return -1;
56 }
Cristi Magherusan19a99c62009-09-25 22:21:47 +000057
Patrick Georgib7b56dd82009-09-14 13:29:27 +000058 comp_func_ptr compress = compression_function(algo);
59 if (!compress)
60 return -1;
61
Hung-Te Linc13e4bf2013-01-29 15:22:11 +080062 DEBUG("start: parse_elf_to_payload\n");
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000063 headers = ehdr->e_phnum;
64 header = (char *)ehdr;
65
66 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
67 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
68
69 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
70
71 /* Count the number of headers - look for the .notes.pinfo
72 * section */
73
74 for (i = 0; i < ehdr->e_shnum; i++) {
75 char *name;
76
77 if (i == ehdr->e_shstrndx)
78 continue;
79
80 if (shdr[i].sh_size == 0)
81 continue;
82
83 name = (char *)(strtab + shdr[i].sh_name);
84
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000085 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000086 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000087 isize += (unsigned int)shdr[i].sh_size;
88 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000089 }
90
91 /* Now, regular headers - we only care about PT_LOAD headers,
92 * because thats what we're actually going to load
93 */
94
95 for (i = 0; i < headers; i++) {
96 if (phdr[i].p_type != PT_LOAD)
97 continue;
98
99 /* Empty segments are never interesting */
100 if (phdr[i].p_memsz == 0)
101 continue;
102
103 isize += phdr[i].p_filesz;
104
105 segments++;
106 }
107
108 /* Allocate a block of memory to store the data in */
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800109 if (buffer_create(output, (segments * sizeof(*segs)) + isize,
110 input->name) != 0)
111 return -1;
112 memset(output->data, 0, output->size);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000113
Peter Stuge1d862de2009-04-14 00:08:34 +0000114 doffset = (segments * sizeof(struct cbfs_payload_segment));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000115
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800116 segs = (struct cbfs_payload_segment *)output->data;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000117 segments = 0;
118
119 for (i = 0; i < ehdr->e_shnum; i++) {
120 char *name;
121
122 if (i == ehdr->e_shstrndx)
123 continue;
124
125 if (shdr[i].sh_size == 0)
126 continue;
127
128 name = (char *)(strtab + shdr[i].sh_name);
129
130 if (!strcmp(name, ".note.pinfo")) {
131 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
132 segs[segments].load_addr = 0;
133 segs[segments].len = (unsigned int)shdr[i].sh_size;
134 segs[segments].offset = doffset;
135
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800136 memcpy((unsigned long *)(output->data + doffset),
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000137 &header[shdr[i].sh_offset], shdr[i].sh_size);
138
139 doffset += segs[segments].len;
140 osize += segs[segments].len;
141
142 segments++;
143 }
144 }
145
146 for (i = 0; i < headers; i++) {
147 if (phdr[i].p_type != PT_LOAD)
148 continue;
149
150 if (phdr[i].p_memsz == 0)
151 continue;
152
153 if (phdr[i].p_filesz == 0) {
154 segs[segments].type = PAYLOAD_SEGMENT_BSS;
155 segs[segments].load_addr =
Stefan Reinauera1e48242011-10-21 14:24:57 -0700156 (uint64_t)htonll(phdr[i].p_paddr);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000157 segs[segments].mem_len =
Stefan Reinauera1e48242011-10-21 14:24:57 -0700158 (uint32_t)htonl(phdr[i].p_memsz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000159 segs[segments].offset = htonl(doffset);
160
161 segments++;
162 continue;
163 }
164
Stefan Reinauer51f6a202012-01-11 12:40:14 -0800165 if (phdr[i].p_flags & PF_X)
166 segs[segments].type = PAYLOAD_SEGMENT_CODE;
167 else
168 segs[segments].type = PAYLOAD_SEGMENT_DATA;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700169 segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
170 segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000171 segs[segments].compression = htonl(algo);
172 segs[segments].offset = htonl(doffset);
173
174 int len;
175 compress((char *)&header[phdr[i].p_offset],
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800176 phdr[i].p_filesz, output->data + doffset, &len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000177 segs[segments].len = htonl(len);
178
179 /* If the compressed section is larger, then use the
180 original stuff */
181
Stefan Reinauer14ad50e2009-04-04 22:18:26 +0000182 if ((unsigned int)len > phdr[i].p_filesz) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000183 segs[segments].compression = 0;
184 segs[segments].len = htonl(phdr[i].p_filesz);
185
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800186 memcpy(output->data + doffset,
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000187 &header[phdr[i].p_offset], phdr[i].p_filesz);
188 }
189
190 doffset += ntohl(segs[segments].len);
191 osize += ntohl(segs[segments].len);
192
193 segments++;
194 }
195
196 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800197 segs[segments++].load_addr = htonll(ehdr->e_entry);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000198
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800199 output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize;
200 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000201}
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800202
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800203int parse_flat_binary_to_payload(const struct buffer *input,
204 struct buffer *output,
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800205 uint32_t loadaddress,
206 uint32_t entrypoint,
207 comp_algo algo)
208{
209 comp_func_ptr compress;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800210 struct cbfs_payload_segment *segs;
211 int doffset, len = 0;
212
213 compress = compression_function(algo);
214 if (!compress)
215 return -1;
216
217 DEBUG("start: parse_flat_binary_to_payload\n");
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800218 if (buffer_create(output, (2 * sizeof(*segs) + input->size),
219 input->name) != 0)
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800220 return -1;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800221 memset(output->data, 0, output->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800222
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800223 segs = (struct cbfs_payload_segment *)output->data;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800224 doffset = (2 * sizeof(*segs));
225
226 /* Prepare code segment */
227 segs[0].type = PAYLOAD_SEGMENT_CODE;
228 segs[0].load_addr = htonll(loadaddress);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800229 segs[0].mem_len = htonl(input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800230 segs[0].offset = htonl(doffset);
231
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800232 compress(input->data, input->size, output->data + doffset, &len);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800233 segs[0].compression = htonl(algo);
234 segs[0].len = htonl(len);
235
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800236 if ((unsigned int)len >= input->size) {
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800237 WARN("Compressing data would make it bigger - disabled.\n");
238 segs[0].compression = 0;
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800239 segs[0].len = htonl(input->size);
240 memcpy(output->data + doffset, input->data, input->size);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800241 }
242
243 /* prepare entry point segment */
244 segs[1].type = PAYLOAD_SEGMENT_ENTRY;
245 segs[1].load_addr = htonll(entrypoint);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800246 output->size = doffset + ntohl(segs[0].len);
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800247
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800248 return 0;
Hung-Te Lin05dccae2013-01-28 15:04:30 +0800249}