8478c5a492cc69005dc6acdc593db6fc9f4609ef
[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 <libgen.h>
25 #include "common.h"
26 #include "cbfs.h"
27 #include "elf.h"
28
29 #define dprintf
30
31 uint32_t getfilesize(const char *filename)
32 {
33         uint32_t size;
34         FILE *file = fopen(filename, "rb");
35         fseek(file, 0, SEEK_END);
36         size = ftell(file);
37         fclose(file);
38         return size;
39 }
40
41 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
42                int place)
43 {
44         FILE *file = fopen(filename, "rb");
45         if (file == NULL)
46                 return NULL;
47         fseek(file, 0, SEEK_END);
48         *romsize_p = ftell(file);
49         fseek(file, 0, SEEK_SET);
50         if (!content) {
51                 content = malloc(*romsize_p);
52                 if (!content) {
53                         printf("Could not get %d bytes for file %s\n",
54                                *romsize_p, filename);
55                         exit(1);
56                 }
57         } else if (place == SEEK_END)
58                 content -= *romsize_p;
59
60         if (!fread(content, *romsize_p, 1, file)) {
61                 printf("failed to read %s\n", filename);
62                 return NULL;
63         }
64         fclose(file);
65         return content;
66 }
67
68 struct cbfs_header *master_header;
69 uint32_t phys_start, phys_end, align, romsize;
70 void *offset;
71
72 void recalculate_rom_geometry(void *romarea)
73 {
74         offset = romarea + romsize - 0x100000000ULL;
75         master_header = (struct cbfs_header *)
76             phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
77         phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
78         phys_end =
79             (0 - ntohl(master_header->bootblocksize) -
80              sizeof(struct cbfs_header)) & 0xffffffff;
81         align = ntohl(master_header->align);
82 }
83
84 void *loadrom(const char *filename)
85 {
86         void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
87         if (romarea == NULL)
88                 return NULL;
89         recalculate_rom_geometry(romarea);
90         return romarea;
91 }
92
93 int writerom(const char *filename, void *start, uint32_t size)
94 {
95         FILE *file = fopen(filename, "wb");
96         if (!file) {
97                 fprintf(stderr, "Could not open '%s' for writing: ", filename);
98                 perror("");
99                 return 1;
100         }
101
102         if (fwrite(start, size, 1, file) != 1) {
103                 fprintf(stderr, "Could not write to '%s': ", filename);
104                 perror("");
105                 return 1;
106         }
107
108         fclose(file);
109         return 0;
110 }
111
112 int cbfs_file_header(uint32_t physaddr)
113 {
114         /* maybe improve this test */
115         return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
116 }
117
118 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
119 {
120         struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
121         strncpy(nextfile->magic, "LARCHIVE", 8);
122         nextfile->len = htonl(size);
123         nextfile->type = htonl(0xffffffff);
124         nextfile->checksum = 0; // FIXME?
125         nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
126         memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
127         return nextfile;
128 }
129
130 int iself(unsigned char *input)
131 {
132         Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
133         return !memcmp(ehdr->e_ident, ELFMAG, 4);
134 }
135
136 struct filetypes_t {
137         uint32_t type;
138         const char *name;
139 } filetypes[] = {
140         {CBFS_COMPONENT_STAGE, "stage"},
141         {CBFS_COMPONENT_PAYLOAD, "payload"},
142         {CBFS_COMPONENT_OPTIONROM, "optionrom"},
143         {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
144         {CBFS_COMPONENT_RAW, "raw"},
145         {CBFS_COMPONENT_VSA, "vsa"},
146         {CBFS_COMPONENT_MBI, "mbi"},
147         {CBFS_COMPONENT_MICROCODE, "microcode"},
148         {CBFS_COMPONENT_DELETED, "deleted"},
149         {CBFS_COMPONENT_NULL, "null"}
150 };
151
152 void print_supported_filetypes(void)
153 {
154         int i, number = ARRAY_SIZE(filetypes);
155
156         for (i=0; i<number; i++) {
157                 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
158                 if ((i%8) == 7)
159                         printf("\n");
160         }
161 }
162
163 const char *strfiletype(uint32_t number)
164 {
165         int i;
166         for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
167                 if (filetypes[i].type == number)
168                         return filetypes[i].name;
169         return "unknown";
170 }
171
172 uint64_t intfiletype(const char *name)
173 {
174         int i;
175         for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
176                 if (strcmp(filetypes[i].name, name) == 0)
177                         return filetypes[i].type;
178         return -1;
179 }
180
181 void print_cbfs_directory(const char *filename)
182 {
183         printf
184             ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
185              basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
186              romsize, ntohl(master_header->offset), align);
187         printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
188         uint32_t current = phys_start;
189         while (current < phys_end) {
190                 if (!cbfs_file_header(current)) {
191                         current += align;
192                         continue;
193                 }
194                 struct cbfs_file *thisfile =
195                     (struct cbfs_file *)phys_to_virt(current);
196                 uint32_t length = ntohl(thisfile->len);
197                 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
198                 if (strlen(fname) == 0)
199                         fname = "(empty)";
200
201                 printf("%-30s 0x%-8x %-12s %d\n", fname,
202                        current - phys_start, strfiletype(ntohl(thisfile->type)),
203                        length);
204                 current =
205                     ALIGN(current + ntohl(thisfile->len) +
206                           ntohl(thisfile->offset), align);
207         }
208 }
209
210 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
211 {
212         uint32_t current = phys_start;
213         while (current < phys_end) {
214                 if (!cbfs_file_header(current)) {
215                         current += align;
216                         continue;
217                 }
218                 struct cbfs_file *thisfile =
219                     (struct cbfs_file *)phys_to_virt(current);
220                 uint32_t length = ntohl(thisfile->len);
221
222                 dprintf("at %x, %x bytes\n", current, length);
223                 /* Is this a free chunk? */
224                 if ((thisfile->type == CBFS_COMPONENT_DELETED)
225                     || (thisfile->type == CBFS_COMPONENT_NULL)) {
226                         dprintf("null||deleted at %x, %x bytes\n", current,
227                                 length);
228                         /* if this is the right size, and if specified, the right location, use it */
229                         if ((contentsize <= length)
230                             && ((location == 0) || (current == location))) {
231                                 if (contentsize < length) {
232                                         dprintf
233                                             ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
234                                              length, contentsize,
235                                              ALIGN(current + contentsize,
236                                                    align),
237                                              length - contentsize);
238                                         uint32_t start =
239                                             ALIGN(current + contentsize, align);
240                                         uint32_t size =
241                                             current + ntohl(thisfile->offset)
242                                             + length - start - 16 -
243                                             sizeof(struct cbfs_file);
244                                         cbfs_create_empty_file(start, size);
245                                 }
246                                 dprintf("copying data\n");
247                                 memcpy(phys_to_virt(current), content,
248                                        contentsize);
249                                 return 0;
250                         }
251                         if (location != 0) {
252                                 /* CBFS has the constraint that the chain always moves up in memory. so once
253                                    we're past the place we seek, we don't need to look any further */
254                                 if (current > location) {
255                                         printf
256                                             ("the requested space is not available\n");
257                                         return 1;
258                                 }
259
260                                 /* Is the requested location inside the current chunk? */
261                                 if ((current < location)
262                                     && ((location + contentsize) <=
263                                         (current + length))) {
264                                         /* Split it up. In the next iteration the code will be at the right place. */
265                                         dprintf("split up. new length: %x\n",
266                                                 location - current -
267                                                 ntohl(thisfile->offset));
268                                         thisfile->len =
269                                             htonl(location - current -
270                                                   ntohl(thisfile->offset));
271                                         struct cbfs_file *nextfile =
272                                             cbfs_create_empty_file(location,
273                                                                    length -
274                                                                    (location -
275                                                                     current));
276                                 }
277                         }
278                 }
279                 current =
280                     ALIGN(current + ntohl(thisfile->len) +
281                           ntohl(thisfile->offset), align);
282         }
283         printf("Could not add the file to CBFS, it's probably too big.\n");
284         printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
285         return 1;
286 }
287
288 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
289    the new location that points to the cbfs_file header */
290 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
291                        uint32_t type, uint32_t * location)
292 {
293         uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
294         uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
295         if ((location != 0) && (*location != 0)) {
296                 uint32_t offset = *location % align;
297                 /* If offset >= (headersize % align), we can stuff the header into the offset.
298                    Otherwise the header has to be aligned itself, and put before the offset data */
299                 if (offset >= (headersize % align)) {
300                         offset -= (headersize % align);
301                 } else {
302                         offset += align - (headersize % align);
303                 }
304                 headersize += offset;
305                 *location -= headersize;
306         }
307         void *newdata = malloc(*datasize + headersize);
308         if (!newdata) {
309                 printf("Could not get %d bytes for CBFS file.\n", *datasize +
310                        headersize);
311                 exit(1);
312         }
313         memset(newdata, 0xff, *datasize + headersize);
314         struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
315         strncpy(nextfile->magic, "LARCHIVE", 8);
316         nextfile->len = htonl(*datasize);
317         nextfile->type = htonl(type);
318         nextfile->checksum = 0; // FIXME?
319         nextfile->offset = htonl(headersize);
320         strcpy(newdata + sizeof(struct cbfs_file), filename);
321         memcpy(newdata + headersize, data, *datasize);
322         *datasize += headersize;
323         return newdata;
324 }
325
326 int create_cbfs_image(const char *romfile, uint32_t _romsize,
327                       const char *bootblock, uint32_t align)
328 {
329         romsize = _romsize;
330         unsigned char *romarea = malloc(romsize);
331         if (!romarea) {
332                 printf("Could not get %d bytes of memory for CBFS image.\n",
333                        romsize);
334                 exit(1);
335         }
336         memset(romarea, 0xff, romsize);
337
338         // Set up physical/virtual mapping
339         offset = romarea + romsize - 0x100000000ULL;
340
341         if (align == 0)
342                 align = 64;
343
344         uint32_t bootblocksize = 0;
345         loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
346         struct cbfs_header *master_header =
347             (struct cbfs_header *)(romarea + romsize - bootblocksize -
348                                    sizeof(struct cbfs_header));
349         master_header->magic = ntohl(0x4f524243);
350         master_header->version = ntohl(0x31313131);
351         master_header->romsize = htonl(romsize);
352         master_header->bootblocksize = htonl(bootblocksize);
353         master_header->align = htonl(align);
354         master_header->offset = htonl(0);
355         ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
356             virt_to_phys(master_header);
357
358         recalculate_rom_geometry(romarea);
359
360         struct cbfs_file *one_empty_file =
361             cbfs_create_empty_file((0 - romsize) & 0xffffffff,
362                                    romsize - bootblocksize -
363                                    sizeof(struct cbfs_header) -
364                                    sizeof(struct cbfs_file) - 16);
365
366         writerom(romfile, romarea, romsize);
367         return 0;
368 }
369
370 static int in_segment(int addr, int size, int gran)
371 {
372         return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
373 }
374
375 uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
376                             const char *filename, uint32_t alignment)
377 {
378         void *rom = loadrom(romfile);
379         int filename_size = strlen(filename);
380
381         int headersize =
382             sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
383                                              16) + sizeof(struct cbfs_stage);
384         int totalsize = headersize + filesize;
385
386         uint32_t current = phys_start;
387         while (current < phys_end) {
388                 if (!cbfs_file_header(current)) {
389                         current += align;
390                         continue;
391                 }
392                 struct cbfs_file *thisfile =
393                     (struct cbfs_file *)phys_to_virt(current);
394
395                 uint32_t top =
396                     current + ntohl(thisfile->len) + ntohl(thisfile->offset);
397                 if (((ntohl(thisfile->type) == 0x0)
398                      || (ntohl(thisfile->type) == 0xffffffff))
399                     && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
400                         totalsize)) {
401                         if (in_segment
402                             (current + headersize, filesize, alignment))
403                                 return current + headersize;
404                         if ((ALIGN(current, alignment) + filesize < top)
405                             && (ALIGN(current, alignment) - headersize >
406                                 current)
407                             && in_segment(ALIGN(current, alignment), filesize,
408                                           alignment))
409                                 return ALIGN(current, alignment);
410                         if ((ALIGN(current, alignment) + alignment + filesize <
411                              top)
412                             && (ALIGN(current, alignment) + alignment -
413                                 headersize > current)
414                             && in_segment(ALIGN(current, alignment) + alignment,
415                                           filesize, alignment))
416                                 return ALIGN(current, alignment) + alignment;
417                 }
418                 current =
419                     ALIGN(current + ntohl(thisfile->len) +
420                           ntohl(thisfile->offset), align);
421         }
422         return 0;
423 }