fdtoverlay.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2017 Konsulko Group Inc. All rights reserved.
  4. *
  5. * Author:
  6. * Pantelis Antoniou <pantelis.antoniou@konsulko.com>
  7. */
  8. #include <assert.h>
  9. #include <ctype.h>
  10. #include <getopt.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <inttypes.h>
  15. #include <libfdt.h>
  16. #include "util.h"
  17. #define BUF_INCREMENT 65536
  18. /* Usage related data. */
  19. static const char usage_synopsis[] =
  20. "apply a number of overlays to a base blob\n"
  21. " fdtoverlay <options> [<overlay.dtbo> [<overlay.dtbo>]]";
  22. static const char usage_short_opts[] = "i:o:v" USAGE_COMMON_SHORT_OPTS;
  23. static struct option const usage_long_opts[] = {
  24. {"input", required_argument, NULL, 'i'},
  25. {"output", required_argument, NULL, 'o'},
  26. {"verbose", no_argument, NULL, 'v'},
  27. USAGE_COMMON_LONG_OPTS,
  28. };
  29. static const char * const usage_opts_help[] = {
  30. "Input base DT blob",
  31. "Output DT blob",
  32. "Verbose messages",
  33. USAGE_COMMON_OPTS_HELP
  34. };
  35. int verbose = 0;
  36. static void *apply_one(char *base, const char *overlay, size_t *buf_len,
  37. const char *name)
  38. {
  39. char *tmp = NULL;
  40. char *tmpo;
  41. int ret;
  42. bool has_symbols;
  43. /*
  44. * We take copies first, because a failed apply can trash
  45. * both the base blob and the overlay
  46. */
  47. tmpo = xmalloc(fdt_totalsize(overlay));
  48. do {
  49. tmp = xrealloc(tmp, *buf_len);
  50. ret = fdt_open_into(base, tmp, *buf_len);
  51. if (ret) {
  52. fprintf(stderr,
  53. "\nFailed to make temporary copy: %s\n",
  54. fdt_strerror(ret));
  55. goto fail;
  56. }
  57. ret = fdt_path_offset(tmp, "/__symbols__");
  58. has_symbols = ret >= 0;
  59. memcpy(tmpo, overlay, fdt_totalsize(overlay));
  60. ret = fdt_overlay_apply(tmp, tmpo);
  61. if (ret == -FDT_ERR_NOSPACE) {
  62. *buf_len += BUF_INCREMENT;
  63. }
  64. } while (ret == -FDT_ERR_NOSPACE);
  65. if (ret) {
  66. fprintf(stderr, "\nFailed to apply '%s': %s\n",
  67. name, fdt_strerror(ret));
  68. if (!has_symbols) {
  69. fprintf(stderr,
  70. "base blob does not have a '/__symbols__' node, "
  71. "make sure you have compiled the base blob with '-@' option\n");
  72. }
  73. goto fail;
  74. }
  75. free(base);
  76. free(tmpo);
  77. return tmp;
  78. fail:
  79. free(tmpo);
  80. if (tmp)
  81. free(tmp);
  82. return NULL;
  83. }
  84. static int do_fdtoverlay(const char *input_filename,
  85. const char *output_filename,
  86. int argc, char *argv[])
  87. {
  88. char *blob = NULL;
  89. char **ovblob = NULL;
  90. size_t buf_len;
  91. int i, ret = -1;
  92. blob = utilfdt_read(input_filename, &buf_len);
  93. if (!blob) {
  94. fprintf(stderr, "\nFailed to read '%s'\n", input_filename);
  95. goto out_err;
  96. }
  97. if (fdt_totalsize(blob) > buf_len) {
  98. fprintf(stderr,
  99. "\nBase blob is incomplete (%lu / %" PRIu32 " bytes read)\n",
  100. (unsigned long)buf_len, fdt_totalsize(blob));
  101. goto out_err;
  102. }
  103. /* allocate blob pointer array */
  104. ovblob = xmalloc(sizeof(*ovblob) * argc);
  105. memset(ovblob, 0, sizeof(*ovblob) * argc);
  106. /* read and keep track of the overlay blobs */
  107. for (i = 0; i < argc; i++) {
  108. size_t ov_len;
  109. ovblob[i] = utilfdt_read(argv[i], &ov_len);
  110. if (!ovblob[i]) {
  111. fprintf(stderr, "\nFailed to read '%s'\n", argv[i]);
  112. goto out_err;
  113. }
  114. if (fdt_totalsize(ovblob[i]) > ov_len) {
  115. fprintf(stderr,
  116. "\nOverlay '%s' is incomplete (%lu / %" PRIu32 " bytes read)\n",
  117. argv[i], (unsigned long)ov_len,
  118. fdt_totalsize(ovblob[i]));
  119. goto out_err;
  120. }
  121. }
  122. buf_len = fdt_totalsize(blob);
  123. /* apply the overlays in sequence */
  124. for (i = 0; i < argc; i++) {
  125. blob = apply_one(blob, ovblob[i], &buf_len, argv[i]);
  126. if (!blob)
  127. goto out_err;
  128. }
  129. fdt_pack(blob);
  130. ret = utilfdt_write(output_filename, blob);
  131. if (ret)
  132. fprintf(stderr, "\nFailed to write '%s'\n",
  133. output_filename);
  134. out_err:
  135. if (ovblob) {
  136. for (i = 0; i < argc; i++) {
  137. if (ovblob[i])
  138. free(ovblob[i]);
  139. }
  140. free(ovblob);
  141. }
  142. free(blob);
  143. return ret;
  144. }
  145. int main(int argc, char *argv[])
  146. {
  147. int opt, i;
  148. char *input_filename = NULL;
  149. char *output_filename = NULL;
  150. while ((opt = util_getopt_long()) != EOF) {
  151. switch (opt) {
  152. case_USAGE_COMMON_FLAGS
  153. case 'i':
  154. input_filename = optarg;
  155. break;
  156. case 'o':
  157. output_filename = optarg;
  158. break;
  159. case 'v':
  160. verbose = 1;
  161. break;
  162. }
  163. }
  164. if (!input_filename)
  165. usage("missing input file");
  166. if (!output_filename)
  167. usage("missing output file");
  168. argv += optind;
  169. argc -= optind;
  170. if (argc <= 0)
  171. usage("missing overlay file(s)");
  172. if (verbose) {
  173. printf("input = %s\n", input_filename);
  174. printf("output = %s\n", output_filename);
  175. for (i = 0; i < argc; i++)
  176. printf("overlay[%d] = %s\n", i, argv[i]);
  177. }
  178. if (do_fdtoverlay(input_filename, output_filename, argc, argv))
  179. return 1;
  180. return 0;
  181. }