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