nvmutil.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #include <err.h>
  33. ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags,
  34. size_t size);
  35. void cmd_setmac(const char *strMac);
  36. uint8_t hextonum(char chs);
  37. uint8_t rhex(void);
  38. void cmd_dump(void);
  39. void showmac(int partnum);
  40. void hexdump(int partnum);
  41. void cmd_setchecksum(void);
  42. void cmd_brick(void);
  43. void cmd_swap(void);
  44. void cmd_copy(void);
  45. int validChecksum(int partnum);
  46. uint16_t word(int pos16, int partnum);
  47. void setWord(int pos16, int partnum, uint16_t val);
  48. void byteswap(uint8_t *byte);
  49. void writeGbeFile(int *fd, const char *filename);
  50. #define FILENAME argv[1]
  51. #define COMMAND argv[2]
  52. #define MAC_ADDRESS argv[3]
  53. #define PARTNUM argv[3]
  54. #define SIZE_4KB 0x1000
  55. #define SIZE_8KB 0x2000
  56. uint8_t gbe[SIZE_8KB];
  57. int part, gbeFileModified = 0;
  58. uint8_t nvmPartModified[2];
  59. uint16_t test;
  60. uint8_t little_endian;
  61. int
  62. main(int argc, char *argv[])
  63. {
  64. int fd;
  65. int flags = O_RDWR;
  66. char *strMac = NULL;
  67. char *strRMac = "??:??:??:??:??:??";
  68. void (*cmd)(void) = NULL;
  69. nvmPartModified[0] = 0;
  70. nvmPartModified[1] = 0;
  71. test = 1;
  72. little_endian = ((uint8_t *) &test)[0];
  73. #ifdef HAVE_PLEDGE
  74. if (pledge("stdio wpath", NULL) == -1)
  75. err(1, "pledge");
  76. #endif
  77. if (argc == 3) {
  78. if (strcmp(COMMAND, "dump") == 0) {
  79. #ifdef HAVE_PLEDGE
  80. if (pledge("stdio rpath", NULL) == -1)
  81. err(1, "pledge");
  82. #endif
  83. flags = O_RDONLY;
  84. cmd = &cmd_dump;
  85. } else if (strcmp(COMMAND, "setmac") == 0)
  86. strMac = strRMac;
  87. else if (strcmp(COMMAND, "swap") == 0)
  88. cmd = &cmd_swap;
  89. } else if (argc == 4) {
  90. if (strcmp(COMMAND, "setmac") == 0)
  91. strMac = MAC_ADDRESS;
  92. else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
  93. || PARTNUM[1])
  94. errno = EINVAL;
  95. else if (strcmp(COMMAND, "setchecksum") == 0)
  96. cmd = &cmd_setchecksum;
  97. else if (strcmp(COMMAND, "brick") == 0)
  98. cmd = &cmd_brick;
  99. else if (strcmp(COMMAND, "copy") == 0)
  100. cmd = &cmd_copy;
  101. }
  102. if ((strMac == NULL) && (cmd == NULL))
  103. errno = EINVAL;
  104. else if (readFromFile(&fd, gbe, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
  105. goto nvmutil_exit;
  106. if (errno == 0) {
  107. if (strMac != NULL)
  108. cmd_setmac(strMac);
  109. else if (cmd != NULL)
  110. (*cmd)();
  111. if (gbeFileModified)
  112. writeGbeFile(&fd, FILENAME);
  113. }
  114. nvmutil_exit:
  115. if (!((errno == ECANCELED) && (flags == O_RDONLY)))
  116. if (errno != 0)
  117. fprintf(stderr, "%s\n", strerror(errno));
  118. return errno;
  119. }
  120. ssize_t
  121. readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
  122. {
  123. struct stat st;
  124. if (opendir(path) != NULL) {
  125. errno = EISDIR;
  126. return -1;
  127. } else if (((*fd) = open(path, flags)) == -1) {
  128. return -1;
  129. } else if (size == SIZE_8KB) {
  130. fstat((*fd), &st);
  131. if ((st.st_size != SIZE_8KB) && strcmp(path, "/dev/urandom")) {
  132. fprintf(stderr, "Bad file size\n");
  133. errno = ECANCELED;
  134. return -1;
  135. }
  136. }
  137. if (errno == ENOTDIR)
  138. errno = 0;
  139. return read((*fd), buf, size);
  140. }
  141. void
  142. cmd_setmac(const char *strMac)
  143. {
  144. uint8_t o, val8;
  145. uint16_t val16;
  146. int partnum, byte, nib;
  147. uint16_t mac[3] = {0, 0, 0};
  148. uint64_t total = 0;
  149. if (strnlen(strMac, 20) != 17)
  150. goto invalid_mac_address;
  151. for (o = 0; o < 16; o += 3) {
  152. if (o != 15)
  153. if (strMac[o + 2] != ':')
  154. goto invalid_mac_address;
  155. byte = o / 3;
  156. for (nib = 0; nib < 2; nib++, total += val8) {
  157. if ((val8 = hextonum(strMac[o + nib])) > 15) {
  158. if (errno != 0)
  159. return;
  160. goto invalid_mac_address;
  161. } else if ((byte == 0 && (nib == 1))) {
  162. val8 &= 0xE;
  163. val8 |= 2;
  164. }
  165. val16 = val8;
  166. if ((byte % 2) ^ 1)
  167. byteswap((uint8_t *) &val16);
  168. val16 <<= 4 * (nib ^ 1);
  169. mac[byte >> 1] |= val16;
  170. }
  171. }
  172. test = mac[0];
  173. if (little_endian)
  174. byteswap((uint8_t *) &test);
  175. if (total == 0 || (((uint8_t *) &test)[0] & 1))
  176. goto invalid_mac_address;
  177. if (little_endian)
  178. for (o = 0; o < 3; o++)
  179. byteswap((uint8_t *) &mac[o]);
  180. for (partnum = 0; partnum < 2; partnum++) {
  181. if (!validChecksum(partnum))
  182. continue;
  183. for (o = 0; o < 3; o++)
  184. setWord(o, partnum, mac[o]);
  185. part = partnum;
  186. cmd_setchecksum();
  187. }
  188. return;
  189. invalid_mac_address:
  190. fprintf(stderr, "Bad MAC address\n");
  191. errno = ECANCELED;
  192. }
  193. uint8_t
  194. hextonum(char chs)
  195. {
  196. uint8_t val8, ch;
  197. ch = (uint8_t) chs;
  198. if ((ch >= '0') && (ch <= '9'))
  199. val8 = ch - '0';
  200. else if ((ch >= 'A') && (ch <= 'F'))
  201. val8 = ch - 'A' + 10;
  202. else if ((ch >= 'a') && (ch <= 'f'))
  203. val8 = ch - 'a' + 10;
  204. else if (ch == '?')
  205. val8 = rhex();
  206. else
  207. return 16;
  208. return val8;
  209. }
  210. uint8_t
  211. rhex(void)
  212. {
  213. static int rfd = -1;
  214. static uint8_t *rbuf = NULL;
  215. static size_t rindex = BUFSIZ;
  216. if ((rindex == BUFSIZ)) {
  217. rindex = 0;
  218. if ((rbuf == NULL))
  219. if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL)
  220. err(1, NULL);
  221. if ((rfd != -1)) {
  222. close(rfd);
  223. rfd = -1;
  224. }
  225. if (readFromFile(&rfd, rbuf, "/dev/urandom", O_RDONLY, BUFSIZ)
  226. != BUFSIZ) {
  227. warn("%s", "/dev/urandom");
  228. return 16;
  229. }
  230. if ((errno != 0))
  231. return 16;
  232. }
  233. return rbuf[rindex++] & 0xf;
  234. }
  235. void
  236. cmd_dump(void)
  237. {
  238. int numInvalid, partnum;
  239. numInvalid = 0;
  240. for (partnum = 0; (partnum < 2); partnum++) {
  241. if (!validChecksum(partnum))
  242. ++numInvalid;
  243. printf("MAC (part %d): ", partnum);
  244. showmac(partnum);
  245. hexdump(partnum);
  246. }
  247. if (numInvalid < 2)
  248. errno = 0;
  249. }
  250. void
  251. showmac(int partnum)
  252. {
  253. int c;
  254. uint16_t val16;
  255. uint8_t *byte;
  256. for (c = 0; c < 3; c++) {
  257. val16 = word(c, partnum);
  258. byte = (uint8_t *) &val16;
  259. if (!little_endian)
  260. byteswap(byte);
  261. printf("%02x:%02x", byte[0], byte[1]);
  262. if (c == 2)
  263. printf("\n");
  264. else
  265. printf(":");
  266. }
  267. }
  268. void
  269. hexdump(int partnum)
  270. {
  271. int row, c;
  272. uint16_t val16;
  273. uint8_t *byte;
  274. for (row = 0; row < 8; row++) {
  275. printf("%07x ", row << 4);
  276. for (c = 0; c < 8; c++) {
  277. val16 = word((row << 3) + c, partnum);
  278. byte = (uint8_t *) &val16;
  279. if (!little_endian)
  280. byteswap(byte);
  281. printf("%02x%02x ", byte[1], byte[0]);
  282. }
  283. printf("\n");
  284. }
  285. }
  286. void
  287. cmd_setchecksum(void)
  288. {
  289. int c;
  290. uint16_t val16 = 0;
  291. for (c = 0; c < 0x3F; c++)
  292. val16 += word(c, part);
  293. setWord(0x3F, part, 0xBABA - val16);
  294. }
  295. void
  296. cmd_brick(void)
  297. {
  298. if (validChecksum(part))
  299. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  300. }
  301. void
  302. cmd_swap(void)
  303. {
  304. int part0, part1;
  305. part0 = validChecksum(0);
  306. part1 = validChecksum(1);
  307. if ((part0 | part1)) {
  308. for(part0 = 0; part0 < SIZE_4KB; part0++) {
  309. gbe[part0] ^= gbe[part1 = (part0 | SIZE_4KB)];
  310. gbe[part1] ^= gbe[part0];
  311. gbe[part0] ^= gbe[part1];
  312. }
  313. gbeFileModified = 1;
  314. nvmPartModified[0] = 1;
  315. nvmPartModified[1] = 1;
  316. errno = 0;
  317. }
  318. }
  319. void
  320. cmd_copy(void)
  321. {
  322. int src = (part << 12);
  323. int destPart = (part ^ 1);
  324. int dest = (destPart << 12);
  325. if (validChecksum(part)) {
  326. memcpy((gbe + dest), (gbe + src), SIZE_4KB);
  327. gbeFileModified = 1;
  328. nvmPartModified[destPart] = 1;
  329. }
  330. }
  331. int
  332. validChecksum(int partnum)
  333. {
  334. int w;
  335. uint16_t total = 0;
  336. for(w = 0; w <= 0x3F; w++)
  337. total += word(w, partnum);
  338. if (total == 0xBABA)
  339. return 1;
  340. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  341. errno = ECANCELED;
  342. return 0;
  343. }
  344. uint16_t
  345. word(int pos16, int partnum)
  346. {
  347. uint16_t val16 = ((uint16_t *) gbe)[pos16 + (partnum << 11)];
  348. if (!little_endian)
  349. byteswap((uint8_t *) &val16);
  350. return val16;
  351. }
  352. void
  353. setWord(int pos16, int partnum, uint16_t val)
  354. {
  355. ((uint16_t *) gbe)[pos16 + (partnum << 11)] = val;
  356. if (!little_endian)
  357. byteswap(gbe + (pos16 << 1) + (partnum << 12));
  358. gbeFileModified = 1;
  359. nvmPartModified[partnum] = 1;
  360. }
  361. void
  362. byteswap(uint8_t *byte)
  363. {
  364. byte[0] ^= byte[1];
  365. byte[1] ^= byte[0];
  366. byte[0] ^= byte[1];
  367. }
  368. void
  369. writeGbeFile(int *fd, const char *filename)
  370. {
  371. int partnum;
  372. errno = 0;
  373. if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB)
  374. close((*fd));
  375. if (errno != 0)
  376. return;
  377. for (partnum = 0; partnum < 2; partnum++) {
  378. if (nvmPartModified[partnum])
  379. printf("Part %d modified\n", partnum);
  380. else
  381. fprintf (stderr,
  382. "Part %d NOT modified\n", partnum);
  383. }
  384. printf("File `%s` successfully modified\n", filename);
  385. }