Blame fdtoverlay.c

Packit 2ad57b
/*
Packit 2ad57b
 * Copyright (c) 2017 Konsulko Group Inc. All rights reserved.
Packit 2ad57b
 *
Packit 2ad57b
 * Author:
Packit 2ad57b
 *	 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Packit 2ad57b
 *
Packit 2ad57b
 * This program is free software; you can redistribute it and/or
Packit 2ad57b
 * modify it under the terms of the GNU General Public License as
Packit 2ad57b
 * published by the Free Software Foundation; either version 2 of
Packit 2ad57b
 * the License, or (at your option) any later version.
Packit 2ad57b
 *
Packit 2ad57b
 * This program is distributed in the hope that it will be useful,
Packit 2ad57b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2ad57b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2ad57b
 * GNU General Public License for more details.
Packit 2ad57b
 *
Packit 2ad57b
 * You should have received a copy of the GNU General Public License
Packit 2ad57b
 * along with this program; if not, write to the Free Software
Packit 2ad57b
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
Packit 2ad57b
 * MA 02111-1307 USA
Packit 2ad57b
 */
Packit 2ad57b
Packit 2ad57b
#include <assert.h>
Packit 2ad57b
#include <ctype.h>
Packit 2ad57b
#include <getopt.h>
Packit 2ad57b
#include <stdio.h>
Packit 2ad57b
#include <stdlib.h>
Packit 2ad57b
#include <string.h>
Packit 2ad57b
#include <inttypes.h>
Packit 2ad57b
Packit 2ad57b
#include <libfdt.h>
Packit 2ad57b
Packit 2ad57b
#include "util.h"
Packit 2ad57b
Packit 2ad57b
/* Usage related data. */
Packit 2ad57b
static const char usage_synopsis[] =
Packit 2ad57b
	"apply a number of overlays to a base blob\n"
Packit 2ad57b
	"	fdtoverlay <options> [<overlay.dtbo> [<overlay.dtbo>]]\n"
Packit 2ad57b
	"\n"
Packit 2ad57b
	USAGE_TYPE_MSG;
Packit 2ad57b
static const char usage_short_opts[] = "i:o:v" USAGE_COMMON_SHORT_OPTS;
Packit 2ad57b
static struct option const usage_long_opts[] = {
Packit 2ad57b
	{"input",            required_argument, NULL, 'i'},
Packit 2ad57b
	{"output",	     required_argument, NULL, 'o'},
Packit 2ad57b
	{"verbose",	           no_argument, NULL, 'v'},
Packit 2ad57b
	USAGE_COMMON_LONG_OPTS,
Packit 2ad57b
};
Packit 2ad57b
static const char * const usage_opts_help[] = {
Packit 2ad57b
	"Input base DT blob",
Packit 2ad57b
	"Output DT blob",
Packit 2ad57b
	"Verbose messages",
Packit 2ad57b
	USAGE_COMMON_OPTS_HELP
Packit 2ad57b
};
Packit 2ad57b
Packit 2ad57b
int verbose = 0;
Packit 2ad57b
Packit 2ad57b
static int do_fdtoverlay(const char *input_filename,
Packit 2ad57b
			 const char *output_filename,
Packit 2ad57b
			 int argc, char *argv[])
