blob: f8cc1616b0449e21fab0abd9e8e6863e22738a93 [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>
25#include <unistd.h>
26#include "elf.h"
27#include <fcntl.h>
28#include <getopt.h>
29#include <sys/stat.h>
30#include <arpa/inet.h>
31
32#include "common.h"
Patrick Georgib7b56dd82009-09-14 13:29:27 +000033#include "cbfs.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000034
Patrick Georgib7b56dd82009-09-14 13:29:27 +000035int parse_elf_to_payload(unsigned char *input, unsigned char **output,
36 comp_algo algo)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000037{
38 Elf32_Phdr *phdr;
39 Elf32_Ehdr *ehdr;
40 Elf32_Shdr *shdr;
41 char *header;
42 char *strtab;
43 unsigned char *sptr;
44 int headers;
45 int segments = 1;
46 int isize = 0, osize = 0;
47 int doffset = 0;
Peter Stuge1d862de2009-04-14 00:08:34 +000048 struct cbfs_payload_segment *segs;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000049 int i;
50
Patrick Georgib7b56dd82009-09-14 13:29:27 +000051 comp_func_ptr compress = compression_function(algo);
52 if (!compress)
53 return -1;
54
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000055 ehdr = (Elf32_Ehdr *) input;
56 headers = ehdr->e_phnum;
57 header = (char *)ehdr;
58
59 phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
60 shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
61
62 strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
63
64 /* Count the number of headers - look for the .notes.pinfo
65 * section */
66
67 for (i = 0; i < ehdr->e_shnum; i++) {
68 char *name;
69
70 if (i == ehdr->e_shstrndx)
71 continue;
72
73 if (shdr[i].sh_size == 0)
74 continue;
75
76 name = (char *)(strtab + shdr[i].sh_name);
77
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000078 if (!strcmp(name, ".note.pinfo")) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000079 segments++;
Ronald G. Minnich5a1af7b2009-09-17 15:35:08 +000080 isize += (unsigned int)shdr[i].sh_size;
81 }
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000082 }
83
84 /* Now, regular headers - we only care about PT_LOAD headers,
85 * because thats what we're actually going to load
86 */
87
88 for (i = 0; i < headers; i++) {
89 if (phdr[i].p_type != PT_LOAD)
90 continue;
91
92 /* Empty segments are never interesting */
93 if (phdr[i].p_memsz == 0)
94 continue;
95
96 isize += phdr[i].p_filesz;
97
98 segments++;
99 }
100
101 /* Allocate a block of memory to store the data in */
102
103 sptr =
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000104 calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, 1);
Peter Stuge1d862de2009-04-14 00:08:34 +0000105 doffset = (segments * sizeof(struct cbfs_payload_segment));
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000106
107 if (sptr == NULL)
108 goto err;
109
Peter Stuge1d862de2009-04-14 00:08:34 +0000110 segs = (struct cbfs_payload_segment *)sptr;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000111 segments = 0;
112
113 for (i = 0; i < ehdr->e_shnum; i++) {
114 char *name;
115
116 if (i == ehdr->e_shstrndx)
117 continue;
118
119 if (shdr[i].sh_size == 0)
120 continue;
121
122 name = (char *)(strtab + shdr[i].sh_name);
123
124 if (!strcmp(name, ".note.pinfo")) {
125 segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
126 segs[segments].load_addr = 0;
127 segs[segments].len = (unsigned int)shdr[i].sh_size;
128 segs[segments].offset = doffset;
129
130 memcpy((unsigned long *)(sptr + doffset),
131 &header[shdr[i].sh_offset], shdr[i].sh_size);
132
133 doffset += segs[segments].len;
134 osize += segs[segments].len;
135
136 segments++;
137 }
138 }
139
140 for (i = 0; i < headers; i++) {
141 if (phdr[i].p_type != PT_LOAD)
142 continue;
143
144 if (phdr[i].p_memsz == 0)
145 continue;
146
147 if (phdr[i].p_filesz == 0) {
148 segs[segments].type = PAYLOAD_SEGMENT_BSS;
149 segs[segments].load_addr =
150 (unsigned long long)htonl(phdr[i].p_paddr);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000151 segs[segments].mem_len =
152 (unsigned int)htonl(phdr[i].p_memsz);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000153 segs[segments].offset = htonl(doffset);
154
155 segments++;
156 continue;
157 }
158
159 segs[segments].type = PAYLOAD_SEGMENT_DATA;
160 segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
161 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
162 segs[segments].compression = htonl(algo);
163 segs[segments].offset = htonl(doffset);
164
165 int len;
166 compress((char *)&header[phdr[i].p_offset],
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000167 phdr[i].p_filesz, (char *)(sptr + doffset), &len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000168 segs[segments].len = htonl(len);
169
170 /* If the compressed section is larger, then use the
171 original stuff */
172
Stefan Reinauer14ad50e2009-04-04 22:18:26 +0000173 if ((unsigned int)len > phdr[i].p_filesz) {
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000174 segs[segments].compression = 0;
175 segs[segments].len = htonl(phdr[i].p_filesz);
176
177 memcpy((char *)(sptr + doffset),
178 &header[phdr[i].p_offset], phdr[i].p_filesz);
179 }
180
181 doffset += ntohl(segs[segments].len);
182 osize += ntohl(segs[segments].len);
183
184 segments++;
185 }
186
187 segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
188 segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
189
190 *output = sptr;
191
Peter Stuge1d862de2009-04-14 00:08:34 +0000192 return (segments * sizeof(struct cbfs_payload_segment)) + osize;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000193
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000194 err:
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +0000195 return -1;
196}