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");
34 fseek(file, 0, SEEK_END);
35 *romsize_p = ftell(file);
36 fseek(file, 0, SEEK_SET);
38 content = malloc(*romsize_p);
39 else if (place == SEEK_END)
40 content -= *romsize_p;
41 if (!fread(content, *romsize_p, 1, file)) {
42 printf("failed to read %s\n", filename);
49 struct cbfs_header *master_header;
50 uint32_t phys_start, phys_end, align, romsize;
53 void recalculate_rom_geometry(void *romarea)
55 offset = romarea + romsize - 0x100000000ULL;
56 master_header = (struct cbfs_header *)
57 phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
58 phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
60 (0 - ntohl(master_header->bootblocksize) -
61 sizeof(struct cbfs_header)) & 0xffffffff;
62 align = ntohl(master_header->align);
65 void *loadrom(const char *filename)
67 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
68 recalculate_rom_geometry(romarea);
72 void writerom(const char *filename, void *start, uint32_t size)
74 FILE *file = fopen(filename, "wb");
75 fwrite(start, size, 1, file);
79 int cbfs_file_header(uint32_t physaddr)
81 /* maybe improve this test */
82 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
85 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
87 struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
88 strncpy(nextfile->magic, "LARCHIVE", 8);
89 nextfile->len = htonl(size);
90 nextfile->type = htonl(0xffffffff);
91 nextfile->checksum = 0; // FIXME?
92 nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
93 memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
97 int iself(unsigned char *input)
99 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
100 return !memcmp(ehdr->e_ident, ELFMAG, 4);
107 {CBFS_COMPONENT_STAGE, "stage"},
108 {CBFS_COMPONENT_PAYLOAD, "payload"},
109 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
110 {CBFS_COMPONENT_DELETED, "deleted"},
111 {CBFS_COMPONENT_NULL, "null"}
114 const char *strfiletype(uint32_t number)
117 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
118 if (filetypes[i].type == number)
119 return filetypes[i].name;
123 uint64_t intfiletype(const char *name)
126 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
127 if (strcmp(filetypes[i].name, name) == 0)
128 return filetypes[i].type;
132 void print_cbfs_directory(const char *filename)
135 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
136 filename, romsize / 1024, ntohl(master_header->bootblocksize),
137 romsize, ntohl(master_header->offset), align);
138 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
139 uint32_t current = phys_start;
140 while (current < phys_end) {
141 if (!cbfs_file_header(current)) {
145 struct cbfs_file *thisfile =
146 (struct cbfs_file *)phys_to_virt(current);
147 uint32_t length = ntohl(thisfile->len);
148 printf("%-30s 0x%-8x %-12s %d\n",
149 (const char *)(phys_to_virt(current) +
150 sizeof(struct cbfs_file)),
151 current - phys_start, strfiletype(ntohl(thisfile->type)),
154 ALIGN(current + ntohl(thisfile->len) +
155 ntohl(thisfile->offset), align);
159 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
161 uint32_t current = phys_start;
162 while (current < phys_end) {
163 if (!cbfs_file_header(current)) {
167 struct cbfs_file *thisfile =
168 (struct cbfs_file *)phys_to_virt(current);
169 uint32_t length = ntohl(thisfile->len);
171 dprintf("at %x, %x bytes\n", current, length);
172 /* Is this a free chunk? */
173 if ((thisfile->type == CBFS_COMPONENT_DELETED)
174 || (thisfile->type == CBFS_COMPONENT_NULL)) {
175 dprintf("null||deleted at %x, %x bytes\n", current,
177 /* if this is the right size, and if specified, the right location, use it */
178 if ((contentsize <= length)
179 && ((location == 0) || (current == location))) {
180 if (contentsize < length) {
182 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
184 ALIGN(current + contentsize,
186 length - contentsize);
188 ALIGN(current + contentsize, align);
190 current + ntohl(thisfile->offset)
191 + length - start - 16 -
192 sizeof(struct cbfs_file);
193 cbfs_create_empty_file(start, size);
195 dprintf("copying data\n");
196 memcpy(phys_to_virt(current), content,
203 /* CBFS has the constraint that the chain always moves up in memory. so once
204 we're past the place we seek, we don't need to look any further */
205 if (current > location) {
207 ("the requested space is not available\n");
211 /* Is the requested location inside the current chunk? */
212 if ((current < location)
213 && ((location + contentsize) <= (current + length))) {
214 /* Split it up. In the next iteration the code will be at the right place. */
215 dprintf("split up. new length: %x\n",
217 ntohl(thisfile->offset));
219 htonl(location - current -
220 ntohl(thisfile->offset));
221 struct cbfs_file *nextfile =
222 cbfs_create_empty_file(location,
228 ALIGN(current + ntohl(thisfile->len) +
229 ntohl(thisfile->offset), align);
234 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
235 the new location that points to the cbfs_file header */
236 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
237 uint32_t type, uint32_t * location)
239 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
240 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
241 if ((location != 0) && (*location != 0)) {
242 uint32_t offset = *location % align;
243 /* If offset >= (headersize % align), we can stuff the header into the offset.
244 Otherwise the header has to be aligned itself, and put before the offset data */
245 if (offset >= (headersize % align)) {
246 offset -= (headersize % align);
248 offset += align - (headersize % align);
250 headersize += offset;
251 *location -= headersize;
253 void *newdata = malloc(*datasize + headersize);
254 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
255 strncpy(nextfile->magic, "LARCHIVE", 8);
256 nextfile->len = htonl(*datasize);
257 nextfile->type = htonl(type);
258 nextfile->checksum = 0; // FIXME?
259 nextfile->offset = htonl(headersize);
260 strcpy(newdata + sizeof(struct cbfs_file), filename);
261 memcpy(newdata + headersize, data, *datasize);
262 *datasize += headersize;
266 int create_cbfs_image(const char *romfile, uint32_t _romsize,
267 const char *bootblock, uint32_t align)
270 unsigned char *romarea = malloc(romsize);
271 memset(romarea, 0xff, romsize);
272 recalculate_rom_geometry(romarea);
277 uint32_t bootblocksize = 0;
278 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
279 struct cbfs_header *master_header =
280 (struct cbfs_header *)(romarea + romsize - bootblocksize -
281 sizeof(struct cbfs_header));
282 master_header->magic = ntohl(0x4f524243);
283 master_header->version = ntohl(0x31313131);
284 master_header->romsize = htonl(romsize);
285 master_header->bootblocksize = htonl(bootblocksize);
286 master_header->align = htonl(align);
287 master_header->offset = htonl(0);
288 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
289 virt_to_phys(master_header);
290 struct cbfs_file *one_empty_file =
291 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
292 romsize - bootblocksize -
293 sizeof(struct cbfs_header) -
294 sizeof(struct cbfs_file) - 16);
296 writerom(romfile, romarea, romsize);