nvmutil.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* SPDX-License-Identifier: MIT */
  2. /* SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org> */
  3. /* SPDX-FileCopyrightText: 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), cmd_copy(void), writeGbeFile(void),
  15. cmd_dump(void), cmd_setmac(void), readGbeFile(void), showmac(int partnum),
  16. hexdump(int partnum), handle_endianness(int partnum), openFiles(const char *path);
  17. int macAddress(const char *strMac, uint16_t *mac), validChecksum(int partnum);
  18. uint8_t hextonum(char chs), rhex(void);
  19. #define COMMAND argv[2]
  20. #define MAC_ADDRESS argv[3]
  21. #define PARTNUM argv[3]
  22. #define SIZE_4KB 0x1000
  23. uint16_t buf16[SIZE_4KB], mac[3] = {0, 0, 0};
  24. uint8_t *buf = (uint8_t *) &buf16;
  25. size_t nf = 128, gbe[2];
  26. uint8_t nvmPartModified[2] = {0, 0}, skipread[2] = {0, 0};
  27. int e = 1, flags = O_RDWR, rfd, fd, part, gbeFileModified = 0;
  28. const char *strMac = NULL, *strRMac = "??:??:??:??:??:??", *filename = NULL;
  29. typedef struct op {
  30. char *str;
  31. void (*cmd)(void);
  32. int args;
  33. } op_t;
  34. op_t op[] = {
  35. { .str = "dump", .cmd = cmd_dump, .args = 3},
  36. { .str = "setmac", .cmd = cmd_setmac, .args = 3},
  37. { .str = "swap", .cmd = writeGbeFile, .args = 3},
  38. { .str = "copy", .cmd = cmd_copy, .args = 4},
  39. { .str = "brick", .cmd = cmd_brick, .args = 4},
  40. { .str = "setchecksum", .cmd = cmd_setchecksum, .args = 4},
  41. };
  42. void (*cmd)(void) = NULL;
  43. #define ERR() errno = errno ? errno : ECANCELED
  44. #define err_if(x) if (x) err(ERR(), "%s", filename)
  45. #define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
  46. if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
  47. if (fstat(f, &st) == -1) err(ERR(), "%s", l)
  48. #define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
  49. #define setWord(pos16, p, val16) if ((gbeFileModified = 1) && \
  50. word(pos16, p) != val16) nvmPartModified[p] = 1 | (word(pos16, p) = val16)
  51. int
  52. main(int argc, char *argv[])
  53. {
  54. if (argc < 3) {
  55. fprintf(stderr, "USAGE:\n");
  56. fprintf(stderr, " %s FILE dump\n", argv[0]);
  57. fprintf(stderr, " %s FILE setmac [MAC]\n", argv[0]);
  58. fprintf(stderr, " %s FILE swap\n", argv[0]);
  59. fprintf(stderr, " %s FILE copy 0|1\n", argv[0]);
  60. fprintf(stderr, " %s FILE brick 0|1\n", argv[0]);
  61. fprintf(stderr, " %s FILE setchecksum 0|1\n", argv[0]);
  62. err(errno = ECANCELED, "Too few arguments");
  63. }
  64. flags = (strcmp(COMMAND, "dump") == 0) ? O_RDONLY : flags;
  65. filename = argv[1];
  66. #ifdef __OpenBSD__
  67. err_if(unveil("/dev/urandom", "r") == -1);
  68. err_if(unveil(filename, flags == O_RDONLY ? "r" : "rw") == -1);
  69. err_if(pledge(flags == O_RDONLY ? "stdio rpath" : "stdio rpath wpath",
  70. NULL) == -1);
  71. #endif
  72. openFiles(filename);
  73. #ifdef __OpenBSD__
  74. err_if(pledge("stdio", NULL) == -1);
  75. #endif
  76. for (int i = 0; i < 6; i++)
  77. if (strcmp(COMMAND, op[i].str) == 0)
  78. if ((cmd = argc >= op[i].args ? op[i].cmd : NULL))
  79. break;
  80. if (cmd == cmd_setmac)
  81. strMac = (argc > 3) ? MAC_ADDRESS : strRMac;
  82. else if ((cmd != NULL) && (argc > 3))
  83. err_if((errno = (!((part = PARTNUM[0] - '0') == 0 || part == 1))
  84. || PARTNUM[1] ? EINVAL : errno));
  85. err_if((errno = (cmd == NULL) ? EINVAL : errno));
  86. readGbeFile();
  87. (*cmd)();
  88. if ((gbeFileModified) && (flags != O_RDONLY) && (cmd != writeGbeFile))
  89. writeGbeFile();
  90. err_if((errno != 0) && (cmd != cmd_dump));
  91. return errno;
  92. }
  93. void
  94. openFiles(const char *path)
  95. {
  96. struct stat st;
  97. xopen(fd, path, flags);
  98. if ((st.st_size != (SIZE_4KB << 1)))
  99. err(errno = ECANCELED, "File `%s` not 8KiB", path);
  100. xopen(rfd, "/dev/urandom", O_RDONLY);
  101. errno = errno != ENOTDIR ? errno : 0;
  102. }
  103. void
  104. readGbeFile(void)
  105. {
  106. nf = ((cmd == writeGbeFile) || (cmd == cmd_copy)) ? SIZE_4KB : nf;
  107. skipread[part ^ 1] = (cmd == cmd_copy) | (cmd == cmd_setchecksum)
  108. | (cmd == cmd_brick);
  109. gbe[1] = (gbe[0] = (size_t) buf) + SIZE_4KB;
  110. for (int p = 0; p < 2; p++) {
  111. if (skipread[p])
  112. continue;
  113. err_if(pread(fd, (uint8_t *) gbe[p], nf, p << 12) == -1);
  114. handle_endianness(p);
  115. }
  116. }
  117. void
  118. cmd_setmac(void)
  119. {
  120. if (macAddress(strMac, mac))
  121. err(errno = ECANCELED, "Bad MAC address");
  122. for (int partnum = 0; partnum < 2; partnum++) {
  123. if (!validChecksum(part = partnum))
  124. continue;
  125. for (int w = 0; w < 3; w++)
  126. setWord(w, partnum, mac[w]);
  127. cmd_setchecksum();
  128. }
  129. }
  130. int
  131. macAddress(const char *strMac, uint16_t *mac)
  132. {
  133. uint64_t total = 0;
  134. if (strnlen(strMac, 20) == 17) {
  135. for (uint8_t h, i = 0; i < 16; i += 3) {
  136. if (i != 15)
  137. if (strMac[i + 2] != ':')
  138. return 1;
  139. int byte = i / 3;
  140. for (int nib = 0; nib < 2; nib++, total += h) {
  141. if ((h = hextonum(strMac[i + nib])) > 15)
  142. return 1;
  143. if ((byte == 0) && (nib == 1))
  144. if (strMac[i + nib] == '?')
  145. h = (h & 0xE) | 2; /* local, unicast */
  146. mac[byte >> 1] |= ((uint16_t ) h)
  147. << ((8 * (byte % 2)) + (4 * (nib ^ 1)));
  148. }
  149. }}
  150. return ((total == 0) | (mac[0] & 1)); /* multicast/all-zero banned */
  151. }
  152. uint8_t
  153. hextonum(char ch)
  154. {
  155. if ((ch >= '0') && (ch <= '9'))
  156. return ch - '0';
  157. else if ((ch >= 'A') && (ch <= 'F'))
  158. return ch - 'A' + 10;
  159. else if ((ch >= 'a') && (ch <= 'f'))
  160. return ch - 'a' + 10;
  161. return (ch == '?') ? rhex() : 16;
  162. }
  163. uint8_t
  164. rhex(void)
  165. {
  166. static uint8_t n = 0, rnum[16];
  167. if (!n)
  168. err_if(pread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0) == -1);
  169. return rnum[n--] & 0xf;
  170. }
  171. void
  172. cmd_dump(void)
  173. {
  174. for (int partnum = 0, numInvalid = 0; partnum < 2; partnum++) {
  175. if (!validChecksum(partnum))
  176. ++numInvalid;
  177. printf("MAC (part %d): ", partnum);
  178. showmac(partnum), hexdump(partnum);
  179. errno = ((numInvalid < 2) && (partnum)) ? 0 : errno;
  180. }
  181. }
  182. void
  183. showmac(int partnum)
  184. {
  185. for (int c = 0; c < 3; c++) {
  186. uint16_t val16 = word(c, partnum);
  187. printf("%02x:%02x", val16 & 0xff, val16 >> 8);
  188. printf(c == 2 ? "\n" : ":");
  189. }
  190. }
  191. void
  192. hexdump(int partnum)
  193. {
  194. for (int row = 0; row < 8; row++) {
  195. printf("%07x", row << 4);
  196. for (int c = 0; c < 8; c++) {
  197. uint16_t val16 = word((row << 3) + c, partnum);
  198. printf(" %02x%02x", val16 >> 8, val16 & 0xff);
  199. } printf("\n");
  200. }
  201. }
  202. void
  203. cmd_setchecksum(void)
  204. {
  205. uint16_t val16 = 0;
  206. for (int c = 0; c < 0x3F; c++)
  207. val16 += word(c, part);
  208. setWord(0x3F, part, 0xBABA - val16);
  209. }
  210. void
  211. cmd_brick(void)
  212. {
  213. if (validChecksum(part))
  214. setWord(0x3F, part, ((word(0x3F, part)) ^ 0xFF));
  215. }
  216. void
  217. cmd_copy(void)
  218. {
  219. if ((gbeFileModified = nvmPartModified[part ^ 1] = validChecksum(part)))
  220. gbe[part ^ 1] = gbe[part]; /* speedhack: copy ptr, not words */
  221. }
  222. int
  223. validChecksum(int partnum)
  224. {
  225. uint16_t total = 0;
  226. for(int w = 0; w <= 0x3F; w++)
  227. total += word(w, partnum);
  228. if (total == 0xBABA)
  229. return 1;
  230. fprintf(stderr, "WARNING: BAD checksum in part %d\n", partnum);
  231. return (errno = ECANCELED) & 0;
  232. }
  233. void
  234. writeGbeFile(void)
  235. {
  236. err_if((cmd == writeGbeFile) && !(validChecksum(0) || validChecksum(1)));
  237. for (int p = 0, x = (cmd == writeGbeFile) ? 1 : 0; p < 2; p++) {
  238. if ((!nvmPartModified[p]) && (cmd != writeGbeFile))
  239. continue;
  240. handle_endianness(p^x);
  241. err_if(pwrite(fd, (uint8_t *) gbe[p^x], nf, p << 12) == -1);
  242. }
  243. errno = 0;
  244. err_if(close(fd) == -1);
  245. }
  246. void
  247. handle_endianness(int partnum)
  248. {
  249. uint8_t *n = (uint8_t *) gbe[partnum];
  250. for (size_t w = nf * ((uint8_t *) &e)[0], x = 1; w < nf; w += 2, x += 2)
  251. n[w] ^= n[x], n[x] ^= n[w], n[w] ^= n[x];
  252. }