nvmutil.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /*
  24. * This file is part of Libreboot. See:
  25. * https://libreboot.org/docs/install/nvmutil.html
  26. */
  27. #include <sys/stat.h>
  28. #include <dirent.h>
  29. #include <err.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. void readGbeFile(int *fd, const char *path, int flags,
  38. size_t nr);
  39. void cmd_setmac(const char *strMac);
  40. int invalidMacAddress(const char *strMac, uint16_t *mac);
  41. uint8_t hextonum(char chs);
  42. uint8_t rhex(void);
  43. void cmd_dump(void);
  44. void showmac(int partnum);
  45. void hexdump(int partnum);
  46. void cmd_setchecksum(void);
  47. void cmd_brick(void);
  48. void cmd_swap(void);
  49. void cmd_copy(void);
  50. int validChecksum(int partnum);
  51. uint16_t word(int pos16, int partnum);
  52. void setWord(int pos16, int partnum, uint16_t val16);
  53. void byteswap(int n, int partnum);
  54. void writeGbeFile(int *fd, const char *filename, size_t nw);
  55. #define FILENAME argv[1]
  56. #define COMMAND argv[2]
  57. #define MAC_ADDRESS argv[3]
  58. #define PARTNUM argv[3]
  59. #define SIZE_4KB 0x1000
  60. #define SIZE_8KB 0x2000
  61. uint16_t buf16[SIZE_4KB];
  62. uint8_t *buf;
  63. size_t gbe[2];
  64. uint8_t skipread[2] = {0, 0};
  65. int part, gbeFileModified = 0;
  66. uint8_t nvmPartModified[2] = {0, 0};
  67. uint16_t test;
  68. uint8_t big_endian;
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. size_t nr = 128;
  73. int fd, flags = O_RDWR;
  74. void (*cmd)(void) = NULL;
  75. const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
  76. #ifdef HAVE_PLEDGE /* openbsd */
  77. if (pledge("stdio wpath", NULL) == -1)
  78. err(errno, "pledge");
  79. #endif
  80. buf = (uint8_t *) &buf16;
  81. gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
  82. test = 1;
  83. big_endian = ((uint8_t *) &test)[0] ^ 1;
  84. if (argc == 3) {
  85. if (strcmp(COMMAND, "dump") == 0) {
  86. #ifdef HAVE_PLEDGE /* openbsd */
  87. if (pledge("stdio rpath", NULL) == -1)
  88. err(errno, "pledge");
  89. #endif
  90. flags = O_RDONLY;
  91. cmd = &cmd_dump;
  92. } else if (strcmp(COMMAND, "setmac") == 0) {
  93. strMac = (char *) strRMac; /* random mac address */
  94. } else if (strcmp(COMMAND, "swap") == 0) {
  95. cmd = &cmd_swap;
  96. nr = SIZE_4KB;
  97. }
  98. } else if (argc == 4) {
  99. if (strcmp(COMMAND, "setmac") == 0) {
  100. strMac = MAC_ADDRESS; /* user-supplied mac address */
  101. } else if ((!((part = PARTNUM[0] - '0') == 0 || part == 1))
  102. || PARTNUM[1]) { /* only allow '1' or '0' */
  103. errno = EINVAL;
  104. } else if (strcmp(COMMAND, "setchecksum") == 0) {
  105. cmd = &cmd_setchecksum;
  106. } else if (strcmp(COMMAND, "brick") == 0) {
  107. cmd = &cmd_brick;
  108. } else if (strcmp(COMMAND, "copy") == 0) {
  109. cmd = &cmd_copy;
  110. nr = SIZE_4KB;
  111. }
  112. }
  113. if ((strMac == NULL) && (cmd == NULL))
  114. errno = EINVAL;
  115. if (errno == 0) {
  116. skipread[part ^ 1] = (cmd == &cmd_copy) |
  117. (cmd == &cmd_setchecksum) | (cmd == &cmd_brick);
  118. readGbeFile(&fd, FILENAME, flags, nr);
  119. if (strMac != NULL)
  120. cmd_setmac(strMac); /* nvm gbe.bin setmac */
  121. else if (cmd != NULL)
  122. (*cmd)(); /* all other commands except setmac */
  123. writeGbeFile(&fd, FILENAME, nr);
  124. }
  125. if ((errno != 0) && (cmd != &cmd_dump))
  126. err(errno, NULL);
  127. return errno;
  128. }
  129. void
  130. readGbeFile(int *fd, const char *path, int flags, size_t nr)
  131. {
  132. struct stat st;
  133. if (opendir(path) != NULL)
  134. err(errno = EISDIR, "%s", path);
  135. else if (((*fd) = open(path, flags)) == -1)
  136. err(errno, "%s", path);
  137. else if (fstat((*fd), &st) == -1)
  138. err(errno, "%s", path);
  139. else if ((st.st_size != SIZE_8KB))
  140. err(errno = ECANCELED, "File `%s` not 8KiB", path);
  141. else if (errno == ENOTDIR)
  142. errno = 0;
  143. for (int p = 0; p < 2; p++) {
  144. if (skipread[p])
  145. continue;
  146. if (pread((*fd), (uint8_t *) gbe[p], nr, p << 12) == -1)
  147. err(errno, "%s", path);
  148. if (big_endian)
  149. byteswap(nr, p);
  150. }
  151. }
  152. void
  153. cmd_setmac(const char *strMac)
  154. {
  155. uint16_t mac[3] = {0, 0, 0};
  156. if (invalidMacAddress(strMac, mac))
  157. err(errno = ECANCELED, "Bad MAC address");
  158. for (int partnum = 0; partnum < 2; partnum++) {
  159. if (validChecksum(part = partnum)) {
  160. for (int w = 0; w < 3; w++)
  161. setWord(w, partnum, mac[w]);
  162. cmd_setchecksum();
  163. }
  164. }
  165. }
  166. int
  167. invalidMacAddress(const char *strMac, uint16_t *mac)
  168. {
  169. uint8_t h;
  170. uint64_t total = 0;
  171. if (strnlen(strMac, 20) == 17) {
  172. for (int i = 0; i < 16; i += 3) {
  173. if (i != 15)
  174. if (strMac[i + 2] != ':')
  175. return 1;
  176. int byte = i / 3;
  177. for (int nib = 0; nib < 2; nib++, total += h) {
  178. if ((h = hextonum(strMac[i + nib])) > 15)
  179. return 1;
  180. if ((byte == 0) && (nib == 1))
  181. if (strMac[i + nib] == '?')
  182. h = (h & 0xE) | 2; /* local, unicast */
  183. mac[byte >> 1] |= ((uint16_t ) h)
  184. << ((8 * (byte % 2)) + (4 * (nib ^ 1)));
  185. }
  186. }}
  187. return ((total == 0) | (mac[0] & 1)); /* multicast/all-zero banned */
  188. }
  189. uint8_t
  190. hextonum(char ch)
  191. {
  192. if ((ch >= '0') && (ch <= '9'))
  193. return ch - '0';
  194. else if ((ch >= 'A') && (ch <= 'F'))
  195. return ch - 'A' + 10;
  196. else if ((ch >= 'a') && (ch <= 'f'))
  197. return ch - 'a' + 10;
  198. else if (ch == '?')
  199. return rhex(); /* random number */
  200. else
  201. return 16;
  202. }
  203. uint8_t
  204. rhex(void)
  205. {
  206. static int rfd = -1;
  207. static uint64_t rnum = 0;
  208. if (rnum == 0) {
  209. if (rfd == -1)
  210. if ((rfd = open("/dev/urandom", O_RDONLY)) == -1)
  211. err(errno, "/dev/urandom");
  212. if (read(rfd, (uint8_t *) &rnum, 8) == -1)
  213. err(errno, "/dev/urandom");
  214. }
  215. uint8_t rval = (uint8_t) (rnum & 0xf);
  216. rnum >>= 4;
  217. return rval;
  218. }
  219. void
  220. cmd_dump(void)
  221. {
  222. int partnum, numInvalid = 0;
  223. for (partnum = 0; partnum < 2; partnum++) {
  224. if (!validChecksum(partnum))
  225. ++numInvalid;
  226. printf("MAC (part %d): ", partnum);
  227. showmac(partnum);
  228. hexdump(partnum);
  229. }
  230. if (numInvalid < 2)
  231. errno = 0;
  232. }
  233. void
  234. showmac(int partnum)
  235. {
  236. uint16_t val16;
  237. for (int c = 0; c < 3; c++) {
  238. val16 = word(c, partnum);
  239. printf("%02x:%02x", val16 & 0xff, val16 >> 8);
  240. printf(c == 2 ? "\n" : ":");
  241. }
  242. }
  243. void
  244. hexdump(int partnum)
  245. {
  246. for (int row = 0; row < 8; row++) {
  247. printf("%07x", row << 4);
  248. for (int c = 0; c < 8; c++) {
  249. uint16_t val16 = word((row << 3) + c, partnum);
  250. printf(" %02x%02x", val16 >> 8, val16 & 0xff);
  251. }
  252. printf("\n");
  253. }
  254. }
  255. void
  256. cmd_setchecksum(void)
  257. {
  258. uint16_t val16 = 0;
  259. for (int c = 0; c < 0x3F; c++)
  260. val16 += word(c, part);
  261. setWord(0x3F, part, 0xBABA - val16);
  262. }
  263. void
  264. cmd_brick(void)
  265. {
  266. if (validChecksum(part))
  267. setWord(0x3F, part, (word(0x3F, part)) ^ 0xFF);
  268. }
  269. void
  270. cmd_swap(void)
  271. {
  272. gbe[0] ^= gbe[1]; /* speedhack: swap ptr, not words */
  273. gbe[1] ^= gbe[0];
  274. gbe[0] ^= gbe[1];
  275. gbeFileModified = nvmPartModified[0] = nvmPartModified[1]
  276. = validChecksum(1) | validChecksum(0);
  277. }
  278. void
  279. cmd_copy(void)
  280. {
  281. gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
  282. gbeFileModified = nvmPartModified[part ^ 1] = validChecksum(part);
  283. }
  284. int
  285. validChecksum(int partnum)
  286. {
  287. uint16_t total = 0;
  288. for(int w = 0; w <= 0x3F; w++)
  289. total += word(w, partnum);
  290. if (total == 0xBABA)
  291. return 1;
  292. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  293. return (errno = ECANCELED) & 0;
  294. }
  295. uint16_t
  296. word(int pos16, int partnum)
  297. {
  298. return buf16[pos16 + (partnum << 11)];
  299. }
  300. void
  301. setWord(int pos16, int partnum, uint16_t val16)
  302. {
  303. gbeFileModified = 1;
  304. if (word(pos16, partnum) == val16)
  305. return;
  306. buf16[pos16 + (partnum << 11)] = val16;
  307. nvmPartModified[partnum] = 1;
  308. }
  309. void
  310. byteswap(int n, int partnum)
  311. {
  312. int b1, b2, wcount = n >> 1;
  313. uint8_t *nbuf = (uint8_t *) gbe[partnum];
  314. for (int w = 0; w < wcount; w++) {
  315. b1 = b2 = w << 1;
  316. nbuf[b1] ^= nbuf[++b2]; /* xor swap */
  317. nbuf[b2] ^= nbuf[b1];
  318. nbuf[b1] ^= nbuf[b2];
  319. }
  320. }
  321. void
  322. writeGbeFile(int *fd, const char *filename, size_t nw)
  323. {
  324. if (gbeFileModified)
  325. errno = 0;
  326. for (int p = 0; p < 2; p++) {
  327. if (gbe[0] > gbe[1])
  328. p ^= 1; /* speedhack: write sequentially on-disk */
  329. if (!nvmPartModified[p])
  330. goto next_part;
  331. if (big_endian)
  332. byteswap(nw, p);
  333. if (pwrite((*fd), (uint8_t *) gbe[p], nw, p << 12) == -1)
  334. err(errno, "%s", filename);
  335. next_part:
  336. if (gbe[0] > gbe[1])
  337. p ^= 1; /* speedhack: write sequentially on-disk */
  338. }
  339. if (close((*fd)))
  340. err(errno, "%s", filename);
  341. }