1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2017 Konsulko Group Inc. All rights reserved.
6 * Pantelis Antoniou <pantelis.antoniou@konsulko.com>
21 #define BUF_INCREMENT 65536
23 /* Usage related data. */
24 static const char usage_synopsis[] =
25 "apply a number of overlays to a base blob\n"
26 " fdtoverlay <options> [<overlay.dtbo> [<overlay.dtbo>]]\n"
29 static const char usage_short_opts[] = "i:o:v" USAGE_COMMON_SHORT_OPTS;
30 static struct option const usage_long_opts[] = {
31 {"input", required_argument, NULL, 'i'},
32 {"output", required_argument, NULL, 'o'},
33 {"verbose", no_argument, NULL, 'v'},
34 USAGE_COMMON_LONG_OPTS,
36 static const char * const usage_opts_help[] = {
40 USAGE_COMMON_OPTS_HELP
45 static void *apply_one(char *base, const char *overlay, size_t *buf_len,
53 * We take a copies first, because a a failed apply can trash
54 * both the base blob and the overlay
56 tmpo = xmalloc(fdt_totalsize(overlay));
59 tmp = xrealloc(tmp, *buf_len);
60 ret = fdt_open_into(base, tmp, *buf_len);
63 "\nFailed to make temporary copy: %s\n",
68 memcpy(tmpo, overlay, fdt_totalsize(overlay));
70 ret = fdt_overlay_apply(tmp, tmpo);
71 if (ret == -FDT_ERR_NOSPACE) {
72 *buf_len += BUF_INCREMENT;
74 } while (ret == -FDT_ERR_NOSPACE);
77 fprintf(stderr, "\nFailed to apply '%s': %s\n",
78 name, fdt_strerror(ret));
93 static int do_fdtoverlay(const char *input_filename,
94 const char *output_filename,
95 int argc, char *argv[])
102 blob = utilfdt_read(input_filename, &buf_len);
104 fprintf(stderr, "\nFailed to read '%s'\n", input_filename);
107 if (fdt_totalsize(blob) > buf_len) {
109 "\nBase blob is incomplete (%lu / %" PRIu32 " bytes read)\n",
110 (unsigned long)buf_len, fdt_totalsize(blob));
114 /* allocate blob pointer array */
115 ovblob = xmalloc(sizeof(*ovblob) * argc);
116 memset(ovblob, 0, sizeof(*ovblob) * argc);
118 /* read and keep track of the overlay blobs */
119 for (i = 0; i < argc; i++) {
121 ovblob[i] = utilfdt_read(argv[i], &ov_len);
123 fprintf(stderr, "\nFailed to read '%s'\n", argv[i]);
126 if (fdt_totalsize(ovblob[i]) > ov_len) {
128 "\nOverlay '%s' is incomplete (%lu / %" PRIu32 " bytes read)\n",
129 argv[i], (unsigned long)ov_len,
130 fdt_totalsize(ovblob[i]));
135 buf_len = fdt_totalsize(blob);
137 /* apply the overlays in sequence */
138 for (i = 0; i < argc; i++) {
139 blob = apply_one(blob, ovblob[i], &buf_len, argv[i]);
145 ret = utilfdt_write(output_filename, blob);
147 fprintf(stderr, "\nFailed to write '%s'\n",
152 for (i = 0; i < argc; i++) {
163 int main(int argc, char *argv[])
166 char *input_filename = NULL;
167 char *output_filename = NULL;
169 while ((opt = util_getopt_long()) != EOF) {
171 case_USAGE_COMMON_FLAGS
174 input_filename = optarg;
177 output_filename = optarg;
186 usage("missing input file");
188 if (!output_filename)
189 usage("missing output file");
195 usage("missing overlay file(s)");
198 printf("input = %s\n", input_filename);
199 printf("output = %s\n", output_filename);
200 for (i = 0; i < argc; i++)
201 printf("overlay[%d] = %s\n", i, argv[i]);
204 if (do_fdtoverlay(input_filename, output_filename, argc, argv))