fiptool.c 29.5 KB
Newer Older
dp-arm's avatar
dp-arm committed
1
/*
2
 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
dp-arm's avatar
dp-arm committed
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
dp-arm's avatar
dp-arm committed
5
6
7
8
9
10
11
12
13
14
15
16
17
 */

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

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
18

dp-arm's avatar
dp-arm committed
19
20
21
22
23
#include "fiptool.h"
#include "tbbr_config.h"

#define OPT_TOC_ENTRY 0
#define OPT_PLAT_TOC_FLAGS 1
24
#define OPT_ALIGN 2
dp-arm's avatar
dp-arm committed
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

static int info_cmd(int argc, char *argv[]);
static void info_usage(void);
static int create_cmd(int argc, char *argv[]);
static void create_usage(void);
static int update_cmd(int argc, char *argv[]);
static void update_usage(void);
static int unpack_cmd(int argc, char *argv[]);
static void unpack_usage(void);
static int remove_cmd(int argc, char *argv[]);
static void remove_usage(void);
static int version_cmd(int argc, char *argv[]);
static void version_usage(void);
static int help_cmd(int argc, char *argv[]);
static void usage(void);

/* Available subcommands. */
static cmd_t cmds[] = {
	{ .name = "info",    .handler = info_cmd,    .usage = info_usage    },
	{ .name = "create",  .handler = create_cmd,  .usage = create_usage  },
	{ .name = "update",  .handler = update_cmd,  .usage = update_usage  },
	{ .name = "unpack",  .handler = unpack_cmd,  .usage = unpack_usage  },
	{ .name = "remove",  .handler = remove_cmd,  .usage = remove_usage  },
	{ .name = "version", .handler = version_cmd, .usage = version_usage },
	{ .name = "help",    .handler = help_cmd,    .usage = NULL          },
};

52
53
static image_desc_t *image_desc_head;
static size_t nr_image_descs;
54
static const uuid_t uuid_null;
dp-arm's avatar
dp-arm committed
55
56
static int verbose;

57
static void vlog(int prio, const char *msg, va_list ap)
dp-arm's avatar
dp-arm committed
58
59
60
61
62
63
64
65
{
	char *prefix[] = { "DEBUG", "WARN", "ERROR" };

	fprintf(stderr, "%s: ", prefix[prio]);
	vfprintf(stderr, msg, ap);
	fputc('\n', stderr);
}

66
static void log_dbgx(const char *msg, ...)
dp-arm's avatar
dp-arm committed
67
68
69
70
71
72
73
74
{
	va_list ap;

	va_start(ap, msg);
	vlog(LOG_DBG, msg, ap);
	va_end(ap);
}

75
static void log_warnx(const char *msg, ...)
dp-arm's avatar
dp-arm committed
76
77
78
79
80
81
82
83
{
	va_list ap;

	va_start(ap, msg);
	vlog(LOG_WARN, msg, ap);
	va_end(ap);
}

84
static void log_err(const char *msg, ...)
dp-arm's avatar
dp-arm committed
85
86
87
88
89
90
91
92
93
94
95
{
	char buf[512];
	va_list ap;

	va_start(ap, msg);
	snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
	vlog(LOG_ERR, buf, ap);
	va_end(ap);
	exit(1);
}

96
static void log_errx(const char *msg, ...)
dp-arm's avatar
dp-arm committed
97
98
99
100
101
102
103
104
105
{
	va_list ap;

	va_start(ap, msg);
	vlog(LOG_ERR, msg, ap);
	va_end(ap);
	exit(1);
}

106
107
108
109
110
111
static char *xstrdup(const char *s, const char *msg)
{
	char *d;

	d = strdup(s);
	if (d == NULL)
112
		log_errx("strdup: %s", msg);
113
114
115
116
117
118
119
120
121
	return d;
}

static void *xmalloc(size_t size, const char *msg)
{
	void *d;

	d = malloc(size);
	if (d == NULL)
122
		log_errx("malloc: %s", msg);
123
124
125
	return d;
}

126
127
128
129
130
static void *xzalloc(size_t size, const char *msg)
{
	return memset(xmalloc(size, msg), 0, size);
}

131
132
133
134
135
136
static void xfwrite(void *buf, size_t size, FILE *fp, const char *filename)
{
	if (fwrite(buf, 1, size, fp) != size)
		log_errx("Failed to write %s", filename);
}

137
138
static image_desc_t *new_image_desc(const uuid_t *uuid,
    const char *name, const char *cmdline_name)
dp-arm's avatar
dp-arm committed
139
{
140
141
	image_desc_t *desc;

142
	desc = xzalloc(sizeof(*desc),
143
144
145
146
147
148
149
150
	    "failed to allocate memory for image descriptor");
	memcpy(&desc->uuid, uuid, sizeof(uuid_t));
	desc->name = xstrdup(name,
	    "failed to allocate memory for image name");
	desc->cmdline_name = xstrdup(cmdline_name,
	    "failed to allocate memory for image command line name");
	desc->action = DO_UNSPEC;
	return desc;
dp-arm's avatar
dp-arm committed
151
152
}

153
154
155
156
157
static void set_image_desc_action(image_desc_t *desc, int action,
    const char *arg)
{
	assert(desc != NULL);

158
	if (desc->action_arg != (char *)DO_UNSPEC)
159
160
161
162
163
164
165
166
		free(desc->action_arg);
	desc->action = action;
	desc->action_arg = NULL;
	if (arg != NULL)
		desc->action_arg = xstrdup(arg,
		    "failed to allocate memory for argument");
}

