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 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
33 FILE *file = fopen(filename, "rb");
36 fseek(file, 0, SEEK_END);
37 *romsize_p = ftell(file);
38 fseek(file, 0, SEEK_SET);
40 content = malloc(*romsize_p);
41 else if (place == SEEK_END)
42 content -= *romsize_p;
43 if (!fread(content, *romsize_p, 1, file)) {
44 printf("failed to read %s\n", filename);
51 struct cbfs_header *master_header;
52 uint32_t phys_start, phys_end, align, romsize;
55 void recalculate_rom_geometry(void *romarea)
57 offset = romarea + romsize - 0x100000000ULL;
58 master_header = (struct cbfs_header *)
59 phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
60 phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
62 (0 - ntohl(master_header->bootblocksize) -
63 sizeof(struct cbfs_header)) & 0xffffffff;
64 align = ntohl(master_header->align);
67 void *loadrom(const char *filename)
69 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
72 recalculate_rom_geometry(romarea);
76 void writerom(const char *filename, void *start, uint32_t size)
78 FILE *file = fopen(filename, "wb");
79 fwrite(start, size, 1, file);
83 int cbfs_file_header(uint32_t physaddr)
85 /* maybe improve this test */
86 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
89 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
91 struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
92 strncpy(nextfile->magic, "LARCHIVE", 8);
93 nextfile->len = htonl(size);
94 nextfile->type = htonl(0xffffffff);
95 nextfile->checksum = 0; // FIXME?
96 nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
97 memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
101 int iself(unsigned char *input)
103 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
104 return !memcmp(ehdr->e_ident, ELFMAG, 4);
111 {CBFS_COMPONENT_STAGE, "stage"},
112 {CBFS_COMPONENT_PAYLOAD, "payload"},
113 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
114 {CBFS_COMPONENT_DELETED, "deleted"},
115 {CBFS_COMPONENT_NULL, "null"}
118 const char *strfiletype(uint32_t number)
121 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
122 if (filetypes[i].type == number)
123 return filetypes[i].name;
127 uint64_t intfiletype(const char *name)
130 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
131 if (strcmp(filetypes[i].name, name) == 0)
132 return filetypes[i].type;
136 void print_cbfs_directory(const char *filename)
139 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
140 filename, romsize / 1024, ntohl(master_header->bootblocksize),
141 romsize, ntohl(master_header->offset), align);
142 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
143 uint32_t current = phys_start;
144 while (current < phys_end) {
145 if (!cbfs_file_header(current)) {
149 struct cbfs_file *thisfile =
150 (struct cbfs_file *)phys_to_virt(current);
151 uint32_t length = ntohl(thisfile->len);
152 printf("%-30s 0x%-8x %-12s %d\n",
153 (const char *)(phys_to_virt(current) +
154 sizeof(struct cbfs_file)),
155 current - phys_start, strfiletype(ntohl(thisfile->type)),
158 ALIGN(current + ntohl(thisfile->len) +
159 ntohl(thisfile->offset), align);
163 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
165 uint32_t current = phys_start;
166 while (current < phys_end) {
167 if (!cbfs_file_header(current)) {
171 struct cbfs_file *thisfile =
172 (struct cbfs_file *)phys_to_virt(current);
173 uint32_t length = ntohl(thisfile->len);
175 dprintf("at %x, %x bytes\n", current, length);
176 /* Is this a free chunk? */
177 if ((thisfile->type == CBFS_COMPONENT_DELETED)
178 || (thisfile->type == CBFS_COMPONENT_NULL)) {
179 dprintf("null||deleted at %x, %x bytes\n", current,
181 /* if this is the right size, and if specified, the right location, use it */
182 if ((contentsize <= length)
183 && ((location == 0) || (current == location))) {
184 if (contentsize < length) {
186 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
188 ALIGN(current + contentsize,
190 length - contentsize);
192 ALIGN(current + contentsize, align);
194 current + ntohl(thisfile->offset)
195 + length - start - 16 -
196 sizeof(struct cbfs_file);
197 cbfs_create_empty_file(start, size);
199 dprintf("copying data\n");
200 memcpy(phys_to_virt(current), content,
207 /* CBFS has the constraint that the chain always moves up in memory. so once
208 we're past the place we seek, we don't need to look any further */
209 if (current > location) {
211 ("the requested space is not available\n");
215 /* Is the requested location inside the current chunk? */
216 if ((current < location)
217 && ((location + contentsize) <= (current + length))) {
218 /* Split it up. In the next iteration the code will be at the right place. */
219 dprintf("split up. new length: %x\n",
221 ntohl(thisfile->offset));
223 htonl(location - current -
224 ntohl(thisfile->offset));
225 struct cbfs_file *nextfile =
226 cbfs_create_empty_file(location,
232 ALIGN(current + ntohl(thisfile->len) +
233 ntohl(thisfile->offset), align);
238 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
239 the new location that points to the cbfs_file header */
240 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
241 uint32_t type, uint32_t * location)
243 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
244 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
245 if ((location != 0) && (*location != 0)) {
246 uint32_t offset = *location % align;
247 /* If offset >= (headersize % align), we can stuff the header into the offset.
248 Otherwise the header has to be aligned itself, and put before the offset data */
249 if (offset >= (headersize % align)) {
250 offset -= (headersize % align);
252 offset += align - (headersize % align);
254 headersize += offset;
255 *location -= headersize;
257 void *newdata = malloc(*datasize + headersize);
258 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
259 strncpy(nextfile->magic, "LARCHIVE", 8);
260 nextfile->len = htonl(*datasize);
261 nextfile->type = htonl(type);
262 nextfile->checksum = 0; // FIXME?
263 nextfile->offset = htonl(headersize);
264 strcpy(newdata + sizeof(struct cbfs_file), filename);
265 memcpy(newdata + headersize, data, *datasize);
266 *datasize += headersize;
270 int create_cbfs_image(const char *romfile, uint32_t _romsize,
271 const char *bootblock, uint32_t align)
274 unsigned char *romarea = malloc(romsize);
275 memset(romarea, 0xff, romsize);
276 recalculate_rom_geometry(romarea);
281 uint32_t bootblocksize = 0;
282 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
283 struct cbfs_header *master_header =
284 (struct cbfs_header *)(romarea + romsize - bootblocksize -
285 sizeof(struct cbfs_header));
286 master_header->magic = ntohl(0x4f524243);
287 master_header->version = ntohl(0x31313131);
288 master_header->romsize = htonl(romsize);
289 master_header->bootblocksize = htonl(bootblocksize);
290 master_header->align = htonl(align);
291 master_header->offset = htonl(0);
292 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
293 virt_to_phys(master_header);
294 struct cbfs_file *one_empty_file =
295 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
296 romsize - bootblocksize -
297 sizeof(struct cbfs_header) -
298 sizeof(struct cbfs_file) - 16);
300 writerom(romfile, romarea, romsize);