blob: 6c67c396f495451c85a559a1db973411de3af7f5 [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
Stefan Reinauer63217582012-10-29 16:52:36 -070031size_t getfilesize(const char *filename)
Patrick Georgi0da38dd2009-11-09 17:18:02 +000032{
Stefan Reinauer63217582012-10-29 16:52:36 -070033 size_t size;
Patrick Georgi0da38dd2009-11-09 17:18:02 +000034 FILE *file = fopen(filename, "rb");
Stefan Reinauer63217582012-10-29 16:52:36 -070035 if (file == NULL)
36 return -1;
37
Patrick Georgi0da38dd2009-11-09 17:18:02 +000038 fseek(file, 0, SEEK_END);
39 size = ftell(file);
40 fclose(file);
41 return size;
42}
43
Patrick Georgib7b56dd82009-09-14 13:29:27 +000044void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
45 int place)
46{
47 FILE *file = fopen(filename, "rb");
Patrick Georgi45d8a832009-09-15 08:21:46 +000048 if (file == NULL)
49 return NULL;
Stefan Reinauer63217582012-10-29 16:52:36 -070050
Patrick Georgib7b56dd82009-09-14 13:29:27 +000051 fseek(file, 0, SEEK_END);
52 *romsize_p = ftell(file);
53 fseek(file, 0, SEEK_SET);
Stefan Reinauer853270a2009-09-22 15:55:01 +000054 if (!content) {
Patrick Georgib7b56dd82009-09-14 13:29:27 +000055 content = malloc(*romsize_p);
Stefan Reinauer853270a2009-09-22 15:55:01 +000056 if (!content) {
57 printf("Could not get %d bytes for file %s\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +000058 *romsize_p, filename);
Stefan Reinauer853270a2009-09-22 15:55:01 +000059 exit(1);
60 }
61 } else if (place == SEEK_END)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000062 content -= *romsize_p;
Stefan Reinauer853270a2009-09-22 15:55:01 +000063
Patrick Georgib7b56dd82009-09-14 13:29:27 +000064 if (!fread(content, *romsize_p, 1, file)) {
Stefan Reinauer63217582012-10-29 16:52:36 -070065 printf("Failed to read %s\n", filename);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000066 return NULL;
67 }
68 fclose(file);
69 return content;
70}
71
Stefan Reinauer63217582012-10-29 16:52:36 -070072static struct cbfs_header *master_header;
73static uint32_t phys_start, phys_end, align;
74uint32_t romsize;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000075void *offset;
76
77void recalculate_rom_geometry(void *romarea)
78{
79 offset = romarea + romsize - 0x100000000ULL;
80 master_header = (struct cbfs_header *)
81 phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
82 phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
83 phys_end =
84 (0 - ntohl(master_header->bootblocksize) -
85 sizeof(struct cbfs_header)) & 0xffffffff;
86 align = ntohl(master_header->align);
87}
88
89void *loadrom(const char *filename)
90{
91 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
Patrick Georgi45d8a832009-09-15 08:21:46 +000092 if (romarea == NULL)
93 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000094 recalculate_rom_geometry(romarea);
95 return romarea;
96}
97
Stefan Reinauer9bb043852010-06-24 13:37:59 +000098int writerom(const char *filename, void *start, uint32_t size)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000099{
100 FILE *file = fopen(filename, "wb");
Stefan Reinauer9bb043852010-06-24 13:37:59 +0000101 if (!file) {
102 fprintf(stderr, "Could not open '%s' for writing: ", filename);
103 perror("");
104 return 1;
105 }
106
107 if (fwrite(start, size, 1, file) != 1) {
108 fprintf(stderr, "Could not write to '%s': ", filename);
109 perror("");
110 return 1;
111 }
112
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000113 fclose(file);
Stefan Reinauer9bb043852010-06-24 13:37:59 +0000114 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000115}
116
117int cbfs_file_header(uint32_t physaddr)
118{
119 /* maybe improve this test */
120 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
121}
122
123struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
124{
125 struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
Stefan Reinauera1e48242011-10-21 14:24:57 -0700126 strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000127 nextfile->len = htonl(size);
128 nextfile->type = htonl(0xffffffff);
129 nextfile->checksum = 0; // FIXME?
130 nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
131 memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
132 return nextfile;
133}
134
135int iself(unsigned char *input)
136{
137 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
138 return !memcmp(ehdr->e_ident, ELFMAG, 4);
139}
140
Mathias Krause941158f2012-04-12 21:36:23 +0200141static struct filetypes_t {
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000142 uint32_t type;
143 const char *name;
144} filetypes[] = {
145 {CBFS_COMPONENT_STAGE, "stage"},
146 {CBFS_COMPONENT_PAYLOAD, "payload"},
147 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000148 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
149 {CBFS_COMPONENT_RAW, "raw"},
150 {CBFS_COMPONENT_VSA, "vsa"},
151 {CBFS_COMPONENT_MBI, "mbi"},
152 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgia865b172011-01-14 07:40:24 +0000153 {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"},
Mathias Krause941158f2012-04-12 21:36:23 +0200154 {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000155 {CBFS_COMPONENT_DELETED, "deleted"},
156 {CBFS_COMPONENT_NULL, "null"}
157};
158
Stefan Reinauer07040582010-04-24 21:24:06 +0000159void print_supported_filetypes(void)
160{
161 int i, number = ARRAY_SIZE(filetypes);
162
163 for (i=0; i<number; i++) {
164 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
165 if ((i%8) == 7)
166 printf("\n");
167 }
168}
169
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000170const char *strfiletype(uint32_t number)
171{
Mathias Krause41c229c2012-07-17 21:17:15 +0200172 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000173 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
174 if (filetypes[i].type == number)
175 return filetypes[i].name;
176 return "unknown";
177}
178
179uint64_t intfiletype(const char *name)
180{
Mathias Krause41c229c2012-07-17 21:17:15 +0200181 size_t i;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000182 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
183 if (strcmp(filetypes[i].name, name) == 0)
184 return filetypes[i].type;
185 return -1;
186}
187
188void print_cbfs_directory(const char *filename)
189{
190 printf
191 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
Uwe Hermann942a40d2010-02-10 19:52:35 +0000192 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000193 romsize, ntohl(master_header->offset), align);
194 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
195 uint32_t current = phys_start;
196 while (current < phys_end) {
197 if (!cbfs_file_header(current)) {
198 current += align;
199 continue;
200 }
201 struct cbfs_file *thisfile =
202 (struct cbfs_file *)phys_to_virt(current);
203 uint32_t length = ntohl(thisfile->len);
Maciej Pijankaf44eb782010-01-07 21:37:18 +0000204 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
Stefan Reinauer14e22772010-04-27 06:56:47 +0000205 if (strlen(fname) == 0)
Maciej Pijankaf44eb782010-01-07 21:37:18 +0000206 fname = "(empty)";
207
Stefan Reinauer14e22772010-04-27 06:56:47 +0000208 printf("%-30s 0x%-8x %-12s %d\n", fname,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000209 current - phys_start, strfiletype(ntohl(thisfile->type)),
210 length);
211 current =
212 ALIGN(current + ntohl(thisfile->len) +
213 ntohl(thisfile->offset), align);
214 }
215}
216
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000217int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath)
218{
219 // Identify the coreboot image.
220 printf(
221 "%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
222 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
223 romsize, ntohl(master_header->offset), align);
224
225 FILE *outfile = NULL;
226 uint32_t current = phys_start;
227 while (current < phys_end) {
228 if (!cbfs_file_header(current)) {
229 current += align;
230 continue;
231 }
232
233 // Locate the file start struct
234 struct cbfs_file *thisfile =
235 (struct cbfs_file *)phys_to_virt(current);
236 // And its length
237 uint32_t length = ntohl(thisfile->len);
238 // Locate the file name
239 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
Stefan Reinauera1e48242011-10-21 14:24:57 -0700240
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000241 // It's not the file we are looking for..
242 if (strcmp(fname, payloadname) != 0)
243 {
244 current =
245 ALIGN(current + ntohl(thisfile->len) +
246 ntohl(thisfile->offset), align);
247 continue;
248 }
249
250 // Else, it's our file.
Peter Stuge441426b2011-01-17 05:08:32 +0000251 printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname,
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000252 current - phys_start, strfiletype(ntohl(thisfile->type)),
253 length);
254
255 // If we are not dumping to stdout, open the out file.
256 outfile = fopen(outpath, "wb");
257 if (!outfile)
258 {
259 printf("Could not open the file %s for writing. Aborting.\n", outpath);
260 return 1;
261 }
262
263 if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
264 {
265 printf("Warning: only 'raw' files are safe to extract.\n");
266 }
267
268 fwrite(((char *)thisfile)
269 + ntohl(thisfile->offset), length, 1, outfile);
270
271 fclose(outfile);
Peter Stuge441426b2011-01-17 05:08:32 +0000272 printf("Successfully dumped the file.\n");
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000273
274 // We'll only dump one file.
275 return 0;
276 }
Stefan Reinauera1e48242011-10-21 14:24:57 -0700277 printf("File %s not found.\n", payloadname);
278 return 1;
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000279}
280
281
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000282int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
283{
284 uint32_t current = phys_start;
285 while (current < phys_end) {
286 if (!cbfs_file_header(current)) {
287 current += align;
288 continue;
289 }
290 struct cbfs_file *thisfile =
291 (struct cbfs_file *)phys_to_virt(current);
292 uint32_t length = ntohl(thisfile->len);
293
294 dprintf("at %x, %x bytes\n", current, length);
295 /* Is this a free chunk? */
296 if ((thisfile->type == CBFS_COMPONENT_DELETED)
297 || (thisfile->type == CBFS_COMPONENT_NULL)) {
298 dprintf("null||deleted at %x, %x bytes\n", current,
299 length);
300 /* if this is the right size, and if specified, the right location, use it */
301 if ((contentsize <= length)
302 && ((location == 0) || (current == location))) {
303 if (contentsize < length) {
304 dprintf
305 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
306 length, contentsize,
307 ALIGN(current + contentsize,
308 align),
309 length - contentsize);
310 uint32_t start =
311 ALIGN(current + contentsize, align);
312 uint32_t size =
313 current + ntohl(thisfile->offset)
314 + length - start - 16 -
315 sizeof(struct cbfs_file);
316 cbfs_create_empty_file(start, size);
317 }
318 dprintf("copying data\n");
319 memcpy(phys_to_virt(current), content,
320 contentsize);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000321 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000322 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000323 if (location != 0) {
324 /* CBFS has the constraint that the chain always moves up in memory. so once
325 we're past the place we seek, we don't need to look any further */
326 if (current > location) {
327 printf
328 ("the requested space is not available\n");
329 return 1;
330 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000331
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000332 /* Is the requested location inside the current chunk? */
333 if ((current < location)
334 && ((location + contentsize) <=
335 (current + length))) {
336 /* Split it up. In the next iteration the code will be at the right place. */
337 dprintf("split up. new length: %x\n",
338 location - current -
339 ntohl(thisfile->offset));
340 thisfile->len =
341 htonl(location - current -
342 ntohl(thisfile->offset));
Stefan Reinauera1e48242011-10-21 14:24:57 -0700343 cbfs_create_empty_file(location,
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000344 length -
345 (location -
346 current));
347 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000348 }
349 }
350 current =
351 ALIGN(current + ntohl(thisfile->len) +
352 ntohl(thisfile->offset), align);
353 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000354 printf("Could not add the file to CBFS, it's probably too big.\n");
Uwe Hermann6a3ca4e2009-11-12 20:06:32 +0000355 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000356 return 1;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000357}
358
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800359
360static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first,
361 struct cbfs_file *second)
362{
363 uint32_t new_length =
364 ntohl(first->len) + ntohl(second->len) + ntohl(second->offset);
365 first->len = htonl(new_length);
366 first->checksum = 0; // FIXME?
367 return first;
368}
369
370static struct cbfs_file *next_file(struct cbfs_file *prev)
371{
372 uint32_t pos = (prev == NULL) ? phys_start :
373 ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset),
374 align);
375
376 for (; pos < phys_end; pos += align) {
377 if (cbfs_file_header(pos))
378 return (struct cbfs_file *)phys_to_virt(pos);
379 }
380 return NULL;
381}
382
383
384int remove_file_from_cbfs(const char *filename)
385{
386 struct cbfs_file *prev = NULL;
387 struct cbfs_file *cur = next_file(prev);
388 struct cbfs_file *next = next_file(cur);
389 for (; cur; prev = cur, cur = next, next = next_file(next)) {
390
391 /* Check if this is the file to remove. */
392 char *name = (char *)cur + sizeof(*cur);
393 if (strcmp(name, filename))
394 continue;
395
396 /* Mark the file as free space and erase its name. */
397 cur->type = CBFS_COMPONENT_NULL;
398 name[0] = '\0';
399
400 /* Merge it with the previous file if possible. */
401 if (prev && prev->type == CBFS_COMPONENT_NULL)
402 cur = merge_adjacent_files(prev, cur);
403
404 /* Merge it with the next file if possible. */
405 if (next && next->type == CBFS_COMPONENT_NULL)
406 merge_adjacent_files(cur, next);
407
408 return 0;
409 }
410 printf("CBFS file %s not found.\n", filename);
411 return 1;
412}
413
414
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000415/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
416 the new location that points to the cbfs_file header */
417void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
418 uint32_t type, uint32_t * location)
419{
420 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
421 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
422 if ((location != 0) && (*location != 0)) {
423 uint32_t offset = *location % align;
424 /* If offset >= (headersize % align), we can stuff the header into the offset.
425 Otherwise the header has to be aligned itself, and put before the offset data */
426 if (offset >= (headersize % align)) {
427 offset -= (headersize % align);
428 } else {
429 offset += align - (headersize % align);
430 }
431 headersize += offset;
432 *location -= headersize;
433 }
434 void *newdata = malloc(*datasize + headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000435 if (!newdata) {
436 printf("Could not get %d bytes for CBFS file.\n", *datasize +
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000437 headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000438 exit(1);
439 }
Peter Stugef4aca1d2009-12-06 12:14:39 +0000440 memset(newdata, 0xff, *datasize + headersize);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000441 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
Stefan Reinauera1e48242011-10-21 14:24:57 -0700442 strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000443 nextfile->len = htonl(*datasize);
444 nextfile->type = htonl(type);
445 nextfile->checksum = 0; // FIXME?
446 nextfile->offset = htonl(headersize);
447 strcpy(newdata + sizeof(struct cbfs_file), filename);
448 memcpy(newdata + headersize, data, *datasize);
449 *datasize += headersize;
450 return newdata;
451}
452
453int create_cbfs_image(const char *romfile, uint32_t _romsize,
Stefan Reinauera90bd522012-08-15 16:05:50 -0700454 const char *bootblock, uint32_t align, uint32_t offs)
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000455{
Stefan Reinauer63217582012-10-29 16:52:36 -0700456 uint32_t bootblocksize = 0;
457 struct cbfs_header *master_header;
458 unsigned char *romarea, *bootblk;
459
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000460 romsize = _romsize;
Stefan Reinauer63217582012-10-29 16:52:36 -0700461 romarea = malloc(romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000462 if (!romarea) {
Stefan Reinauer63217582012-10-29 16:52:36 -0700463 fprintf(stderr, "E: Could not get %d bytes of memory"
464 " for CBFS image.\n", romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000465 exit(1);
466 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000467 memset(romarea, 0xff, romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000468
469 // Set up physical/virtual mapping
470 offset = romarea + romsize - 0x100000000ULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000471
472 if (align == 0)
473 align = 64;
474
Stefan Reinauer63217582012-10-29 16:52:36 -0700475 bootblk = loadfile(bootblock, &bootblocksize,
476 romarea + romsize, SEEK_END);
477 if (!bootblk) {
478 fprintf(stderr, "E: Could not load bootblock %s.\n",
479 bootblock);
480 free(romarea);
481 return 1;
482 }
483
484 master_header =
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000485 (struct cbfs_header *)(romarea + romsize - bootblocksize -
486 sizeof(struct cbfs_header));
487 master_header->magic = ntohl(0x4f524243);
488 master_header->version = ntohl(0x31313131);
489 master_header->romsize = htonl(romsize);
490 master_header->bootblocksize = htonl(bootblocksize);
491 master_header->align = htonl(align);
Stefan Reinauera90bd522012-08-15 16:05:50 -0700492 master_header->offset = htonl(offs);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000493 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
494 virt_to_phys(master_header);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000495
496 recalculate_rom_geometry(romarea);
497
Stefan Reinauera90bd522012-08-15 16:05:50 -0700498 cbfs_create_empty_file((0 - romsize + offs) & 0xffffffff,
499 romsize - offs - bootblocksize -
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000500 sizeof(struct cbfs_header) -
501 sizeof(struct cbfs_file) - 16);
502
503 writerom(romfile, romarea, romsize);
Stefan Reinauer63217582012-10-29 16:52:36 -0700504 free(romarea);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000505 return 0;
506}
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000507
508static int in_segment(int addr, int size, int gran)
509{
510 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
511}
512
513uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
514 const char *filename, uint32_t alignment)
515{
Stefan Reinauer63217582012-10-29 16:52:36 -0700516 void *rom;
517 size_t filename_size, headersize, totalsize;
518 int ret = 0;
519 uint32_t current;
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000520
Stefan Reinauer63217582012-10-29 16:52:36 -0700521 rom = loadrom(romfile);
522 if (rom == NULL) {
523 fprintf(stderr, "E: Could not load ROM image '%s'.\n",
524 romfile);
525 return 0;
526 }
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000527
Stefan Reinauer63217582012-10-29 16:52:36 -0700528 filename_size = strlen(filename);
529 headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) +
530 sizeof(struct cbfs_stage);
531 totalsize = headersize + filesize;
532
533 current = phys_start;
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000534 while (current < phys_end) {
Stefan Reinauer63217582012-10-29 16:52:36 -0700535 uint32_t top;
536 struct cbfs_file *thisfile;
537
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000538 if (!cbfs_file_header(current)) {
539 current += align;
540 continue;
541 }
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000542
Stefan Reinauer63217582012-10-29 16:52:36 -0700543 thisfile = (struct cbfs_file *)phys_to_virt(current);
544
545 top = current + ntohl(thisfile->len) + ntohl(thisfile->offset);
546
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000547 if (((ntohl(thisfile->type) == 0x0)
548 || (ntohl(thisfile->type) == 0xffffffff))
549 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
550 totalsize)) {
551 if (in_segment
Stefan Reinauer63217582012-10-29 16:52:36 -0700552 (current + headersize, filesize, alignment)) {
553 ret = current + headersize;
554 break;
555 }
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000556 if ((ALIGN(current, alignment) + filesize < top)
557 && (ALIGN(current, alignment) - headersize >
558 current)
559 && in_segment(ALIGN(current, alignment), filesize,
Stefan Reinauer63217582012-10-29 16:52:36 -0700560 alignment)) {
561 ret = ALIGN(current, alignment);
562 break;
563 }
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000564 if ((ALIGN(current, alignment) + alignment + filesize <
565 top)
566 && (ALIGN(current, alignment) + alignment -
567 headersize > current)
568 && in_segment(ALIGN(current, alignment) + alignment,
Stefan Reinauer63217582012-10-29 16:52:36 -0700569 filesize, alignment)) {
570 ret = ALIGN(current, alignment) + alignment;
571 break;
572 }
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000573 }
574 current =
575 ALIGN(current + ntohl(thisfile->len) +
576 ntohl(thisfile->offset), align);
577 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700578
579 free(rom);
580 return ret;
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000581}