167
static void free_image_desc(image_desc_t *desc)
dp-arm's avatar
dp-arm committed
168
{
169
170
171
	free(desc->name);
	free(desc->cmdline_name);
	free(desc->action_arg);
172
173
174
175
	if (desc->image) {
		free(desc->image->buffer);
		free(desc->image);
	}
176
	free(desc);
dp-arm's avatar
dp-arm committed
177
178
}

179
static void add_image_desc(image_desc_t *desc)
dp-arm's avatar
dp-arm committed
180
{
181
182
183
184
185
	image_desc_t **p = &image_desc_head;

	while (*p)
		p = &(*p)->next;

186
	assert(*p == NULL);
187
	*p = desc;
188
189
	nr_image_descs++;
}
dp-arm's avatar
dp-arm committed
190

191
192
193
194
195
196
197
198
199
static void free_image_descs(void)
{
	image_desc_t *desc = image_desc_head, *tmp;

	while (desc != NULL) {
		tmp = desc->next;
		free_image_desc(desc);
		desc = tmp;
		nr_image_descs--;
dp-arm's avatar
dp-arm committed
200
	}
201
	assert(nr_image_descs == 0);
dp-arm's avatar
dp-arm committed
202
203
}

204
static void fill_image_descs(void)
dp-arm's avatar
dp-arm committed
205
{
206
	toc_entry_t *toc_entry;
dp-arm's avatar
dp-arm committed
207

208
209
210
211
212
213
214
215
216
	for (toc_entry = toc_entries;
	     toc_entry->cmdline_name != NULL;
	     toc_entry++) {
		image_desc_t *desc;

		desc = new_image_desc(&toc_entry->uuid,
		    toc_entry->name,
		    toc_entry->cmdline_name);
		add_image_desc(desc);
dp-arm's avatar
dp-arm committed
217
	}
218
}
dp-arm's avatar
dp-arm committed
219

220
static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
dp-arm's avatar
dp-arm committed
221
{
222
	image_desc_t *desc;
dp-arm's avatar
dp-arm committed
223

224
225
226
227
228
229
230
231
232
233
234
235
236
	for (desc = image_desc_head; desc != NULL; desc = desc->next)
		if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
			return desc;
	return NULL;
}

static image_desc_t *lookup_image_desc_from_opt(const char *opt)
{
	image_desc_t *desc;

	for (desc = image_desc_head; desc != NULL; desc = desc->next)
		if (strcmp(desc->cmdline_name, opt) == 0)
			return desc;
dp-arm's avatar
dp-arm committed
237
238
239
	return NULL;
}

240
241
242
243
static void uuid_to_str(char *s, size_t len, const uuid_t *u)
{
	assert(len >= (_UUID_STR_LEN + 1));

244
245
246
247
248
249
250
251
252
	snprintf(s, len,
	    "%02X%02X%02X%02X-%02X%02X-%02X%02X-%04X-%04X%04X%04X",
	    u->time_low[0], u->time_low[1], u->time_low[2], u->time_low[3],
	    u->time_mid[0], u->time_mid[1],
	    u->time_hi_and_version[0], u->time_hi_and_version[1],
	    (u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
	    (u->node[0] << 8) | u->node[1],
	    (u->node[2] << 8) | u->node[3],
	    (u->node[4] << 8) | u->node[5]);
253
254
255
256
257
258
259
260
261
262
263
264
}

static void uuid_from_str(uuid_t *u, const char *s)
{
	int n;

	if (s == NULL)
		log_errx("UUID cannot be NULL");
	if (strlen(s) != _UUID_STR_LEN)
		log_errx("Invalid UUID: %s", s);

	n = sscanf(s,
265
266
267
268
269
270
271
272
	    "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
	    &u->time_low[0], &u->time_low[1], &u->time_low[2], &u->time_low[3],
	    &u->time_mid[0], &u->time_mid[1],
	    &u->time_hi_and_version[0], &u->time_hi_and_version[1],
	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low,
	    &u->node[0], &u->node[1],
	    &u->node[2], &u->node[3],
	    &u->node[4], &u->node[5]);
273
	/*
274
	 * Given the format specifier above, we expect 16 items to be scanned
275
276
	 * for a properly formatted UUID.
	 */
277
	if (n != 16)
278
279
280
		log_errx("Invalid UUID: %s", s);
}

281
static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm's avatar
dp-arm committed
282
{
283
	struct BLD_PLAT_STAT st;
dp-arm's avatar
dp-arm committed
284
285
286
287
288
289
	FILE *fp;
	char *buf, *bufend;
	fip_toc_header_t *toc_header;
	fip_toc_entry_t *toc_entry;
	int terminated = 0;

290
	fp = fopen(filename, "rb");
dp-arm's avatar
dp-arm committed
291
292
293
294
295
296
	if (fp == NULL)
		log_err("fopen %s", filename);

	if (fstat(fileno(fp), &st) == -1)
		log_err("fstat %s", filename);

297
	buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm's avatar
dp-arm committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	if (fread(buf, 1, st.st_size, fp) != st.st_size)
		log_errx("Failed to read %s", filename);
	bufend = buf + st.st_size;
	fclose(fp);

	if (st.st_size < sizeof(fip_toc_header_t))
		log_errx("FIP %s is truncated", filename);

	toc_header = (fip_toc_header_t *)buf;
	toc_entry = (fip_toc_entry_t *)(toc_header + 1);

	if (toc_header->name != TOC_HEADER_NAME)
		log_errx("%s is not a FIP file", filename);

	/* Return the ToC header if the caller wants it. */
	if (toc_header_out != NULL)
		*toc_header_out = *toc_header;

	/* Walk through each ToC entry in the file. */
	while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
318
319
320
		image_t *image;
		image_desc_t *desc;

dp-arm's avatar
dp-arm committed
321
322
323
324
325
326
327
328
329
330
		/* Found the ToC terminator, we are done. */
		if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
			terminated = 1;
			break;
		}

		/*
		 * Build a new image out of the ToC entry and add it to the
		 * table of images.
		 */
331
		image = xzalloc(sizeof(*image),
332
		    "failed to allocate memory for image");
333
		image->toc_e = *toc_entry;
334
335
		image->buffer = xmalloc(toc_entry->size,
		    "failed to allocate image buffer, is FIP file corrupted?");
dp-arm's avatar
dp-arm committed
336
337
338
339
340
341
342
343
344
		/* Overflow checks before memory copy. */
		if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
			log_errx("FIP %s is corrupted", filename);
		if (toc_entry->size + toc_entry->offset_address > st.st_size)
			log_errx("FIP %s is corrupted", filename);

		memcpy(image->buffer, buf + toc_entry->offset_address,
		    toc_entry->size);

345
		/* If this is an unknown image, create a descriptor for it. */
346
		desc = lookup_image_desc_from_uuid(&toc_entry->uuid);
347
348
349
		if (desc == NULL) {
			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];

350
			uuid_to_str(name, sizeof(name), &toc_entry->uuid);
351
352
			snprintf(filename, sizeof(filename), "%s%s",
			    name, ".bin");
353
			desc = new_image_desc(&toc_entry->uuid, name, "blob");
354
355
356
357
358
359
			desc->action = DO_UNPACK;
			desc->action_arg = xstrdup(filename,
			    "failed to allocate memory for blob filename");
			add_image_desc(desc);
		}

