nvmutil.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* SPDX-License-Identifier: MIT */
  2. /* Copyright (c) 2022-2025 Leah Rowe <leah@libreboot.org> */
  3. /* Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com> */
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <err.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void),
  15. cmd_dump(void), cmd_setmac(void), readGbe(void), checkdir(const char *path),
  16. macf(int partnum), hexdump(int partnum), openFiles(const char *path),
  17. cmd_copy(void), parseMacString(const char *strMac, uint16_t *mac),
  18. cmd_swap(void);
  19. int goodChecksum(int partnum);
  20. uint8_t hextonum(char chs), rhex(void);
  21. #define COMMAND argv[2]
  22. #define MAC_ADDRESS argv[3]
  23. #define PARTN argv[3]
  24. #define NVM_CHECKSUM 0xBABA
  25. #define NVM_CHECKSUM_WORD 0x3F
  26. #define NVM_SIZE 128
  27. #define SIZE_4KB 0x1000
  28. #define SIZE_8KB 0x2000
  29. #define SIZE_16KB 0x4000
  30. #define SIZE_128KB 0x20000
  31. uint16_t mac[3] = {0, 0, 0};
  32. ssize_t nf;
  33. size_t partsize, gbe[2];
  34. uint8_t nvmPartChanged[2] = {0, 0}, do_read[2] = {1, 1};
  35. int flags, rfd, fd, part;
  36. const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *filename = NULL;
  37. typedef struct op {
  38. char *str;
  39. void (*cmd)(void);
  40. int args;
  41. } op_t;
  42. op_t op[] = {
  43. { .str = "dump", .cmd = cmd_dump, .args = 3},
  44. { .str = "setmac", .cmd = cmd_setmac, .args = 3},
  45. { .str = "swap", .cmd = cmd_swap, .args = 3},
  46. { .str = "copy", .cmd = cmd_copy, .args = 4},
  47. { .str = "brick", .cmd = cmd_brick, .args = 4},
  48. { .str = "setchecksum", .cmd = cmd_setchecksum, .args = 4},
  49. };
  50. void (*cmd)(void) = NULL;
  51. #define ERR() errno = errno ? errno : ECANCELED
  52. #define err_if(x) if (x) err(ERR(), "%s", filename)
  53. #define xopen(f,l,p) if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
  54. if (fstat(f, &st) == -1) err(ERR(), "%s", l)
  55. #define word(pos16, partnum) ((uint16_t *) gbe[partnum])[pos16]
  56. #define setWord(pos16, p, val16) if (word(pos16, p) != val16) \
  57. nvmPartChanged[p] = 1 | (word(pos16, p) = val16)
  58. int
  59. main(int argc, char *argv[])
  60. {
  61. #ifdef __OpenBSD__
  62. err_if(pledge("stdio rpath wpath unveil", NULL) == -1);
  63. #endif
  64. if (argc < 2) {
  65. #ifdef __OpenBSD__
  66. err_if(pledge("stdio", NULL) == -1);
  67. #endif
  68. fprintf(stderr, "Modify Intel GbE NVM images e.g. set MAC\n");
  69. fprintf(stderr, "USAGE:\n");
  70. fprintf(stderr, " %s FILE dump\n", argv[0]);
  71. fprintf(stderr, " %s FILE\n # same as setmac without arg\n",
  72. argv[0]);
  73. fprintf(stderr, " %s FILE setmac [MAC]\n", argv[0]);
  74. fprintf(stderr, " %s FILE swap\n", argv[0]);
  75. fprintf(stderr, " %s FILE copy 0|1\n", argv[0]);
  76. fprintf(stderr, " %s FILE brick 0|1\n", argv[0]);
  77. fprintf(stderr, " %s FILE setchecksum 0|1\n", argv[0]);
  78. err(errno = ECANCELED, "Too few arguments");
  79. }
  80. filename = argv[1];
  81. flags = O_RDWR;
  82. if (argc > 2) {
  83. if (strcmp(COMMAND, "dump") == 0) {
  84. flags = O_RDONLY;
  85. #ifdef __OpenBSD__
  86. err_if(pledge("stdio rpath unveil", NULL) == -1);
  87. #endif
  88. }
  89. }
  90. checkdir("/dev/urandom");
  91. checkdir(filename);
  92. #ifdef __OpenBSD__
  93. err_if(unveil("/dev/urandom", "r") == -1);
  94. if (flags == O_RDONLY) {
  95. err_if(unveil(filename, "r") == -1);
  96. err_if(unveil(NULL, NULL) == -1);
  97. err_if(pledge("stdio rpath", NULL) == -1);
  98. } else {
  99. err_if(unveil(filename, "rw") == -1);
  100. err_if(unveil(NULL, NULL) == -1);
  101. err_if(pledge("stdio rpath wpath", NULL) == -1);
  102. }
  103. #endif
  104. openFiles(filename);
  105. #ifdef __OpenBSD__
  106. err_if(pledge("stdio", NULL) == -1);
  107. #endif
  108. if (argc > 2) {
  109. for (int i = 0; (i < 6) && (cmd == NULL); i++) {
  110. if (strcmp(COMMAND, op[i].str) != 0)
  111. continue;
  112. if (argc >= op[i].args) {
  113. cmd = op[i].cmd;
  114. break;
  115. }
  116. err(errno = EINVAL, "Too few args on command '%s'",
  117. op[i].str);
  118. }
  119. } else {
  120. cmd = cmd_setmac;
  121. }
  122. if ((cmd == NULL) && (argc > 2)) { /* nvm gbe [MAC] */
  123. strMac = COMMAND;
  124. cmd = cmd_setmac;
  125. } else if (cmd == cmd_setmac) { /* nvm gbe setmac [MAC] */
  126. strMac = strRMac; /* random MAC */
  127. if (argc > 3)
  128. strMac = MAC_ADDRESS;
  129. } else if ((cmd != NULL) && (argc > 3)) { /* user-supplied partnum */
  130. err_if((errno = (!((part = PARTN[0] - '0') == 0 || part == 1))
  131. || PARTN[1] ? EINVAL : errno)); /* only allow '0' or '1' */
  132. }
  133. err_if((errno = (cmd == NULL) ? EINVAL : errno));
  134. readGbe();
  135. (*cmd)();
  136. writeGbe();
  137. err_if((errno != 0) && (cmd != cmd_dump));
  138. return errno;
  139. }
  140. void
  141. checkdir(const char *path)
  142. {
  143. if (opendir(path) != NULL)
  144. err(errno = EISDIR, "%s", path);
  145. if (errno == ENOTDIR)
  146. errno = 0;
  147. err_if(errno);
  148. }
  149. void
  150. openFiles(const char *path)
  151. {
  152. struct stat st;
  153. xopen(fd, path, flags);
  154. switch(st.st_size) {
  155. case SIZE_8KB:
  156. case SIZE_16KB:
  157. case SIZE_128KB:
  158. partsize = st.st_size >> 1;
  159. break;
  160. default:
  161. err(errno = ECANCELED, "Invalid file size (not 8/16/128KiB)");
  162. break;
  163. }
  164. xopen(rfd, "/dev/urandom", O_RDONLY);
  165. }
  166. void
  167. readGbe(void)
  168. {
  169. if ((cmd == cmd_swap) || (cmd == cmd_copy))
  170. nf = SIZE_4KB;
  171. else
  172. nf = NVM_SIZE;
  173. if ((cmd == cmd_copy) || (cmd == cmd_setchecksum) || (cmd == cmd_brick))
  174. do_read[part ^ 1] = 0;
  175. char *buf = malloc(nf << (do_read[0] & do_read[1]));
  176. if (buf == NULL)
  177. err(errno, NULL);
  178. gbe[0] = (size_t) buf;
  179. gbe[1] = gbe[0] + (nf * (do_read[0] & do_read[1]));
  180. ssize_t tnr = 0;
  181. for (int p = 0; p < 2; p++) {
  182. if (!do_read[p])
  183. continue;
  184. ssize_t nr = pread(fd, (uint8_t *) gbe[p], nf, p * partsize);
  185. err_if(nr == -1);
  186. if (nr != nf)
  187. err(errno == ECANCELED,
  188. "%ld bytes read from '%s', expected %ld bytes\n",
  189. nr, filename, nf);
  190. tnr += nr;
  191. swap(p); /* handle big-endian host CPU */
  192. }
  193. printf("%ld bytes read from file '%s'\n", tnr, filename);
  194. }
  195. void
  196. cmd_setmac(void)
  197. {
  198. int mac_updated = 0;
  199. parseMacString(strMac, mac);
  200. printf("MAC address to be written: %s\n", strMac);
  201. for (int partnum = 0; partnum < 2; partnum++) {
  202. if (!goodChecksum(part = partnum))
  203. continue;
  204. for (int w = 0; w < 3; w++)
  205. setWord(w, partnum, mac[w]);
  206. printf("Wrote MAC address to part %d: ", partnum);
  207. macf(partnum);
  208. cmd_setchecksum();
  209. mac_updated = 1;
  210. }
  211. if (mac_updated)
  212. errno = 0;
  213. }
  214. void
  215. parseMacString(const char *strMac, uint16_t *mac)
  216. {
  217. uint64_t total = 0;
  218. if (strnlen(strMac, 20) != 17)
  219. err(errno = EINVAL, "Invalid MAC address string length");
  220. for (uint8_t h, i = 0; i < 16; i += 3) {
  221. if (i != 15)
  222. if (strMac[i + 2] != ':')
  223. err(errno = EINVAL,
  224. "Invalid MAC address separator '%c'",
  225. strMac[i + 2]);
  226. int byte = i / 3;
  227. for (int nib = 0; nib < 2; nib++, total += h) {
  228. if ((h = hextonum(strMac[i + nib])) > 15)
  229. err(errno = EINVAL, "Invalid character '%c'",
  230. strMac[i + nib]);
  231. /* If random, ensure that local/unicast bits are set */
  232. if ((byte == 0) && (nib == 1))
  233. if ((strMac[i + nib] == '?') ||
  234. (strMac[i + nib] == 'x') ||
  235. (strMac[i + nib] == 'X')) /* random */
  236. h = (h & 0xE) | 2; /* local, unicast */
  237. mac[byte >> 1] |= ((uint16_t ) h)
  238. << ((8 * (byte % 2)) + (4 * (nib ^ 1)));
  239. }
  240. }
  241. if (total == 0)
  242. err(errno = EINVAL, "Invalid MAC (all-zero MAC address)");
  243. if (mac[0] & 1)
  244. err(errno = EINVAL, "Invalid MAC (multicast bit set)");
  245. }
  246. uint8_t
  247. hextonum(char ch)
  248. {
  249. if ((ch >= '0') && (ch <= '9'))
  250. return ch - '0';
  251. else if ((ch >= 'A') && (ch <= 'F'))
  252. return ch - 'A' + 10;
  253. else if ((ch >= 'a') && (ch <= 'f'))
  254. return ch - 'a' + 10;
  255. else if ((ch == '?') || (ch == 'x') || (ch == 'X'))
  256. return rhex(); /* random hex value */
  257. else
  258. return 16; /* error: invalid character */
  259. }
  260. uint8_t
  261. rhex(void)
  262. {
  263. static uint8_t n = 0, rnum[16];
  264. if (!n)
  265. err_if(pread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0) == -1);
  266. return rnum[n--] & 0xf;
  267. }
  268. void
  269. cmd_dump(void)
  270. {
  271. for (int partnum = 0, numInvalid = 0; partnum < 2; partnum++) {
  272. if ((cmd != cmd_dump) && (flags != O_RDONLY) &&
  273. (!nvmPartChanged[partnum]))
  274. continue;
  275. if (!goodChecksum(partnum))
  276. ++numInvalid;
  277. printf("MAC (part %d): ", partnum);
  278. macf(partnum);
  279. hexdump(partnum);
  280. if ((numInvalid < 2) && (partnum))
  281. errno = 0;
  282. }
  283. }
  284. void
  285. macf(int partnum)
  286. {
  287. for (int c = 0; c < 3; c++) {
  288. uint16_t val16 = word(c, partnum);
  289. printf("%02x:%02x", val16 & 0xff, val16 >> 8);
  290. if (c == 2)
  291. printf("\n");
  292. else
  293. printf(":");
  294. }
  295. }
  296. void
  297. hexdump(int partnum)
  298. {
  299. for (int row = 0; row < 8; row++) {
  300. printf("%08x ", row << 4);
  301. for (int c = 0; c < 8; c++) {
  302. uint16_t val16 = word((row << 3) + c, partnum);
  303. if (c == 4)
  304. printf(" ");
  305. printf(" %02x %02x", val16 & 0xff, val16 >> 8);
  306. }
  307. printf("\n");
  308. }
  309. }
  310. void
  311. cmd_setchecksum(void)
  312. {
  313. uint16_t val16 = 0;
  314. for (int c = 0; c < NVM_CHECKSUM_WORD; c++)
  315. val16 += word(c, part);
  316. setWord(NVM_CHECKSUM_WORD, part, NVM_CHECKSUM - val16);
  317. }
  318. void
  319. cmd_brick(void)
  320. {
  321. if (goodChecksum(part))
  322. setWord(NVM_CHECKSUM_WORD, part,
  323. ((word(NVM_CHECKSUM_WORD, part)) ^ 0xFF));
  324. }
  325. void
  326. cmd_copy(void)
  327. {
  328. nvmPartChanged[part ^ 1] = goodChecksum(part);
  329. }
  330. void
  331. cmd_swap(void) {
  332. err_if(!(goodChecksum(0) || goodChecksum(1)));
  333. errno = 0;
  334. gbe[0] ^= gbe[1];
  335. gbe[1] ^= gbe[0];
  336. gbe[0] ^= gbe[1];
  337. nvmPartChanged[0] = nvmPartChanged[1] = 1;
  338. }
  339. int
  340. goodChecksum(int partnum)
  341. {
  342. uint16_t total = 0;
  343. for(int w = 0; w <= NVM_CHECKSUM_WORD; w++)
  344. total += word(w, partnum);
  345. if (total == NVM_CHECKSUM)
  346. return 1;
  347. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  348. errno = ECANCELED;
  349. return 0;
  350. }
  351. void
  352. writeGbe(void)
  353. {
  354. ssize_t tnw = 0;
  355. for (int p = 0; p < 2; p++) {
  356. if ((!nvmPartChanged[p]) || (flags == O_RDONLY))
  357. continue;
  358. swap(p); /* swap bytes on big-endian host CPUs */
  359. ssize_t nw = pwrite(fd, (uint8_t *) gbe[p], nf, p * partsize);
  360. err_if(nw == -1);
  361. if (nw != nf)
  362. err(errno == ECANCELED,
  363. "%ld bytes written to '%s', expected %ld bytes\n",
  364. nw, filename, nf);
  365. tnw += nf;
  366. }
  367. if ((flags != O_RDONLY) && (cmd != cmd_dump)) {
  368. if (nvmPartChanged[0] || nvmPartChanged[1])
  369. printf("The following nvm words were written:\n");
  370. cmd_dump();
  371. }
  372. if ((!tnw) && (flags != O_RDONLY) && (!errno))
  373. fprintf(stderr, "No changes needed on file '%s'\n", filename);
  374. else if (tnw)
  375. printf("%ld bytes written to file '%s'\n", tnw, filename);
  376. if (tnw)
  377. errno = 0;
  378. err_if(close(fd) == -1);
  379. }
  380. void
  381. swap(int partnum)
  382. {
  383. size_t w, x;
  384. uint8_t *n = (uint8_t *) gbe[partnum];
  385. int e = 1;
  386. for (w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1; w < NVM_SIZE;
  387. w += 2, x += 2) {
  388. n[w] ^= n[x];
  389. n[x] ^= n[w];
  390. n[w] ^= n[x];
  391. }
  392. }