nvmutil.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 PROGNAME argv[0]
  51. #define FILENAME argv[1]
  52. #define COMMAND argv[2]
  53. #define MAC_ADDRESS argv[3]
  54. #define PARTNUM argv[3]
  55. #define SIZE_4KB 0x1000
  56. #define SIZE_8KB 0x2000
  57. uint8_t *buf = NULL;
  58. size_t gbe[2];
  59. int part, gbeFileModified = 0;
  60. uint8_t nvmPartModified[2];
  61. uint16_t test;
  62. uint8_t little_endian;
  63. int
  64. main(int argc, char *argv[])
  65. {
  66. int fd;
  67. int flags = O_RDWR;
  68. char *strMac = NULL;
  69. char *strRMac = "??:??:??:??:??:??";
  70. void (*cmd)(void) = NULL;
  71. if ((buf = (uint8_t *) malloc(SIZE_8KB)) == NULL)
  72. err(errno, NULL);
  73. gbe[0] = gbe[1] = (size_t) buf;
  74. gbe[1] += SIZE_4KB;
  75. nvmPartModified[0] = 0;
  76. nvmPartModified[1] = 0;
  77. test = 1;
  78. little_endian = ((uint8_t *) &test)[0];
  79. #ifdef HAVE_PLEDGE
  80. if (pledge("stdio wpath", NULL) == -1)
  81. err(errno, "pledge");
  82. #endif
  83. if (argc == 3) {
  84. if (strcmp(COMMAND, "dump") == 0) {
  85. #ifdef HAVE_PLEDGE
  86. if (pledge("stdio rpath", NULL) == -1)
  87. err(errno, "pledge");
  88. #endif
  89. flags = O_RDONLY;
  90. cmd = &cmd_dump;
  91. } else if (strcmp(COMMAND, "setmac") == 0)
  92. strMac = strRMac;
  93. else if (strcmp(COMMAND, "swap") == 0)
  94. cmd = &cmd_swap;
  95. } else if (argc == 4) {
  96. if (strcmp(COMMAND, "setmac") == 0)
  97. strMac = MAC_ADDRESS;
  98. else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
  99. || PARTNUM[1])
  100. errno = EINVAL;
  101. else if (strcmp(COMMAND, "setchecksum") == 0)
  102. cmd = &cmd_setchecksum;
  103. else if (strcmp(COMMAND, "brick") == 0)
  104. cmd = &cmd_brick;
  105. else if (strcmp(COMMAND, "copy") == 0)
  106. cmd = &cmd_copy;
  107. }
  108. if ((strMac == NULL) && (cmd == NULL))
  109. errno = EINVAL;
  110. else if (readFromFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB)
  111. goto nvmutil_exit;
  112. if (errno == 0) {
  113. if (strMac != NULL)
  114. cmd_setmac(strMac);
  115. else if (cmd != NULL)
  116. (*cmd)();
  117. if (gbeFileModified)
  118. writeGbeFile(&fd, FILENAME);
  119. }
  120. nvmutil_exit:
  121. if (!((errno == ECANCELED) && (flags == O_RDONLY)))
  122. if (errno != 0)
  123. err(errno, NULL);
  124. return errno;
  125. }
  126. ssize_t
  127. readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
  128. {
  129. struct stat st;
  130. if (opendir(path) != NULL) {
  131. errno = EISDIR;
  132. return -1;
  133. } else if (((*fd) = open(path, flags)) == -1) {
  134. return -1;
  135. } else if (size == SIZE_8KB) {
  136. if (fstat((*fd), &st) == -1)
  137. return -1;
  138. if ((st.st_size != SIZE_8KB) && strcmp(path, "/dev/urandom")) {
  139. fprintf(stderr, "Bad file size\n");
  140. errno = ECANCELED;
  141. return -1;
  142. }
  143. }
  144. if (errno == ENOTDIR)
  145. errno = 0;
  146. else if (errno != 0)
  147. return -1;
  148. return read((*fd), buf, size);
  149. }
  150. void
  151. cmd_setmac(const char *strMac)
  152. {
  153. uint8_t o, val8;
  154. uint16_t val16;
  155. int partnum, byte, nib;
  156. uint16_t mac[3] = {0, 0, 0};
  157. uint64_t total = 0;
  158. if (strnlen(strMac, 20) != 17)
  159. goto invalid_mac_address;
  160. for (o = 0; o < 16; o += 3) {
  161. if (o != 15)
  162. if (strMac[o + 2] != ':')
  163. goto invalid_mac_address;
  164. byte = o / 3;
  165. for (nib = 0; nib < 2; nib++, total += val8) {
  166. if ((val8 = hextonum(strMac[o + nib])) > 15)
  167. goto invalid_mac_address;
  168. if ((byte == 0) && (nib == 1)) {
  169. if (strMac[o + nib] == '?')
  170. val8 = (val8 & 0xE) | 2;
  171. }
  172. val16 = val8;
  173. if ((byte % 2) ^ 1)
  174. byteswap((uint8_t *) &val16);
  175. val16 <<= 4 * (nib ^ 1);
  176. mac[byte >> 1] |= val16;
  177. }
  178. }
  179. test = mac[0];
  180. if (little_endian)
  181. byteswap((uint8_t *) &test);
  182. if (total == 0 || (((uint8_t *) &test)[0] & 1))
  183. goto invalid_mac_address;
  184. if (little_endian)
  185. for (o = 0; o < 3; o++)
  186. byteswap((uint8_t *) &mac[o]);
  187. for (partnum = 0; partnum < 2; partnum++) {
  188. if (!validChecksum(partnum))
  189. continue;
  190. for (o = 0; o < 3; o++)
  191. setWord(o, partnum, mac[o]);
  192. part = partnum;
  193. cmd_setchecksum();
  194. }
  195. return;
  196. invalid_mac_address:
  197. fprintf(stderr, "Bad MAC address\n");
  198. errno = ECANCELED;
  199. }
  200. uint8_t
  201. hextonum(char chs)
  202. {
  203. uint8_t val8, ch;
  204. ch = (uint8_t) chs;
  205. if ((ch >= '0') && (ch <= '9'))
  206. val8 = ch - '0';
  207. else if ((ch >= 'A') && (ch <= 'F'))
  208. val8 = ch - 'A' + 10;
  209. else if ((ch >= 'a') && (ch <= 'f'))
  210. val8 = ch - 'a' + 10;
  211. else if (ch == '?')
  212. val8 = rhex();
  213. else
  214. return 16;
  215. return val8;
  216. }
  217. uint8_t
  218. rhex(void)
  219. {
  220. static int rfd = -1;
  221. static uint8_t *rbuf = NULL;
  222. static size_t rindex = BUFSIZ;
  223. if (rindex == BUFSIZ) {
  224. rindex = 0;
  225. if (rbuf == NULL)
  226. if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL)
  227. err(errno, NULL);
  228. if (rfd != -1) {
  229. if (close(rfd))
  230. err(errno, "/dev/urandom");
  231. rfd = -1;
  232. }
  233. if (readFromFile(&rfd, rbuf, "/dev/urandom", O_RDONLY, BUFSIZ)
  234. != BUFSIZ)
  235. err(errno, "/dev/urandom");
  236. if (errno != 0)
  237. err(errno, "/dev/urandom");
  238. }
  239. return rbuf[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 = 0;
  297. for (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. int part0, part1;
  311. part0 = validChecksum(0);
  312. part1 = validChecksum(1);
  313. if (part0 || part1) {
  314. gbe[0] ^= gbe[1];
  315. gbe[1] ^= gbe[0];
  316. gbe[0] ^= gbe[1];
  317. gbeFileModified = 1;
  318. nvmPartModified[0] = 1;
  319. nvmPartModified[1] = 1;
  320. errno = 0;
  321. }
  322. }
  323. void
  324. cmd_copy(void)
  325. {
  326. if (validChecksum(part)) {
  327. gbe[part ^ 1] = gbe[part];
  328. gbeFileModified = 1;
  329. nvmPartModified[part ^ 1] = 1;
  330. }
  331. }
  332. int
  333. validChecksum(int partnum)
  334. {
  335. int w;
  336. uint16_t total = 0;
  337. for(w = 0; w <= 0x3F; w++)
  338. total += word(w, partnum);
  339. if (total == 0xBABA)
  340. return 1;
  341. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  342. errno = ECANCELED;
  343. return 0;
  344. }
  345. uint16_t
  346. word(int pos16, int partnum)
  347. {
  348. uint16_t val16 = ((uint16_t *) buf)[pos16 + (partnum << 11)];
  349. if (!little_endian)
  350. byteswap((uint8_t *) &val16);
  351. return val16;
  352. }
  353. void
  354. setWord(int pos16, int partnum, uint16_t val)
  355. {
  356. ((uint16_t *) buf)[pos16 + (partnum << 11)] = val;
  357. if (!little_endian)
  358. byteswap(buf + (pos16 << 1) + (partnum << 12));
  359. gbeFileModified = 1;
  360. nvmPartModified[partnum] = 1;
  361. }
  362. void
  363. byteswap(uint8_t *byte)
  364. {
  365. byte[0] ^= byte[1];
  366. byte[1] ^= byte[0];
  367. byte[0] ^= byte[1];
  368. }
  369. void
  370. writeGbeFile(int *fd, const char *filename)
  371. {
  372. int partnum;
  373. errno = 0;
  374. if (pwrite((*fd), (uint8_t *) gbe[0], SIZE_4KB, 0) != SIZE_4KB)
  375. err(errno, "%s", filename);
  376. if (pwrite((*fd), (uint8_t *) gbe[1], SIZE_4KB, SIZE_4KB) != SIZE_4KB)
  377. err(errno, "%s", filename);
  378. if (close((*fd)))
  379. err(errno, "%s", filename);
  380. if (errno != 0)
  381. err(errno, "%s", filename);
  382. for (partnum = 0; partnum < 2; partnum++) {
  383. if (nvmPartModified[partnum])
  384. printf("Part %d modified\n", partnum);
  385. else
  386. fprintf (stderr,
  387. "Part %d NOT modified\n", partnum);
  388. }
  389. printf("File `%s` successfully modified\n", filename);
  390. }