fdtoverlay.c 4.2 KB

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