blob: dd675cac03128392af2b4d2adeea824005bfe7ca [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>
24#include "common.h"
25#include "cbfs.h"
26#include "elf.h"
27
28#define dprintf
29
Patrick Georgi0da38dd2009-11-09 17:18:02 +000030uint32_t getfilesize(const char *filename)
31{
32 uint32_t size;
33 FILE *file = fopen(filename, "rb");
34 fseek(file, 0, SEEK_END);
35 size = ftell(file);
36 fclose(file);
37 return size;
38}
39
Patrick Georgib7b56dd82009-09-14 13:29:27 +000040void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
41 int place)
42{
43 FILE *file = fopen(filename, "rb");
Patrick Georgi45d8a832009-09-15 08:21:46 +000044 if (file == NULL)
45 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000046 fseek(file, 0, SEEK_END);
47 *romsize_p = ftell(file);
48 fseek(file, 0, SEEK_SET);
Stefan Reinauer853270a2009-09-22 15:55:01 +000049 if (!content) {
Patrick Georgib7b56dd82009-09-14 13:29:27 +000050 content = malloc(*romsize_p);
Stefan Reinauer853270a2009-09-22 15:55:01 +000051 if (!content) {
52 printf("Could not get %d bytes for file %s\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +000053 *romsize_p, filename);
Stefan Reinauer853270a2009-09-22 15:55:01 +000054 exit(1);
55 }
56 } else if (place == SEEK_END)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000057 content -= *romsize_p;
Stefan Reinauer853270a2009-09-22 15:55:01 +000058
Patrick Georgib7b56dd82009-09-14 13:29:27 +000059 if (!fread(content, *romsize_p, 1, file)) {
60 printf("failed to read %s\n", filename);
61 return NULL;
62 }
63 fclose(file);
64 return content;
65}
66
67struct cbfs_header *master_header;
68uint32_t phys_start, phys_end, align, romsize;
69void *offset;
70
71void recalculate_rom_geometry(void *romarea)
72{
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;
77 phys_end =
78 (0 - ntohl(master_header->bootblocksize) -
79 sizeof(struct cbfs_header)) & 0xffffffff;
80 align = ntohl(master_header->align);
81}
82
83void *loadrom(const char *filename)
84{
85 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
Patrick Georgi45d8a832009-09-15 08:21:46 +000086 if (romarea == NULL)
87 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000088 recalculate_rom_geometry(romarea);
89 return romarea;
90}
91
92void writerom(const char *filename, void *start, uint32_t size)
93{
94 FILE *file = fopen(filename, "wb");
95 fwrite(start, size, 1, file);
96 fclose(file);
97}
98
99int cbfs_file_header(uint32_t physaddr)
100{
101 /* maybe improve this test */
102 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
103}
104
105struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
106{
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);
114 return nextfile;
115}
116
117int iself(unsigned char *input)
118{
119 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
120 return !memcmp(ehdr->e_ident, ELFMAG, 4);
121}
122
123struct filetypes_t {
124 uint32_t type;
125 const char *name;
126} filetypes[] = {
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"}
132};
133
134const char *strfiletype(uint32_t number)
135{
136 int i;
137 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
138 if (filetypes[i].type == number)
139 return filetypes[i].name;
140 return "unknown";
141}
142
143uint64_t intfiletype(const char *name)
144{
145 int i;
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;
149 return -1;
150}
151
152void print_cbfs_directory(const char *filename)
153{
154 printf
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)) {
162 current += align;
163 continue;
164 }
165 struct cbfs_file *thisfile =
166 (struct cbfs_file *)phys_to_virt(current);
167 uint32_t length = ntohl(thisfile->len);
168 printf("%-30s 0x%-8x %-12s %d\n",
169 (const char *)(phys_to_virt(current) +
170 sizeof(struct cbfs_file)),
171 current - phys_start, strfiletype(ntohl(thisfile->type)),
172 length);
173 current =
174 ALIGN(current + ntohl(thisfile->len) +
175 ntohl(thisfile->offset), align);
176 }
177}
178
179int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
180{
181 uint32_t current = phys_start;
182 while (current < phys_end) {
183 if (!cbfs_file_header(current)) {
184 current += align;
185 continue;
186 }
187 struct cbfs_file *thisfile =
188 (struct cbfs_file *)phys_to_virt(current);
189 uint32_t length = ntohl(thisfile->len);
190
191 dprintf("at %x, %x bytes\n", current, length);
192 /* Is this a free chunk? */
193 if ((thisfile->type == CBFS_COMPONENT_DELETED)
194 || (thisfile->type == CBFS_COMPONENT_NULL)) {
195 dprintf("null||deleted at %x, %x bytes\n", current,
196 length);
197 /* if this is the right size, and if specified, the right location, use it */
198 if ((contentsize <= length)
199 && ((location == 0) || (current == location))) {
200 if (contentsize < length) {
201 dprintf
202 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
203 length, contentsize,
204 ALIGN(current + contentsize,
205 align),
206 length - contentsize);
207 uint32_t start =
208 ALIGN(current + contentsize, align);
209 uint32_t size =
210 current + ntohl(thisfile->offset)
211 + length - start - 16 -
212 sizeof(struct cbfs_file);
213 cbfs_create_empty_file(start, size);
214 }
215 dprintf("copying data\n");
216 memcpy(phys_to_virt(current), content,
217 contentsize);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000218 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000219 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000220 if (location != 0) {
221 /* CBFS has the constraint that the chain always moves up in memory. so once
222 we're past the place we seek, we don't need to look any further */
223 if (current > location) {
224 printf
225 ("the requested space is not available\n");
226 return 1;
227 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000228
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000229 /* Is the requested location inside the current chunk? */
230 if ((current < location)
231 && ((location + contentsize) <=
232 (current + length))) {
233 /* Split it up. In the next iteration the code will be at the right place. */
234 dprintf("split up. new length: %x\n",
235 location - current -
236 ntohl(thisfile->offset));
237 thisfile->len =
238 htonl(location - current -
239 ntohl(thisfile->offset));
240 struct cbfs_file *nextfile =
241 cbfs_create_empty_file(location,
242 length -
243 (location -
244 current));
245 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000246 }
247 }
248 current =
249 ALIGN(current + ntohl(thisfile->len) +
250 ntohl(thisfile->offset), align);
251 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000252 printf("Could not add the file to CBFS, it's probably too big.\n");
Uwe Hermann6a3ca4e2009-11-12 20:06:32 +0000253 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000254 return 1;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000255}
256
257/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
258 the new location that points to the cbfs_file header */
259void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
260 uint32_t type, uint32_t * location)
261{
262 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
263 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
264 if ((location != 0) && (*location != 0)) {
265 uint32_t offset = *location % align;
266 /* If offset >= (headersize % align), we can stuff the header into the offset.
267 Otherwise the header has to be aligned itself, and put before the offset data */
268 if (offset >= (headersize % align)) {
269 offset -= (headersize % align);
270 } else {
271 offset += align - (headersize % align);
272 }
273 headersize += offset;
274 *location -= headersize;
275 }
276 void *newdata = malloc(*datasize + headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000277 if (!newdata) {
278 printf("Could not get %d bytes for CBFS file.\n", *datasize +
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000279 headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000280 exit(1);
281 }
Peter Stugef4aca1d2009-12-06 12:14:39 +0000282 memset(newdata, 0xff, *datasize + headersize);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000283 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
284 strncpy(nextfile->magic, "LARCHIVE", 8);
285 nextfile->len = htonl(*datasize);
286 nextfile->type = htonl(type);
287 nextfile->checksum = 0; // FIXME?
288 nextfile->offset = htonl(headersize);
289 strcpy(newdata + sizeof(struct cbfs_file), filename);
290 memcpy(newdata + headersize, data, *datasize);
291 *datasize += headersize;
292 return newdata;
293}
294
295int create_cbfs_image(const char *romfile, uint32_t _romsize,
296 const char *bootblock, uint32_t align)
297{
298 romsize = _romsize;
299 unsigned char *romarea = malloc(romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000300 if (!romarea) {
301 printf("Could not get %d bytes of memory for CBFS image.\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000302 romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000303 exit(1);
304 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000305 memset(romarea, 0xff, romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000306
307 // Set up physical/virtual mapping
308 offset = romarea + romsize - 0x100000000ULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000309
310 if (align == 0)
311 align = 64;
312
313 uint32_t bootblocksize = 0;
314 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
315 struct cbfs_header *master_header =
316 (struct cbfs_header *)(romarea + romsize - bootblocksize -
317 sizeof(struct cbfs_header));
318 master_header->magic = ntohl(0x4f524243);
319 master_header->version = ntohl(0x31313131);
320 master_header->romsize = htonl(romsize);
321 master_header->bootblocksize = htonl(bootblocksize);
322 master_header->align = htonl(align);
323 master_header->offset = htonl(0);
324 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
325 virt_to_phys(master_header);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000326
327 recalculate_rom_geometry(romarea);
328
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000329 struct cbfs_file *one_empty_file =
330 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
331 romsize - bootblocksize -
332 sizeof(struct cbfs_header) -
333 sizeof(struct cbfs_file) - 16);
334
335 writerom(romfile, romarea, romsize);
336 return 0;
337}
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000338
339static int in_segment(int addr, int size, int gran)
340{
341 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
342}
343
344uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
345 const char *filename, uint32_t alignment)
346{
347 void *rom = loadrom(romfile);
348 int filename_size = strlen(filename);
349
350 int headersize =
351 sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
352 16) + sizeof(struct cbfs_stage);
353 int totalsize = headersize + filesize;
354
355 uint32_t current = phys_start;
356 while (current < phys_end) {
357 if (!cbfs_file_header(current)) {
358 current += align;
359 continue;
360 }
361 struct cbfs_file *thisfile =
362 (struct cbfs_file *)phys_to_virt(current);
363
364 uint32_t top =
365 current + ntohl(thisfile->len) + ntohl(thisfile->offset);
366 if (((ntohl(thisfile->type) == 0x0)
367 || (ntohl(thisfile->type) == 0xffffffff))
368 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
369 totalsize)) {
370 if (in_segment
371 (current + headersize, filesize, alignment))
372 return current + headersize;
373 if ((ALIGN(current, alignment) + filesize < top)
374 && (ALIGN(current, alignment) - headersize >
375 current)
376 && in_segment(ALIGN(current, alignment), filesize,
377 alignment))
378 return ALIGN(current, alignment);
379 if ((ALIGN(current, alignment) + alignment + filesize <
380 top)
381 && (ALIGN(current, alignment) + alignment -
382 headersize > current)
383 && in_segment(ALIGN(current, alignment) + alignment,
384 filesize, alignment))
385 return ALIGN(current, alignment) + alignment;
386 }
387 current =
388 ALIGN(current + ntohl(thisfile->len) +
389 ntohl(thisfile->offset), align);
390 }
391 return 0;
392}