360
361
		assert(desc->image == NULL);
		desc->image = image;
dp-arm's avatar
dp-arm committed
362
363
364
365
366
367
368
369
370
371
372

		toc_entry++;
	}

	if (terminated == 0)
		log_errx("FIP %s does not have a ToC terminator entry",
		    filename);
	free(buf);
	return 0;
}

373
static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm's avatar
dp-arm committed
374
{
375
	struct BLD_PLAT_STAT st;
dp-arm's avatar
dp-arm committed
376
377
378
	image_t *image;
	FILE *fp;

379
	assert(uuid != NULL);
380
	assert(filename != NULL);
381

382
	fp = fopen(filename, "rb");
dp-arm's avatar
dp-arm committed
383
384
385
386
387
388
	if (fp == NULL)
		log_err("fopen %s", filename);

	if (fstat(fileno(fp), &st) == -1)
		log_errx("fstat %s", filename);

389
	image = xzalloc(sizeof(*image), "failed to allocate memory for image");
390
	image->toc_e.uuid = *uuid;
391
	image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm's avatar
dp-arm committed
392
393
	if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
		log_errx("Failed to read %s", filename);
394
	image->toc_e.size = st.st_size;
dp-arm's avatar
dp-arm committed
395
396
397
398
399

	fclose(fp);
	return image;
}

400
static int write_image_to_file(const image_t *image, const char *filename)
dp-arm's avatar
dp-arm committed
401
402
403
{
	FILE *fp;

404
	fp = fopen(filename, "wb");
dp-arm's avatar
dp-arm committed
405
406
	if (fp == NULL)
		log_err("fopen");
407
	xfwrite(image->buffer, image->toc_e.size, fp, filename);
dp-arm's avatar
dp-arm committed
408
409
410
411
	fclose(fp);
	return 0;
}

412
413
static struct option *add_opt(struct option *opts, size_t *nr_opts,
    const char *name, int has_arg, int val)
dp-arm's avatar
dp-arm committed
414
{
415
416
417
418
419
420
421
422
423
	opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
	if (opts == NULL)
		log_err("realloc");
	opts[*nr_opts].name = name;
	opts[*nr_opts].has_arg = has_arg;
	opts[*nr_opts].flag = NULL;
	opts[*nr_opts].val = val;
	++*nr_opts;
	return opts;
dp-arm's avatar
dp-arm committed
424
425
}

426
427
static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
    int has_arg)
dp-arm's avatar
dp-arm committed
428
{
429
430
431
432
433
434
	image_desc_t *desc;

	for (desc = image_desc_head; desc != NULL; desc = desc->next)
		opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
		    OPT_TOC_ENTRY);
	return opts;
dp-arm's avatar
dp-arm committed
435
436
}

437
static void md_print(const unsigned char *md, size_t len)
438
439
440
441
442
443
444
{
	size_t i;

	for (i = 0; i < len; i++)
		printf("%02x", md[i]);
}

dp-arm's avatar
dp-arm committed
445
446
static int info_cmd(int argc, char *argv[])
{
447
	image_desc_t *desc;
dp-arm's avatar
dp-arm committed
448
449
450
	fip_toc_header_t toc_header;

	if (argc != 2)
451
		info_usage();
dp-arm's avatar
dp-arm committed
452
453
454
455
456
457
458
459
460
461
462
463
464
	argc--, argv++;

	parse_fip(argv[0], &toc_header);

	if (verbose) {
		log_dbgx("toc_header[name]: 0x%llX",
		    (unsigned long long)toc_header.name);
		log_dbgx("toc_header[serial_number]: 0x%llX",
		    (unsigned long long)toc_header.serial_number);
		log_dbgx("toc_header[flags]: 0x%llX",
		    (unsigned long long)toc_header.flags);
	}

465
466
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
		image_t *image = desc->image;
467

468
469
		if (image == NULL)
			continue;
470
471
472
473
		printf("%s: offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
		       desc->name,
		       (unsigned long long)image->toc_e.offset_address,
		       (unsigned long long)image->toc_e.size,
474
		       desc->cmdline_name);
475
#ifndef _MSC_VER	/* We don't have SHA256 for Visual Studio. */
476
477
478
		if (verbose) {
			unsigned char md[SHA256_DIGEST_LENGTH];

479
			SHA256(image->buffer, image->toc_e.size, md);
480
481
482
			printf(", sha256=");
			md_print(md, sizeof(md));
		}
483
#endif
484
		putchar('\n');
dp-arm's avatar
dp-arm committed
485
486
487
488
489
490
491
492
	}

	return 0;
}

