summaryrefslogtreecommitdiff
path: root/package/mtd/src/fis.c
blob: 3108c5a9e6bf0df9c34593c78a7f56f2bb58d2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * FIS table updating code for mtd
 *
 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <sys/mman.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "crc32.h"
#include "mtd.h"
#include "fis.h"

struct fis_image_hdr {
	unsigned char name[16];
	uint32_t flash_base;
	uint32_t mem_base;
	uint32_t size;
	uint32_t entry_point;
	uint32_t data_length;
} __attribute__((packed));

struct fis_image_crc {
	uint32_t desc;
	uint32_t file;
} __attribute__((packed));

struct fis_image_desc {
	struct fis_image_hdr hdr;
	char _pad[256 - sizeof(struct fis_image_hdr) - sizeof(struct fis_image_crc)];
	struct fis_image_crc crc;
} __attribute__((packed));

static int fis_fd = -1;
static struct fis_image_desc *fis_desc;
static int fis_erasesize = 0;

static void
fis_close(void)
{
	if (fis_desc)
		munmap(fis_desc, fis_erasesize);

	if (fis_fd >= 0)
		close(fis_fd);

	fis_fd = -1;
	fis_desc = NULL;
}

static struct fis_image_desc *
fis_open(void)
{
	struct fis_image_desc *desc;

	if (fis_fd >= 0)
		fis_close();

	fis_fd = mtd_check_open("FIS directory");
	if (fis_fd < 0)
		goto error;

	close(fis_fd);
	fis_fd = mtd_open("FIS directory", true);
	if (fis_fd < 0)
		goto error;

	fis_erasesize = erasesize;
	desc = mmap(NULL, erasesize, PROT_READ|PROT_WRITE, MAP_SHARED, fis_fd, 0);
	if (desc == MAP_FAILED)
		goto error;

	fis_desc = desc;
	return desc;

error:
	fis_close();
	return NULL;
}

int
fis_validate(struct fis_part *old, int n_old, struct fis_part *new, int n_new)
{
	struct fis_image_desc *desc;
	void *end;
	int found = 0;
	int i;

	desc = fis_open();
	if (!desc)
		return -1;

	for (i = 0; i < n_new - 1; i++) {
		if (!new[i].size) {
			fprintf(stderr, "FIS error: only the last partition can detect the size automatically\n");
			i = -1;
			goto done;
		}
	}

	end = desc;
	end = (char *) end + fis_erasesize;
	while ((void *) desc < end) {
		if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
			break;

		for (i = 0; i < n_old; i++) {
			if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) {
				found++;
				goto next;
			}
		}
next:
		desc++;
		continue;
	}

	if (found == n_old)
		i = 1;
	else
		i = -1;

done:
	fis_close();
	return i;
}

int
fis_remap(struct fis_part *old, int n_old, struct fis_part *new, int n_new)
{
	struct fis_image_desc *fisdir = NULL;
	struct fis_image_desc *redboot = NULL;
	struct fis_image_desc *first = NULL;
	struct fis_image_desc *last = NULL;
	struct fis_image_desc *desc;
	struct fis_part *part;
	uint32_t offset = 0, size = 0;
	char *end, *tmp;
	int i;

	desc = fis_open();
	if (!desc)
		return -1;

	if (!quiet)
		fprintf(stderr, "Updating FIS table... \n");

	end = (char *) desc + fis_erasesize;
	while ((char *) desc < end) {
		if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff))
			break;

		if (!strcmp((char *) desc->hdr.name, "FIS directory"))
			fisdir = desc;

		if (!strcmp((char *) desc->hdr.name, "RedBoot"))
			redboot = desc;

		for (i = 0; i < n_old; i++) {
			if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) {
				size += desc->hdr.size;
				last = desc;
				if (!first)
					first = desc;
				break;
			}
		}
		desc++;
	}
	desc--;

	if (desc == last) {
		desc = fisdir;
	}

	/* size fixup */
	if (desc && (last->hdr.flash_base < desc->hdr.flash_base - last->hdr.size))
			size += (desc->hdr.flash_base - last->hdr.flash_base) - last->hdr.size;

#ifdef notyet
	desc = first - 1;
	if (redboot && (desc >= redboot)) {
		if (first->hdr.flash_base - desc->hdr.size > desc->hdr.flash_base) {
			int delta = first->hdr.flash_base - desc->hdr.size - desc->hdr.flash_base;

			offset -= delta;
			size += delta;
		}
	}
#endif

	last++;
	desc = first + n_new;
	offset = first->hdr.flash_base;

	if (desc != last) {
		if (desc > last)
			tmp = (char *) desc;
		else
			tmp = (char *) last;

		memmove(desc, last, end - tmp);
		if (desc < last) {
			tmp = end - (last - desc) * sizeof(struct fis_image_desc);
			memset(tmp, 0xff, tmp - end);
		}
	}

	for (part = new, desc = first; desc < first + n_new; desc++, part++) {
		memset(desc, 0, sizeof(struct fis_image_desc));
		memcpy(desc->hdr.name, part->name, sizeof(desc->hdr.name));
		desc->crc.desc = 0;
		desc->crc.file = 0;

		desc->hdr.flash_base = offset;
		desc->hdr.mem_base = part->loadaddr;
		desc->hdr.entry_point = part->loadaddr;
		desc->hdr.size = (part->size > 0) ? part->size : size;
		desc->hdr.data_length = desc->hdr.size;

		offset += desc->hdr.size;
		size -= desc->hdr.size;
	}

	msync(fis_desc, fis_erasesize, MS_SYNC|MS_INVALIDATE);
	fis_close();

	return 0;
}