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 void resize_usage(void)
30 printf("resize [SIZE] [ERASEBLOCK]\tResize the ROM\n");
33 int resize_handler(struct rom *rom, int argc, char **argv)
35 unsigned int align, offset;
38 int bootblocksize = ntohl(rom->header->bootblocksize);
46 ERROR("ROM file %s does not exist\n", rom->name);
50 align = ntohl(rom->header->align);
51 size = get_size(argv[0]);
54 align = strtoul(argv[1], NULL, 0);
56 if (size == rom->size && align == ntohl(rom->header->align)) {
57 ERROR("New parameters are the same as the old\n");
61 if (size < bootblocksize) {
62 ERROR("The new size is smaller then the bootblock size\n");
66 /* if the current ROM is too big for the new size, then complain */
68 if (rom_used_space(rom) > size - bootblocksize) {
69 ERROR("The new size is too small for the current ROM\n");
73 /* Grow the rom if we need to */
75 if (size > rom->size) {
76 munmap(rom->ptr, rom->size);
78 lseek(rom->fd, size - 1, SEEK_SET);
79 write(rom->fd, &null, 1);
81 rom->ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
84 if (rom->ptr == MAP_FAILED) {
85 ERROR("Unable to grow the ROM\n");
90 /* We only have to rewrite the entries if the alignment changed */
92 if (align != ntohl(rom->header->align)) {
95 /* The first entry doesn't have to move */
97 c = rom_find(rom, rom->header->offset);
98 offset = rom->header->offset;
101 struct cbfs_file *n = rom_find_next(rom, c);
107 /* Calculate a new location for the entry */
110 c) + ALIGN(ntohl(c->offset) +
111 ntohl(c->len), align);
113 /* Copy the next entry there */
114 memmove(ROM_PTR(rom, next), n,
115 ntohl(n->offset) + ntohl(n->len));
117 c = (struct cbfs_file *)ROM_PTR(rom, next);
119 /* If the previous header wasn't overwritten by the change,
120 corrupt the header so we don't accidently find it */
122 if (ROM_OFFSET(rom, n) >
123 next + ntohl(c->len) + ntohl(c->offset))
124 memset(n->magic, 0, sizeof(n->magic));
128 /* Copy the bootblock */
130 memmove(rom->ptr + size - bootblocksize,
131 rom->ptr + rom->size - bootblocksize, bootblocksize);
133 /* Recacluate the location of the header */
135 offset = ROM_READL(rom, size - 12);
137 rom->header = (struct cbfs_header *)
138 ROM_PTR(rom, size - (0xFFFFFFFF - offset) - 1);
140 /* Put the new values in the header */
141 rom->header->romsize = htonl(size);
142 rom->header->align = htonl(align);
144 /* Truncate the file if we have to */
146 if (size < rom->size) {
147 munmap(rom->ptr, rom->size);
151 if (ftruncate(rom->fd, size)) {
152 ERROR("Unable to truncate the ROM\n");
156 rom->ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
159 if (rom->ptr == MAP_FAILED) {
160 ERROR("Unable to truncate the ROM\n");
166 rom->fssize = size - bootblocksize;