util/nvmutil: optimised disk reads

only read the required number of bytes, per command
fsdg20230625
Leah Rowe 2023-01-27 15:09:34 +00:00
parent 24d5645676
commit 5a5a8662a6
1 changed files with 41 additions and 25 deletions

View File

@ -32,7 +32,7 @@
#include <dirent.h> #include <dirent.h>
#include <err.h> #include <err.h>
ssize_t readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, void readGbeFile(int *fd, const char *path, int flags,
size_t nr); size_t nr);
void cmd_setmac(const char *strMac); void cmd_setmac(const char *strMac);
uint8_t hextonum(char chs); uint8_t hextonum(char chs);
@ -60,6 +60,7 @@ void writeGbeFile(int *fd, const char *filename);
uint8_t *buf = NULL; uint8_t *buf = NULL;
size_t gbe[2]; size_t gbe[2];
uint8_t skipread[2] = {0, 0};
int part, gbeFileModified = 0; int part, gbeFileModified = 0;
uint8_t nvmPartModified[2]; uint8_t nvmPartModified[2];
@ -71,6 +72,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd; int fd;
size_t nr = 128;
int flags = O_RDWR; int flags = O_RDWR;
char *strMac = NULL; char *strMac = NULL;
char *strRMac = "??:??:??:??:??:??"; char *strRMac = "??:??:??:??:??:??";
@ -119,18 +121,24 @@ main(int argc, char *argv[])
if ((strMac == NULL) && (cmd == NULL)) if ((strMac == NULL) && (cmd == NULL))
errno = EINVAL; errno = EINVAL;
else if (readGbeFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) if (errno != 0)
goto nvmutil_exit; goto nvmutil_exit;
if (errno == 0) { if ((cmd == &cmd_copy) || (cmd == &cmd_swap))
nr = SIZE_4KB;
if ((cmd == &cmd_copy) || (cmd == &cmd_setchecksum) ||
(cmd == &cmd_brick))
skipread[part ^ 1] = 1;
readGbeFile(&fd, FILENAME, flags, nr);
if (strMac != NULL) if (strMac != NULL)
cmd_setmac(strMac); cmd_setmac(strMac);
else if (cmd != NULL) else if (cmd != NULL)
(*cmd)(); (*cmd)();
if (gbeFileModified) if (gbeFileModified)
writeGbeFile(&fd, FILENAME); writeGbeFile(&fd, FILENAME);
}
nvmutil_exit: nvmutil_exit:
if (!((errno == ECANCELED) && (flags == O_RDONLY))) if (!((errno == ECANCELED) && (flags == O_RDONLY)))
@ -140,30 +148,38 @@ nvmutil_exit:
return errno; return errno;
} }
ssize_t void
readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, size_t nr) readGbeFile(int *fd, const char *path, int flags, size_t nr)
{ {
struct stat st; struct stat st;
int r, tr = 0;
int p;
if (opendir(path) != NULL) { if (opendir(path) != NULL)
errno = EISDIR; err(errno = EISDIR, path);
return -1; if (((*fd) = open(path, flags)) == -1)
} err(errno, path);
if (((*fd) = open(path, flags)) == -1) {
return -1;
}
if (fstat((*fd), &st) == -1) if (fstat((*fd), &st) == -1)
return -1; err(errno, path);
if ((st.st_size != SIZE_8KB)) { if ((st.st_size != SIZE_8KB)) {
fprintf(stderr, "%s: Bad file size\n", path); fprintf(stderr, "%s: Bad file size (must be 8KiB)\n", path);
errno = ECANCELED; err(errno = ECANCELED, NULL);
return -1;
} }
if (errno == ENOTDIR) if (errno == ENOTDIR)
errno = 0; errno = 0;
if (errno != 0) if (errno != 0)
return -1; err(errno, path);
return read((*fd), buf, nr);
for (p = 0; p < 2; p++) {
if (skipread[p])
continue;
if ((r = pread((*fd), (uint8_t *) gbe[p], nr, p << 12)) == -1)
err(errno, path);
tr += r;
}
printf("%d bytes read from file: `%s`\n", tr, path);
} }
void void