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 uninitialized_flash_value = 0xff;
30 void flashinit(void *ptr, size_t len)
32 memset(ptr, uninitialized_flash_value, len);
35 int get_size(const char *size)
38 int val = strtoul(size, &next, 0);
40 /* Support modifiers for the size kK and mM for kbytes and
41 mbytes respectfully */
44 if (*next == 'k' || *next == 'K')
46 else if (*next == 'm' || *next == 'M')
53 int copy_from_fd(int fd, void *ptr, int size)
55 unsigned char *p = ptr;
58 int ret = read(fd, p, size);
61 ERROR("Error while reading: %m\n");
66 ERROR("Unexpected end of file\n");
77 int size_and_open(const char *filename, unsigned int *size)
81 int fd = open(filename, O_RDONLY);
84 ERROR("Unable to open %s: %m\n", filename);
89 ERROR("Unable to stat %s: %m\n", filename);
98 int map_rom(struct rom *rom, int size)
100 rom->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
103 if (rom->ptr == MAP_FAILED) {
104 ERROR("Could not map the rom: %m\n");
112 int open_rom(struct rom *rom, const char *filename)
115 unsigned long offset;
117 if (stat(filename, &s)) {
118 ERROR("Could not stat %s: %m\n", filename);
122 rom->fd = open(filename, O_RDWR);
125 ERROR("Could not open %s: %m\n", filename);
130 if (map_rom(rom, s.st_size))
133 /* Find the master header */
135 offset = ROM_READL(rom, s.st_size - 4);
137 rom->header = (struct cbfs_header *)
138 ROM_PTR(rom, s.st_size - (0xFFFFFFFF - offset) - 1);
140 if (ntohl(rom->header->magic) != HEADER_MAGIC) {
141 ERROR("This does not appear to be a valid ROM\n");
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");
151 /* Sanity check that the size matches the file size */
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);
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.
164 rom->rombase = 0-rom->size;
165 rom->rombase &= 0xffffffff;
166 rom->fssize = rom->size - ntohl(rom->header->bootblocksize);
171 if (rom->ptr != NULL)
172 munmap(rom->ptr, s.st_size);
182 int create_rom(struct rom *rom, const unsigned char *filename,
183 int romsize, const char *bootblockname,
184 int bootblocksize, int align)
186 unsigned char null = '\0';
189 ERROR("%s already exists - cannot create\n", filename);
193 /* Remember the size of the entire ROM */
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);
200 /* Open the file descriptor */
202 rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
205 ERROR("Could not create %s: %m\n", filename);
209 /* Size the new file appropriately */
210 lseek(rom->fd, romsize - 1, SEEK_SET);
211 write(rom->fd, &null, 1);
213 if (map_rom(rom, romsize)) {
218 /* mmap'ed pages are by default zero-filled. Fix that. */
219 flashinit(rom->ptr, romsize);
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);
230 if (add_bootblock(rom, bootblockname) == -1)
233 /* Write the cbfs master header address at the end of the ROM. */
235 ROM_WRITEL(rom, rom->size - 4,
236 0xFFFFFFF0 - bootblocksize - sizeof(struct cbfs_header));
238 /* write the empty header */
239 rom_set_header(rom, (struct cbfs_file *)rom->ptr, "", -1, CBFS_COMPONENT_NULL);
243 int add_bootblock(struct rom *rom, const char *filename)
246 int fd = size_and_open(filename, &size);
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));
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);
264 ERROR("Unable to add %s to the bootblock\n", filename);
271 int rom_exists(struct rom *rom)