aboutsummaryrefslogtreecommitdiff
path: root/tools/mkfwumdata.c
blob: 9732a8ddc5ade85a6686e21e5d233cd3e925a703 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2023, Linaro Limited
 */

#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <u-boot/crc.h>
#include <unistd.h>
#include <uuid/uuid.h>

/* This will dynamically allocate the fwu_mdata */
#define CONFIG_FWU_NUM_BANKS		0
#define CONFIG_FWU_NUM_IMAGES_PER_BANK	0

/* Since we can not include fwu.h, redefine version here. */
#define FWU_MDATA_VERSION		1

typedef uint8_t u8;
typedef int16_t s16;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

#include <fwu_mdata.h>

/* TODO: Endianness conversion may be required for some arch. */

static const char *opts_short = "b:i:a:p:gh";

static struct option options[] = {
	{"banks", required_argument, NULL, 'b'},
	{"images", required_argument, NULL, 'i'},
	{"guid", required_argument, NULL, 'g'},
	{"active-bank", required_argument, NULL, 'a'},
	{"previous-bank", required_argument, NULL, 'p'},
	{"help", no_argument, NULL, 'h'},
	{NULL, 0, NULL, 0},
};

static void print_usage(void)
{
	fprintf(stderr, "Usage: mkfwumdata [options] <UUIDs list> <output file>\n");
	fprintf(stderr, "Options:\n"
		"\t-i, --images <num>          Number of images (mandatory)\n"
		"\t-b, --banks  <num>          Number of banks (mandatory)\n"
		"\t-a, --active-bank  <num>    Active bank (default=0)\n"
		"\t-p, --previous-bank  <num>  Previous active bank (default=active_bank - 1)\n"
		"\t-g, --guid                  Use GUID instead of UUID\n"
		"\t-h, --help                  print a help message\n"
		);
	fprintf(stderr, "  UUIDs list syntax:\n"
		"\t  <location uuid>,<image type uuid>,<images uuid list>\n"
		"\t     images uuid list syntax:\n"
		"\t        img_uuid_00,img_uuid_01...img_uuid_0b,\n"
		"\t        img_uuid_10,img_uuid_11...img_uuid_1b,\n"
		"\t        ...,\n"
		"\t        img_uuid_i0,img_uuid_i1...img_uuid_ib,\n"
		"\t          where 'b' and 'i' are number of banks and number\n"
		"\t          of images in a bank respectively.\n"
	       );
}

struct fwu_mdata_object {
	size_t images;
	size_t banks;
	size_t size;
	struct fwu_mdata *mdata;
};

static int previous_bank, active_bank;
static bool __use_guid;

static struct fwu_mdata_object *fwu_alloc_mdata(size_t images, size_t banks)
{
	struct fwu_mdata_object *mobj;

	mobj = calloc(1, sizeof(*mobj));
	if (!mobj)
		return NULL;

	mobj->size = sizeof(struct fwu_mdata) +
		(sizeof(struct fwu_image_entry) +
		 sizeof(struct fwu_image_bank_info) * banks) * images;
	mobj->images = images;
	mobj->banks = banks;

	mobj->mdata = calloc(1, mobj->size);
	if (!mobj->mdata) {
		free(mobj);
		return NULL;
	}

	return mobj;
}

static struct fwu_image_entry *
fwu_get_image(struct fwu_mdata_object *mobj, size_t idx)
{
	size_t offset;

	offset = sizeof(struct fwu_mdata) +
		(sizeof(struct fwu_image_entry) +
		 sizeof(struct fwu_image_bank_info) * mobj->banks) * idx;

	return (struct fwu_image_entry *)((char *)mobj->mdata + offset);
}

static struct fwu_image_bank_info *
fwu_get_bank(struct fwu_mdata_object *mobj, size_t img_idx, size_t bnk_idx)
{
	size_t offset;

	offset = sizeof(struct fwu_mdata) +
		(sizeof(struct fwu_image_entry) +
		 sizeof(struct fwu_image_bank_info) * mobj->banks) * img_idx +
		sizeof(struct fwu_image_entry) +
		sizeof(struct fwu_image_bank_info) * bnk_idx;

	return (struct fwu_image_bank_info *)((char *)mobj->mdata + offset);
}

/**
 * convert_uuid_to_guid() - convert UUID to GUID
 * @buf:	UUID binary
 *
 * UUID and GUID have the same data structure, but their binary
 * formats are different due to the endianness. See lib/uuid.c.
 * Since uuid_parse() can handle only UUID, this function must
 * be called to get correct data for GUID when parsing a string.
 *
 * The correct data will be returned in @buf.
 */
static void convert_uuid_to_guid(unsigned char *buf)
{
	unsigned char c;

	c = buf[0];
	buf[0] = buf[3];
	buf[3] = c;
	c = buf[1];
	buf[1] = buf[2];
	buf[2] = c;

	c = buf[4];
	buf[4] = buf[5];
	buf[5] = c;

	c = buf[6];
	buf[6] = buf[7];
	buf[7] = c;
}

