nvmutil.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2022 Leah Rowe <info@minifree.org>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <dirent.h>
  32. #ifdef HAVE_PLEDGE
  33. #include <err.h>
  34. #endif
  35. ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags,
  36. size_t size);
  37. void setmac(const char *strMac);
  38. void cmd(const char *command);
  39. int validChecksum(int partnum);
  40. uint16_t word(int pos16, int partnum);
  41. void setWord(int pos16, int partnum, uint16_t val);
  42. void byteswap(uint8_t *byte);
  43. #define FILENAME argv[1]
  44. #define COMMAND argv[2]
  45. #define MAC_ADDRESS argv[3]
  46. #define PARTNUM argv[3]
  47. #define SIZE_4KB 0x1000
  48. #define SIZE_8KB 0x2000
  49. uint8_t gbe[SIZE_8KB];
  50. int part, modified = 0;
  51. uint16_t test;
  52. uint8_t little_endian;
  53. int
  54. main(int argc, char *argv[])
  55. {
  56. int fd;
  57. int flags = O_RDWR;
  58. char *strMac = NULL;
  59. char *strRMac = "??:??:??:??:??:??";
  60. test = 1;
  61. little_endian = ((uint8_t *) &test)[0];
  62. #ifdef HAVE_PLEDGE
  63. if (pledge("stdio wpath", NULL) == -1)
  64. err(1, "pledge");
  65. #endif
  66. if (argc == 3) {
  67. if (strcmp(COMMAND, "dump") == 0) {
  68. #ifdef HAVE_PLEDGE
  69. if (pledge("stdio rpath", NULL) == -1)
  70. err(1, "pledge");
  71. #endif
  72. flags = O_RDONLY;
  73. } else if (strcmp(COMMAND, "setmac") == 0) {
  74. strMac = strRMac;
  75. }
  76. } else if (argc == 4) {
  77. if (strcmp(COMMAND, "setmac") == 0)
  78. strMac = MAC_ADDRESS;
  79. else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
  80. || PARTNUM[1])
  81. errno = EINVAL;
  82. } else
  83. errno = EINVAL;
  84. if (errno != 0)
  85. goto nvmutil_exit;
  86. if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB)
  87. == SIZE_8KB)
  88. {
  89. if (strMac != NULL)
  90. setmac(strMac);
  91. else
  92. cmd(COMMAND);
  93. if (modified) {
  94. errno = 0;
  95. if (pwrite(fd, gbe, SIZE_8KB, 0) == SIZE_8KB)
  96. close(fd);
  97. if (errno == 0)
  98. printf("%s successfully modified\n", FILENAME);
  99. }
  100. }
  101. nvmutil_exit:
  102. if (errno == ENOTDIR)
  103. errno = 0;
  104. if (!((errno == ECANCELED) && (flags == O_RDONLY)))
  105. if (errno != 0)
  106. fprintf(stderr, "%s\n", strerror(errno));
  107. return errno;
  108. }
  109. ssize_t
  110. readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
  111. {
  112. struct stat st;
  113. if (opendir(path) != NULL) {
  114. errno = EISDIR;
  115. return -1;
  116. } else if (((*fd) = open(path, flags)) == -1) {
  117. return -1;
  118. } else if (size == SIZE_8KB) {
  119. fstat((*fd), &st);
  120. if (st.st_size != SIZE_8KB) {
  121. fprintf(stderr, "Bad file size\n");
  122. errno = ECANCELED;
  123. return -1;
  124. }
  125. }
  126. return read((*fd), buf, size);
  127. }
  128. void
  129. setmac(const char *strMac)
  130. {
  131. uint8_t rmac[12];
  132. uint8_t o, ch, val8;
  133. uint16_t val16;
  134. int macfd, partnum, random, byte, nib;
  135. uint16_t mac[3] = {0, 0, 0};
  136. uint64_t total = 0;
  137. if (readFromFile(&macfd, rmac, "/dev/urandom", O_RDONLY, 12) != 12)
  138. return;
  139. else if (strnlen(strMac, 20) != 17)
  140. goto invalid_mac_address;
  141. for (o = 0, random = 0; o < 16; o += 3) {
  142. if (o != 15)
  143. if (strMac[o + 2] != ':')
  144. goto invalid_mac_address;
  145. byte = o / 3;
  146. for (nib = 0; nib < 2; nib++, total += val8) {
  147. ch = strMac[o + nib];
  148. if ((ch >= '0') && ch <= '9') {
  149. val8 = ch - '0';
  150. } else if ((ch >= 'A') && (ch <= 'F')) {
  151. val8 = ch - 'A' + 10;
  152. } else if ((ch >= 'a') && (ch <= 'f')) {
  153. val8 = ch - 'a' + 10;
  154. } else if (ch == '?') {
  155. val8 = rmac[random++] & 0xf;
  156. if ((byte == 0 && (nib == 1))) {
  157. val8 &= 0xE;
  158. val8 |= 2;
  159. }
  160. } else {
  161. goto invalid_mac_address;
  162. }
  163. val16 = val8;
  164. if ((byte % 2) ^ 1)
  165. byteswap((uint8_t *) &val16);
  166. val16 <<= 4 * (nib ^ 1);
  167. mac[byte >> 1] |= val16;
  168. }
  169. }
  170. test = mac[0];
  171. if (little_endian)
  172. byteswap((uint8_t *) &test);
  173. if (total == 0 || (((uint8_t *) &test)[0] & 1))
  174. goto invalid_mac_address;
  175. if (little_endian)
  176. for (o = 0; o < 3; o++)
  177. byteswap((uint8_t *) &mac[o]);
  178. for (partnum = 0; partnum < 2; partnum++) {
  179. if (validChecksum(partnum)) {
  180. for (o = 0; o < 3; o++)
  181. setWord(o, partnum, mac[o]);
  182. part = partnum;
  183. cmd("setchecksum");
  184. }
  185. }
  186. return;
  187. invalid_mac_address:
  188. fprintf(stderr, "Bad MAC address\n");
  189. errno = ECANCELED;
  190. return;
  191. }
  192. void
  193. cmd(const char *command)
  194. {
  195. int c, partnum, part0, part1, row, numInvalid;
  196. uint8_t *byte;
  197. uint16_t val16;
  198. if (strcmp(command, "dump") == 0) {
  199. numInvalid = 0;
  200. for (partnum = 0; partnum < 2; partnum++) {
  201. if (!validChecksum(partnum))
  202. ++numInvalid;
  203. printf("Part %d:\n", partnum);
  204. printf("MAC: ");
  205. for (c = 0; c < 3; c++) {
  206. val16 = word(c, partnum);
  207. byte = (uint8_t *) &val16;
  208. if (!little_endian)
  209. byteswap(byte);
  210. printf("%02x:%02x", byte[0], byte[1]);
  211. if (c == 2)
  212. printf("\n");
  213. else
  214. printf(":");
  215. }
  216. for (row = 0; row < 8; row++) {
  217. printf("%07x ", row << 4);
  218. for (c = 0; c < 8; c++) {
  219. val16 = word((row << 3) + c, partnum);
  220. byte = (uint8_t *) &val16;
  221. if (!little_endian)
  222. byteswap(byte);
  223. printf("%02x%02x ", byte[1], byte[0]);
  224. }
  225. printf("\n");
  226. }
  227. }
  228. if (numInvalid < 2) {
  229. errno = 0;
  230. }
  231. } else if (strcmp(command, "setchecksum") == 0) {
  232. val16 = 0;
  233. for (c = 0; c < 0x3F; c++)
  234. val16 += word(c, part);
  235. setWord(0x3F, part, 0xBABA - val16);
  236. } else if (strcmp(command, "brick") == 0) {
  237. if (validChecksum(part))
  238. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  239. } else if (strcmp(command, "swap") == 0) {
  240. part0 = validChecksum(0);
  241. part1 = validChecksum(1);
  242. if ((modified = (part0 | part1))) {
  243. for(part0 = 0; part0 < SIZE_4KB; part0++) {
  244. gbe[part0] ^= gbe[part1 = (part0 | SIZE_4KB)];
  245. gbe[part1] ^= gbe[part0];
  246. gbe[part0] ^= gbe[part1];
  247. }
  248. }
  249. } else if (strcmp(command, "copy") == 0) {
  250. if (validChecksum(part))
  251. memcpy(gbe + ((part ^ (modified = 1)) << 12),
  252. gbe + (part << 12), SIZE_4KB);
  253. } else
  254. errno = EINVAL;
  255. }
  256. int
  257. validChecksum(int partnum)
  258. {
  259. int w;
  260. uint16_t total = 0;
  261. for(w = 0; w <= 0x3F; w++)
  262. total += word(w, partnum);
  263. if (total != 0xBABA) {
  264. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  265. errno = ECANCELED;
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. uint16_t
  271. word(int pos16, int partnum)
  272. {
  273. uint16_t val16 = ((uint16_t *) gbe)[pos16 + (partnum << 11)];
  274. if (!little_endian)
  275. byteswap((uint8_t *) &val16);
  276. return val16;
  277. }
  278. void
  279. setWord(int pos16, int partnum, uint16_t val)
  280. {
  281. ((uint16_t *) gbe)[pos16 + (partnum << 11)] = val;
  282. if (!little_endian)
  283. byteswap(gbe + (pos16 << 1) + (partnum << 12));
  284. modified = 1;
  285. }
  286. void
  287. byteswap(uint8_t *byte) {
  288. byte[0] ^= byte[1];
  289. byte[1] ^= byte[0];
  290. byte[0] ^= byte[1];
  291. }