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