diff options
author | Leah Rowe <leah@libreboot.org> | 2023-01-27 14:17:30 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2023-01-27 14:18:46 +0000 |
commit | ef84329a81d9082e23aafc2f1a06599d7b3611f5 (patch) | |
tree | 1d3120c5c2a92ed345404721d6e1a0779f6ebcf0 /util | |
parent | 88a51531cf247a2825ad1a5496201972eb3eabc1 (diff) |
util/nvmutil: optimise rhex() for speed
don't constantly open/close the file: /dev/urandom
only read 12 bytes at a time
because of this change, the readFromFile() function now only
handles gbe files
Diffstat (limited to 'util')
-rw-r--r-- | util/nvmutil/nvmutil.c | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index ef89876b..e2dafec7 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -32,8 +32,8 @@ #include <dirent.h> #include <err.h> -ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, - size_t size); +ssize_t readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, + size_t nr); void cmd_setmac(const char *strMac); uint8_t hextonum(char chs); uint8_t rhex(void); @@ -119,7 +119,7 @@ main(int argc, char *argv[]) if ((strMac == NULL) && (cmd == NULL)) errno = EINVAL; - else if (readFromFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) + else if (readGbeFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) goto nvmutil_exit; if (errno == 0) { @@ -141,31 +141,29 @@ nvmutil_exit: } ssize_t -readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size) +readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, size_t nr) { struct stat st; if (opendir(path) != NULL) { errno = EISDIR; return -1; - } else if (((*fd) = open(path, flags)) == -1) { + } + if (((*fd) = open(path, flags)) == -1) { + return -1; + } + if (fstat((*fd), &st) == -1) + return -1; + if ((st.st_size != SIZE_8KB)) { + fprintf(stderr, "%s: Bad file size\n", path); + errno = ECANCELED; return -1; - } else if (size == SIZE_8KB) { - if (fstat((*fd), &st) == -1) - return -1; - if ((st.st_size != SIZE_8KB) && strcmp(path, "/dev/urandom")) { - fprintf(stderr, "Bad file size\n"); - errno = ECANCELED; - return -1; - } } - if (errno == ENOTDIR) errno = 0; - else if (errno != 0) + if (errno != 0) return -1; - - return read((*fd), buf, size); + return read((*fd), buf, nr); } void @@ -251,23 +249,17 @@ rhex(void) { static int rfd = -1; static uint8_t *rbuf = NULL; - static size_t rindex = BUFSIZ; - int bsize = BUFSIZ; + static size_t rindex = 12; - if (rindex == bsize) { + if (rindex == 12) { rindex = 0; if (rbuf == NULL) - if ((rbuf = (uint8_t *) malloc(bsize)) == NULL) + if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL) err(errno, NULL); - if (rfd != -1) { - if (close(rfd)) + if (rfd == -1) + if ((rfd = open("/dev/urandom", O_RDONLY)) == -1) err(errno, "/dev/urandom"); - rfd = -1; - } - if (readFromFile(&rfd, rbuf, "/dev/urandom", O_RDONLY, bsize) - != bsize) - err(errno, "/dev/urandom"); - if (errno != 0) + if (read(rfd, rbuf, 12) == -1) err(errno, "/dev/urandom"); } |