b36e9187a08f2a9577157a7d061ec525a1306ff0
[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 get_size(const char *size)
29 {
30         char *next;
31         int val = strtoul(size, &next, 0);
32
33         /* Support modifiers for the size kK and mM for kbytes and 
34            mbytes respectfully */
35
36         if (next != NULL) {
37                 if (*next == 'k' || *next == 'K')
38                         val *= 1024;
39                 else if (*next == 'm' || *next == 'M')
40                         val *= (1024 * 1024);
41         }
42
43         return val;
44 }
45
46 int copy_from_fd(int fd, void *ptr, int size)
47 {
48         unsigned char *p = ptr;
49
50         while (size > 0) {
51                 int ret = read(fd, p, size);
52
53                 if (ret == -1) {
54                         ERROR("Error while reading: %m\n");
55                         return -1;
56                 }
57
58                 if (ret == 0) {
59                         ERROR("Unexpected end of file\n");
60                         return -1;
61                 }
62
63                 p += ret;
64                 size -= ret;
65         }
66
67         return 0;
68 }
69
70 int size_and_open(const char *filename, unsigned int *size)
71 {
72         struct stat s;
73
74         int fd = open(filename, O_RDONLY);
75
76         if (fd == -1) {
77                 ERROR("Unable to open %s: %m\n", filename);
78                 return -1;
79         }
80
81         if (fstat(fd, &s)) {
82                 ERROR("Unable to stat %s: %m\n", filename);
83                 close(fd);
84                 return -1;
85         }
86
87         *size = s.st_size;
88         return fd;
89 }
90
91 int map_rom(struct rom *rom, int size)
92 {
93         rom->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
94                         rom->fd, 0);
95
96         if (rom->ptr == MAP_FAILED) {
97                 ERROR("Could not map the rom: %m\n");
98                 rom->ptr = NULL;
99                 return -1;
100         }
101
102         return 0;
103 }
104
105 int open_rom(struct rom *rom, const char *filename)
106 {
107         struct stat s;
108         unsigned long offset;
109
110         if (stat(filename, &s)) {
111                 ERROR("Could not stat %s: %m\n", filename);
112                 return -1;
113         }
114
115         rom->fd = open(filename, O_RDWR);
116
117         if (rom->fd == -1) {
118                 ERROR("Could not open %s: %m\n", filename);
119                 rom->fd = 0;
120                 return -1;
121         }
122
123         if (map_rom(rom, s.st_size))
124                 goto err;
125
126         /* Find the master header */
127
128         offset = ROM_READL(rom, s.st_size - 4);
129
130         rom->header = (struct cbfs_header *)
131             ROM_PTR(rom, s.st_size - (0xFFFFFFFF - offset) - 1);
132
133         if (ntohl(rom->header->magic) != HEADER_MAGIC) {
134                 ERROR("This does not appear to be a valid ROM\n");
135                 goto err;
136         }
137
138         /* Check that the alignment is correct */
139         if (ntohl(rom->header->align) == 0) {
140                 ERROR("The alignment in the ROM is 0 - probably malformed\n");
141                 goto err;
142         }
143
144         /* Sanity check that the size matches the file size */
145
146         if (ntohl(rom->header->romsize) != s.st_size) {
147                 ERROR("The ROM size in the header does not match the file\n");
148                 ERROR("ROM size is %d bytes, file size is %d bytes\n",
149                       ntohl(rom->header->romsize), (unsigned int)s.st_size);
150                 goto err;
151         }
152
153         rom->size = ntohl(rom->header->romsize);
154         rom->fssize = rom->size - ntohl(rom->header->bootblocksize);
155
156         return 0;
157
158 err:
159         if (rom->ptr != NULL)
160                 munmap(rom->ptr, s.st_size);
161
162         if (rom->fd > 0)
163                 close(rom->fd);
164
165         rom->ptr = NULL;
166         rom->fd = 0;
167         return -1;
168 }
169
170 int create_rom(struct rom *rom, const unsigned char *filename,
171                int romsize, const char *bootblockname,
172                int bootblocksize, int align)
173 {
174         unsigned char null = '\0';
175
176         if (rom->fd != 0) {
177                 ERROR("%s already exists - cannot create\n", filename);
178                 return -1;
179         }
180
181         /* Remember the size of the entire ROM */
182         rom->size = romsize;
183
184         /* The size of the archive section is everything but the bootblock and
185          * the cbfs master header. */
186         rom->fssize = romsize - bootblocksize - sizeof(struct cbfs_header);
187
188         /* Open the file descriptor */
189
190         rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
191
192         if (rom->fd == -1) {
193                 ERROR("Could not create %s: %m\n", filename);
194                 return -1;
195         }
196
197         /* Size the new file appropriately */
198         lseek(rom->fd, romsize - 1, SEEK_SET);
199         write(rom->fd, &null, 1);
200
201         if (map_rom(rom, romsize)) {
202                 close(rom->fd);
203                 return -1;
204         }
205
206         /* This is a pointer to the header for easy access */
207         rom->header = (struct cbfs_header *)
208             ROM_PTR(rom, rom->size - 16 - bootblocksize - sizeof(struct cbfs_header));
209         rom->header->magic = htonl(HEADER_MAGIC);
210         rom->header->romsize = htonl(romsize);
211         rom->header->bootblocksize = htonl(bootblocksize);
212         rom->header->align = htonl(align);
213         rom->header->offset = htonl(0);
214
215         add_bootblock(rom, bootblockname);
216
217         /* Write the cbfs master header address at the end of the ROM. */
218
219         ROM_WRITEL(rom, rom->size - 4,
220                    0xFFFFFFF0 - bootblocksize - sizeof(struct cbfs_header));
221
222         /* write the empty header */
223         rom_set_header(rom, (struct cbfs_file *)rom->ptr, "", -1, CBFS_COMPONENT_NULL);
224         return 0;
225 }
226
227 int add_bootblock(struct rom *rom, const char *filename)
228 {
229         unsigned int size;
230         int fd = size_and_open(filename, &size);
231         int ret;
232
233         if (fd == -1)
234                 return -1;
235
236         if (size > ntohl(rom->header->bootblocksize)) {
237                 ERROR("The bootblock size is not correct (%d vs %d)\n",
238                       size, ntohl(rom->header->bootblocksize));
239                 return -1;
240         }
241
242         /* Copy the bootblock into place at the end of the file */
243         ret = copy_from_fd(fd, ROM_PTR(rom, rom->size - ntohl(rom->header->bootblocksize)), size);
244
245         close(fd);
246
247         if (ret) {
248                 ERROR("Unable to add %s to the bootblock\n", filename);
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 int rom_exists(struct rom *rom)
256 {
257         if (rom->fd <= 0)
258                 return 0;
259         return 1;
260 }