blob: 49da7aea6c8415767b6284e1b6f4b66d09d4d10b [file] [log] [blame]
Patrick Georgib7b56dd82009-09-14 13:29:27 +00001/*
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>
Uwe Hermann942a40d2010-02-10 19:52:35 +000024#include <libgen.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000025#include "common.h"
26#include "cbfs.h"
27#include "elf.h"
28
Stefan Reinauera1e48242011-10-21 14:24:57 -070029#define dprintf(x...)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000030
Patrick Georgi0da38dd2009-11-09 17:18:02 +000031uint32_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
Patrick Georgib7b56dd82009-09-14 13:29:27 +000041void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
42 int place)
43{
44 FILE *file = fopen(filename, "rb");
Patrick Georgi45d8a832009-09-15 08:21:46 +000045 if (file == NULL)
46 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000047 fseek(file, 0, SEEK_END);
48 *romsize_p = ftell(file);
49 fseek(file, 0, SEEK_SET);
Stefan Reinauer853270a2009-09-22 15:55:01 +000050 if (!content) {
Patrick Georgib7b56dd82009-09-14 13:29:27 +000051 content = malloc(*romsize_p);
Stefan Reinauer853270a2009-09-22 15:55:01 +000052 if (!content) {
53 printf("Could not get %d bytes for file %s\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +000054 *romsize_p, filename);
Stefan Reinauer853270a2009-09-22 15:55:01 +000055 exit(1);
56 }
57 } else if (place == SEEK_END)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000058 content -= *romsize_p;
Stefan Reinauer853270a2009-09-22 15:55:01 +000059
Patrick Georgib7b56dd82009-09-14 13:29:27 +000060 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
68struct cbfs_header *master_header;
69uint32_t phys_start, phys_end, align, romsize;
70void *offset;
71
72void 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
84void *loadrom(const char *filename)
85{
86 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
Patrick Georgi45d8a832009-09-15 08:21:46 +000087 if (romarea == NULL)
88 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000089 recalculate_rom_geometry(romarea);
90 return romarea;
91}
92
Stefan Reinauer9bb043852010-06-24 13:37:59 +000093int writerom(const char *filename, void *start, uint32_t size)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000094{
95 FILE *file = fopen(filename, "wb");
Stefan Reinauer9bb043852010-06-24 13:37:59 +000096 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
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000108 fclose(file);
Stefan Reinauer9bb043852010-06-24 13:37:59 +0000109 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000110}
111
112int cbfs_file_header(uint32_t physaddr)
113{
114 /* maybe improve this test */
115 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
116}
117
118struct 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);
Stefan Reinauera1e48242011-10-21 14:24:57 -0700121 strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000122 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
130int iself(unsigned char *input)
131{
132 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
133 return !memcmp(ehdr->e_ident, ELFMAG, 4);
134}
135
136struct 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"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000143 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
144 {CBFS_COMPONENT_RAW, "raw"},
145 {CBFS_COMPONENT_VSA, "vsa"},
146 {CBFS_COMPONENT_MBI, "mbi"},
147 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000148 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000149 {CBFS_COMPONENT_DELETED, "deleted"},
150 {CBFS_COMPONENT_NULL, "null"}
151};
152
Stefan Reinauer07040582010-04-24 21:24:06 +0000153void print_supported_filetypes(void)
154{
155 int i, number = ARRAY_SIZE(filetypes);
156
157 for (i=0; i<number; i++) {
158 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
159 if ((i%8) == 7)
160 printf("\n");
161 }
162}
163
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000164const char *strfiletype(uint32_t number)
165{
166 int i;
167 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
168 if (filetypes[i].type == number)
169 return filetypes[i].name;
170 return "unknown";
171}
172
173uint64_t intfiletype(const char *name)
174{
175 int i;
176 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
177 if (strcmp(filetypes[i].name, name) == 0)
178 return filetypes[i].type;
179 return -1;
180}
181
182void print_cbfs_directory(const char *filename)
183{
184 printf
185 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
Uwe Hermann942a40d2010-02-10 19:52:35 +0000186 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000187 romsize, ntohl(master_header->offset), align);
188 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
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);
Maciej Pijankaf44eb782010-01-07 21:37:18 +0000198 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000199 if (strlen(fname) == 0)
Maciej Pijankaf44eb782010-01-07 21:37:18 +0000200 fname = "(empty)";
201
Stefan Reinauer14e22772010-04-27 06:56:47 +0000202 printf("%-30s 0x%-8x %-12s %d\n", fname,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000203 current - phys_start, strfiletype(ntohl(thisfile->type)),
204 length);
205 current =
206 ALIGN(current + ntohl(thisfile->len) +
207 ntohl(thisfile->offset), align);
208 }
209}
210
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000211int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath)
212{
213 // Identify the coreboot image.
214 printf(
215 "%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
216 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
217 romsize, ntohl(master_header->offset), align);
218
219 FILE *outfile = NULL;
220 uint32_t current = phys_start;
221 while (current < phys_end) {
222 if (!cbfs_file_header(current)) {
223 current += align;
224 continue;
225 }
226
227 // Locate the file start struct
228 struct cbfs_file *thisfile =
229 (struct cbfs_file *)phys_to_virt(current);
230 // And its length
231 uint32_t length = ntohl(thisfile->len);
232 // Locate the file name
233 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
Stefan Reinauera1e48242011-10-21 14:24:57 -0700234
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000235 // It's not the file we are looking for..
236 if (strcmp(fname, payloadname) != 0)
237 {
238 current =
239 ALIGN(current + ntohl(thisfile->len) +
240 ntohl(thisfile->offset), align);
241 continue;
242 }
243
244 // Else, it's our file.
Peter Stuge441426b2011-01-17 05:08:32 +0000245 printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname,
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000246 current - phys_start, strfiletype(ntohl(thisfile->type)),
247 length);
248
249 // If we are not dumping to stdout, open the out file.
250 outfile = fopen(outpath, "wb");
251 if (!outfile)
252 {
253 printf("Could not open the file %s for writing. Aborting.\n", outpath);
254 return 1;
255 }
256
257 if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
258 {
259 printf("Warning: only 'raw' files are safe to extract.\n");
260 }
261
262 fwrite(((char *)thisfile)
263 + ntohl(thisfile->offset), length, 1, outfile);
264
265 fclose(outfile);
Peter Stuge441426b2011-01-17 05:08:32 +0000266 printf("Successfully dumped the file.\n");
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000267
268 // We'll only dump one file.
269 return 0;
270 }
Stefan Reinauera1e48242011-10-21 14:24:57 -0700271 printf("File %s not found.\n", payloadname);
272 return 1;
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000273}
274
275
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000276int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
277{
278 uint32_t current = phys_start;
279 while (current < phys_end) {
280 if (!cbfs_file_header(current)) {
281 current += align;
282 continue;
283 }
284 struct cbfs_file *thisfile =
285 (struct cbfs_file *)phys_to_virt(current);
286 uint32_t length = ntohl(thisfile->len);
287
288 dprintf("at %x, %x bytes\n", current, length);
289 /* Is this a free chunk? */
290 if ((thisfile->type == CBFS_COMPONENT_DELETED)
291 || (thisfile->type == CBFS_COMPONENT_NULL)) {
292 dprintf("null||deleted at %x, %x bytes\n", current,
293 length);
294 /* if this is the right size, and if specified, the right location, use it */
295 if ((contentsize <= length)
296 && ((location == 0) || (current == location))) {
297 if (contentsize < length) {
298 dprintf
299 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
300 length, contentsize,
301 ALIGN(current + contentsize,
302 align),
303 length - contentsize);
304 uint32_t start =
305 ALIGN(current + contentsize, align);
306 uint32_t size =
307 current + ntohl(thisfile->offset)
308 + length - start - 16 -
309 sizeof(struct cbfs_file);
310 cbfs_create_empty_file(start, size);
311 }
312 dprintf("copying data\n");
313 memcpy(phys_to_virt(current), content,
314 contentsize);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000315 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000316 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000317 if (location != 0) {
318 /* CBFS has the constraint that the chain always moves up in memory. so once
319 we're past the place we seek, we don't need to look any further */
320 if (current > location) {
321 printf
322 ("the requested space is not available\n");
323 return 1;
324 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000325
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000326 /* Is the requested location inside the current chunk? */
327 if ((current < location)
328 && ((location + contentsize) <=
329 (current + length))) {
330 /* Split it up. In the next iteration the code will be at the right place. */
331 dprintf("split up. new length: %x\n",
332 location - current -
333 ntohl(thisfile->offset));
334 thisfile->len =
335 htonl(location - current -
336 ntohl(thisfile->offset));
Stefan Reinauera1e48242011-10-21 14:24:57 -0700337 cbfs_create_empty_file(location,
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000338 length -
339 (location -
340 current));
341 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000342 }
343 }
344 current =
345 ALIGN(current + ntohl(thisfile->len) +
346 ntohl(thisfile->offset), align);
347 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000348 printf("Could not add the file to CBFS, it's probably too big.\n");
Uwe Hermann6a3ca4e2009-11-12 20:06:32 +0000349 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000350 return 1;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000351}
352
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800353
354static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first,
355 struct cbfs_file *second)
356{
357 uint32_t new_length =
358 ntohl(first->len) + ntohl(second->len) + ntohl(second->offset);
359 first->len = htonl(new_length);
360 first->checksum = 0; // FIXME?
361 return first;
362}
363
364static struct cbfs_file *next_file(struct cbfs_file *prev)
365{
366 uint32_t pos = (prev == NULL) ? phys_start :
367 ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset),
368 align);
369
370 for (; pos < phys_end; pos += align) {
371 if (cbfs_file_header(pos))
372 return (struct cbfs_file *)phys_to_virt(pos);
373 }
374 return NULL;
375}
376
377
378int remove_file_from_cbfs(const char *filename)
379{
380 struct cbfs_file *prev = NULL;
381 struct cbfs_file *cur = next_file(prev);
382 struct cbfs_file *next = next_file(cur);
383 for (; cur; prev = cur, cur = next, next = next_file(next)) {
384
385 /* Check if this is the file to remove. */
386 char *name = (char *)cur + sizeof(*cur);
387 if (strcmp(name, filename))
388 continue;
389
390 /* Mark the file as free space and erase its name. */
391 cur->type = CBFS_COMPONENT_NULL;
392 name[0] = '\0';
393
394 /* Merge it with the previous file if possible. */
395 if (prev && prev->type == CBFS_COMPONENT_NULL)
396 cur = merge_adjacent_files(prev, cur);
397
398 /* Merge it with the next file if possible. */
399 if (next && next->type == CBFS_COMPONENT_NULL)
400 merge_adjacent_files(cur, next);
401
402 return 0;
403 }
404 printf("CBFS file %s not found.\n", filename);
405 return 1;
406}
407
408
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000409/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
410 the new location that points to the cbfs_file header */
411void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
412 uint32_t type, uint32_t * location)
413{
414 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
415 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
416 if ((location != 0) && (*location != 0)) {
417 uint32_t offset = *location % align;
418 /* If offset >= (headersize % align), we can stuff the header into the offset.
419 Otherwise the header has to be aligned itself, and put before the offset data */
420 if (offset >= (headersize % align)) {
421 offset -= (headersize % align);
422 } else {
423 offset += align - (headersize % align);
424 }
425 headersize += offset;
426 *location -= headersize;
427 }
428 void *newdata = malloc(*datasize + headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000429 if (!newdata) {
430 printf("Could not get %d bytes for CBFS file.\n", *datasize +
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000431 headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000432 exit(1);
433 }
Peter Stugef4aca1d2009-12-06 12:14:39 +0000434 memset(newdata, 0xff, *datasize + headersize);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000435 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700436 strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000437 nextfile->len = htonl(*datasize);
438 nextfile->type = htonl(type);
439 nextfile->checksum = 0; // FIXME?
440 nextfile->offset = htonl(headersize);
441 strcpy(newdata + sizeof(struct cbfs_file), filename);
442 memcpy(newdata + headersize, data, *datasize);
443 *datasize += headersize;
444 return newdata;
445}
446
447int create_cbfs_image(const char *romfile, uint32_t _romsize,
448 const char *bootblock, uint32_t align)
449{
450 romsize = _romsize;
451 unsigned char *romarea = malloc(romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000452 if (!romarea) {
453 printf("Could not get %d bytes of memory for CBFS image.\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000454 romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000455 exit(1);
456 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000457 memset(romarea, 0xff, romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000458
459 // Set up physical/virtual mapping
460 offset = romarea + romsize - 0x100000000ULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000461
462 if (align == 0)
463 align = 64;
464
465 uint32_t bootblocksize = 0;
466 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
467 struct cbfs_header *master_header =
468 (struct cbfs_header *)(romarea + romsize - bootblocksize -
469 sizeof(struct cbfs_header));
470 master_header->magic = ntohl(0x4f524243);
471 master_header->version = ntohl(0x31313131);
472 master_header->romsize = htonl(romsize);
473 master_header->bootblocksize = htonl(bootblocksize);
474 master_header->align = htonl(align);
475 master_header->offset = htonl(0);
476 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
477 virt_to_phys(master_header);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000478
479 recalculate_rom_geometry(romarea);
480
Stefan Reinauera1e48242011-10-21 14:24:57 -0700481 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000482 romsize - bootblocksize -
483 sizeof(struct cbfs_header) -
484 sizeof(struct cbfs_file) - 16);
485
486 writerom(romfile, romarea, romsize);
487 return 0;
488}
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000489
490static int in_segment(int addr, int size, int gran)
491{
492 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
493}
494
495uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
496 const char *filename, uint32_t alignment)
497{
Stefan Reinauera1e48242011-10-21 14:24:57 -0700498 loadrom(romfile);
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000499 int filename_size = strlen(filename);
500
501 int headersize =
502 sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
503 16) + sizeof(struct cbfs_stage);
504 int totalsize = headersize + filesize;
505
506 uint32_t current = phys_start;
507 while (current < phys_end) {
508 if (!cbfs_file_header(current)) {
509 current += align;
510 continue;
511 }
512 struct cbfs_file *thisfile =
513 (struct cbfs_file *)phys_to_virt(current);
514
515 uint32_t top =
516 current + ntohl(thisfile->len) + ntohl(thisfile->offset);
517 if (((ntohl(thisfile->type) == 0x0)
518 || (ntohl(thisfile->type) == 0xffffffff))
519 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
520 totalsize)) {
521 if (in_segment
522 (current + headersize, filesize, alignment))
523 return current + headersize;
524 if ((ALIGN(current, alignment) + filesize < top)
525 && (ALIGN(current, alignment) - headersize >
526 current)
527 && in_segment(ALIGN(current, alignment), filesize,
528 alignment))
529 return ALIGN(current, alignment);
530 if ((ALIGN(current, alignment) + alignment + filesize <
531 top)
532 && (ALIGN(current, alignment) + alignment -
533 headersize > current)
534 && in_segment(ALIGN(current, alignment) + alignment,
535 filesize, alignment))
536 return ALIGN(current, alignment) + alignment;
537 }
538 current =
539 ALIGN(current + ntohl(thisfile->len) +
540 ntohl(thisfile->offset), align);
541 }
542 return 0;
543}