nvmutil.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. uint8_t rval;
  231. if (rnum == 0) {
  232. if (rfd == -1)
  233. if ((rfd = open("/dev/urandom", O_RDONLY)) == -1)
  234. err(errno, "/dev/urandom");
  235. if (read(rfd, (uint8_t *) &rnum, 8) == -1)
  236. err(errno, "/dev/urandom");
  237. }
  238. rval = (uint8_t) (rnum & 0xf);
  239. rnum >>= 4;
  240. return rval;
  241. }
  242. void
  243. cmd_dump(void)
  244. {
  245. int numInvalid, partnum;
  246. numInvalid = 0;
  247. for (partnum = 0; (partnum < 2); partnum++) {
  248. if (!validChecksum(partnum))
  249. ++numInvalid;
  250. printf("MAC (part %d): ", partnum);
  251. showmac(partnum);
  252. hexdump(partnum);
  253. }
  254. if (numInvalid < 2)
  255. errno = 0;
  256. }
  257. void
  258. showmac(int partnum)
  259. {
  260. int c;
  261. uint16_t val16;
  262. uint8_t *byte;
  263. for (c = 0; c < 3; c++) {
  264. val16 = word(c, partnum);
  265. byte = (uint8_t *) &val16;
  266. if (!little_endian)
  267. byteswap(byte);
  268. printf("%02x:%02x", byte[0], byte[1]);
  269. if (c == 2)
  270. printf("\n");
  271. else
  272. printf(":");
  273. }
  274. }
  275. void
  276. hexdump(int partnum)
  277. {
  278. int row, c;
  279. uint16_t val16;
  280. uint8_t *byte;
  281. for (row = 0; row < 8; row++) {
  282. printf("%07x ", row << 4);
  283. for (c = 0; c < 8; c++) {
  284. val16 = word((row << 3) + c, partnum);
  285. byte = (uint8_t *) &val16;
  286. if (!little_endian)
  287. byteswap(byte);
  288. printf("%02x%02x ", byte[1], byte[0]);
  289. }
  290. printf("\n");
  291. }
  292. }
  293. void
  294. cmd_setchecksum(void)
  295. {
  296. int c;
  297. uint16_t val16;
  298. for (val16 = 0, c = 0; c < 0x3F; c++)
  299. val16 += word(c, part);
  300. setWord(0x3F, part, 0xBABA - val16);
  301. }
  302. void
  303. cmd_brick(void)
  304. {
  305. if (validChecksum(part))
  306. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  307. }
  308. void
  309. cmd_swap(void)
  310. {
  311. if (validChecksum(1) || validChecksum(0)) {
  312. gbe[0] ^= gbe[1];
  313. gbe[1] ^= gbe[0];
  314. gbe[0] ^= gbe[1];
  315. gbeFileModified = 1;
  316. nvmPartModified[0] = 1;
  317. nvmPartModified[1] = 1;
  318. errno = 0;
  319. }
  320. }
  321. void
  322. cmd_copy(void)
  323. {
  324. if (validChecksum(part)) {
  325. gbe[part ^ 1] = gbe[part];
  326. gbeFileModified = 1;
  327. nvmPartModified[part ^ 1] = 1;
  328. }
  329. }
  330. int
  331. validChecksum(int partnum)
  332. {
  333. int w;
  334. uint16_t total;
  335. for(total = 0, w = 0; w <= 0x3F; w++)
  336. total += word(w, partnum);
  337. if (total == 0xBABA)
  338. return 1;
  339. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  340. errno = ECANCELED;
  341. return 0;
  342. }
  343. uint16_t
  344. word(int pos16, int partnum)
  345. {
  346. uint8_t *nbuf;
  347. uint16_t pos8, val16;
  348. nbuf = (uint8_t *) gbe[partnum];
  349. pos8 = pos16 << 1;
  350. val16 = nbuf[pos8 + 1];
  351. val16 <<= 8;
  352. val16 |= nbuf[pos8];
  353. return val16;
  354. }
  355. void
  356. setWord(int pos16, int partnum, uint16_t val16)
  357. {
  358. uint8_t val8[2], *nbuf;
  359. uint16_t pos8;
  360. gbeWriteAttempted = 1;
  361. if (word(pos16, partnum) == val16)
  362. return;
  363. nbuf = (uint8_t *) gbe[partnum];
  364. val8[0] = (uint8_t) (val16 & 0xff);
  365. val8[1] = (uint8_t) (val16 >> 8);
  366. pos8 = pos16 << 1;
  367. nbuf[pos8] = val8[0];
  368. nbuf[pos8 + 1] = val8[1];
  369. gbeFileModified = 1;
  370. nvmPartModified[partnum] = 1;
  371. }
  372. void
  373. byteswap(uint8_t *byte)
  374. {
  375. byte[0] ^= byte[1];
  376. byte[1] ^= byte[0];
  377. byte[0] ^= byte[1];
  378. }
  379. void
  380. writeGbeFile(int *fd, const char *filename)
  381. {
  382. int p, nw, tw;
  383. errno = 0;
  384. /* if copy/swap not performed, write only the nvm part */
  385. if ((gbe[0] != gbe[1]) && (gbe[0] < gbe[1]))
  386. nw = 128;
  387. else
  388. nw = SIZE_4KB;
  389. for (tw = 0, p = 0; p < 2; p++) {
  390. if (gbe[0] > gbe[1])
  391. p ^= 1;
  392. if (nvmPartModified[p]) {
  393. printf("Part %d modified\n", p);
  394. } else {
  395. fprintf (stderr,
  396. "Part %d NOT modified\n", p);
  397. goto next_part;
  398. }
  399. if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) != nw)
  400. err(errno, "%s", filename);
  401. tw += nw;
  402. next_part:
  403. if (gbe[0] > gbe[1])
  404. p ^= 1;
  405. }
  406. if (close((*fd)))
  407. err(errno, "%s", filename);
  408. printf("%d bytes written to file: `%s`\n", tw, filename);
  409. }