static void info_usage(void)
{
	printf("fiptool info FIP_FILENAME\n");
493
	exit(1);
dp-arm's avatar
dp-arm committed
494
495
}

496
static int pack_images(const char *filename, uint64_t toc_flags, unsigned long align)
dp-arm's avatar
dp-arm committed
497
498
{
	FILE *fp;
499
	image_desc_t *desc;
dp-arm's avatar
dp-arm committed
500
501
502
	fip_toc_header_t *toc_header;
	fip_toc_entry_t *toc_entry;
	char *buf;
503
	uint64_t entry_offset, buf_size, payload_size = 0, pad_size;
504
505
506
507
508
	size_t nr_images = 0;

	for (desc = image_desc_head; desc != NULL; desc = desc->next)
		if (desc->image != NULL)
			nr_images++;
dp-arm's avatar
dp-arm committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

	buf_size = sizeof(fip_toc_header_t) +
	    sizeof(fip_toc_entry_t) * (nr_images + 1);
	buf = calloc(1, buf_size);
	if (buf == NULL)
		log_err("calloc");

	/* Build up header and ToC entries from the image table. */
	toc_header = (fip_toc_header_t *)buf;
	toc_header->name = TOC_HEADER_NAME;
	toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
	toc_header->flags = toc_flags;

	toc_entry = (fip_toc_entry_t *)(toc_header + 1);

	entry_offset = buf_size;
525
526
527
528
529
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
		image_t *image = desc->image;

		if (image == NULL)
			continue;
530
		payload_size += image->toc_e.size;
531
		entry_offset = (entry_offset + align - 1) & ~(align - 1);
532
533
534
		image->toc_e.offset_address = entry_offset;
		*toc_entry++ = image->toc_e;
		entry_offset += image->toc_e.size;
dp-arm's avatar
dp-arm committed
535
536
	}

537
538
539
540
541
	/*
	 * Append a null uuid entry to mark the end of ToC entries.
	 * NOTE the offset address for the last toc_entry must match the fip
	 * size.
	 */
542
	memset(toc_entry, 0, sizeof(*toc_entry));
543
	toc_entry->offset_address = (entry_offset + align - 1) & ~(align - 1);
dp-arm's avatar
dp-arm committed
544
545

	/* Generate the FIP file. */
546
	fp = fopen(filename, "wb");
dp-arm's avatar
dp-arm committed
547
548
549
550
551
552
	if (fp == NULL)
		log_err("fopen %s", filename);

	if (verbose)
		log_dbgx("Metadata size: %zu bytes", buf_size);

553
	xfwrite(buf, buf_size, fp, filename);
dp-arm's avatar
dp-arm committed
554
555
556
557

	if (verbose)
		log_dbgx("Payload size: %zu bytes", payload_size);

558
559
560
561
562
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
		image_t *image = desc->image;

		if (image == NULL)
			continue;
563
564
565
		if (fseek(fp, image->toc_e.offset_address, SEEK_SET))
			log_errx("Failed to set file position");

566
		xfwrite(image->buffer, image->toc_e.size, fp, filename);
567
	}
dp-arm's avatar
dp-arm committed
568

569
570
571
572
573
574
575
	if (fseek(fp, entry_offset, SEEK_SET))
		log_errx("Failed to set file position");

	pad_size = toc_entry->offset_address - entry_offset;
	while (pad_size--)
		fputc(0x0, fp);

Andreas Färber's avatar
Andreas Färber committed
576
	free(buf);
dp-arm's avatar
dp-arm committed
577
578
579
580
581
582
583
584
585
586
587
588
589
	fclose(fp);
	return 0;
}

/*
 * This function is shared between the create and update subcommands.
 * The difference between the two subcommands is that when the FIP file
 * is created, the parsing of an existing FIP is skipped.  This results
 * in update_fip() creating the new FIP file from scratch because the
 * internal image table is not populated.
 */
static void update_fip(void)
{
590
	image_desc_t *desc;
dp-arm's avatar
dp-arm committed
591
592

	/* Add or replace images in the FIP file. */
593
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
594
		image_t *image;
595

596
		if (desc->action != DO_PACK)
dp-arm's avatar
dp-arm committed
597
598
			continue;

599
		image = read_image_from_file(&desc->uuid,
600
		    desc->action_arg);
601
		if (desc->image != NULL) {
602
			if (verbose) {
603
				log_dbgx("Replacing %s with %s",
604
605
606
				    desc->cmdline_name,
				    desc->action_arg);
			}
607
608
			free(desc->image);
			desc->image = image;
dp-arm's avatar
dp-arm committed
609
610
611
		} else {
			if (verbose)
				log_dbgx("Adding image %s",
612
				    desc->action_arg);
613
			desc->image = image;
dp-arm's avatar
dp-arm committed
614
615
616
617
		}
	}
}

