fdtoverlay.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /*
  43. * We take copies first, because a failed apply can trash
  44. * both the base blob and the overlay
  45. */
  46. tmpo = xmalloc(fdt_totalsize(overlay));
  47. do {
  48. tmp = xrealloc(tmp, *buf_len);
  49. ret = fdt_open_into(base, tmp, *buf_len);
  50. if (ret) {
  51. fprintf(stderr,
  52. "\nFailed to make temporary copy: %s\n",
  53. fdt_strerror(ret));
  54. goto fail;
  55. }
  56. memcpy(tmpo, overlay, fdt_totalsize(overlay));
  57. ret = fdt_overlay_apply(tmp, tmpo);
  58. if (ret == -FDT_ERR_NOSPACE) {
  59. *buf_len += BUF_INCREMENT;
  60. }
  61. } while (ret == -FDT_ERR_NOSPACE);
  62. if (ret) {
  63. fprintf(stderr, "\nFailed to apply '%s': %s\n",
  64. name, fdt_strerror(ret));
  65. goto fail;
  66. }
  67. free(base);
  68. free(tmpo);
  69. return tmp;
  70. fail:
  71. free(tmpo);
  72. if (tmp)
  73. free(tmp);
  74. return NULL;
  75. }
  76. static int do_fdtoverlay(const char *input_filename,
  77. const char *output_filename,
  78. int argc, char *argv[])
  79. {
  80. char *blob = NULL;
  81. char **ovblob = NULL;
  82. size_t buf_len;
  83. int i, ret = -1;
  84. blob = utilfdt_read(input_filename, &buf_len);
  85. if (!blob) {
  86. fprintf(stderr, "\nFailed to read '%s'\n", input_filename);
  87. goto out_err;
  88. }
  89. if (fdt_totalsize(blob) > buf_len) {
  90. fprintf(stderr,
  91. "\nBase blob is incomplete (%lu / %" PRIu32 " bytes read)\n",
  92. (unsigned long)buf_len, fdt_totalsize(blob));
  93. goto out_err;
  94. }
  95. /* allocate blob pointer array */
  96. ovblob = xmalloc(sizeof(*ovblob) * argc);
  97. memset(ovblob, 0, sizeof(*ovblob) * argc);
  98. /* read and keep track of the overlay blobs */
  99. for (i = 0; i < argc; i++) {
  100. size_t ov_len;
  101. ovblob[i] = utilfdt_read(argv[i], &ov_len);
  102. if (!ovblob[i]) {
  103. fprintf(stderr, "\nFailed to read '%s'\n", argv[i]);
  104. goto out_err;
  105. }
  106. if (fdt_totalsize(ovblob[i]) > ov_len) {
  107. fprintf(stderr,
  108. "\nOverlay '%s' is incomplete (%lu / %" PRIu32 " bytes read)\n",
  109. argv[i], (unsigned long)ov_len,
  110. fdt_totalsize(ovblob[i]));
  111. goto out_err;
  112. }
  113. }
  114. buf_len = fdt_totalsize(blob);
  115. /* apply the overlays in sequence */
  116. for (i = 0; i < argc; i++) {
  117. blob = apply_one(blob, ovblob[i], &buf_len, argv[i]);
  118. if (!blob)
  119. goto out_err;
  120. }
  121. fdt_pack(blob);
  122. ret = utilfdt_write(output_filename, blob);
  123. if (ret)
  124. fprintf(stderr, "\nFailed to write '%s'\n",
  125. output_filename);
  126. out_err:
  127. if (ovblob) {
  128. for (i = 0; i < argc; i++) {
  129. if (ovblob[i])
  130. free(ovblob[i]);
  131. }
  132. free(ovblob);
  133. }
  134. free(blob);
  135. return ret;
  136. }
  137. int main(int argc, char *argv[])
  138. {
  139. int opt, i;
  140. char *input_filename = NULL;
  141. char *output_filename = NULL;
  142. while ((opt = util_getopt_long()) != EOF) {
  143. switch (opt) {
  144. case_USAGE_COMMON_FLAGS
  145. case 'i':
  146. input_filename = optarg;
  147. break;
  148. case 'o':
  149. output_filename = optarg;
  150. break;
  151. case 'v':
  152. verbose = 1;
  153. break;
  154. }
  155. }
  156. if (!input_filename)
  157. usage("missing input file");
  158. if (!output_filename)
  159. usage("missing output file");
  160. argv += optind;
  161. argc -= optind;
  162. if (argc <= 0)
  163. usage("missing overlay file(s)");
  164. if (verbose) {
  165. printf("input = %s\n", input_filename);
  166. printf("output = %s\n", output_filename);
  167. for (i = 0; i < argc; i++)
  168. printf("overlay[%d] = %s\n", i, argv[i]);
  169. }
  170. if (do_fdtoverlay(input_filename, output_filename, argc, argv))
  171. return 1;
  172. return 0;
  173. }