New cbfstool. Works without mmap or fork/exec and
[coreboot.git] / util / cbfstool / common.c
1 /*
2  * common utility functions for cbfstool
3  *
4  * Copyright (C) 2009 coresystems GmbH
5  *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "common.h"
25 #include "cbfs.h"
26 #include "elf.h"
27
28 #define dprintf
29
30 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
31                int place)
32 {
33         FILE *file = fopen(filename, "rb");
34         fseek(file, 0, SEEK_END);
35         *romsize_p = ftell(file);
36         fseek(file, 0, SEEK_SET);
37         if (!content)
38                 content = malloc(*romsize_p);
39         else if (place == SEEK_END)
40                 content -= *romsize_p;
41         if (!fread(content, *romsize_p, 1, file)) {
42                 printf("failed to read %s\n", filename);
43                 return NULL;
44         }
45         fclose(file);
46         return content;
47 }
48
49 struct cbfs_header *master_header;
50 uint32_t phys_start, phys_end, align, romsize;
51 void *offset;
52
53 void recalculate_rom_geometry(void *romarea)
54 {
55         offset = romarea + romsize - 0x100000000ULL;
56         master_header = (struct cbfs_header *)
57             phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
58         phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
59         phys_end =
60             (0 - ntohl(master_header->bootblocksize) -
61              sizeof(struct cbfs_header)) & 0xffffffff;
62         align = ntohl(master_header->align);
63 }
64
65 void *loadrom(const char *filename)
66 {
67         void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
68         recalculate_rom_geometry(romarea);
69         return romarea;
70 }
71
72 void writerom(const char *filename, void *start, uint32_t size)
73 {
74         FILE *file = fopen(filename, "wb");
75         fwrite(start, size, 1, file);
76         fclose(file);
77 }
78
79 int cbfs_file_header(uint32_t physaddr)
80 {
81         /* maybe improve this test */
82         return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
83 }
84
85 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
86 {
87         struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
88         strncpy(nextfile->magic, "LARCHIVE", 8);
89         nextfile->len = htonl(size);
90         nextfile->type = htonl(0xffffffff);
91         nextfile->checksum = 0; // FIXME?
92         nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
93         memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
94         return nextfile;
95 }
96
97 int iself(unsigned char *input)
98 {
99         Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
100         return !memcmp(ehdr->e_ident, ELFMAG, 4);
101 }
102
103 struct filetypes_t {
104         uint32_t type;
105         const char *name;
106 } filetypes[] = {
107         {CBFS_COMPONENT_STAGE, "stage"},
108         {CBFS_COMPONENT_PAYLOAD, "payload"},
109         {CBFS_COMPONENT_OPTIONROM, "optionrom"},
110         {CBFS_COMPONENT_DELETED, "deleted"},
111         {CBFS_COMPONENT_NULL, "null"}
112 };
113
114 const char *strfiletype(uint32_t number)
115 {
116         int i;
117         for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
118                 if (filetypes[i].type == number)
119                         return filetypes[i].name;
120         return "unknown";
121 }
122
123 uint64_t intfiletype(const char *name)
124 {
125         int i;
126         for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
127                 if (strcmp(filetypes[i].name, name) == 0)
128                         return filetypes[i].type;
129         return -1;
130 }
131
132 void print_cbfs_directory(const char *filename)
133 {
134         printf
135             ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
136              filename, romsize / 1024, ntohl(master_header->bootblocksize),
137              romsize, ntohl(master_header->offset), align);
138         printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
139         uint32_t current = phys_start;
140         while (current < phys_end) {
141                 if (!cbfs_file_header(current)) {
142                         current += align;
143                         continue;
144                 }
145                 struct cbfs_file *thisfile =
146                     (struct cbfs_file *)phys_to_virt(current);
147                 uint32_t length = ntohl(thisfile->len);
148                 printf("%-30s 0x%-8x %-12s %d\n",
149                        (const char *)(phys_to_virt(current) +
150                                       sizeof(struct cbfs_file)),
151                        current - phys_start, strfiletype(ntohl(thisfile->type)),
152                        length);
153                 current =
154                     ALIGN(current + ntohl(thisfile->len) +
155                           ntohl(thisfile->offset), align);
156         }
157 }
158
159 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
160 {
161         uint32_t current = phys_start;
162         while (current < phys_end) {
163                 if (!cbfs_file_header(current)) {
164                         current += align;
165                         continue;
166                 }
167                 struct cbfs_file *thisfile =
168                     (struct cbfs_file *)phys_to_virt(current);
169                 uint32_t length = ntohl(thisfile->len);
170
171                 dprintf("at %x, %x bytes\n", current, length);
172                 /* Is this a free chunk? */
173                 if ((thisfile->type == CBFS_COMPONENT_DELETED)
174                     || (thisfile->type == CBFS_COMPONENT_NULL)) {
175                         dprintf("null||deleted at %x, %x bytes\n", current,
176                                 length);
177                         /* if this is the right size, and if specified, the right location, use it */
178                         if ((contentsize <= length)
179                             && ((location == 0) || (current == location))) {
180                                 if (contentsize < length) {
181                                         dprintf
182                                             ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
183                                              length, contentsize,
184                                              ALIGN(current + contentsize,
185                                                    align),
186                                              length - contentsize);
187                                         uint32_t start =
188                                             ALIGN(current + contentsize, align);
189                                         uint32_t size =
190                                             current + ntohl(thisfile->offset)
191                                             + length - start - 16 -
192                                             sizeof(struct cbfs_file);
193                                         cbfs_create_empty_file(start, size);
194                                 }
195                                 dprintf("copying data\n");
196                                 memcpy(phys_to_virt(current), content,
197                                        contentsize);
198                                 break;
199                         }
200                         if (location == 0)
201                                 continue;
202
203                         /* CBFS has the constraint that the chain always moves up in memory. so once
204                            we're past the place we seek, we don't need to look any further */
205                         if (current > location) {
206                                 printf
207                                     ("the requested space is not available\n");
208                                 return 1;
209                         }
210
211                         /* Is the requested location inside the current chunk? */
212                         if ((current < location)
213                             && ((location + contentsize) <= (current + length))) {
214                                 /* Split it up. In the next iteration the code will be at the right place. */
215                                 dprintf("split up. new length: %x\n",
216                                         location - current -
217                                         ntohl(thisfile->offset));
218                                 thisfile->len =
219                                     htonl(location - current -
220                                           ntohl(thisfile->offset));
221                                 struct cbfs_file *nextfile =
222                                     cbfs_create_empty_file(location,
223                                                            length - (location -
224                                                                      current));
225                         }
226                 }
227                 current =
228                     ALIGN(current + ntohl(thisfile->len) +
229                           ntohl(thisfile->offset), align);
230         }
231         return 0;
232 }
233
234 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
235    the new location that points to the cbfs_file header */
236 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
237                        uint32_t type, uint32_t * location)
238 {
239         uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
240         uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
241         if ((location != 0) && (*location != 0)) {
242                 uint32_t offset = *location % align;
243                 /* If offset >= (headersize % align), we can stuff the header into the offset.
244                    Otherwise the header has to be aligned itself, and put before the offset data */
245                 if (offset >= (headersize % align)) {
246                         offset -= (headersize % align);
247                 } else {
248                         offset += align - (headersize % align);
249                 }
250                 headersize += offset;
251                 *location -= headersize;
252         }
253         void *newdata = malloc(*datasize + headersize);
254         struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
255         strncpy(nextfile->magic, "LARCHIVE", 8);
256         nextfile->len = htonl(*datasize);
257         nextfile->type = htonl(type);
258         nextfile->checksum = 0; // FIXME?
259         nextfile->offset = htonl(headersize);
260         strcpy(newdata + sizeof(struct cbfs_file), filename);
261         memcpy(newdata + headersize, data, *datasize);
262         *datasize += headersize;
263         return newdata;
264 }
265
266 int create_cbfs_image(const char *romfile, uint32_t _romsize,
267                       const char *bootblock, uint32_t align)
268 {
269         romsize = _romsize;
270         unsigned char *romarea = malloc(romsize);
271         memset(romarea, 0xff, romsize);
272         recalculate_rom_geometry(romarea);
273
274         if (align == 0)
275                 align = 64;
276
277         uint32_t bootblocksize = 0;
278         loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
279         struct cbfs_header *master_header =
280             (struct cbfs_header *)(romarea + romsize - bootblocksize -
281                                    sizeof(struct cbfs_header));
282         master_header->magic = ntohl(0x4f524243);
283         master_header->version = ntohl(0x31313131);
284         master_header->romsize = htonl(romsize);
285         master_header->bootblocksize = htonl(bootblocksize);
286         master_header->align = htonl(align);
287         master_header->offset = htonl(0);
288         ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
289             virt_to_phys(master_header);
290         struct cbfs_file *one_empty_file =
291             cbfs_create_empty_file((0 - romsize) & 0xffffffff,
292                                    romsize - bootblocksize -
293                                    sizeof(struct cbfs_header) -
294                                    sizeof(struct cbfs_file) - 16);
295
296         writerom(romfile, romarea, romsize);
297         return 0;
298 }