618
static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm's avatar
dp-arm committed
619
620
621
622
623
624
625
626
627
628
629
630
{
	unsigned long long flags;
	char *endptr;

	errno = 0;
	flags = strtoull(arg, &endptr, 16);
	if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
		log_errx("Invalid platform ToC flags: %s", arg);
	/* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
	*toc_flags |= flags << 32;
}

631
632
633
634
635
636
637
638
639
640
641
static int is_power_of_2(unsigned long x)
{
	return x && !(x & (x - 1));
}

static unsigned long get_image_align(char *arg)
{
	char *endptr;
	unsigned long align;

	errno = 0;
642
	align = strtoul(arg, &endptr, 0);
643
644
645
646
647
648
	if (*endptr != '\0' || !is_power_of_2(align) || errno != 0)
		log_errx("Invalid alignment: %s", arg);

	return align;
}

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
{
	char *p;

	for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
		if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
			p += strlen("uuid=");
			uuid_from_str(uuid, p);
		} else if (strncmp(p, "file=", strlen("file=")) == 0) {
			p += strlen("file=");
			snprintf(filename, len, "%s", p);
		}
	}
}

dp-arm's avatar
dp-arm committed
664
665
static int create_cmd(int argc, char *argv[])
{
666
667
	struct option *opts = NULL;
	size_t nr_opts = 0;
dp-arm's avatar
dp-arm committed
668
	unsigned long long toc_flags = 0;
669
	unsigned long align = 1;
dp-arm's avatar
dp-arm committed
670
671

	if (argc < 2)
672
		create_usage();
dp-arm's avatar
dp-arm committed
673

674
675
	opts = fill_common_opts(opts, &nr_opts, required_argument);
	opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm's avatar
dp-arm committed
676
	    OPT_PLAT_TOC_FLAGS);
677
	opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
678
	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
679
	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm's avatar
dp-arm committed
680
681

	while (1) {
682
		int c, opt_index = 0;
dp-arm's avatar
dp-arm committed
683

684
		c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm's avatar
dp-arm committed
685
686
687
688
689
		if (c == -1)
			break;

		switch (c) {
		case OPT_TOC_ENTRY: {
690
691
692
			image_desc_t *desc;

			desc = lookup_image_desc_from_opt(opts[opt_index].name);
693
			set_image_desc_action(desc, DO_PACK, optarg);
dp-arm's avatar
dp-arm committed
694
695
696
697
698
			break;
		}
		case OPT_PLAT_TOC_FLAGS:
			parse_plat_toc_flags(optarg, &toc_flags);
			break;
699
700
701
		case OPT_ALIGN:
			align = get_image_align(optarg);
			break;
702
703
704
		case 'b': {
			char name[_UUID_STR_LEN + 1];
			char filename[PATH_MAX] = { 0 };
705
			uuid_t uuid = uuid_null;
706
707
708
709
710
711
712
713
714
715
			image_desc_t *desc;

			parse_blob_opt(optarg, &uuid,
			    filename, sizeof(filename));

			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
			    filename[0] == '\0')
				create_usage();

			desc = lookup_image_desc_from_uuid(&uuid);
716
			if (desc == NULL) {
717
718
719
720
				uuid_to_str(name, sizeof(name), &uuid);
				desc = new_image_desc(&uuid, name, "blob");
				add_image_desc(desc);
			}
721
			set_image_desc_action(desc, DO_PACK, filename);
722
723
			break;
		}
dp-arm's avatar
dp-arm committed
724
		default:
725
			create_usage();
dp-arm's avatar
dp-arm committed
726
727
728
729
		}
	}
	argc -= optind;
	argv += optind;
730
	free(opts);
dp-arm's avatar
dp-arm committed
731
732

	if (argc == 0)
733
		create_usage();
dp-arm's avatar
dp-arm committed
734
735
736

	update_fip();

737
	pack_images(argv[0], toc_flags, align);
dp-arm's avatar
dp-arm committed
738
739
740
741
742
743
744
	return 0;
}

static void create_usage(void)
{
	toc_entry_t *toc_entry = toc_entries;

745
746
747
	printf("fiptool create [opts] FIP_FILENAME\n");
	printf("\n");
	printf("Options:\n");
748
	printf("  --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
749
750
	printf("  --blob uuid=...,file=...\tAdd an image with the given UUID pointed to by file.\n");
	printf("  --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
751
	printf("\n");
dp-arm's avatar
dp-arm committed
752
753
754
755
	printf("Specific images are packed with the following options:\n");
	for (; toc_entry->cmdline_name != NULL; toc_entry++)
		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
		    toc_entry->name);
756
	exit(1);
dp-arm's avatar
dp-arm committed
757
758
759
760
}

static int update_cmd(int argc, char *argv[])
{
761
762
	struct option *opts = NULL;
	size_t nr_opts = 0;
763
	char outfile[PATH_MAX] = { 0 };
dp-arm's avatar
dp-arm committed
764
765
	fip_toc_header_t toc_header = { 0 };
	unsigned long long toc_flags = 0;
766
	unsigned long align = 1;
dp-arm's avatar
dp-arm committed
767
768
769
	int pflag = 0;

	if (argc < 2)
770
		update_usage();
dp-arm's avatar
dp-arm committed
771

772
	opts = fill_common_opts(opts, &nr_opts, required_argument);
773
	opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
774
	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
775
776
	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
	opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm's avatar
dp-arm committed
777
	    OPT_PLAT_TOC_FLAGS);
778
	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm's avatar
dp-arm committed
779
780

	while (1) {
781
		int c, opt_index = 0;
dp-arm's avatar
dp-arm committed
782

783
		c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm's avatar
dp-arm committed
784
785
786
787
788
		if (c == -1)
			break;

		switch (c) {
		case OPT_TOC_ENTRY: {
789
790
791
			image_desc_t *desc;

			desc = lookup_image_desc_from_opt(opts[opt_index].name);
792
			set_image_desc_action(desc, DO_PACK, optarg);
dp-arm's avatar
dp-arm committed
793
794
			break;
		}
795
		case OPT_PLAT_TOC_FLAGS:
dp-arm's avatar
dp-arm committed
796
797
798
			parse_plat_toc_flags(optarg, &toc_flags);
			pflag = 1;
			break;
799
800
801
		case 'b': {
			char name[_UUID_STR_LEN + 1];
			char filename[PATH_MAX] = { 0 };
802
			uuid_t uuid = uuid_null;
803
804
805
806
807
808
809
810
811
812
			image_desc_t *desc;

			parse_blob_opt(optarg, &uuid,
			    filename, sizeof(filename));

			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
			    filename[0] == '\0')
				update_usage();

			desc = lookup_image_desc_from_uuid(&uuid);
813
			if (desc == NULL) {
814
815
816
817
				uuid_to_str(name, sizeof(name), &uuid);
				desc = new_image_desc(&uuid, name, "blob");
				add_image_desc(desc);
			}
818
			set_image_desc_action(desc, DO_PACK, filename);
819
820
			break;
		}
821
822
823
		case OPT_ALIGN:
			align = get_image_align(optarg);
			break;
dp-arm's avatar
dp-arm committed
824
825
826
827
		case 'o':
			snprintf(outfile, sizeof(outfile), "%s", optarg);
			break;
		default:
828
			update_usage();
dp-arm's avatar
dp-arm committed
829
830
831
832
		}
	}
	argc -= optind;
	argv += optind;
833
	free(opts);
dp-arm's avatar
dp-arm committed
834
835

	if (argc == 0)
836
		update_usage();
dp-arm's avatar
dp-arm committed
837
838
839
840

	if (outfile[0] == '\0')
		snprintf(outfile, sizeof(outfile), "%s", argv[0]);

841
	if (access(argv[0], F_OK) == 0)
dp-arm's avatar
dp-arm committed
842
843
844
845
846
847
848
849
		parse_fip(argv[0], &toc_header);

	if (pflag)
		toc_header.flags &= ~(0xffffULL << 32);
	toc_flags = (toc_header.flags |= toc_flags);

	update_fip();

850
	pack_images(outfile, toc_flags, align);
dp-arm's avatar
dp-arm committed
851
852
853
854
855
856
857
	return 0;
}

static void update_usage(void)
{
	toc_entry_t *toc_entry = toc_entries;

858
859
860
	printf("fiptool update [opts] FIP_FILENAME\n");
	printf("\n");
	printf("Options:\n");
861
	printf("  --align <value>\t\tEach image is aligned to <value> (default: 1).\n");
862
	printf("  --blob uuid=...,file=...\tAdd or update an image with the given UUID pointed to by file.\n");
dp-arm's avatar
dp-arm committed
863
	printf("  --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
864
	printf("  --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n");
865
	printf("\n");
dp-arm's avatar
dp-arm committed
866
867
868
869
	printf("Specific images are packed with the following options:\n");
	for (; toc_entry->cmdline_name != NULL; toc_entry++)
		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
		    toc_entry->name);
870
	exit(1);
dp-arm's avatar
dp-arm committed
871
872
873
874
}

static int unpack_cmd(int argc, char *argv[])
{
875
876
	struct option *opts = NULL;
	size_t nr_opts = 0;
877
	char outdir[PATH_MAX] = { 0 };
878
	image_desc_t *desc;
dp-arm's avatar
dp-arm committed
879
880
881
882
	int fflag = 0;
	int unpack_all = 1;

	if (argc < 2)
883
		unpack_usage();
dp-arm's avatar
dp-arm committed
884

885
	opts = fill_common_opts(opts, &nr_opts, required_argument);
886
	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
887
888
889
	opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm's avatar
dp-arm committed
890
891

	while (1) {
892
		int c, opt_index = 0;
dp-arm's avatar
dp-arm committed
893

894
		c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm's avatar
dp-arm committed
895
896
897
898
		if (c == -1)
			break;

		switch (c) {
899
900
901
902
		case OPT_TOC_ENTRY: {
			image_desc_t *desc;

			desc = lookup_image_desc_from_opt(opts[opt_index].name);
903
			set_image_desc_action(desc, DO_UNPACK, optarg);
904
			unpack_all = 0;
dp-arm's avatar
dp-arm committed
905
			break;
906
		}
907
908
909
		case 'b': {
			char name[_UUID_STR_LEN + 1];
			char filename[PATH_MAX] = { 0 };
910
			uuid_t uuid = uuid_null;
911
912
913
914
915
916
917
918
919
920
			image_desc_t *desc;

			parse_blob_opt(optarg, &uuid,
			    filename, sizeof(filename));

			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
			    filename[0] == '\0')
				unpack_usage();

			desc = lookup_image_desc_from_uuid(&uuid);
921
			if (desc == NULL) {
922
923
924
925
				uuid_to_str(name, sizeof(name), &uuid);
				desc = new_image_desc(&uuid, name, "blob");
				add_image_desc(desc);
			}
926
			set_image_desc_action(desc, DO_UNPACK, filename);
927
928
929
			unpack_all = 0;
			break;
		}
dp-arm's avatar
dp-arm committed
930
931
932
933
934
935
936
		case 'f':
			fflag = 1;
			break;
		case 'o':
			snprintf(outdir, sizeof(outdir), "%s", optarg);
			break;
		default:
937
			unpack_usage();
dp-arm's avatar
dp-arm committed
938
939
940
941
		}
	}
	argc -= optind;
	argv += optind;
942
	free(opts);
dp-arm's avatar
dp-arm committed
943
944

	if (argc == 0)
945
		unpack_usage();
dp-arm's avatar
dp-arm committed
946
947
948
949
950
951
952
953

	parse_fip(argv[0], NULL);

	if (outdir[0] != '\0')
		if (chdir(outdir) == -1)
			log_err("chdir %s", outdir);

	/* Unpack all specified images. */
954
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
955
		char file[PATH_MAX];
956
		image_t *image = desc->image;
957

958
		if (!unpack_all && desc->action != DO_UNPACK)
dp-arm's avatar
dp-arm committed
959
960
961
			continue;

		/* Build filename. */
962
		if (desc->action_arg == NULL)
dp-arm's avatar
dp-arm committed
963
			snprintf(file, sizeof(file), "%s.bin",
964
			    desc->cmdline_name);
dp-arm's avatar
dp-arm committed
965
966
		else
			snprintf(file, sizeof(file), "%s",
967
			    desc->action_arg);
dp-arm's avatar
dp-arm committed
968

969
970
		if (image == NULL) {
			if (!unpack_all)
971
				log_warnx("%s does not exist in %s",
972
				    file, argv[0]);
dp-arm's avatar
dp-arm committed
973
974
975
976
977
978
			continue;
		}

		if (access(file, F_OK) != 0 || fflag) {
			if (verbose)
				log_dbgx("Unpacking %s", file);
979
			write_image_to_file(image, file);
dp-arm's avatar
dp-arm committed
980
981
982
983
984
985
986
987
988
989
990
991
992
		} else {
			log_warnx("File %s already exists, use --force to overwrite it",
			    file);
		}
	}

	return 0;
}

