nvmutil.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2022, 2023 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. void readGbeFile(int *fd, const char *path, int flags,
  34. size_t nr);
  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 val16);
  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 buf[SIZE_8KB];
  57. size_t gbe[2];
  58. uint8_t skipread[2] = {0, 0};
  59. int part, gbeWriteAttempted = 0, gbeFileModified = 0;
  60. uint8_t nvmPartModified[2] = {0, 0};
  61. uint16_t test;
  62. uint8_t little_endian;
  63. int
  64. main(int argc, char *argv[])
  65. {
  66. size_t nr;
  67. int fd, flags = O_RDWR;
  68. void (*cmd)(void) = NULL;
  69. const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
  70. #ifdef HAVE_PLEDGE
  71. if (pledge("stdio wpath", NULL) == -1)
  72. err(errno, "pledge");
  73. #endif
  74. gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
  75. test = 1;
  76. little_endian = ((uint8_t *) &test)[0];
  77. if (argc == 3) {
  78. if (strcmp(COMMAND, "dump") == 0) {
  79. #ifdef HAVE_PLEDGE
  80. if (pledge("stdio rpath", NULL) == -1)
  81. err(errno, "pledge");
  82. #endif
  83. flags = O_RDONLY;
  84. cmd = &cmd_dump;
  85. } else if (strcmp(COMMAND, "setmac") == 0)
  86. strMac = (char *) 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. if (errno != 0)
  105. goto nvmutil_exit;
  106. if ((cmd == &cmd_copy) || (cmd == &cmd_swap))
  107. nr = SIZE_4KB;
  108. else
  109. nr = 128;
  110. if ((cmd == &cmd_copy) || (cmd == &cmd_setchecksum) ||
  111. (cmd == &cmd_brick))
  112. skipread[part ^ 1] = 1;
  113. readGbeFile(&fd, FILENAME, flags, nr);
  114. if (strMac != NULL)
  115. cmd_setmac(strMac);
  116. else if (cmd != NULL)
  117. (*cmd)();
  118. if (gbeFileModified) {
  119. writeGbeFile(&fd, FILENAME);
  120. } else if ((cmd != &cmd_dump)) {
  121. printf("File `%s` not modified.\n", FILENAME);
  122. if (gbeWriteAttempted)
  123. errno = 0;
  124. }
  125. nvmutil_exit:
  126. if ((errno != 0) && (cmd != &cmd_dump))
  127. err(errno, NULL);
  128. else
  129. return errno;
  130. }
  131. void
  132. readGbeFile(int *fd, const char *path, int flags, size_t nr)
  133. {
  134. struct stat st;
  135. int p, r, tr;
  136. if (opendir(path) != NULL)
  137. err(errno = EISDIR, "%s", path);
  138. if (((*fd) = open(path, flags)) == -1)
  139. err(errno, "%s", path);
  140. if (fstat((*fd), &st) == -1)
  141. err(errno, "%s", path);
  142. if ((st.st_size != SIZE_8KB)) {
  143. fprintf(stderr, "%s: Bad file size (must be 8KiB)\n", path);
  144. err(errno = ECANCELED, NULL);
  145. }
  146. if (errno == ENOTDIR)
  147. errno = 0;
  148. if (errno != 0)
  149. err(errno, "%s", path);
  150. for (tr = 0, p = 0; p < 2; p++) {
  151. if (skipread[p])
  152. continue;
  153. if ((r = pread((*fd), (uint8_t *) gbe[p], nr, p << 12)) == -1)
  154. err(errno, "%s", path);
  155. tr += r;
  156. }
  157. printf("%d bytes read from file: `%s`\n", tr, path);
  158. }
  159. void
  160. cmd_setmac(const char *strMac)
  161. {
  162. int partnum, byte, nib;
  163. uint8_t o, val8;
  164. uint16_t val16, mac[3] = {0, 0, 0};
  165. uint64_t total;
  166. if (strnlen(strMac, 20) != 17)
  167. goto invalid_mac_address;
  168. for (o = 0; o < 16; o += 3) {
  169. if (o != 15)
  170. if (strMac[o + 2] != ':')
  171. goto invalid_mac_address;
  172. byte = o / 3;
  173. for (total = 0, nib = 0; nib < 2; nib++, total += val8) {
  174. if ((val8 = hextonum(strMac[o + nib])) > 15)
  175. goto invalid_mac_address;
  176. if ((byte == 0) && (nib == 1)) {
  177. if (strMac[o + nib] == '?')
  178. val8 = (val8 & 0xE) | 2;
  179. }
  180. val16 = val8;
  181. if ((byte % 2) ^ 1)
  182. byteswap((uint8_t *) &val16);
  183. val16 <<= 4 * (nib ^ 1);
  184. mac[byte >> 1] |= val16;
  185. }
  186. }
  187. test = mac[0];
  188. if (little_endian)
  189. byteswap((uint8_t *) &test);
  190. if (total == 0 || (((uint8_t *) &test)[0] & 1))
  191. goto invalid_mac_address;
  192. if (little_endian)
  193. for (o = 0; o < 3; o++)
  194. byteswap((uint8_t *) &mac[o]);
  195. for (partnum = 0; partnum < 2; partnum++) {
  196. if (!validChecksum(partnum))
  197. continue;
  198. for (o = 0; o < 3; o++)
  199. setWord(o, partnum, mac[o]);
  200. part = partnum;
  201. cmd_setchecksum();
  202. }
  203. return;
  204. invalid_mac_address:
  205. fprintf(stderr, "Bad MAC address\n");
  206. errno = ECANCELED;
  207. }
  208. uint8_t
  209. hextonum(char chs)
  210. {
  211. uint8_t val8, ch;
  212. ch = (uint8_t) chs;
  213. if ((ch >= '0') && (ch <= '9'))
  214. val8 = ch - '0';
  215. else if ((ch >= 'A') && (ch <= 'F'))
  216. val8 = ch - 'A' + 10;
  217. else if ((ch >= 'a') && (ch <= 'f'))
  218. val8 = ch - 'a' + 10;
  219. else if (ch == '?')
  220. val8 = rhex();
  221. else
  222. return 16;
  223. return val8;
  224. }
  225. uint8_t
  226. rhex(void)
  227. {
  228. static int rfd = -1;
  229. static uint64_t rnum = 0;
  230. static size_t rindex = 8;
  231. if (rindex == 8) {
  232. rindex = 0;
  233. if (rfd == -1)
  234. if ((rfd = open("/dev/urandom", O_RDONLY)) == -1)
  235. err(errno, "/dev/urandom");
  236. if (read(rfd, (uint8_t *) &rnum, 8) == -1)
  237. err(errno, "/dev/urandom");
  238. }
  239. return ((uint8_t *) &rnum)[rindex++] & 0xf;
  240. }
  241. void
  242. cmd_dump(void)
  243. {
  244. int numInvalid, partnum;
  245. numInvalid = 0;
  246. for (partnum = 0; (partnum < 2); partnum++) {
  247. if (!validChecksum(partnum))
  248. ++numInvalid;
  249. printf("MAC (part %d): ", partnum);
  250. showmac(partnum);
  251. hexdump(partnum);
  252. }
  253. if (numInvalid < 2)
  254. errno = 0;
  255. }
  256. void
  257. showmac(int partnum)
  258. {
  259. int c;
  260. uint16_t val16;
  261. uint8_t *byte;
  262. for (c = 0; c < 3; c++) {
  263. val16 = word(c, partnum);
  264. byte = (uint8_t *) &val16;
  265. if (!little_endian)
  266. byteswap(byte);
  267. printf("%02x:%02x", byte[0], byte[1]);
  268. if (c == 2)
  269. printf("\n");
  270. else
  271. printf(":");
  272. }
  273. }
  274. void
  275. hexdump(int partnum)
  276. {
  277. int row, c;
  278. uint16_t val16;
  279. uint8_t *byte;
  280. for (row = 0; row < 8; row++) {
  281. printf("%07x ", row << 4);
  282. for (c = 0; c < 8; c++) {
  283. val16 = word((row << 3) + c, partnum);
  284. byte = (uint8_t *) &val16;
  285. if (!little_endian)
  286. byteswap(byte);
  287. printf("%02x%02x ", byte[1], byte[0]);
  288. }
  289. printf("\n");
  290. }
  291. }
  292. void
  293. cmd_setchecksum(void)
  294. {
  295. int c;
  296. uint16_t val16;
  297. for (val16 = 0, c = 0; c < 0x3F; c++)
  298. val16 += word(c, part);
  299. setWord(0x3F, part, 0xBABA - val16);
  300. }
  301. void
  302. cmd_brick(void)
  303. {
  304. if (validChecksum(part))
  305. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  306. }
  307. void
  308. cmd_swap(void)
  309. {
  310. if (validChecksum(1) || validChecksum(0)) {
  311. gbe[0] ^= gbe[1];
  312. gbe[1] ^= gbe[0];
  313. gbe[0] ^= gbe[1];
  314. gbeFileModified = 1;
  315. nvmPartModified[0] = 1;
  316. nvmPartModified[1] = 1;
  317. errno = 0;
  318. }
  319. }
  320. void
  321. cmd_copy(void)
  322. {
  323. if (validChecksum(part)) {
  324. gbe[part ^ 1] = gbe[part];
  325. gbeFileModified = 1;
  326. nvmPartModified[part ^ 1] = 1;
  327. }
  328. }
  329. int
  330. validChecksum(int partnum)
  331. {
  332. int w;
  333. uint16_t total;
  334. for(total = 0, w = 0; w <= 0x3F; w++)
  335. total += word(w, partnum);
  336. if (total == 0xBABA)
  337. return 1;
  338. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  339. errno = ECANCELED;
  340. return 0;
  341. }
  342. uint16_t
  343. word(int pos16, int partnum)
  344. {
  345. uint8_t *nbuf;
  346. uint16_t pos8, val16;
  347. nbuf = (uint8_t *) gbe[partnum];
  348. pos8 = pos16 << 1;
  349. val16 = nbuf[pos8 + 1];
  350. val16 <<= 8;
  351. val16 |= nbuf[pos8];
  352. return val16;
  353. }
  354. void
  355. setWord(int pos16, int partnum, uint16_t val16)
  356. {
  357. uint8_t val8[2], *nbuf;
  358. uint16_t pos8;
  359. gbeWriteAttempted = 1;
  360. if (word(pos16, partnum) == val16)
  361. return;
  362. nbuf = (uint8_t *) gbe[partnum];
  363. val8[0] = (uint8_t) (val16 & 0xff);
  364. val8[1] = (uint8_t) (val16 >> 8);
  365. pos8 = pos16 << 1;
  366. nbuf[pos8] = val8[0];
  367. nbuf[pos8 + 1] = val8[1];
  368. gbeFileModified = 1;
  369. nvmPartModified[partnum] = 1;
  370. }
  371. void
  372. byteswap(uint8_t *byte)
  373. {
  374. byte[0] ^= byte[1];
  375. byte[1] ^= byte[0];
  376. byte[0] ^= byte[1];
  377. }
  378. void
  379. writeGbeFile(int *fd, const char *filename)
  380. {
  381. int p, nw, tw;
  382. errno = 0;
  383. /* if copy/swap not performed, write only the nvm part */
  384. if ((gbe[0] != gbe[1]) && (gbe[0] < gbe[1]))
  385. nw = 128;
  386. else
  387. nw = SIZE_4KB;
  388. for (tw = 0, p = 0; p < 2; p++) {
  389. if (gbe[0] > gbe[1])
  390. p ^= 1;
  391. if (nvmPartModified[p]) {
  392. printf("Part %d modified\n", p);
  393. } else {
  394. fprintf (stderr,
  395. "Part %d NOT modified\n", p);
  396. goto next_part;
  397. }
  398. if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) != nw)
  399. err(errno, "%s", filename);
  400. tw += nw;
  401. next_part:
  402. if (gbe[0] > gbe[1])
  403. p ^= 1;
  404. }
  405. if (close((*fd)))
  406. err(errno, "%s", filename);
  407. printf("%d bytes written to file: `%s`\n", tw, filename);
  408. }