patch-dtb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * patch-dtb.c - patch a dtb into an image
  3. *
  4. * Copyright (C) 2006 Felix Fietkau <[email protected]>
  5. * Copyright (C) 2012 John Crispin <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. * based on patch-cmdline.c
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stddef.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <sys/mman.h>
  29. #include <sys/stat.h>
  30. #include <string.h>
  31. #define DTB_MAX (16 * 1024)
  32. int main(int argc, char **argv)
  33. {
  34. int fd, fddtb, found = 0, len, ret = -1;
  35. char *ptr, *ptrdtb, *p;
  36. struct stat s;
  37. unsigned int search_space , dtb_max_size;
  38. if (argc <= 2 || argc > 4) {
  39. fprintf(stderr, "Usage: %s <file> <dtb> [size]\n", argv[0]);
  40. goto err1;
  41. } else if (argc == 3) {
  42. fprintf(stdout, "DT size used is default of 16KB\n");
  43. search_space = dtb_max_size = DTB_MAX;
  44. } else {
  45. search_space = dtb_max_size = atoi(argv[3]);
  46. }
  47. fddtb = open(argv[1], O_RDONLY);
  48. if (!fddtb)
  49. goto err1;
  50. if (stat(argv[2], &s)) {
  51. fprintf(stderr, "DTB not found\n");
  52. goto err1;
  53. }
  54. len = s.st_size;
  55. if (len + 8 > dtb_max_size) {
  56. fprintf(stderr, "DTB too big\n");
  57. goto err1;
  58. }
  59. if (((fddtb = open(argv[2], O_RDONLY)) < 0) ||
  60. (ptrdtb = (char *) mmap(0, dtb_max_size, PROT_READ, MAP_SHARED, fddtb, 0)) == (void *) (-1)) {
  61. fprintf(stderr, "Could not open DTB");
  62. goto err2;
  63. }
  64. if (((fd = open(argv[1], O_RDWR)) < 0) ||
  65. (ptr = (char *) mmap(0, search_space + dtb_max_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1)) {
  66. fprintf(stderr, "Could not open kernel image");
  67. goto err3;
  68. }
  69. for (p = ptr; p < (ptr + search_space); p += 4) {
  70. if (memcmp(p, "OWRTDTB:", 8) == 0) {
  71. found = 1;
  72. p += 8;
  73. break;
  74. }
  75. }
  76. if (!found) {
  77. fprintf(stderr, "DTB marker not found!\n");
  78. goto err4;
  79. }
  80. memset(p, 0, dtb_max_size - 8);
  81. memcpy(p, ptrdtb, len);
  82. msync(p, len, MS_SYNC|MS_INVALIDATE);
  83. ret = 0;
  84. err4:
  85. munmap((void *) ptr, len);
  86. err3:
  87. if (fd > 0)
  88. close(fd);
  89. munmap((void *) ptrdtb, len);
  90. err2:
  91. if (fddtb > 0)
  92. close(fddtb);
  93. err1:
  94. return ret;
  95. }