aboutsummaryrefslogtreecommitdiff
path: root/ubi-utils/src/ubigen.c
blob: 8a464dda28adb9222c68ba32e829328d5a625729 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Oliver Lohmann
 *
 * Tool to add UBI headers to binary images.
 *
 * 1.0 Initial version
 * 1.1 Different CRC32 start value
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <getopt.h>
#include <argp.h>
#include <unistd.h>
#include <sys/stat.h>
#include <mtd/ubi-header.h>

#include "ubigen.h"
#include "config.h"

typedef enum action_t {
	ACT_NORMAL	     = 0x00000001,
	ACT_BROKEN_UPDATE    = 0x00000002,
} action_t;


const char *argp_program_version = PACKAGE_VERSION;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static char doc[] = "\nVersion: " PACKAGE_VERSION "\n\tBuilt on "
	BUILD_CPU" "BUILD_OS" at "__DATE__" "__TIME__"\n"
	"\n"
	"ubigen - a tool for adding UBI information to a binary input file.\n";

static const char copyright [] __attribute__((unused)) =
	"FIXME: insert license type"; /* FIXME */

#define CHECK_ENDP(option, endp) do {			\
	if (*endp) {					\
		fprintf(stderr,				\
			"Parse error option \'%s\'. "	\
			"No correct numeric value.\n"	\
			, option);			\
		exit(EXIT_FAILURE);			\
	}						\
} while(0)

static struct argp_option options[] = {
	/* COMMON */
	{ name: NULL, key: 0, arg: NULL, flags: 0,
	  doc: "Common settings:",
	  group: 1},

	{ name: "copyright", key: 'c', arg: NULL,    flags: 0,
	  doc: "Print copyright information.",
	  group: 1 },

	{ name: "verbose", key: 'v', arg: NULL,	   flags: 0,
	  doc: "Print more progress information.",
	  group: 1 },

	{ name: "debug", key: 'd', arg: NULL, flags: 0,
	  group: 1 },


	/* INPUT */
	{ name: NULL, key: 0, arg: NULL, flags: 0,
	  doc: "UBI Settings:",
	  group: 4},

	{ name: "alignment", key: 'A', arg: "<num>", flags: 0,
	  doc: "Set the alignment size to <num> (default 1).\n"
	       "Values can be specified as bytes, 'ki' or 'Mi'.",
	  group: 4 },

	{ name: "blocksize", key: 'B', arg: "<num>", flags: 0,
	  doc: "Set the eraseblock size to <num> (default 128 KiB).\n"
	       "Values can be specified as bytes, 'ki' or 'Mi'.",
	  group: 4 },

	{ name: "erasecount", key: 'E', arg: "<num>", flags: 0,
	  doc: "Set the erase count to <num> (default 0)",
	  group: 4 },

	{ name: "setver", key: 'X', arg: "<num>", flags: 0,
	  doc: "Set UBI version number to <num> (default 1)",
	  group: 4 },

	{ name: "id", key: 'I', arg: "<num>", flags: 0,
	  doc: "The UBI volume id.",
	  group: 4 },


	{ name: "offset", key: 'O', arg: "<num>", flags: 0,
	  doc: "Offset from start of an erase block to the UBI volume header.",
	  group: 4 },

	{ name: "type", key: 'T', arg: "<num>", flags: 0,
	  doc: "The UBI volume type:\n1 = dynamic, 2 = static",
	  group: 4 },

	/* INPUT/OUTPUT */
	{ name: NULL, key: 0, arg: NULL, flags: 0,
	  doc: "Input/Output:",
	  group: 5 },

	{ name: "infile", key: 'i', arg: "<filename>", flags: 0,
	  doc: "Read input from file.",
	  group: 5 },

	{ name: "outfile", key: 'o', arg: "<filename>", flags: 0,
	  doc: "Write output to file (default is stdout).",
	  group: 5 },

	/* Special options */
	{ name: NULL, key: 0, arg: NULL, flags: 0,
	  doc: "Special options:",
	  group: 6 },

	{ name: "broken-update", key: 'U', arg: "<leb>", flags: 0,
	  doc: "Create an ubi image which simulates a broken update.\n"
	       "<leb> specifies the logical eraseblock number to update.\n",
	  group: 6 },

	{ name: NULL, key: 0, arg: NULL, flags: 0, doc: NULL, group: 0 },
};

typedef struct myargs {
	/* common settings */
	action_t action;
	int verbose;

	int32_t id;
	uint8_t type;
	uint32_t eb_size;
	uint64_t ec;
	uint8_t version;
	uint32_t hdr_offset;
	uint32_t update_block;
	uint32_t alignment;

	FILE* fp_in;
	FILE* fp_out;

	/* special stuff needed to get additional arguments */
	char *arg1;
	char **options;			/* [STRING...] */
} myargs;


