nvmutil.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  98. }
  99. nvmutil_exit:
  100. if (errno == ENOTDIR)
  101. errno = 0;
  102. if (!((errno == ECANCELED) && (flags == O_RDONLY)))
  103. if (errno != 0)
  104. fprintf(stderr, "%s\n", strerror(errno));
  105. return errno;
  106. }
  107. ssize_t
  108. readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
  109. {
  110. struct stat st;
  111. if (opendir(path) != NULL) {
  112. errno = EISDIR;
  113. return -1;
  114. } else if (((*fd) = open(path, flags)) == -1) {
  115. return -1;
  116. } else if (size == SIZE_8KB) {
  117. fstat((*fd), &st);
  118. if (st.st_size != SIZE_8KB) {
  119. fprintf(stderr, "Bad file size\n");
  120. errno = ECANCELED;
  121. return -1;
  122. }
  123. }
  124. return read((*fd), buf, size);
  125. }
  126. void
  127. setmac(const char *strMac)
  128. {
  129. uint8_t rmac[12];
  130. uint8_t o, ch, val8;
  131. uint16_t val16;
  132. int macfd, partnum, random, byte, nib;
  133. uint16_t mac[3] = {0, 0, 0};
  134. uint64_t total = 0;
  135. if (readFromFile(&macfd, rmac, "/dev/urandom", O_RDONLY, 12) != 12)
  136. return;
  137. else if (strnlen(strMac, 20) != 17)
  138. goto invalid_mac_address;
  139. for (o = 0, random = 0; o < 16; o += 3) {
  140. if (o != 15)
  141. if (strMac[o + 2] != ':')
  142. goto invalid_mac_address;
  143. byte = o / 3;
  144. for (nib = 0; nib < 2; nib++, total += val8) {
  145. ch = strMac[o + nib];
  146. if ((ch >= '0') && ch <= '9') {
  147. val8 = ch - '0';
  148. } else if ((ch >= 'A') && (ch <= 'F')) {
  149. val8 = ch - 'A' + 10;
  150. } else if ((ch >= 'a') && (ch <= 'f')) {
  151. val8 = ch - 'a' + 10;
  152. } else if (ch == '?') {
  153. val8 = rmac[random++] & 0xf;
  154. if ((byte == 0 && (nib == 1))) {
  155. val8 &= 0xE;
  156. val8 |= 2;
  157. }
  158. } else {
  159. goto invalid_mac_address;
  160. }
  161. val16 = val8;
  162. if ((byte % 2) ^ 1)
  163. byteswap((uint8_t *) &val16);
  164. val16 <<= 4 * (nib ^ 1);
  165. mac[byte >> 1] |= val16;
  166. }
  167. }
  168. test = mac[0];
  169. if (little_endian)
  170. byteswap((uint8_t *) &test);
  171. if (total == 0 || (((uint8_t *) &test)[0] & 1))
  172. goto invalid_mac_address;
  173. if (little_endian)
  174. for (o = 0; o < 3; o++)
  175. byteswap((uint8_t *) &mac[o]);
  176. for (partnum = 0; partnum < 2; partnum++) {
  177. if (validChecksum(partnum)) {
  178. for (o = 0; o < 3; o++)
  179. setWord(o, partnum, mac[o]);
  180. part = partnum;
  181. cmd("setchecksum");
  182. }
  183. }
  184. return;
  185. invalid_mac_address:
  186. fprintf(stderr, "Bad MAC address\n");
  187. errno = ECANCELED;
  188. return;
  189. }
  190. void
  191. cmd(const char *command)
  192. {
  193. int c, partnum, part0, part1, row, numInvalid;
  194. uint8_t *byte;
  195. uint16_t val16;
  196. if (strcmp(command, "dump") == 0) {
  197. numInvalid = 0;
  198. for (partnum = 0; partnum < 2; partnum++) {
  199. if (!validChecksum(partnum))
  200. ++numInvalid;
  201. printf("Part %d:\n", partnum);
  202. printf("MAC: ");
  203. for (c = 0; c < 3; c++) {
  204. val16 = word(c, partnum);
  205. byte = (uint8_t *) &val16;
  206. if (!little_endian)
  207. byteswap(byte);
  208. printf("%02x:%02x", byte[0], byte[1]);
  209. if (c == 2)
  210. printf("\n");
  211. else
  212. printf(":");
  213. }
  214. for (row = 0; row < 8; row++) {
  215. printf("%07x ", row << 4);
  216. for (c = 0; c < 8; c++) {
  217. val16 = word((row << 3) + c, partnum);
  218. byte = (uint8_t *) &val16;
  219. if (!little_endian)
  220. byteswap(byte);
  221. printf("%02x%02x ", byte[1], byte[0]);
  222. }
  223. printf("\n");
  224. }
  225. }
  226. if (numInvalid < 2) {
  227. errno = 0;
  228. }
  229. } else if (strcmp(command, "setchecksum") == 0) {
  230. val16 = 0;
  231. for (c = 0; c < 0x3F; c++)
  232. val16 += word(c, part);
  233. setWord(0x3F, part, 0xBABA - val16);
  234. } else if (strcmp(command, "brick") == 0) {
  235. if (validChecksum(part))
  236. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  237. } else if (strcmp(command, "swap") == 0) {
  238. part0 = validChecksum(0);
  239. part1 = validChecksum(1);
  240. if ((modified = (part0 | part1))) {
  241. for(part0 = 0; part0 < SIZE_4KB; part0++) {
  242. gbe[part0] ^= gbe[part1 = (part0 | SIZE_4KB)];
  243. gbe[part1] ^= gbe[part0];
  244. gbe[part0] ^= gbe[part1];
  245. }
  246. }
  247. } else if (strcmp(command, "copy") == 0) {
  248. if (validChecksum(part))
  249. memcpy(gbe + ((part ^ (modified = 1)) << 12),
  250. gbe + (part << 12), SIZE_4KB);
  251. } else
  252. errno = EINVAL;
  253. }
  254. int
  255. validChecksum(int partnum)
  256. {
  257. int w;
  258. uint16_t total = 0;
  259. for(w = 0; w <= 0x3F; w++)
  260. total += word(w, partnum);
  261. if (total != 0xBABA) {
  262. fprintf(stderr, "BAD checksum in part %d\n", partnum);
  263. errno = ECANCELED;
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. uint16_t
  269. word(int pos16, int partnum)
  270. {
  271. uint16_t val16 = ((uint16_t *) gbe)[pos16 + (partnum << 11)];
  272. if (!little_endian)
  273. byteswap((uint8_t *) &val16);
  274. return val16;
  275. }
  276. void
  277. setWord(int pos16, int partnum, uint16_t val)
  278. {
  279. ((uint16_t *) gbe)[pos16 + (partnum << 11)] = val;
  280. if (!little_endian)
  281. byteswap(gbe + (pos16 << 1) + (partnum << 12));
  282. modified = 1;
  283. }
  284. void
  285. byteswap(uint8_t *byte) {
  286. byte[0] ^= byte[1];
  287. byte[1] ^= byte[0];
  288. byte[0] ^= byte[1];
  289. }