nvmutil.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (c) 2022, 2023 Leah Rowe <info@minifree.org> */
  2. /* SPDX-License-Identifier: MIT */
  3. #include <sys/stat.h>
  4. #include <dirent.h>
  5. #include <err.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. void openFiles(const char *path);
  14. void readGbeFile(const char *path);
  15. void cmd_setmac(void);
  16. int invalidMacAddress(const char *strMac, uint16_t *mac);
  17. uint8_t hextonum(char chs);
  18. uint8_t rhex(void);
  19. void cmd_dump(void);
  20. void showmac(int partnum);
  21. void hexdump(int partnum);
  22. void cmd_setchecksum(void);
  23. void cmd_brick(void);
  24. void cmd_swap(void);
  25. void cmd_copy(void);
  26. int validChecksum(int partnum);
  27. void setWord(int pos16, int partnum, uint16_t val16);
  28. void xorswap_buf(int partnum);
  29. void writeGbeFile(const char *filename);
  30. #define FILENAME argv[1]
  31. #define COMMAND argv[2]
  32. #define MAC_ADDRESS argv[3]
  33. #define PARTNUM argv[3]
  34. #define SIZE_4KB 0x1000
  35. #define SIZE_8KB 0x2000
  36. uint16_t buf16[SIZE_4KB], mac[3] = {0, 0, 0};
  37. uint8_t *buf = (uint8_t *) &buf16;
  38. size_t nf = 128, gbe[2];
  39. uint8_t nvmPartModified[2] = {0, 0}, skipread[2] = {0, 0};
  40. int endian = 1, flags = O_RDWR, rfd, fd, part, gbeFileModified = 0;
  41. const char *strMac = NULL, *strRMac = "??:??:??:??:??:??";
  42. typedef struct op {
  43. char *str;
  44. void (*cmd)(void);
  45. int args;
  46. } op_t;
  47. op_t op[] = {
  48. { .str = "dump", .cmd = cmd_dump, .args = 3},
  49. { .str = "setmac", .cmd = cmd_setmac, .args = 3},
  50. { .str = "swap", .cmd = cmd_swap, .args = 3},
  51. { .str = "copy", .cmd = cmd_copy, .args = 4},
  52. { .str = "brick", .cmd = cmd_brick, .args = 4},
  53. { .str = "setchecksum", .cmd = cmd_setchecksum, .args = 4},
  54. };
  55. void (*cmd)(void) = NULL;
  56. #define ERR() errno = errno ? errno : ECANCELED
  57. #define err_if(x) if (x) err(ERR(), NULL)
  58. #define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
  59. if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
  60. if (fstat(f, &st) == -1) err(ERR(), "%s", l)
  61. #define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l)
  62. #define handle_endianness(r) if (((uint8_t *) &endian)[0] ^ 1) xorswap_buf(r)
  63. #define xpwrite(f, b, n, o, l) if (pwrite(f, b, n, o) == -1) err(ERR(), "%s", l)
  64. #define xclose(f, l) if (close(f) == -1) err(ERR(), "%s", l)
  65. #define xorswap(x, y) x ^= y, y ^= x, x ^= y
  66. #define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
  67. void
  68. xpledge(const char *promises, const char *execpromises)
  69. {
  70. (void)promises; (void)execpromises;
  71. #ifdef __OpenBSD__
  72. if (pledge(promises, execpromises) == -1)
  73. err(ERR(), "pledge");
  74. #endif
  75. }
  76. void
  77. xunveil(const char *path, const char *permissions)
  78. {
  79. (void)path; (void)permissions;
  80. #ifdef __OpenBSD__
  81. if (unveil(path, permissions) == -1)
  82. err(ERR(), "unveil");
  83. #endif
  84. }