static int ustrtoul(const char *cp, char **endp, unsigned int base)
{
	unsigned long result = strtoul(cp, endp, base);

	switch (**endp) {
	case 'G':
		result *= 1024;
	case 'M':
		result *= 1024;
	case 'k':
	case 'K':
		result *= 1024;
	/* "Ki", "ki", "Mi" or "Gi" are to be used. */
		if ((*endp)[1] == 'i')
			(*endp) += 2;
	}
	return result;
}

static error_t
parse_opt(int key, char *arg, struct argp_state *state)
{
	int err = 0;
	char* endp;

	myargs *args = state->input;

	switch (key) {
	case 'c':
		fprintf(stderr, "%s\n", copyright);
		exit(0);
		break;
	case 'o': /* output */
		args->fp_out = fopen(arg, "wb");
		if ((args->fp_out) == NULL) {
			fprintf(stderr, "Cannot open file %s for output\n",
					arg);
			exit(1);
		}
		break;
	case 'i': /* input */
		args->fp_in = fopen(arg, "rb");
		if ((args->fp_in) == NULL) {
			fprintf(stderr, "Cannot open file %s for input\n",
					arg);
			exit(1);
		}
		break;
	case 'v': /* verbose */
		args->verbose = 1;
		break;

	case 'B': /* eb_size */
		args->eb_size = (uint32_t) ustrtoul(arg, &endp, 0);
		CHECK_ENDP("B", endp);
		break;
	case 'E': /* erasecount */
		args->ec = (uint64_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("E", endp);
		break;
	case 'I': /* id */
		args->id = (uint16_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("I", endp);
		break;
	case 'T': /* type */
		args->type =  (uint16_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("T", endp);
		break;
	case 'X': /* versionnr */
		args->version =	 (uint8_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("X", endp);
		break;
	case 'O': /* offset for volume hdr */
		args->hdr_offset =
			(uint32_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("O", endp);
		break;

	case 'U': /* broken update */
		args->action = ACT_BROKEN_UPDATE;
		args->update_block =
			(uint32_t) strtoul(arg, &endp, 0);
		CHECK_ENDP("U", endp);
		break;

	case ARGP_KEY_ARG:
		if (!args->fp_in) {
			args->fp_in = fopen(arg, "rb");
			if ((args->fp_in) == NULL) {
				fprintf(stderr,
				"Cannot open file %s for input\n", arg);
				exit(1);
			}
		}
		args->arg1 = arg;
		args->options = &state->argv[state->next];
		state->next = state->argc;
		break;
	case ARGP_KEY_END:

		if (args->id < 0) {
			err = 1;
			fprintf(stderr,
				"Please specify an UBI Volume ID.\n");
		}
		if (args->type == 0) {
			err = 1;
			fprintf(stderr,
				"Please specify an UBI Volume type.\n");
		}
		if (err) {
			fprintf(stderr, "\n");
			argp_usage(state);
			exit(1);
		}
		break;
	default:
		return(ARGP_ERR_UNKNOWN);
	}

	return 0;
}

static struct argp argp = {
	options:     options,
	parser:	     parse_opt,
	args_doc:    0,
	doc:	     doc,
	children:    NULL,
	help_filter: NULL,
	argp_domain: NULL,
};


int
main(int argc, char **argv)
{
	int rc = 0;
	ubi_info_t u;
	struct stat file_info;
	off_t input_len = 0; /* only used in static volumes */

	myargs args = {
		.action = ACT_NORMAL,
		.verbose = 0,

		.id = -1,
		.type = 0,
		.eb_size = 0,
		.update_block = 0,
		.ec = 0,
		.version = 0,
		.hdr_offset = (DEFAULT_PAGESIZE) - (UBI_VID_HDR_SIZE),
		.alignment = 1,

		.fp_in = NULL,
		.fp_out = stdout,
		/* arguments */
		.arg1 = NULL,
		.options = NULL,
	};

	ubigen_init(); /* Init CRC32 table in ubigen */

	/* parse arguments */
	argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, &args);

	if (fstat(fileno(args.fp_in), &file_info) != 0) {
		fprintf(stderr, "Cannot fetch file size "
				"from input file.\n");
	}
	input_len = file_info.st_size;

	rc = ubigen_create(&u, (uint32_t)args.id, args.type,
			args.eb_size, args.ec, args.alignment,
			args.version, args.hdr_offset, 0 ,input_len,
			args.fp_in, args.fp_out);

	if  (rc != 0) {
		fprintf(stderr, "Cannot create UBI info handler rc: %d\n", rc);
		exit(EXIT_FAILURE);
	}

	if (!args.fp_in || !args.fp_out) {
		fprintf(stderr, "Input/Output error.\n");
		exit(EXIT_FAILURE);

	}

	if (args.action & ACT_NORMAL) {
		rc = ubigen_write_complete(u);
	}
	else if (args.action & ACT_BROKEN_UPDATE) {
		rc = ubigen_write_broken_update(u, args.update_block);
	}
	if  (rc != 0) {
		fprintf(stderr, "Error converting input data.\n");
		exit(EXIT_FAILURE);
	}

	rc = ubigen_destroy(&u);
	return rc;
}