mkdtbimg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* tools/mkbootimg/mkdtbimg.c
  2. **
  3. ** Copyright 2016-2017, The LineageOS Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. **
  17. ** December 2016, Christopher N. Hesse
  18. ** Separate dt creation from mkbootimg program.
  19. **
  20. ** January 2017, Christopher N. Hesse
  21. ** Adjust to dtbToolCM call syntax.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <limits.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <arpa/inet.h>
  33. #include <assert.h>
  34. #include <dirent.h>
  35. #include <err.h>
  36. #include <stdint.h>
  37. #include <dtbimg.h>
  38. int usage(void)
  39. {
  40. fprintf(stderr,"usage: mkdtimg\n"
  41. " --dt_dir <dtb path>\n"
  42. " -o|--output <filename>\n"
  43. );
  44. return 1;
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. char *dtimg = 0;
  49. char *dt_dir = 0;
  50. void *dt_data = 0;
  51. unsigned pagesize = 0;
  52. unsigned default_pagesize = 2048;
  53. uint32_t dt_size;
  54. int fd;
  55. struct stat sbuf;
  56. argc--;
  57. argv++;
  58. while(argc > 0){
  59. char *arg = argv[0];
  60. char *val = argv[1];
  61. if(argc < 1) {
  62. return usage();
  63. }
  64. argc -= 2;
  65. argv += 2;
  66. if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
  67. dtimg = val;
  68. } else if (!strcmp(arg, "--dt_dir")) {
  69. dt_dir = val;
  70. } else if (!strcmp(arg, "-p")) {
  71. // Ignore this parameter
  72. } else if (!strcmp(arg, "-s")) {
  73. pagesize = strtol(val, NULL, 10);
  74. if (pagesize == 0)
  75. pagesize = default_pagesize;
  76. } else {
  77. // Check if this is the dtb path
  78. int err = stat(arg, &sbuf);
  79. if (err != 0 || !S_ISDIR(sbuf.st_mode))
  80. return usage();
  81. dt_dir = arg;
  82. }
  83. }
  84. if(dtimg == 0) {
  85. fprintf(stderr,"error: no output filename specified\n");
  86. return usage();
  87. }
  88. if(dt_dir == 0) {
  89. fprintf(stderr,"error: no dtb path specified\n");
  90. return usage();
  91. }
  92. dt_data = load_dtbh_block(dt_dir, pagesize, &dt_size);
  93. if (dt_data == 0) {
  94. fprintf(stderr, "error: could not load device tree blobs '%s'\n", dt_dir);
  95. return 1;
  96. }
  97. fd = open(dtimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
  98. if(fd < 0) {
  99. fprintf(stderr,"error: could not create '%s'\n", dtimg);
  100. return 1;
  101. }
  102. if(write(fd, dt_data, dt_size) != dt_size) goto fail;
  103. return 0;
  104. fail:
  105. unlink(dtimg);
  106. close(fd);
  107. fprintf(stderr,"error: failed writing '%s': %s\n", dtimg,
  108. strerror(errno));
  109. return 1;
  110. }