2 * common utility functions for cbfstool
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
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.
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.
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
30 uint32_t getfilesize(const char *filename)
33 FILE *file = fopen(filename, "rb");
34 fseek(file, 0, SEEK_END);
40 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
43 FILE *file = fopen(filename, "rb");
46 fseek(file, 0, SEEK_END);
47 *romsize_p = ftell(file);
48 fseek(file, 0, SEEK_SET);
50 content = malloc(*romsize_p);
52 printf("Could not get %d bytes for file %s\n",
53 *romsize_p, filename);
56 } else if (place == SEEK_END)
57 content -= *romsize_p;
59 if (!fread(content, *romsize_p, 1, file)) {
60 printf("failed to read %s\n", filename);
67 struct cbfs_header *master_header;
68 uint32_t phys_start, phys_end, align, romsize;
71 void recalculate_rom_geometry(void *romarea)
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;
78 (0 - ntohl(master_header->bootblocksize) -
79 sizeof(struct cbfs_header)) & 0xffffffff;
80 align = ntohl(master_header->align);
83 void *loadrom(const char *filename)
85 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
88 recalculate_rom_geometry(romarea);
92 void writerom(const char *filename, void *start, uint32_t size)
94 FILE *file = fopen(filename, "wb");
95 fwrite(start, size, 1, file);
99 int cbfs_file_header(uint32_t physaddr)
101 /* maybe improve this test */
102 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
105 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
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);
117 int iself(unsigned char *input)
119 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
120 return !memcmp(ehdr->e_ident, ELFMAG, 4);
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"}
134 const char *strfiletype(uint32_t number)
137 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
138 if (filetypes[i].type == number)
139 return filetypes[i].name;
143 uint64_t intfiletype(const char *name)
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;
152 void print_cbfs_directory(const char *filename)
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)) {
165 struct cbfs_file *thisfile =
166 (struct cbfs_file *)phys_to_virt(current);
167 uint32_t length = ntohl(thisfile->len);
168 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
169 if (strlen(fname) == 0)
172 printf("%-30s 0x%-8x %-12s %d\n", fname,
173 current - phys_start, strfiletype(ntohl(thisfile->type)),
176 ALIGN(current + ntohl(thisfile->len) +
177 ntohl(thisfile->offset), align);
181 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
183 uint32_t current = phys_start;
184 while (current < phys_end) {
185 if (!cbfs_file_header(current)) {
189 struct cbfs_file *thisfile =
190 (struct cbfs_file *)phys_to_virt(current);
191 uint32_t length = ntohl(thisfile->len);
193 dprintf("at %x, %x bytes\n", current, length);
194 /* Is this a free chunk? */
195 if ((thisfile->type == CBFS_COMPONENT_DELETED)
196 || (thisfile->type == CBFS_COMPONENT_NULL)) {
197 dprintf("null||deleted at %x, %x bytes\n", current,
199 /* if this is the right size, and if specified, the right location, use it */
200 if ((contentsize <= length)
201 && ((location == 0) || (current == location))) {
202 if (contentsize < length) {
204 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
206 ALIGN(current + contentsize,
208 length - contentsize);
210 ALIGN(current + contentsize, align);
212 current + ntohl(thisfile->offset)
213 + length - start - 16 -
214 sizeof(struct cbfs_file);
215 cbfs_create_empty_file(start, size);
217 dprintf("copying data\n");
218 memcpy(phys_to_virt(current), content,
223 /* CBFS has the constraint that the chain always moves up in memory. so once
224 we're past the place we seek, we don't need to look any further */
225 if (current > location) {
227 ("the requested space is not available\n");
231 /* Is the requested location inside the current chunk? */
232 if ((current < location)
233 && ((location + contentsize) <=
234 (current + length))) {
235 /* Split it up. In the next iteration the code will be at the right place. */
236 dprintf("split up. new length: %x\n",
238 ntohl(thisfile->offset));
240 htonl(location - current -
241 ntohl(thisfile->offset));
242 struct cbfs_file *nextfile =
243 cbfs_create_empty_file(location,
251 ALIGN(current + ntohl(thisfile->len) +
252 ntohl(thisfile->offset), align);
254 printf("Could not add the file to CBFS, it's probably too big.\n");
255 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
259 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
260 the new location that points to the cbfs_file header */
261 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
262 uint32_t type, uint32_t * location)
264 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
265 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
266 if ((location != 0) && (*location != 0)) {
267 uint32_t offset = *location % align;
268 /* If offset >= (headersize % align), we can stuff the header into the offset.
269 Otherwise the header has to be aligned itself, and put before the offset data */
270 if (offset >= (headersize % align)) {
271 offset -= (headersize % align);
273 offset += align - (headersize % align);
275 headersize += offset;
276 *location -= headersize;
278 void *newdata = malloc(*datasize + headersize);
280 printf("Could not get %d bytes for CBFS file.\n", *datasize +
284 memset(newdata, 0xff, *datasize + headersize);
285 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
286 strncpy(nextfile->magic, "LARCHIVE", 8);
287 nextfile->len = htonl(*datasize);
288 nextfile->type = htonl(type);
289 nextfile->checksum = 0; // FIXME?
290 nextfile->offset = htonl(headersize);
291 strcpy(newdata + sizeof(struct cbfs_file), filename);
292 memcpy(newdata + headersize, data, *datasize);
293 *datasize += headersize;
297 int create_cbfs_image(const char *romfile, uint32_t _romsize,
298 const char *bootblock, uint32_t align)
301 unsigned char *romarea = malloc(romsize);
303 printf("Could not get %d bytes of memory for CBFS image.\n",
307 memset(romarea, 0xff, romsize);
309 // Set up physical/virtual mapping
310 offset = romarea + romsize - 0x100000000ULL;
315 uint32_t bootblocksize = 0;
316 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
317 struct cbfs_header *master_header =
318 (struct cbfs_header *)(romarea + romsize - bootblocksize -
319 sizeof(struct cbfs_header));
320 master_header->magic = ntohl(0x4f524243);
321 master_header->version = ntohl(0x31313131);
322 master_header->romsize = htonl(romsize);
323 master_header->bootblocksize = htonl(bootblocksize);
324 master_header->align = htonl(align);
325 master_header->offset = htonl(0);
326 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
327 virt_to_phys(master_header);
329 recalculate_rom_geometry(romarea);
331 struct cbfs_file *one_empty_file =
332 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
333 romsize - bootblocksize -
334 sizeof(struct cbfs_header) -
335 sizeof(struct cbfs_file) - 16);
337 writerom(romfile, romarea, romsize);
341 static int in_segment(int addr, int size, int gran)
343 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
346 uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
347 const char *filename, uint32_t alignment)
349 void *rom = loadrom(romfile);
350 int filename_size = strlen(filename);
353 sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
354 16) + sizeof(struct cbfs_stage);
355 int totalsize = headersize + filesize;
357 uint32_t current = phys_start;
358 while (current < phys_end) {
359 if (!cbfs_file_header(current)) {
363 struct cbfs_file *thisfile =
364 (struct cbfs_file *)phys_to_virt(current);
367 current + ntohl(thisfile->len) + ntohl(thisfile->offset);
368 if (((ntohl(thisfile->type) == 0x0)
369 || (ntohl(thisfile->type) == 0xffffffff))
370 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
373 (current + headersize, filesize, alignment))
374 return current + headersize;
375 if ((ALIGN(current, alignment) + filesize < top)
376 && (ALIGN(current, alignment) - headersize >
378 && in_segment(ALIGN(current, alignment), filesize,
380 return ALIGN(current, alignment);
381 if ((ALIGN(current, alignment) + alignment + filesize <
383 && (ALIGN(current, alignment) + alignment -
384 headersize > current)
385 && in_segment(ALIGN(current, alignment) + alignment,
386 filesize, alignment))
387 return ALIGN(current, alignment) + alignment;
390 ALIGN(current + ntohl(thisfile->len) +
391 ntohl(thisfile->offset), align);