Add a "locate" function cbfstool, which helps you find
[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                 printf("%-30s 0x%-8x %-12s %d\n",
169                        (const char *)(phys_to_virt(current) +
170                                       sizeof(struct cbfs_file)),
171                        current - phys_start, strfiletype(ntohl(thisfile->type)),
172                        length);
173                 current =
174                     ALIGN(current + ntohl(thisfile->len) +
175                           ntohl(thisfile->offset), align);
176         }
177 }
178
179 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
180 {
181         uint32_t current = phys_start;
182         while (current < phys_end) {
183                 if (!cbfs_file_header(current)) {
184                         current += align;
185                         continue;
186                 }
187                 struct cbfs_file *thisfile =
188                     (struct cbfs_file *)phys_to_virt(current);
189                 uint32_t length = ntohl(thisfile->len);
190
191                 dprintf("at %x, %x bytes\n", current, length);
192                 /* Is this a free chunk? */
193                 if ((thisfile->type == CBFS_COMPONENT_DELETED)
194                     || (thisfile->type == CBFS_COMPONENT_NULL)) {
195                         dprintf("null||deleted at %x, %x bytes\n", current,
196                                 length);
197                         /* if this is the right size, and if specified, the right location, use it */
198                         if ((contentsize <= length)
199                             && ((location == 0) || (current == location))) {
200                                 if (contentsize < length) {
201                                         dprintf
202                                             ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
203                                              length, contentsize,
204                                              ALIGN(current + contentsize,
205                                                    align),
206                                              length - contentsize);
207                                         uint32_t start =
208                                             ALIGN(current + contentsize, align);
209                                         uint32_t size =
210                                             current + ntohl(thisfile->offset)
211                                             + length - start - 16 -
212                                             sizeof(struct cbfs_file);
213                                         cbfs_create_empty_file(start, size);
214                                 }
215                                 dprintf("copying data\n");
216                                 memcpy(phys_to_virt(current), content,
217                                        contentsize);
218                                 return 0;
219                         }
220                         if (location != 0) {
221                                 /* CBFS has the constraint that the chain always moves up in memory. so once
222                                    we're past the place we seek, we don't need to look any further */
223                                 if (current > location) {
224                                         printf
225                                             ("the requested space is not available\n");
226                                         return 1;
227                                 }
228
229                                 /* Is the requested location inside the current chunk? */
230                                 if ((current < location)
231                                     && ((location + contentsize) <=
232                                         (current + length))) {
233                                         /* Split it up. In the next iteration the code will be at the right place. */
234                                         dprintf("split up. new length: %x\n",
235                                                 location - current -
236                                                 ntohl(thisfile->offset));
237                                         thisfile->len =
238                                             htonl(location - current -
239                                                   ntohl(thisfile->offset));
240                                         struct cbfs_file *nextfile =
241                                             cbfs_create_empty_file(location,
242                                                                    length -
243                                                                    (location -
244                                                                     current));
245                                 }
246                         }
247                 }
248                 current =
249                     ALIGN(current + ntohl(thisfile->len) +
250                           ntohl(thisfile->offset), align);
251         }
252         printf("Could not add the file to CBFS, it's probably too big.\n");
253         return 1;
254 }
255
256 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
257    the new location that points to the cbfs_file header */
258 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
259                        uint32_t type, uint32_t * location)
260 {
261         uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
262         uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
263         if ((location != 0) && (*location != 0)) {
264                 uint32_t offset = *location % align;
265                 /* If offset >= (headersize % align), we can stuff the header into the offset.
266                    Otherwise the header has to be aligned itself, and put before the offset data */
267                 if (offset >= (headersize % align)) {
268                         offset -= (headersize % align);
269                 } else {
270                         offset += align - (headersize % align);
271                 }
272                 headersize += offset;
273                 *location -= headersize;
274         }
275         void *newdata = malloc(*datasize + headersize);
276         if (!newdata) {
277                 printf("Could not get %d bytes for CBFS file.\n", *datasize +
278                        headersize);
279                 exit(1);
280         }
281         struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
282         strncpy(nextfile->magic, "LARCHIVE", 8);
283         nextfile->len = htonl(*datasize);
284         nextfile->type = htonl(type);
285         nextfile->checksum = 0; // FIXME?
286         nextfile->offset = htonl(headersize);
287         strcpy(newdata + sizeof(struct cbfs_file), filename);
288         memcpy(newdata + headersize, data, *datasize);
289         *datasize += headersize;
290         return newdata;
291 }
292
293 int create_cbfs_image(const char *romfile, uint32_t _romsize,
294                       const char *bootblock, uint32_t align)
295 {
296         romsize = _romsize;
297         unsigned char *romarea = malloc(romsize);
298         if (!romarea) {
299                 printf("Could not get %d bytes of memory for CBFS image.\n",
300                        romsize);
301                 exit(1);
302         }
303         memset(romarea, 0xff, romsize);
304
305         // Set up physical/virtual mapping
306         offset = romarea + romsize - 0x100000000ULL;
307
308         if (align == 0)
309                 align = 64;
310
311         uint32_t bootblocksize = 0;
312         loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
313         struct cbfs_header *master_header =
314             (struct cbfs_header *)(romarea + romsize - bootblocksize -
315                                    sizeof(struct cbfs_header));
316         master_header->magic = ntohl(0x4f524243);
317         master_header->version = ntohl(0x31313131);
318         master_header->romsize = htonl(romsize);
319         master_header->bootblocksize = htonl(bootblocksize);
320         master_header->align = htonl(align);
321         master_header->offset = htonl(0);
322         ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
323             virt_to_phys(master_header);
324
325         recalculate_rom_geometry(romarea);
326
327         struct cbfs_file *one_empty_file =
328             cbfs_create_empty_file((0 - romsize) & 0xffffffff,
329                                    romsize - bootblocksize -
330                                    sizeof(struct cbfs_header) -
331                                    sizeof(struct cbfs_file) - 16);
332
333         writerom(romfile, romarea, romsize);
334         return 0;
335 }
336
337 static int in_segment(int addr, int size, int gran)
338 {
339         return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
340 }
341
342 uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
343                             const char *filename, uint32_t alignment)
344 {
345         void *rom = loadrom(romfile);
346         int filename_size = strlen(filename);
347
348         int headersize =
349             sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
350                                              16) + sizeof(struct cbfs_stage);
351         int totalsize = headersize + filesize;
352
353         uint32_t current = phys_start;
354         while (current < phys_end) {
355                 if (!cbfs_file_header(current)) {
356                         current += align;
357                         continue;
358                 }
359                 struct cbfs_file *thisfile =
360                     (struct cbfs_file *)phys_to_virt(current);
361
362                 uint32_t top =
363                     current + ntohl(thisfile->len) + ntohl(thisfile->offset);
364                 if (((ntohl(thisfile->type) == 0x0)
365                      || (ntohl(thisfile->type) == 0xffffffff))
366                     && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
367                         totalsize)) {
368                         if (in_segment
369                             (current + headersize, filesize, alignment))
370                                 return current + headersize;
371                         if ((ALIGN(current, alignment) + filesize < top)
372                             && (ALIGN(current, alignment) - headersize >
373                                 current)
374                             && in_segment(ALIGN(current, alignment), filesize,
375                                           alignment))
376                                 return ALIGN(current, alignment);
377                         if ((ALIGN(current, alignment) + alignment + filesize <
378                              top)
379                             && (ALIGN(current, alignment) + alignment -
380                                 headersize > current)
381                             && in_segment(ALIGN(current, alignment) + alignment,
382                                           filesize, alignment))
383                                 return ALIGN(current, alignment) + alignment;
384                 }
385                 current =
386                     ALIGN(current + ntohl(thisfile->len) +
387                           ntohl(thisfile->offset), align);
388         }
389         return 0;
390 }