summaryrefslogtreecommitdiff
path: root/mkubootenv.c
blob: fce3b1f16df1e65448155bb9945ae10ccf2f7af2 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * mkubootenv.c
 *
 * Create a U-Boot environment image suitable for flashing.
 *
 * The input is a text file containing environment variable
 * definitions in the format "name=value", separeated by newlines.
 *
 * The "environment" (output) is stored as a list of '\0' terminated
 * "name=value" strings. The end of the list is marked by a double '\0'.
 *
 * The environment is preceeded by a 32 bit CRC over the data part.
 *
 * Copyright (c) 2009, Zurich University of Applied Science
 * Copyright (c) 2009, Tobias Klauser <klto@zhaw.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "crc32.h"

#undef DEBUG

#define CMD_NAME		"mkubootenv"

#define CRC32_SIZE		sizeof(uint32_t)
/* minimum trailing null bytes */
#define TRAILER_SIZE		2

#define err(fmt, args...)	fprintf(stderr, "%s: Error: " fmt, CMD_NAME, ##args)
#define warn(fmt, args...)	fprintf(stderr, "%s: Warning: " fmt, CMD_NAME, ##args)
#ifdef DEBUG
# define dbg(fmt, args...)	fprintf(stdout, fmt, ##args)
#else
# define dbg(fmt, args...)
#endif

/* file "object" */
struct file {
	const char *name;
	int fd;
	uint8_t *ptr;
	size_t size;
};

static void usage_and_exit(int status) __attribute__((noreturn));

static inline void uboot_env_init_file(struct file *f)
{
	memset(f, 0, sizeof(struct file));
	f->ptr = MAP_FAILED;
}

static int uboot_env_prepare_source(struct file *s)
{
	struct stat sbuf;

	s->fd = open(s->name, O_RDONLY);
	if (s->fd < 0) {
		err("Can't open source file '%s': %s\n", s->name,
				strerror(errno));
		return EXIT_FAILURE;
	}

	if (fstat(s->fd, &sbuf) < 0) {
		err("Can't stat source file '%s': %s\n", s->name,
				strerror(errno));
		close(s->fd);
		return -1;
	}

	s->ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, s->fd, 0);
	if (s->ptr == MAP_FAILED) {
		err("Can't mmap source image file '%s': %s\n", s->name,
				strerror(errno));
		close(s->fd);
		return -1;
	}

	s->size = sbuf.st_size;

	return 0;
}

static int uboot_env_prepare_target(struct file *t)
{
	t->fd = open(t->name, O_RDWR|O_CREAT|O_TRUNC, 0666);
	if (t->fd < 0) {
		err("Can't open target image file '%s': %s\n", t->name,
				strerror(errno));
		return -1;
	}

	/*
	 * seek to the end of the target file to write a byte, so we have the
	 * whole target image size mapped for writing
	 */
	if (lseek(t->fd, t->size - 1, SEEK_SET) < 0) {
		err("Can't seek in target image file '%s': %s\n", t->name,
				strerror(errno));
		close(t->fd);
		return -1;
	}

	if (write(t->fd, "", 1) < 0) {
		err("Can't write to target image file '%s': %s\n", t->name,
				strerror(errno));
		close(t->fd);
		return -1;
	}

	t->ptr = mmap(NULL, t->size, PROT_READ|PROT_WRITE, MAP_SHARED, t->fd, 0);
	if (t->ptr == MAP_FAILED) {
		err("Can't mmap target image file '%s': %s\n", t->name,
				strerror(errno));
		close(t->fd);
		return -1;
	}

	return 0;
}

static void uboot_env_cleanup_file(struct file *f)
{
	if (f->ptr != MAP_FAILED)
		munmap(f->ptr, f->size);
	if (f->fd > 0)
		close(f->fd);
}

static void uboot_env_to_img(struct file *s, struct file *t)
{
	uint8_t *p, *q, *end;
	uint32_t *crc;

	dbg("source file (env):       %s\n", s->name);
	dbg("target image file (bin): %s\n", t->name);
	dbg("target size:             %zd\n", t->size);

	/* CRC32 placeholder, will be filled later */
	end = t->ptr + CRC32_SIZE;
	for (p = t->ptr; p < end; p++)
		*p = 0;

	/* copy the source file, replacing \n by \0 */
	end = s->ptr + s->size;
	for (q = s->ptr; q < end; q++, p++)
		*p = (*q == '\n') ? '\0' : *q;

	/* trailer */
	end = s->ptr + t->size;
	for (q = s->ptr + s->size; q < end; q++)
		*p = 0;

	/* now for the real CRC32 */
	crc = (uint32_t *) t->ptr;
	*crc = crc32(0, t->ptr + CRC32_SIZE, t->size - CRC32_SIZE);
}

