baa92ff2375629256959330773e43397067fa0af
[coreboot.git] / util / cbfstool / cbfs-mkpayload.c
1 /*
2  * cbfs-mkpayload
3  *
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5  *               2009 coresystems GmbH
6  *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
7  *
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
31 #include "common.h"
32 #include "cbfs.h"
33
34 int parse_elf_to_payload(unsigned char *input, unsigned char **output,
35                          comp_algo algo)
36 {
37         Elf32_Phdr *phdr;
38         Elf32_Ehdr *ehdr;
39         Elf32_Shdr *shdr;
40         char *header;
41         char *strtab;
42         unsigned char *sptr;
43         int headers;
44         int segments = 1;
45         int isize = 0, osize = 0;
46         int doffset = 0;
47         struct cbfs_payload_segment *segs;
48         int i;
49
50         if(!iself(input)){
51                 printf("Fatal error: the payload file is not in ELF format!\n");
52                 exit(1);
53         }
54
55
56         comp_func_ptr compress = compression_function(algo);
57         if (!compress)
58                 return -1;
59
60         ehdr = (Elf32_Ehdr *) input;
61         headers = ehdr->e_phnum;
62         header = (char *)ehdr;
63
64         phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
65         shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
66
67         strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
68
69         /* Count the number of headers - look for the .notes.pinfo
70          * section */
71
72         for (i = 0; i < ehdr->e_shnum; i++) {
73                 char *name;
74
75                 if (i == ehdr->e_shstrndx)
76                         continue;
77
78                 if (shdr[i].sh_size == 0)
79                         continue;
80
81                 name = (char *)(strtab + shdr[i].sh_name);
82
83                 if (!strcmp(name, ".note.pinfo")) {
84                         segments++;
85                         isize += (unsigned int)shdr[i].sh_size;
86                 }
87         }
88
89         /* Now, regular headers - we only care about PT_LOAD headers,
90          * because thats what we're actually going to load
91          */
92
93         for (i = 0; i < headers; i++) {
94                 if (phdr[i].p_type != PT_LOAD)
95                         continue;
96
97                 /* Empty segments are never interesting */
98                 if (phdr[i].p_memsz == 0)
99                         continue;
100
101                 isize += phdr[i].p_filesz;
102
103                 segments++;
104         }
105
106         /* Allocate a block of memory to store the data in */
107
108         sptr =
109             calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, 1);
110         doffset = (segments * sizeof(struct cbfs_payload_segment));
111
112         if (sptr == NULL)
113                 goto err;
114
115         segs = (struct cbfs_payload_segment *)sptr;
116         segments = 0;
117
118         for (i = 0; i < ehdr->e_shnum; i++) {
119                 char *name;
120
121                 if (i == ehdr->e_shstrndx)
122                         continue;
123
124                 if (shdr[i].sh_size == 0)
125                         continue;
126
127                 name = (char *)(strtab + shdr[i].sh_name);
128
129                 if (!strcmp(name, ".note.pinfo")) {
130                         segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
131                         segs[segments].load_addr = 0;
132                         segs[segments].len = (unsigned int)shdr[i].sh_size;
133                         segs[segments].offset = doffset;
134
135                         memcpy((unsigned long *)(sptr + doffset),
136                                &header[shdr[i].sh_offset], shdr[i].sh_size);
137
138                         doffset += segs[segments].len;
139                         osize += segs[segments].len;
140
141                         segments++;
142                 }
143         }
144
145         for (i = 0; i < headers; i++) {
146                 if (phdr[i].p_type != PT_LOAD)
147                         continue;
148
149                 if (phdr[i].p_memsz == 0)
150                         continue;
151
152                 if (phdr[i].p_filesz == 0) {
153                         segs[segments].type = PAYLOAD_SEGMENT_BSS;
154                         segs[segments].load_addr =
155                             (unsigned long long)htonl(phdr[i].p_paddr);
156                         segs[segments].mem_len =
157                             (unsigned int)htonl(phdr[i].p_memsz);
158                         segs[segments].offset = htonl(doffset);
159
160                         segments++;
161                         continue;
162                 }
163
164                 segs[segments].type = PAYLOAD_SEGMENT_DATA;
165                 segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
166                 segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
167                 segs[segments].compression = htonl(algo);
168                 segs[segments].offset = htonl(doffset);
169
170                 int len;
171                 compress((char *)&header[phdr[i].p_offset],
172                          phdr[i].p_filesz, (char *)(sptr + doffset), &len);
173                 segs[segments].len = htonl(len);
174
175                 /* If the compressed section is larger, then use the
176                    original stuff */
177
178                 if ((unsigned int)len > phdr[i].p_filesz) {
179                         segs[segments].compression = 0;
180                         segs[segments].len = htonl(phdr[i].p_filesz);
181
182                         memcpy((char *)(sptr + doffset),
183                                &header[phdr[i].p_offset], phdr[i].p_filesz);
184                 }
185
186                 doffset += ntohl(segs[segments].len);
187                 osize += ntohl(segs[segments].len);
188
189                 segments++;
190         }
191
192         segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
193         segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
194
195         *output = sptr;
196
197         return (segments * sizeof(struct cbfs_payload_segment)) + osize;
198
199       err:
200         return -1;
201 }