static void unpack_usage(void)
{
	toc_entry_t *toc_entry = toc_entries;

993
994
995
	printf("fiptool unpack [opts] FIP_FILENAME\n");
	printf("\n");
	printf("Options:\n");
996
997
	printf("  --blob uuid=...,file=...\tUnpack an image with the given UUID to file.\n");
	printf("  --force\t\t\tIf the output file already exists, use --force to overwrite it.\n");
998
	printf("  --out path\t\t\tSet the output directory path.\n");
999
	printf("\n");
dp-arm's avatar
dp-arm committed
1000
1001
1002
1003
	printf("Specific images are unpacked with the following options:\n");
	for (; toc_entry->cmdline_name != NULL; toc_entry++)
		printf("  --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
		    toc_entry->name);
1004
	printf("\n");
dp-arm's avatar
dp-arm committed
1005
	printf("If no options are provided, all images will be unpacked.\n");
1006
	exit(1);
dp-arm's avatar
dp-arm committed
1007
1008
1009
1010
}

static int remove_cmd(int argc, char *argv[])
{
1011
1012
	struct option *opts = NULL;
	size_t nr_opts = 0;
1013
	char outfile[PATH_MAX] = { 0 };
dp-arm's avatar
dp-arm committed
1014
	fip_toc_header_t toc_header;
1015
	image_desc_t *desc;
1016
	unsigned long align = 1;
dp-arm's avatar
dp-arm committed
1017
1018
1019
	int fflag = 0;

	if (argc < 2)
1020
		remove_usage();
dp-arm's avatar
dp-arm committed
1021

1022
	opts = fill_common_opts(opts, &nr_opts, no_argument);
1023
	opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN);
