There was a missing addition of the size of the .notes.pinfo
[coreboot.git] / util / cbfstool / common.c
1 /*
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
30 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
31                int place)
32 {
33         FILE *file = fopen(filename, "rb");
34         if (file == NULL)
35                 return NULL;
36         fseek(file, 0, SEEK_END);
37         *romsize_p = ftell(file);
38         fseek(file, 0, SEEK_SET);
39         if (!content)
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);
45                 return NULL;
46         }
47         fclose(file);
48         return content;
49 }
50
51 struct cbfs_header *master_header;
52 uint32_t phys_start, phys_end, align, romsize;
53 void *offset;
54
55 void recalculate_rom_geometry(void *romarea)
56 {
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;
61         phys_end =
62             (0 - ntohl(master_header->bootblocksize) -
63              sizeof(struct cbfs_header)) & 0xffffffff;
64         align = ntohl(master_header->align);
65 }
66
67 void *loadrom(const char *filename)
68 {
69         void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
70         if (romarea == NULL)
71                 return NULL;
72         recalculate_rom_geometry(romarea);
73         return romarea;
74 }
75
76 void writerom(const char *filename, void *start, uint32_t size)
77 {
78         FILE *file = fopen(filename, "wb");
79         fwrite(start, size, 1, file);
80         fclose(file);
81 }
82
83 int cbfs_file_header(uint32_t physaddr)
84 {
85         /* maybe improve this test */
86         return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
87 }
88
89 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
90 {
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);
98         return nextfile;
99 }
100
101 int iself(unsigned char *input)
102 {
103         Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
104         return !memcmp(ehdr->e_ident, ELFMAG, 4);
105 }
106
107 struct filetypes_t {
108         uint32_t type;
109         const char *name;
110 } filetypes[] = {
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"}
116 };
117
118 const char *strfiletype(uint32_t number)
119 {
120         int i;
121         for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
122                 if (filetypes[i].type == number)
123                         return filetypes[i].name;
124         return "unknown";
125 }
126
127 uint64_t intfiletype(const char *name)
128 {
129         int i;
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;
133         return -1;
134 }
135
136 void print_cbfs_directory(const char *filename)
137 {
138         printf
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)) {
146                         current += align;
147                         continue;
148                 }
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)),
156                        length);
157                 current =
158                     ALIGN(current + ntohl(thisfile->len) +
159                           ntohl(thisfile->offset), align);
160         }
161 }
162
163 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
164 {
165         uint32_t current = phys_start;
166         while (current < phys_end) {
167                 if (!cbfs_file_header(current)) {
168                         current += align;
169                         continue;
170                 }
171                 struct cbfs_file *thisfile =
172                     (struct cbfs_file *)phys_to_virt(current);
173                 uint32_t length = ntohl(thisfile->len);
174
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,
180                                 length);
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) {
185                                         dprintf
186                                             ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
187                                              length, contentsize,
188                                              ALIGN(current + contentsize,
189                                                    align),
190                                              length - contentsize);
191                                         uint32_t start =
192                                             ALIGN(current + contentsize, align);
193                                         uint32_t size =
194                                             current + ntohl(thisfile->offset)
195                                             + length - start - 16 -
196                                             sizeof(struct cbfs_file);
197                                         cbfs_create_empty_file(start, size);
198                                 }
199                                 dprintf("copying data\n");
200                                 memcpy(phys_to_virt(current), content,
201                                        contentsize);
202                                 break;
203                         }
204                         if (location == 0)
205                                 continue;
206
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) {
210                                 printf
211                                     ("the requested space is not available\n");
212                                 return 1;
213                         }
214
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",
220                                         location - current -
221                                         ntohl(thisfile->offset));
222                                 thisfile->len =
223                                     htonl(location - current -
224                                           ntohl(thisfile->offset));
225                                 struct cbfs_file *nextfile =
226                                     cbfs_create_empty_file(location,
227                                                            length - (location -
228                                                                      current));
229                         }
230                 }
231                 current =
232                     ALIGN(current + ntohl(thisfile->len) +
233                           ntohl(thisfile->offset), align);
234         }
235         return 0;
236 }
237
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)
242 {
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);
251                 } else {
252                         offset += align - (headersize % align);
253                 }
254                 headersize += offset;
255                 *location -= headersize;
256         }
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;
267         return newdata;
268 }
269
270 int create_cbfs_image(const char *romfile, uint32_t _romsize,
271                       const char *bootblock, uint32_t align)
272 {
273         romsize = _romsize;
274         unsigned char *romarea = malloc(romsize);
275         memset(romarea, 0xff, romsize);
276         recalculate_rom_geometry(romarea);
277
278         if (align == 0)
279                 align = 64;
280
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);
299
300         writerom(romfile, romarea, romsize);
301         return 0;
302 }