nvmutil.c 8.7 KB

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