1024
	opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
1025
1026
1027
	opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
	opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
	opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm's avatar
dp-arm committed
1028
1029

	while (1) {
1030
		int c, opt_index = 0;
dp-arm's avatar
dp-arm committed
1031

1032
		c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm's avatar
dp-arm committed
1033
1034
1035
1036
		if (c == -1)
			break;

		switch (c) {
1037
1038
1039
1040
		case OPT_TOC_ENTRY: {
			image_desc_t *desc;

			desc = lookup_image_desc_from_opt(opts[opt_index].name);
1041
			set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm's avatar
dp-arm committed
1042
			break;
1043
		}
1044
1045
1046
		case OPT_ALIGN:
			align = get_image_align(optarg);
			break;
1047
1048
		case 'b': {
			char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1049
			uuid_t uuid = uuid_null;
1050
1051
1052
1053
1054
1055
1056
1057
1058
			image_desc_t *desc;

			parse_blob_opt(optarg, &uuid,
			    filename, sizeof(filename));

			if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
				remove_usage();

			desc = lookup_image_desc_from_uuid(&uuid);
1059
			if (desc == NULL) {
1060
1061
1062
1063
				uuid_to_str(name, sizeof(name), &uuid);
				desc = new_image_desc(&uuid, name, "blob");
				add_image_desc(desc);
			}
1064
			set_image_desc_action(desc, DO_REMOVE, NULL);
1065
1066
			break;
		}
dp-arm's avatar
dp-arm committed
1067
1068
1069
1070
1071
1072
1073
		case 'f':
			fflag = 1;
			break;
		case 'o':
			snprintf(outfile, sizeof(outfile), "%s", optarg);
			break;
		default:
1074
			remove_usage();
dp-arm's avatar
dp-arm committed
1075
1076
1077
1078
		}
	}
	argc -= optind;
	argv += optind;