Packit 2ad57b
{
Packit 2ad57b
	char *blob = NULL;
Packit 2ad57b
	char **ovblob = NULL;
Packit 2ad57b
	off_t blob_len, ov_len, total_len;
Packit 2ad57b
	int i, ret = -1;
Packit 2ad57b
Packit 2ad57b
	blob = utilfdt_read_len(input_filename, &blob_len);
Packit 2ad57b
	if (!blob) {
Packit 2ad57b
		fprintf(stderr, "\nFailed to read base blob %s\n",
Packit 2ad57b
				input_filename);
Packit 2ad57b
		goto out_err;
Packit 2ad57b
	}
Packit 2ad57b
	if (fdt_totalsize(blob) > blob_len) {
Packit 2ad57b
		fprintf(stderr,
Packit 2ad57b
 "\nBase blob is incomplete (%lu / %" PRIu32 " bytes read)\n",
Packit 2ad57b
			(unsigned long)blob_len, fdt_totalsize(blob));
Packit 2ad57b
		goto out_err;
Packit 2ad57b
	}
Packit 2ad57b
	ret = 0;
Packit 2ad57b
Packit 2ad57b
	/* allocate blob pointer array */
Packit 2ad57b
	ovblob = malloc(sizeof(*ovblob) * argc);
Packit 2ad57b
	memset(ovblob, 0, sizeof(*ovblob) * argc);
Packit 2ad57b
Packit 2ad57b
	/* read and keep track of the overlay blobs */
Packit 2ad57b
	total_len = 0;
Packit 2ad57b
	for (i = 0; i < argc; i++) {
Packit 2ad57b
		ovblob[i] = utilfdt_read_len(argv[i], &ov_len);
Packit 2ad57b
		if (!ovblob[i]) {
Packit 2ad57b
			fprintf(stderr, "\nFailed to read overlay %s\n",
Packit 2ad57b
					argv[i]);
Packit 2ad57b
			goto out_err;
Packit 2ad57b
		}
Packit 2ad57b
		total_len += ov_len;
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	/* grow the blob to worst case */
Packit 2ad57b
	blob_len = fdt_totalsize(blob) + total_len;
Packit 2ad57b
	blob = xrealloc(blob, blob_len);
Packit 2ad57b
	fdt_open_into(blob, blob, blob_len);
Packit 2ad57b
Packit 2ad57b
	/* apply the overlays in sequence */
Packit 2ad57b
	for (i = 0; i < argc; i++) {
Packit 2ad57b
		ret = fdt_overlay_apply(blob, ovblob[i]);
Packit 2ad57b
		if (ret) {
Packit 2ad57b
			fprintf(stderr, "\nFailed to apply %s (%d)\n",
Packit 2ad57b
					argv[i], ret);
Packit 2ad57b
			goto out_err;
Packit 2ad57b
		}
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	fdt_pack(blob);
Packit 2ad57b
	ret = utilfdt_write(output_filename, blob);
Packit 2ad57b
	if (ret)
Packit 2ad57b
		fprintf(stderr, "\nFailed to write output blob %s\n",
Packit 2ad57b
				output_filename);
Packit 2ad57b
Packit 2ad57b
out_err:
Packit 2ad57b
	if (ovblob) {
Packit 2ad57b
		for (i = 0; i < argc; i++) {
Packit 2ad57b
			if (ovblob[i])
Packit 2ad57b
				free(ovblob[i]);
Packit 2ad57b
		}
Packit 2ad57b
		free(ovblob);
Packit 2ad57b
	}
Packit 2ad57b
	free(blob);
Packit 2ad57b
Packit 2ad57b
	return ret;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int main(int argc, char *argv[])
Packit 2ad57b
{
Packit 2ad57b
	int opt, i;
Packit 2ad57b
	char *input_filename = NULL;
Packit 2ad57b
	char *output_filename = NULL;
Packit 2ad57b
Packit 2ad57b
	while ((opt = util_getopt_long()) != EOF) {
Packit 2ad57b
		switch (opt) {
Packit 2ad57b
		case_USAGE_COMMON_FLAGS
Packit 2ad57b
Packit 2ad57b
		case 'i':
Packit 2ad57b
			input_filename = optarg;
Packit 2ad57b
			break;
Packit 2ad57b
		case 'o':
Packit 2ad57b
			output_filename = optarg;
Packit 2ad57b
			break;
Packit 2ad57b
		case 'v':
Packit 2ad57b
			verbose = 1;
Packit 2ad57b
			break;
Packit 2ad57b
		}
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	if (!input_filename)
Packit 2ad57b
		usage("missing input file");
Packit 2ad57b
Packit 2ad57b
	if (!output_filename)
Packit 2ad57b
		usage("missing output file");
Packit 2ad57b
Packit 2ad57b
	argv += optind;
Packit 2ad57b
	argc -= optind;
Packit 2ad57b
Packit 2ad57b
	if (argc <= 0)
Packit 2ad57b
		usage("missing overlay file(s)");
Packit 2ad57b
Packit 2ad57b
	if (verbose) {
Packit 2ad57b
		printf("input  = %s\n", input_filename);
Packit 2ad57b
		printf("output = %s\n", output_filename);
Packit 2ad57b
		for (i = 0; i < argc; i++)
Packit 2ad57b
			printf("overlay[%d] = %s\n", i, argv[i]);
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	if (do_fdtoverlay(input_filename, output_filename, argc, argv))
Packit 2ad57b
		return 1;
Packit 2ad57b
Packit 2ad57b
	return 0;
Packit 2ad57b
}