wrg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * wrg.c
  3. *
  4. * Copyright (C) 2005 Mike Baker
  5. * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  6. * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
  7. * Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
  8. * Copyright (C) 2017 George Hopkins <george-hopkins@null.net>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <byteswap.h>
  25. #include <endian.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stddef.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <sys/mman.h>
  32. #include <sys/stat.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <arpa/inet.h>
  36. #include <sys/ioctl.h>
  37. #include <mtd/mtd-user.h>
  38. #include "mtd.h"
  39. #include "md5.h"
  40. #if !defined(__BYTE_ORDER)
  41. #error "Unknown byte order"
  42. #endif
  43. #if __BYTE_ORDER == __BIG_ENDIAN
  44. #define cpu_to_le32(x) bswap_32(x)
  45. #define le32_to_cpu(x) bswap_32(x)
  46. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  47. #define cpu_to_le32(x) (x)
  48. #define le32_to_cpu(x) (x)
  49. #else
  50. #error "Unsupported endianness"
  51. #endif
  52. #define WRG_MAGIC 0x20040220
  53. struct wrg_header {
  54. char signature[32];
  55. uint32_t magic1;
  56. uint32_t magic2;
  57. uint32_t size;
  58. uint32_t offset;
  59. char devname[32];
  60. char digest[16];
  61. } __attribute__ ((packed));
  62. ssize_t pread(int fd, void *buf, size_t count, off_t offset);
  63. ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
  64. int
  65. wrg_fix_md5(struct wrg_header *shdr, int fd, size_t data_offset, size_t data_size)
  66. {
  67. char *buf;
  68. ssize_t res;
  69. MD5_CTX ctx;
  70. unsigned char digest[16];
  71. int i;
  72. int err = 0;
  73. buf = malloc(data_size);
  74. if (!buf) {
  75. err = -ENOMEM;
  76. goto err_out;
  77. }
  78. res = pread(fd, buf, data_size, data_offset);
  79. if (res != data_size) {
  80. perror("pread");
  81. err = -EIO;
  82. goto err_free;
  83. }
  84. MD5_Init(&ctx);
  85. MD5_Update(&ctx, (char *)&shdr->offset, sizeof(shdr->offset));
  86. MD5_Update(&ctx, (char *)&shdr->devname, sizeof(shdr->devname));
  87. MD5_Update(&ctx, buf, data_size);
  88. MD5_Final(digest, &ctx);
  89. if (!memcmp(digest, shdr->digest, sizeof(digest))) {
  90. if (quiet < 2)
  91. fprintf(stderr, "the header is fixed already\n");
  92. return -1;
  93. }
  94. if (quiet < 2) {
  95. fprintf(stderr, "new size: %u, new MD5: ", data_size);
  96. for (i = 0; i < sizeof(digest); i++)
  97. fprintf(stderr, "%02x", digest[i]);
  98. fprintf(stderr, "\n");
  99. }
  100. /* update the size in the image */
  101. shdr->size = cpu_to_le32(data_size);
  102. /* update the checksum in the image */
  103. memcpy(shdr->digest, digest, sizeof(digest));
  104. err_free:
  105. free(buf);
  106. err_out:
  107. return err;
  108. }
  109. int
  110. mtd_fixwrg(const char *mtd, size_t offset, size_t data_size)
  111. {
  112. int fd;
  113. char *first_block;
  114. ssize_t res;
  115. size_t block_offset;
  116. size_t data_offset;
  117. struct wrg_header *shdr;
  118. if (quiet < 2)
  119. fprintf(stderr, "Trying to fix WRG header in %s at 0x%x...\n",
  120. mtd, offset);
  121. block_offset = offset & ~(erasesize - 1);
  122. offset -= block_offset;
  123. fd = mtd_check_open(mtd);
  124. if(fd < 0) {
  125. fprintf(stderr, "Could not open mtd device: %s\n", mtd);
  126. exit(1);
  127. }
  128. if (block_offset + erasesize > mtdsize) {
  129. fprintf(stderr, "Offset too large, device size 0x%x\n",
  130. mtdsize);
  131. exit(1);
  132. }
  133. first_block = malloc(erasesize);
  134. if (!first_block) {
  135. perror("malloc");
  136. exit(1);
  137. }
  138. res = pread(fd, first_block, erasesize, block_offset);
  139. if (res != erasesize) {
  140. perror("pread");
  141. exit(1);
  142. }
  143. shdr = (struct wrg_header *)(first_block + offset);
  144. if (le32_to_cpu(shdr->magic1) != WRG_MAGIC) {
  145. fprintf(stderr, "No WRG header found (%08x != %08x)\n",
  146. le32_to_cpu(shdr->magic1), WRG_MAGIC);
  147. exit(1);
  148. } else if (!le32_to_cpu(shdr->size)) {
  149. fprintf(stderr, "WRG entity with empty image\n");
  150. exit(1);
  151. }
  152. data_offset = offset + sizeof(struct wrg_header);
  153. if (!data_size)
  154. data_size = mtdsize - data_offset;
  155. if (data_size > le32_to_cpu(shdr->size))
  156. data_size = le32_to_cpu(shdr->size);
  157. if (wrg_fix_md5(shdr, fd, data_offset, data_size))
  158. goto out;
  159. if (mtd_erase_block(fd, block_offset)) {
  160. fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
  161. block_offset, strerror(errno));
  162. exit(1);
  163. }
  164. if (quiet < 2)
  165. fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
  166. if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
  167. fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
  168. exit(1);
  169. }
  170. if (quiet < 2)
  171. fprintf(stderr, "Done.\n");
  172. out:
  173. close (fd);
  174. sync();
  175. return 0;
  176. }