mtd: add support for rewriting the fis table layout on redboot based systems
[openwrt/svn-archive/archive.git] / package / mtd / src / fis.c
1 #include <sys/mman.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include "crc32.h"
8 #include "mtd.h"
9 #include "fis.h"
10
11 struct fis_image_hdr {
12 unsigned char name[16];
13 uint32_t flash_base;
14 uint32_t mem_base;
15 uint32_t size;
16 uint32_t entry_point;
17 uint32_t data_length;
18 } __attribute__((packed));
19
20 struct fis_image_crc {
21 uint32_t desc;
22 uint32_t file;
23 } __attribute__((packed));
24
25 struct fis_image_desc {
26 struct fis_image_hdr hdr;
27 char _pad[256 - sizeof(struct fis_image_hdr) - sizeof(struct fis_image_crc)];
28 struct fis_image_crc crc;
29 } __attribute__((packed));
30
31 static int fis_fd = -1;
32 static struct fis_image_desc *fis_desc;
33 static int fis_erasesize = 0;
34
35 static void
36 fis_close(void)
37 {
38 if (fis_desc)
39 munmap(fis_desc, fis_erasesize);
40
41 if (fis_fd >= 0)
42 close(fis_fd);
43
44 fis_fd = -1;
45 fis_desc = NULL;
46 }
47
48 static struct fis_image_desc *
49 fis_open(void)
50 {
51 struct fis_image_desc *desc;
52
53 if (fis_fd >= 0)
54 fis_close();
55
56 fis_fd = mtd_check_open("FIS directory");
57 if (fis_fd < 0)
58 goto error;
59
60 close(fis_fd);
61 fis_fd = mtd_open("FIS directory", true);
62 if (fis_fd < 0)
63 goto error;
64
65 fis_erasesize = erasesize;
66 desc = mmap(NULL, erasesize, PROT_READ|PROT_WRITE, MAP_SHARED, fis_fd, 0);
67 if (desc == MAP_FAILED)
68 goto error;
69
70 fis_desc = desc;
71 return desc;
72
73 error:
74 fis_close();
75 return NULL;
76 }
77
78 int
79 fis_validate(struct fis_part *old, int n_old, struct fis_part *new, int n_new)
80 {
81 struct fis_image_desc *desc;
82 void *end;
83 int found = 0;
84 int i;
85
86 desc = fis_open();
87 if (!desc)
88 return 0;
89
90 for (i = 0; i < n_new - 1; i++) {
91 if (!new[i].size) {
92 fprintf(stderr, "FIS error: only the last partition can detect the size automatically\n");
93 i = -1;
94 goto done;
95 }
96 }
97
98 end = desc;
99 end = (char *) end + fis_erasesize;
100 while ((void *) desc < end) {
101 if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
102 break;
103
104 for (i = 0; i < n_old; i++) {
105 if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) {
106 found++;
107 goto next;
108 }
109 }
110 next:
111 desc++;
112 continue;
113 }
114
115 if (found == n_old)
116 i = 1;
117 else
118 i = -1;
119
120 done:
121 fis_close();
122 return i;
123 }
124
125 int
126 fis_remap(struct fis_part *old, int n_old, struct fis_part *new, int n_new)
127 {
128 struct fis_image_desc *fisdir = NULL;
129 struct fis_image_desc *redboot = NULL;
130 struct fis_image_desc *first = NULL;
131 struct fis_image_desc *last = NULL;
132 struct fis_image_desc *desc;
133 struct fis_part *part;
134 uint32_t offset = 0, size = 0;
135 char *end, *tmp;
136 int i;
137
138 desc = fis_open();
139 if (!desc)
140 return -1;
141
142 if (!quiet)
143 fprintf(stderr, "Updating FIS table... \n");
144
145 end = (char *) desc + fis_erasesize;
146 while ((char *) desc < end) {
147 if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
148 break;
149
150 if (!strcmp((char *) desc->hdr.name, "FIS directory"))
151 fisdir = desc;
152
153 if (!strcmp((char *) desc->hdr.name, "RedBoot"))
154 redboot = desc;
155
156 for (i = 0; i < n_old; i++) {
157 if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) {
158 size += desc->hdr.size;
159 last = desc;
160 if (!first)
161 first = desc;
162 break;
163 }
164 }
165 desc++;
166 }
167 desc--;
168
169 if (desc == last) {
170 desc = fisdir;
171 }
172
173 /* size fixup */
174 if (desc && (last->hdr.flash_base < desc->hdr.flash_base - last->hdr.size))
175 size += (desc->hdr.flash_base - last->hdr.flash_base) - last->hdr.size;
176
177 #ifdef notyet
178 desc = first - 1;
179 if (redboot && (desc >= redboot)) {
180 if (first->hdr.flash_base - desc->hdr.size > desc->hdr.flash_base) {
181 int delta = first->hdr.flash_base - desc->hdr.size - desc->hdr.flash_base;
182
183 offset -= delta;
184 size += delta;
185 }
186 }
187 #endif
188
189 last++;
190 desc = first + n_new;
191 offset = first->hdr.flash_base;
192
193 if (desc != last) {
194 if (desc > last)
195 tmp = (char *) desc;
196 else
197 tmp = (char *) last;
198
199 memmove(desc, last, end - tmp);
200 if (desc < last) {
201 tmp = end - (last - desc) * sizeof(struct fis_image_desc);
202 memset(tmp, 0xff, tmp - end);
203 }
204 }
205
206 for (part = new, desc = first; desc < first + n_new; desc++, part++) {
207 memset(desc, 0, sizeof(struct fis_image_desc));
208 memcpy(desc->hdr.name, part->name, sizeof(desc->hdr.name));
209 desc->crc.desc = 0;
210 desc->crc.file = 0;
211
212 desc->hdr.flash_base = offset;
213 desc->hdr.mem_base = part->loadaddr;
214 desc->hdr.entry_point = part->loadaddr;
215 desc->hdr.size = (part->size > 0) ? part->size : size;
216 desc->hdr.data_length = desc->hdr.size;
217
218 offset += desc->hdr.size;
219 size -= desc->hdr.size;
220 }
221
222 msync(fis_desc, fis_erasesize, MS_SYNC|MS_INVALIDATE);
223 fis_close();
224
225 return 0;
226 }