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