4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
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.
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.
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
28 int get_size(const char *size)
31 int val = strtoul(size, &next, 0);
33 /* Support modifiers for the size kK and mM for kbytes and
34 mbytes respectfully */
37 if (*next == 'k' || *next == 'K')
39 else if (*next == 'm' || *next == 'M')
46 int copy_from_fd(int fd, void *ptr, int size)
48 unsigned char *p = ptr;
51 int ret = read(fd, p, size);
54 ERROR("Error while reading: %m\n");
59 ERROR("Unexpected end of file\n");
70 int size_and_open(const char *filename, unsigned int *size)
74 int fd = open(filename, O_RDONLY);
77 ERROR("Unable to open %s: %m\n", filename);
82 ERROR("Unable to stat %s: %m\n", filename);
91 int map_rom(struct rom *rom, int size)
93 rom->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
96 if (rom->ptr == MAP_FAILED) {
97 ERROR("Could not map the rom: %m\n");
105 int open_rom(struct rom *rom, const char *filename)
108 unsigned long offset;
110 if (stat(filename, &s)) {
111 ERROR("Could not stat %s: %m\n", filename);
115 rom->fd = open(filename, O_RDWR);
118 ERROR("Could not open %s: %m\n", filename);
123 if (map_rom(rom, s.st_size))
126 /* Find the master header */
128 offset = ROM_READL(rom, s.st_size - 4);
130 rom->header = (struct cbfs_header *)
131 ROM_PTR(rom, s.st_size - (0xFFFFFFFF - offset) - 1);
133 if (ntohl(rom->header->magic) != HEADER_MAGIC) {
134 ERROR("This does not appear to be a valid ROM\n");
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");
144 /* Sanity check that the size matches the file size */
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);
153 rom->size = ntohl(rom->header->romsize);
154 rom->fssize = rom->size - ntohl(rom->header->bootblocksize);
159 if (rom->ptr != NULL)
160 munmap(rom->ptr, s.st_size);
170 int create_rom(struct rom *rom, const unsigned char *filename,
171 int romsize, const char *bootblockname,
172 int bootblocksize, int align)
174 unsigned char null = '\0';
177 ERROR("%s already exists - cannot create\n", filename);
181 /* Remember the size of the entire ROM */
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);
188 /* Open the file descriptor */
190 rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
193 ERROR("Could not create %s: %m\n", filename);
197 /* Size the new file appropriately */
198 lseek(rom->fd, romsize - 1, SEEK_SET);
199 write(rom->fd, &null, 1);
201 if (map_rom(rom, romsize)) {
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);
215 add_bootblock(rom, bootblockname);
217 /* Write the cbfs master header address at the end of the ROM. */
219 ROM_WRITEL(rom, rom->size - 4,
220 0xFFFFFFF0 - bootblocksize - sizeof(struct cbfs_header));
222 /* write the empty header */
223 rom_set_header(rom, (struct cbfs_file *)rom->ptr, "", -1, CBFS_COMPONENT_NULL);
227 int add_bootblock(struct rom *rom, const char *filename)
230 int fd = size_and_open(filename, &size);
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));
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);
248 ERROR("Unable to add %s to the bootblock\n", filename);
255 int rom_exists(struct rom *rom)