v2/util: romfs -> cbfs rename
[coreboot.git] / util / cbfstool / resize.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 <ctype.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #include "cbfstool.h"
27
28 void resize_usage(void)
29 {
30         printf("resize [SIZE] [ERASEBLOCK]\tResize the ROM\n");
31 }
32
33 int resize_handler(struct rom *rom, int argc, char **argv)
34 {
35         unsigned int size, align, offset;
36         char null = '\0';
37         int bootblocksize = ntohl(rom->header->bootblocksize);
38
39         if (argc < 1) {
40                 resize_usage();
41                 return -1;
42         }
43
44         if (rom->fd <= 0) {
45                 ERROR("ROM file %s does not exist\n", rom->name);
46                 return -1;
47         }
48
49         align = ntohl(rom->header->align);
50         size = get_size(argv[0]);
51
52         if (argc >= 2)
53                 align = strtoul(argv[1], NULL, 0);
54
55         if (size == rom->size && align == ntohl(rom->header->align)) {
56                 ERROR("New parameters are the same as the old\n");
57                 return 0;
58         }
59
60         if (size < bootblocksize) {
61                 ERROR("The new size is smaller then the bootblock size\n");
62                 return -1;
63         }
64
65         /* if the current ROM is too big for the new size, then complain */
66
67         if (rom_used_space(rom) > size -  bootblocksize) {
68                 ERROR("The new size is too small for the current ROM\n");
69                 return -1;
70         }
71
72         /* Grow the rom if we need to */
73
74         if (size > rom->size) {
75                 munmap(rom->ptr, rom->size);
76
77                 lseek(rom->fd, size - 1, SEEK_SET);
78                 write(rom->fd, &null, 1);
79
80                 rom->ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
81                                 rom->fd, 0);
82
83                 if (rom->ptr == MAP_FAILED) {
84                         ERROR("Unable to grow the ROM\n");
85                         return -1;
86                 }
87         }
88
89         /* We only have to rewrite the entries if the alignment changed */
90
91         if (align != ntohl(rom->header->align)) {
92                 struct cbfs_file *c;
93
94                 /* The first entry doesn't have to move */
95
96                 c = rom_find(rom, rom->header->offset);
97                 offset = rom->header->offset;
98
99                 while (c) {
100                         struct cbfs_file *n = rom_find_next(rom, c);
101                         unsigned int next;
102
103                         if (n == NULL)
104                                 break;
105
106                         /* Calculate a new location for the entry */
107                         next =
108                             ROM_OFFSET(rom,
109                                        c) + ALIGN(ntohl(c->offset) +
110                                                   ntohl(c->len), align);
111
112                         /* Copy the next entry there */
113                         memmove(ROM_PTR(rom, next), n,
114                                 ntohl(n->offset) + ntohl(n->len));
115
116                         c = (struct cbfs_file *)ROM_PTR(rom, next);
117
118                         /* If the previous header wasn't overwritten by the change,
119                            corrupt the header so we don't accidently find it */
120
121                         if (ROM_OFFSET(rom, n) >
122                             next + ntohl(c->len) + ntohl(c->offset))
123                                 memset(n->magic, 0, sizeof(n->magic));
124                 }
125         }
126
127         /* Copy the bootblock */
128
129         memmove(rom->ptr + size -  bootblocksize,
130                 rom->ptr + rom->size -  bootblocksize,  bootblocksize);
131
132         /* Recacluate the location of the header */
133
134         offset = ROM_READL(rom, size - 12);
135
136         rom->header = (struct cbfs_header *)
137             ROM_PTR(rom, size - (0xFFFFFFFF - offset) - 1);
138
139         /* Put the new values in the header */
140         rom->header->romsize = htonl(size);
141         rom->header->align = htonl(align);
142
143         /* Truncate the file if we have to */
144
145         if (size < rom->size) {
146                 munmap(rom->ptr, rom->size);
147
148                 rom->ptr = NULL;
149
150                 if (ftruncate(rom->fd, size)) {
151                         ERROR("Unable to truncate the ROM\n");
152                         return -1;
153                 }
154
155                 rom->ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
156                                 rom->fd, 0);
157
158                 if (rom->ptr == MAP_FAILED) {
159                         ERROR("Unable to truncate the ROM\n");
160                         return -1;
161                 }
162         }
163
164         rom->size = size;
165         rom->fssize = size -  bootblocksize;
166
167         return 0;
168 }