blob: 30d52816b100f811ce22069376419acff1c2b1a1 [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
29#define dprintf
30
Patrick Georgi0da38dd2009-11-09 17:18:02 +000031uint32_t getfilesize(const char *filename)
32{
33 uint32_t size;
34 FILE *file = fopen(filename, "rb");
35 fseek(file, 0, SEEK_END);
36 size = ftell(file);
37 fclose(file);
38 return size;
39}
40
Patrick Georgib7b56dd82009-09-14 13:29:27 +000041void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
42 int place)
43{
44 FILE *file = fopen(filename, "rb");
Patrick Georgi45d8a832009-09-15 08:21:46 +000045 if (file == NULL)
46 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000047 fseek(file, 0, SEEK_END);
48 *romsize_p = ftell(file);
49 fseek(file, 0, SEEK_SET);
Stefan Reinauer853270a2009-09-22 15:55:01 +000050 if (!content) {
Patrick Georgib7b56dd82009-09-14 13:29:27 +000051 content = malloc(*romsize_p);
Stefan Reinauer853270a2009-09-22 15:55:01 +000052 if (!content) {
53 printf("Could not get %d bytes for file %s\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +000054 *romsize_p, filename);
Stefan Reinauer853270a2009-09-22 15:55:01 +000055 exit(1);
56 }
57 } else if (place == SEEK_END)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000058 content -= *romsize_p;
Stefan Reinauer853270a2009-09-22 15:55:01 +000059
Patrick Georgib7b56dd82009-09-14 13:29:27 +000060 if (!fread(content, *romsize_p, 1, file)) {
61 printf("failed to read %s\n", filename);
62 return NULL;
63 }
64 fclose(file);
65 return content;
66}
67
68struct cbfs_header *master_header;
69uint32_t phys_start, phys_end, align, romsize;
70void *offset;
71
72void recalculate_rom_geometry(void *romarea)
73{
74 offset = romarea + romsize - 0x100000000ULL;
75 master_header = (struct cbfs_header *)
76 phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
77 phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
78 phys_end =
79 (0 - ntohl(master_header->bootblocksize) -
80 sizeof(struct cbfs_header)) & 0xffffffff;
81 align = ntohl(master_header->align);
82}
83
84void *loadrom(const char *filename)
85{
86 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
Patrick Georgi45d8a832009-09-15 08:21:46 +000087 if (romarea == NULL)
88 return NULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000089 recalculate_rom_geometry(romarea);
90 return romarea;
91}
92
93void writerom(const char *filename, void *start, uint32_t size)
94{
95 FILE *file = fopen(filename, "wb");
96 fwrite(start, size, 1, file);
97 fclose(file);
98}
99
100int cbfs_file_header(uint32_t physaddr)
101{
102 /* maybe improve this test */
103 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
104}
105
106struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
107{
108 struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
109 strncpy(nextfile->magic, "LARCHIVE", 8);
110 nextfile->len = htonl(size);
111 nextfile->type = htonl(0xffffffff);
112 nextfile->checksum = 0; // FIXME?
113 nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
114 memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
115 return nextfile;
116}
117
118int iself(unsigned char *input)
119{
120 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
121 return !memcmp(ehdr->e_ident, ELFMAG, 4);
122}
123
124struct filetypes_t {
125 uint32_t type;
126 const char *name;
127} filetypes[] = {
128 {CBFS_COMPONENT_STAGE, "stage"},
129 {CBFS_COMPONENT_PAYLOAD, "payload"},
130 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
Stefan Reinauer800379f2010-03-01 08:34:19 +0000131 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
132 {CBFS_COMPONENT_RAW, "raw"},
133 {CBFS_COMPONENT_VSA, "vsa"},
134 {CBFS_COMPONENT_MBI, "mbi"},
135 {CBFS_COMPONENT_MICROCODE, "microcode"},
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000136 {CBFS_COMPONENT_DELETED, "deleted"},
137 {CBFS_COMPONENT_NULL, "null"}
138};
139
Stefan Reinauer07040582010-04-24 21:24:06 +0000140void print_supported_filetypes(void)
141{
142 int i, number = ARRAY_SIZE(filetypes);
143
144 for (i=0; i<number; i++) {
145 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
146 if ((i%8) == 7)
147 printf("\n");
148 }
149}
150
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000151const char *strfiletype(uint32_t number)
152{
153 int i;
154 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
155 if (filetypes[i].type == number)
156 return filetypes[i].name;
157 return "unknown";
158}
159
160uint64_t intfiletype(const char *name)
161{
162 int i;
163 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
164 if (strcmp(filetypes[i].name, name) == 0)
165 return filetypes[i].type;
166 return -1;
167}
168
169void print_cbfs_directory(const char *filename)
170{
171 printf
172 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
Uwe Hermann942a40d2010-02-10 19:52:35 +0000173 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000174 romsize, ntohl(master_header->offset), align);
175 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
176 uint32_t current = phys_start;
177 while (current < phys_end) {
178 if (!cbfs_file_header(current)) {
179 current += align;
180 continue;
181 }
182 struct cbfs_file *thisfile =
183 (struct cbfs_file *)phys_to_virt(current);
184 uint32_t length = ntohl(thisfile->len);
Maciej Pijankaf44eb782010-01-07 21:37:18 +0000185 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
186 if (strlen(fname) == 0)
187 fname = "(empty)";
188
189 printf("%-30s 0x%-8x %-12s %d\n", fname,
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000190 current - phys_start, strfiletype(ntohl(thisfile->type)),
191 length);
192 current =
193 ALIGN(current + ntohl(thisfile->len) +
194 ntohl(thisfile->offset), align);
195 }
196}
197
198int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
199{
200 uint32_t current = phys_start;
201 while (current < phys_end) {
202 if (!cbfs_file_header(current)) {
203 current += align;
204 continue;
205 }
206 struct cbfs_file *thisfile =
207 (struct cbfs_file *)phys_to_virt(current);
208 uint32_t length = ntohl(thisfile->len);
209
210 dprintf("at %x, %x bytes\n", current, length);
211 /* Is this a free chunk? */
212 if ((thisfile->type == CBFS_COMPONENT_DELETED)
213 || (thisfile->type == CBFS_COMPONENT_NULL)) {
214 dprintf("null||deleted at %x, %x bytes\n", current,
215 length);
216 /* if this is the right size, and if specified, the right location, use it */
217 if ((contentsize <= length)
218 && ((location == 0) || (current == location))) {
219 if (contentsize < length) {
220 dprintf
221 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
222 length, contentsize,
223 ALIGN(current + contentsize,
224 align),
225 length - contentsize);
226 uint32_t start =
227 ALIGN(current + contentsize, align);
228 uint32_t size =
229 current + ntohl(thisfile->offset)
230 + length - start - 16 -
231 sizeof(struct cbfs_file);
232 cbfs_create_empty_file(start, size);
233 }
234 dprintf("copying data\n");
235 memcpy(phys_to_virt(current), content,
236 contentsize);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000237 return 0;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000238 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000239 if (location != 0) {
240 /* CBFS has the constraint that the chain always moves up in memory. so once
241 we're past the place we seek, we don't need to look any further */
242 if (current > location) {
243 printf
244 ("the requested space is not available\n");
245 return 1;
246 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000247
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000248 /* Is the requested location inside the current chunk? */
249 if ((current < location)
250 && ((location + contentsize) <=
251 (current + length))) {
252 /* Split it up. In the next iteration the code will be at the right place. */
253 dprintf("split up. new length: %x\n",
254 location - current -
255 ntohl(thisfile->offset));
256 thisfile->len =
257 htonl(location - current -
258 ntohl(thisfile->offset));
259 struct cbfs_file *nextfile =
260 cbfs_create_empty_file(location,
261 length -
262 (location -
263 current));
264 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000265 }
266 }
267 current =
268 ALIGN(current + ntohl(thisfile->len) +
269 ntohl(thisfile->offset), align);
270 }
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000271 printf("Could not add the file to CBFS, it's probably too big.\n");
Uwe Hermann6a3ca4e2009-11-12 20:06:32 +0000272 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000273 return 1;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000274}
275
276/* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
277 the new location that points to the cbfs_file header */
278void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
279 uint32_t type, uint32_t * location)
280{
281 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
282 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
283 if ((location != 0) && (*location != 0)) {
284 uint32_t offset = *location % align;
285 /* If offset >= (headersize % align), we can stuff the header into the offset.
286 Otherwise the header has to be aligned itself, and put before the offset data */
287 if (offset >= (headersize % align)) {
288 offset -= (headersize % align);
289 } else {
290 offset += align - (headersize % align);
291 }
292 headersize += offset;
293 *location -= headersize;
294 }
295 void *newdata = malloc(*datasize + headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000296 if (!newdata) {
297 printf("Could not get %d bytes for CBFS file.\n", *datasize +
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000298 headersize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000299 exit(1);
300 }
Peter Stugef4aca1d2009-12-06 12:14:39 +0000301 memset(newdata, 0xff, *datasize + headersize);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000302 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
303 strncpy(nextfile->magic, "LARCHIVE", 8);
304 nextfile->len = htonl(*datasize);
305 nextfile->type = htonl(type);
306 nextfile->checksum = 0; // FIXME?
307 nextfile->offset = htonl(headersize);
308 strcpy(newdata + sizeof(struct cbfs_file), filename);
309 memcpy(newdata + headersize, data, *datasize);
310 *datasize += headersize;
311 return newdata;
312}
313
314int create_cbfs_image(const char *romfile, uint32_t _romsize,
315 const char *bootblock, uint32_t align)
316{
317 romsize = _romsize;
318 unsigned char *romarea = malloc(romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000319 if (!romarea) {
320 printf("Could not get %d bytes of memory for CBFS image.\n",
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000321 romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000322 exit(1);
323 }
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000324 memset(romarea, 0xff, romsize);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000325
326 // Set up physical/virtual mapping
327 offset = romarea + romsize - 0x100000000ULL;
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000328
329 if (align == 0)
330 align = 64;
331
332 uint32_t bootblocksize = 0;
333 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
334 struct cbfs_header *master_header =
335 (struct cbfs_header *)(romarea + romsize - bootblocksize -
336 sizeof(struct cbfs_header));
337 master_header->magic = ntohl(0x4f524243);
338 master_header->version = ntohl(0x31313131);
339 master_header->romsize = htonl(romsize);
340 master_header->bootblocksize = htonl(bootblocksize);
341 master_header->align = htonl(align);
342 master_header->offset = htonl(0);
343 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
344 virt_to_phys(master_header);
Stefan Reinauer853270a2009-09-22 15:55:01 +0000345
346 recalculate_rom_geometry(romarea);
347
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000348 struct cbfs_file *one_empty_file =
349 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
350 romsize - bootblocksize -
351 sizeof(struct cbfs_header) -
352 sizeof(struct cbfs_file) - 16);
353
354 writerom(romfile, romarea, romsize);
355 return 0;
356}
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000357
358static int in_segment(int addr, int size, int gran)
359{
360 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
361}
362
363uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
364 const char *filename, uint32_t alignment)
365{
366 void *rom = loadrom(romfile);
367 int filename_size = strlen(filename);
368
369 int headersize =
370 sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
371 16) + sizeof(struct cbfs_stage);
372 int totalsize = headersize + filesize;
373
374 uint32_t current = phys_start;
375 while (current < phys_end) {
376 if (!cbfs_file_header(current)) {
377 current += align;
378 continue;
379 }
380 struct cbfs_file *thisfile =
381 (struct cbfs_file *)phys_to_virt(current);
382
383 uint32_t top =
384 current + ntohl(thisfile->len) + ntohl(thisfile->offset);
385 if (((ntohl(thisfile->type) == 0x0)
386 || (ntohl(thisfile->type) == 0xffffffff))
387 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
388 totalsize)) {
389 if (in_segment
390 (current + headersize, filesize, alignment))
391 return current + headersize;
392 if ((ALIGN(current, alignment) + filesize < top)
393 && (ALIGN(current, alignment) - headersize >
394 current)
395 && in_segment(ALIGN(current, alignment), filesize,
396 alignment))
397 return ALIGN(current, alignment);
398 if ((ALIGN(current, alignment) + alignment + filesize <
399 top)
400 && (ALIGN(current, alignment) + alignment -
401 headersize > current)
402 && in_segment(ALIGN(current, alignment) + alignment,
403 filesize, alignment))
404 return ALIGN(current, alignment) + alignment;
405 }
406 current =
407 ALIGN(current + ntohl(thisfile->len) +
408 ntohl(thisfile->offset), align);
409 }
410 return 0;
411}