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