static void uboot_img_to_env(struct file *s, struct file *t)
{
	uint8_t *p, *q, *end;
	uint32_t *crc;

	dbg("source file (bin):       %s\n", s->name);
	dbg("target image file (env): %s\n", t->name);
	dbg("target size:             %zd\n", t->size);

	/* check CRC */
	crc = (uint32_t *) s->ptr;
	if (*crc != crc32(0, s->ptr + CRC32_SIZE, s->size - CRC32_SIZE))
		warn("source image with bad CRC\n");

	p = t->ptr;
	end = s->ptr + CRC32_SIZE + t->size;
	for (q = s->ptr + CRC32_SIZE; q < end; p++, q++)
		*p = (*q == '\0') ? '\n' : *q;
}

static void usage_and_exit(int status)
{
	printf("usage: mkenv [-t <type>] [-s <size>] <source file> <target file>\n"
	       "  -s <size>  set size of the target image file to <size> bytes. If <size> is\n"
	       "             bigger than the source file, the target image gets padded with null\n"
	       "             bytes. If <size> is smaller than the source file, an error is emitted.\n"
	       "  -r         reverse operation: get plaintext env file (target) from binary image\n"
	       "             file (source)\n");
	exit(status);
}

int main(int argc, char **argv)
{
	int i;
	int status = EXIT_FAILURE;
	ssize_t img_size = -1;
	bool reverse = false;
	struct file s, t;	/* source and target file */

	if (argc < 2)
		usage_and_exit(EXIT_FAILURE);

	/* parse commandline options */
	for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
		switch (argv[i][1]) {
		case 's': {
			char *opt = argv[++i];

			if (strlen(opt) > 2 && opt[0] == '0' &&
					(opt[1] == 'x' || opt[1] == 'X')) {
				img_size = strtol(opt, NULL, 16);
			} else {
				img_size = strtol(opt, NULL, 10);

				if (img_size <= 0) {
					err("Invalid target image size: %zd. Must be greater than 0.\n", img_size);
					exit(EXIT_FAILURE);
				}
			}
			break;
		}
		case 'r':
			reverse = true;
			break;
		case 'h':
			status = EXIT_SUCCESS;
			/* fall through */
		default:
			usage_and_exit(status);
			break;
		}
	}

	/* we expect two filenames */
	if (i + 2 > argc)
		usage_and_exit(EXIT_FAILURE);

	if (reverse && img_size >= 0)
		warn("Image size specified in reverse mode will be ignored\n");

	uboot_env_init_file(&s);
	uboot_env_init_file(&t);
	s.name = argv[i];
	t.name = argv[i+1];

	if (uboot_env_prepare_source(&s))
		exit(EXIT_FAILURE);

	if (!reverse) {
		ssize_t min_img_size = CRC32_SIZE + s.size + TRAILER_SIZE;

		/* TODO: Check source file format:
		 *	var=value\n
		 */

		/*
		 * check whether the size hasn't been set or whether the source file +
		 * CRC + trailer fits into the specified size.
		 */
		if (img_size < 0)
			img_size = min_img_size;
		else if (img_size < min_img_size) {
			err("Specified size (%zd) is too small for the source file to "
					"fit into. Must be at least %zd bytes.\n",
					img_size, min_img_size);
			goto cleanup_source;
		}

		t.size = img_size;
		if (uboot_env_prepare_target(&t))
			goto cleanup_source;

		uboot_env_to_img(&s, &t);
	} else {
		uint8_t *p;
		uint8_t *end;
		bool found_data_end = false;

		/* get the length of the data part */
		end = s.ptr + CRC32_SIZE + s.size;
		for (p = s.ptr + CRC32_SIZE; (p < end - 1) && (!found_data_end); p++) {
			/* two null bytes mark the end of the data section */
			if (*p == '\0' && *(p + 1) == '\0')
				found_data_end = true;
		}

		if (!found_data_end)
			warn("No end of list delimiter found in source file\n");

		/* calculate the plain text file size */
		t.size = p - (s.ptr + CRC32_SIZE);
		if (uboot_env_prepare_target(&t))
			goto cleanup_source;

		uboot_img_to_env(&s, &t);
	}

	status = EXIT_SUCCESS;

	uboot_env_cleanup_file(&t);
cleanup_source:
	uboot_env_cleanup_file(&s);

	exit(status);
}