Blame fdtoverlay.c

Packit Service 0ee8e1
// SPDX-License-Identifier: GPL-2.0-or-later
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
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 Service 0ee8e1
#define BUF_INCREMENT	65536
Packit Service 0ee8e1
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 Service 0ee8e1
static void *apply_one(char *base, const char *overlay, size_t *buf_len,
Packit Service 0ee8e1
		       const char *name)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	char *tmp = NULL;
Packit Service 0ee8e1
	char *tmpo;
Packit Service 0ee8e1
	int ret;
Packit Service 0ee8e1
Packit Service 0ee8e1
	/*
Packit Service 0ee8e1
	 * We take a copies first, because a a failed apply can trash
Packit Service 0ee8e1
	 * both the base blob and the overlay
Packit Service 0ee8e1
	 */
Packit Service 0ee8e1
	tmpo = xmalloc(fdt_totalsize(overlay));
Packit Service 0ee8e1
Packit Service 0ee8e1
	do {
Packit Service 0ee8e1
		tmp = xrealloc(tmp, *buf_len);
Packit Service 0ee8e1
		ret = fdt_open_into(base, tmp, *buf_len);
Packit Service 0ee8e1
		if (ret) {
Packit Service 0ee8e1
			fprintf(stderr,
Packit Service 0ee8e1
				"\nFailed to make temporary copy: %s\n",
Packit Service 0ee8e1
				fdt_strerror(ret));
Packit Service 0ee8e1
			goto fail;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
Packit Service 0ee8e1
		memcpy(tmpo, overlay, fdt_totalsize(overlay));
Packit Service 0ee8e1
Packit Service 0ee8e1
		ret = fdt_overlay_apply(tmp, tmpo);
Packit Service 0ee8e1
		if (ret == -FDT_ERR_NOSPACE) {
Packit Service 0ee8e1
			*buf_len += BUF_INCREMENT;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
	} while (ret == -FDT_ERR_NOSPACE);
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (ret) {
Packit Service 0ee8e1
		fprintf(stderr, "\nFailed to apply '%s': %s\n",
Packit Service 0ee8e1
			name, fdt_strerror(ret));
Packit Service 0ee8e1
		goto fail;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit Service 0ee8e1
	free(base);
Packit Service 0ee8e1
	free(tmpo);
Packit Service 0ee8e1
	return tmp;
Packit Service 0ee8e1
Packit Service 0ee8e1
fail:
Packit Service 0ee8e1
	free(tmpo);
Packit Service 0ee8e1
	if (tmp)
Packit Service 0ee8e1
		free(tmp);
Packit Service 0ee8e1
Packit Service 0ee8e1
	return NULL;
Packit Service 0ee8e1
}
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 Service 0ee8e1
	size_t buf_len;
Packit 2ad57b
	int i, ret = -1;
Packit 2ad57b
Packit Service 0ee8e1
	blob = utilfdt_read(input_filename, &buf_len);
Packit 2ad57b
	if (!blob) {
Packit Service 0ee8e1
		fprintf(stderr, "\nFailed to read '%s'\n", input_filename);
Packit 2ad57b
		goto out_err;
Packit 2ad57b
	}
Packit Service 0ee8e1
	if (fdt_totalsize(blob) > buf_len) {
Packit 2ad57b
		fprintf(stderr,
Packit 2ad57b
 "\nBase blob is incomplete (%lu / %" PRIu32 " bytes read)\n",
Packit Service 0ee8e1
			(unsigned long)buf_len, fdt_totalsize(blob));
Packit 2ad57b
		goto out_err;
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	/* allocate blob pointer array */
Packit Service 0ee8e1
	ovblob = xmalloc(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
	for (i = 0; i < argc; i++) {
Packit Service 0ee8e1
		size_t ov_len;
Packit Service 0ee8e1
		ovblob[i] = utilfdt_read(argv[i], &ov_len);
Packit 2ad57b
		if (!ovblob[i]) {
Packit Service 0ee8e1
			fprintf(stderr, "\nFailed to read '%s'\n", argv[i]);
Packit Service 0ee8e1
			goto out_err;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
		if (fdt_totalsize(ovblob[i]) > ov_len) {
Packit Service 0ee8e1
			fprintf(stderr,
Packit Service 0ee8e1
"\nOverlay '%s' is incomplete (%lu / %" PRIu32 " bytes read)\n",
Packit Service 0ee8e1
				argv[i], (unsigned long)ov_len,
Packit Service 0ee8e1
				fdt_totalsize(ovblob[i]));
Packit 2ad57b
			goto out_err;
Packit 2ad57b
		}
Packit 2ad57b
	}
Packit 2ad57b
Packit Service 0ee8e1
	buf_len = fdt_totalsize(blob);
Packit 2ad57b
Packit 2ad57b
	/* apply the overlays in sequence */
Packit 2ad57b
	for (i = 0; i < argc; i++) {
Packit Service 0ee8e1
		blob = apply_one(blob, ovblob[i], &buf_len, argv[i]);
Packit Service 0ee8e1
		if (!blob)
Packit 2ad57b
			goto out_err;
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	fdt_pack(blob);
Packit 2ad57b
	ret = utilfdt_write(output_filename, blob);
Packit 2ad57b
	if (ret)
Packit Service 0ee8e1
		fprintf(stderr, "\nFailed to write '%s'\n",
Packit Service 0ee8e1
			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
}