03479c50954fee159461c291c41be0f0abecc438
[coreboot.git] / util / romtool / util.c
1 /*
2  * romtool
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 "romtool.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 romfs_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, int bootblocksize, int align)
172 {
173         unsigned char null = '\0';
174
175         if (rom->fd != 0) {
176                 ERROR("%s already exists - cannot create\n", filename);
177                 return -1;
178         }
179
180         /* Remember the size of the entire ROM */
181         rom->size = romsize;
182
183         /* The size of the archive section is everything but the bootblock */
184         rom->fssize = romsize - bootblocksize;
185
186         /* Open the file descriptor */
187
188         rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
189
190         if (rom->fd == -1) {
191                 ERROR("Could not create %s: %m\n", filename);
192                 return -1;
193         }
194
195         /* Size the new file appropriately */
196         lseek(rom->fd, romsize - 1, SEEK_SET);
197         write(rom->fd, &null, 1);
198
199         if (map_rom(rom, romsize)) {
200                 close(rom->fd);
201                 return -1;
202         }
203
204         /* Clear the reset vector */
205         memset(rom->ptr + rom->size - 16, 0, 16);
206
207         ROM_WRITEL(rom, rom->size - 4,
208                    0xFFFFFFF0 - sizeof(struct romfs_header));
209
210         /* This is a pointer to the header for easy access */
211         rom->header = (struct romfs_header *)
212             ROM_PTR(rom, rom->size - 16 - sizeof(struct romfs_header));
213
214         rom->header->magic = htonl(HEADER_MAGIC);
215         rom->header->romsize = htonl(romsize);
216         rom->header->bootblocksize = htonl(bootblocksize);
217         rom->header->align = htonl(align);
218         rom->header->offset = htonl(0);
219
220         return 0;
221 }
222
223 int add_bootblock(struct rom *rom, const char *filename)
224 {
225         unsigned int size;
226         //unsigned int offset;
227         int fd = size_and_open(filename, &size);
228         int ret;
229         struct romfs_header tmp;
230
231         if (fd == -1)
232                 return -1;
233
234         if (size > ntohl(rom->header->bootblocksize)) {
235                 ERROR("The bootblock size is not correct (%d vs %d)\n",
236                       size, ntohl(rom->header->bootblocksize));
237                 return -1;
238         }
239
240         /* Copy the current header into a temporary buffer */
241         memcpy(&tmp, rom->header, sizeof(struct romfs_header));
242
243         /* Copy the bootblock into place at the end of the file */
244
245         ret = copy_from_fd(fd, ROM_PTR(rom, rom->size - ntohl(rom->header->bootblocksize)), size);
246
247         close(fd);
248
249         if (ret) {
250                 ERROR("Unable to add %s to the bootblock\n", filename);
251                 return -1;
252         }
253
254         /* FIXME: This should point to a location defined by coreboot */
255
256         ROM_WRITEL(rom, rom->size - 4,
257                    0xFFFFFFF0 - sizeof(struct romfs_header));
258
259         /* This is a pointer to the header for easy access */
260         rom->header = (struct romfs_header *)
261             ROM_PTR(rom, rom->size - 16 - sizeof(struct romfs_header));
262
263 #if 0
264         /* Figure out the new location for the header */
265         offset = ROM_READL(rom, rom->size - 4);
266
267         rom->header = (struct romfs_header *)
268             ROM_PTR(rom, offset - (0xFFFFFFFF - rom->size));
269 #endif
270
271         /* Replace the LAR header */
272         memcpy(rom->header, &tmp, sizeof(struct romfs_header));
273
274         return 0;
275 }
276
277 int rom_exists(struct rom *rom)
278 {
279         if (rom->fd <= 0)
280                 return 0;
281         return 1;
282 }