1079
	free(opts);
dp-arm's avatar
dp-arm committed
1080
1081

	if (argc == 0)
1082
		remove_usage();
dp-arm's avatar
dp-arm committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092

	if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
		log_errx("File %s already exists, use --force to overwrite it",
		    outfile);

	if (outfile[0] == '\0')
		snprintf(outfile, sizeof(outfile), "%s", argv[0]);

	parse_fip(argv[0], &toc_header);

1093
1094
	for (desc = image_desc_head; desc != NULL; desc = desc->next) {
		if (desc->action != DO_REMOVE)
dp-arm's avatar
dp-arm committed
1095
			continue;
1096

1097
		if (desc->image != NULL) {
dp-arm's avatar
dp-arm committed
1098
			if (verbose)
1099
				log_dbgx("Removing %s",
1100
				    desc->cmdline_name);
1101
1102
			free(desc->image);
			desc->image = NULL;
dp-arm's avatar
dp-arm committed
1103
		} else {
1104
			log_warnx("%s does not exist in %s",
1105
			    desc->cmdline_name, argv[0]);
dp-arm's avatar
dp-arm committed
1106
1107
1108
		}
	}

1109
	pack_images(outfile, toc_header.flags, align);
dp-arm's avatar
dp-arm committed
1110
1111
1112
1113
1114
1115
1116
	return 0;
}

static void remove_usage(void)
{
	toc_entry_t *toc_entry = toc_entries;

1117
1118
1119
	printf("fiptool remove [opts] FIP_FILENAME\n");
	printf("\n");
	printf("Options:\n");
1120
	printf("  --align <value>\tEach image is aligned to <value> (default: 1).\n");
1121
	printf("  --blob uuid=...\tRemove an image with the given UUID.\n");
1122
	printf("  --force\t\tIf the output FIP file already exists, use --force to overwrite it.\n");
dp-arm's avatar
dp-arm committed
1123
	printf("  --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1124
	printf("\n");
dp-arm's avatar
dp-arm committed
1125
1126
1127
1128
	printf("Specific images are removed with the following options:\n");
	for (; toc_entry->cmdline_name != NULL; toc_entry++)
		printf("  --%-16s\t%s\n", toc_entry->cmdline_name,
		    toc_entry->name);
1129
	exit(1);
dp-arm's avatar
dp-arm committed
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
}

static int version_cmd(int argc, char *argv[])
{
#ifdef VERSION
	puts(VERSION);
#else
	/* If built from fiptool directory, VERSION is not set. */
	puts("Unknown version");
#endif
	return 0;
}

static void version_usage(void)
{
	printf("fiptool version\n");
1146
	exit(1);
dp-arm's avatar
dp-arm committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
}

static int help_cmd(int argc, char *argv[])
{
	int i;

	if (argc < 2)
		usage();
	argc--, argv++;

	for (i = 0; i < NELEM(cmds); i++) {
		if (strcmp(cmds[i].name, argv[0]) == 0 &&
1159
		    cmds[i].usage != NULL)
dp-arm's avatar
dp-arm committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
			cmds[i].usage();
	}
	if (i == NELEM(cmds))
		printf("No help for subcommand '%s'\n", argv[0]);
	return 0;
}

static void usage(void)
{
1169
	printf("usage: fiptool [--verbose] <command> [<args>]\n");
dp-arm's avatar
dp-arm committed
1170
1171
	printf("Global options supported:\n");
	printf("  --verbose\tEnable verbose output for all commands.\n");
1172
	printf("\n");
dp-arm's avatar
dp-arm committed
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
	printf("Commands supported:\n");
	printf("  info\t\tList images contained in FIP.\n");
	printf("  create\tCreate a new FIP with the given images.\n");
	printf("  update\tUpdate an existing FIP with the given images.\n");
	printf("  unpack\tUnpack images from FIP.\n");
	printf("  remove\tRemove images from FIP.\n");
	printf("  version\tShow fiptool version.\n");
	printf("  help\t\tShow help for given command.\n");
	exit(1);
}

int main(int argc, char *argv[])
{
	int i, ret = 0;

1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
	while (1) {
		int c, opt_index = 0;
		static struct option opts[] = {
			{ "verbose", no_argument, NULL, 'v' },
			{ NULL, no_argument, NULL, 0 }
		};

		/*
		 * Set POSIX mode so getopt stops at the first non-option
		 * which is the subcommand.
		 */
		c = getopt_long(argc, argv, "+v", opts, &opt_index);
		if (c == -1)
			break;
dp-arm's avatar
dp-arm committed
1202

1203
1204
1205
1206
1207
		switch (c) {
		case 'v':
			verbose = 1;
			break;
		default:
1208
			usage();
1209
		}
dp-arm's avatar
dp-arm committed
1210
	}
1211
1212
1213
1214
1215
1216
1217
	argc -= optind;
	argv += optind;
	/* Reset optind for subsequent getopt processing. */
	optind = 0;

	if (argc == 0)
		usage();
dp-arm's avatar
dp-arm committed
1218

1219
	fill_image_descs();
dp-arm's avatar
dp-arm committed
1220
1221
1222
1223
1224
1225
1226
1227
	for (i = 0; i < NELEM(cmds); i++) {
		if (strcmp(cmds[i].name, argv[0]) == 0) {
			ret = cmds[i].handler(argc, argv);
			break;
		}
	}
	if (i == NELEM(cmds))
		usage();
1228
	free_image_descs();
dp-arm's avatar
dp-arm committed
1229
1230
	return ret;
}