static int uuid_guid_parse(char *uuidstr, unsigned char *uuid)
{
	int ret;

	ret = uuid_parse(uuidstr, uuid);
	if (ret < 0)
		return ret;

	if (__use_guid)
		convert_uuid_to_guid(uuid);

	return ret;
}

static int
fwu_parse_fill_image_uuid(struct fwu_mdata_object *mobj,
			  size_t idx, char *uuids)
{
	struct fwu_image_entry *image = fwu_get_image(mobj, idx);
	struct fwu_image_bank_info *bank;
	char *p = uuids, *uuid;
	int i;

	if (!image)
		return -ENOENT;

	/* Image location UUID */
	uuid = strsep(&p, ",");
	if (!uuid)
		return -EINVAL;

	if (strcmp(uuid, "0") &&
	    uuid_guid_parse(uuid, (unsigned char *)&image->location_uuid) < 0)
		return -EINVAL;

	/* Image type UUID */
	uuid = strsep(&p, ",");
	if (!uuid)
		return -EINVAL;

	if (uuid_guid_parse(uuid, (unsigned char *)&image->image_type_uuid) < 0)
		return -EINVAL;

	/* Fill bank image-UUID */
	for (i = 0; i < mobj->banks; i++) {
		bank = fwu_get_bank(mobj, idx, i);
		if (!bank)
			return -ENOENT;
		bank->accepted = 1;
		uuid = strsep(&p, ",");
		if (!uuid)
			return -EINVAL;

		if (strcmp(uuid, "0") &&
		    uuid_guid_parse(uuid, (unsigned char *)&bank->image_uuid) < 0)
			return -EINVAL;
	}
	return 0;
}

/* Caller must ensure that @uuids[] has @mobj->images entries. */
static int fwu_parse_fill_uuids(struct fwu_mdata_object *mobj, char *uuids[])
{
	struct fwu_mdata *mdata = mobj->mdata;
	int i, ret;

	mdata->version = FWU_MDATA_VERSION;
	mdata->active_index = active_bank;
	mdata->previous_active_index = previous_bank;

	for (i = 0; i < mobj->images; i++) {
		ret = fwu_parse_fill_image_uuid(mobj, i, uuids[i]);
		if (ret < 0)
			return ret;
	}

	mdata->crc32 = crc32(0, (const unsigned char *)&mdata->version,
			     mobj->size - sizeof(uint32_t));

	return 0;
}

static int
fwu_make_mdata(size_t images, size_t banks, char *uuids[], char *output)
{
	struct fwu_mdata_object *mobj;
	FILE *file;
	int ret;

	mobj = fwu_alloc_mdata(images, banks);
	if (!mobj)
		return -ENOMEM;

	ret = fwu_parse_fill_uuids(mobj, uuids);
	if (ret < 0)
		goto done_make;

	file = fopen(output, "w");
	if (!file) {
		ret = -errno;
		goto done_make;
	}

	ret = fwrite(mobj->mdata, mobj->size, 1, file);
	if (ret != mobj->size)
		ret = -errno;
	else
		ret = 0;

	fclose(file);

done_make:
	free(mobj->mdata);
	free(mobj);

	return ret;
}

int main(int argc, char *argv[])
{
	unsigned long banks = 0, images = 0;
	int c, ret;

	/* Explicitly initialize defaults */
	active_bank = 0;
	__use_guid = false;
	previous_bank = INT_MAX;

	do {
		c = getopt_long(argc, argv, opts_short, options, NULL);
		switch (c) {
		case 'h':
			print_usage();
			return 0;
		case 'b':
			banks = strtoul(optarg, NULL, 0);
			break;
		case 'i':
			images = strtoul(optarg, NULL, 0);
			break;
		case 'g':
			__use_guid = true;
			break;
		case 'p':
			previous_bank = strtoul(optarg, NULL, 0);
			break;
		case 'a':
			active_bank = strtoul(optarg, NULL, 0);
			break;
		}
	} while (c != -1);

	if (!banks || !images) {
		fprintf(stderr, "Error: The number of banks and images must not be 0.\n");
		return -EINVAL;
	}

	/* This command takes UUIDs * images and output file. */
	if (optind + images + 1 != argc) {
		fprintf(stderr, "Error: UUID list or output file is not specified or too much.\n");
		print_usage();
		return -ERANGE;
	}

	if (previous_bank == INT_MAX) {
		/* set to the earlier bank in round-robin scheme */
		previous_bank = active_bank > 0 ? active_bank - 1 : banks - 1;
	}

	ret = fwu_make_mdata(images, banks, argv + optind, argv[argc - 1]);
	if (ret < 0)
		fprintf(stderr, "Error: Failed to parse and write image: %s\n",
			strerror(-ret));

	return ret;
}