e1da4cb6fc4c1515e0cda1c206c3a9b96d162ab7
[coreboot.git] / util / cbfstool / util.c
1 /*
2  * cbfstool
3  *
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 #include "cbfstool.h"
27
28 int uninitialized_flash_value = 0xff;
29
30 void flashinit(void *ptr, size_t len)
31 {
32         memset(ptr, uninitialized_flash_value, len);
33 }
34
35 int get_size(const char *size)
36 {
37         char *next;
38         int val = strtoul(size, &next, 0);
39
40         /* Support modifiers for the size kK and mM for kbytes and 
41            mbytes respectfully */
42
43         if (next != NULL) {
44                 if (*next == 'k' || *next == 'K')
45                         val *= 1024;
46                 else if (*next == 'm' || *next == 'M')
47                         val *= (1024 * 1024);
48         }
49
50         return val;
51 }
52
53 int copy_from_fd(int fd, void *ptr, int size)
54 {
55         unsigned char *p = ptr;
56
57         while (size > 0) {
58                 int ret = read(fd, p, size);
59
60                 if (ret == -1) {
61                         ERROR("Error while reading: %m\n");
62                         return -1;
63                 }
64
65                 if (ret == 0) {
66                         ERROR("Unexpected end of file\n");
67                         return -1;
68                 }
69
70                 p += ret;
71                 size -= ret;
72         }
73
74         return 0;
75 }
76
77 int size_and_open(const char *filename, unsigned int *size)
78 {
79         struct stat s;
80
81         int fd = open(filename, O_RDONLY);
82
83         if (fd == -1) {
84                 ERROR("Unable to open %s: %m\n", filename);
85                 return -1;
86         }
87
88         if (fstat(fd, &s)) {
89                 ERROR("Unable to stat %s: %m\n", filename);
90                 close(fd);
91                 return -1;
92         }
93
94         *size = s.st_size;
95         return fd;
96 }
97
98 int map_rom(struct rom *rom, int size)
99 {
100         rom->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
101                         rom->fd, 0);
102
103         if (rom->ptr == MAP_FAILED) {
104                 ERROR("Could not map the rom: %m\n");
105                 rom->ptr = NULL;
106                 return -1;
107         }
108
109         return 0;
110 }
111
112 int open_rom(struct rom *rom, const char *filename)
113 {
114         struct stat s;
115         unsigned long offset;
116
117         if (stat(filename, &s)) {
118                 ERROR("Could not stat %s: %m\n", filename);
119                 return -1;
120         }
121
122         rom->fd = open(filename, O_RDWR);
123
124         if (rom->fd == -1) {
125                 ERROR("Could not open %s: %m\n", filename);
126                 rom->fd = 0;
127                 return -1;
128         }
129
130         if (map_rom(rom, s.st_size))
131                 goto err;
132
133         /* Find the master header */
134
135         offset = ROM_READL(rom, s.st_size - 4);
136
137         rom->header = (struct cbfs_header *)
138             ROM_PTR(rom, s.st_size - (0xFFFFFFFF - offset) - 1);
139
140         if (ntohl(rom->header->magic) != HEADER_MAGIC) {
141                 ERROR("This does not appear to be a valid ROM\n");
142                 goto err;
143         }
144
145         /* Check that the alignment is correct */
146         if (ntohl(rom->header->align) == 0) {
147                 ERROR("The alignment in the ROM is 0 - probably malformed\n");
148                 goto err;
149         }
150
151         /* Sanity check that the size matches the file size */
152
153         if (ntohl(rom->header->romsize) != s.st_size) {
154                 ERROR("The ROM size in the header does not match the file\n");
155                 ERROR("ROM size is %d bytes, file size is %d bytes\n",
156                       ntohl(rom->header->romsize), (unsigned int)s.st_size);
157                 goto err;
158         }
159
160         rom->size = ntohl(rom->header->romsize);
161         /* compute a 32-bit value of rombase. 
162          * This does the right thing on 64-bit machines. 
163          */
164         rom->rombase = 0-rom->size;
165         rom->rombase &= 0xffffffff;
166         rom->fssize = rom->size - ntohl(rom->header->bootblocksize);
167
168         return 0;
169
170 err:
171         if (rom->ptr != NULL)
172                 munmap(rom->ptr, s.st_size);
173
174         if (rom->fd > 0)
175                 close(rom->fd);
176
177         rom->ptr = NULL;
178         rom->fd = 0;
179         return -1;
180 }
181
182 int create_rom(struct rom *rom, const unsigned char *filename,
183                int romsize, const char *bootblockname,
184                int bootblocksize, int align)
185 {
186         unsigned char null = '\0';
187
188         if (rom->fd != 0) {
189                 ERROR("%s already exists - cannot create\n", filename);
190                 return -1;
191         }
192
193         /* Remember the size of the entire ROM */
194         rom->size = romsize;
195
196         /* The size of the archive section is everything but the bootblock and
197          * the cbfs master header. */
198         rom->fssize = romsize - bootblocksize - sizeof(struct cbfs_header);
199
200         /* Open the file descriptor */
201
202         rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
203
204         if (rom->fd == -1) {
205                 ERROR("Could not create %s: %m\n", filename);
206                 return -1;
207         }
208
209         /* Size the new file appropriately */
210         lseek(rom->fd, romsize - 1, SEEK_SET);
211         write(rom->fd, &null, 1);
212
213         if (map_rom(rom, romsize)) {
214                 close(rom->fd);
215                 return -1;
216         }
217
218         /* mmap'ed pages are by default zero-filled. Fix that. */
219         flashinit(rom->ptr, romsize);
220
221         /* This is a pointer to the header for easy access */
222         rom->header = (struct cbfs_header *)
223             ROM_PTR(rom, rom->size - 16 - bootblocksize - sizeof(struct cbfs_header));
224         rom->header->magic = htonl(HEADER_MAGIC);
225         rom->header->romsize = htonl(romsize);
226         rom->header->bootblocksize = htonl(bootblocksize);
227         rom->header->align = htonl(align);
228         rom->header->offset = htonl(0);
229
230         if (add_bootblock(rom, bootblockname) == -1)
231                 return -1;
232
233         /* Write the cbfs master header address at the end of the ROM. */
234
235         ROM_WRITEL(rom, rom->size - 4,
236                    0xFFFFFFF0 - bootblocksize - sizeof(struct cbfs_header));
237
238         /* write the empty header */
239         rom_set_header(rom, (struct cbfs_file *)rom->ptr, "", -1, CBFS_COMPONENT_NULL);
240         return 0;
241 }
242
243 int add_bootblock(struct rom *rom, const char *filename)
244 {
245         unsigned int size;
246         int fd = size_and_open(filename, &size);
247         int ret;
248
249         if (fd == -1)
250                 return -1;
251
252         if (size > ntohl(rom->header->bootblocksize)) {
253                 ERROR("The bootblock size is not correct (%d vs %d)\n",
254                       size, ntohl(rom->header->bootblocksize));
255                 return -1;
256         }
257
258         /* Copy the bootblock into place at the end of the file */
259         ret = copy_from_fd(fd, ROM_PTR(rom, rom->size - ntohl(rom->header->bootblocksize)), size);
260
261         close(fd);
262
263         if (ret) {
264                 ERROR("Unable to add %s to the bootblock\n", filename);
265                 return -1;
266         }
267
268         return 0;
269 }
270
271 int rom_exists(struct rom *rom)
272 {
273         if (rom->fd <= 0)
274                 return 0;
275         return 1;
276 }