summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
AgeCommit message (Collapse)Author
2023-06-01util/nvmutil: move non-functions to nvmutil.hLeah Rowe
Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-06-01util/nvmutil: use even more macros (code cleanup)Leah Rowe
Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-06-01util/nvmutil: remove unnecessary parenthesesLeah Rowe
2023-06-01util/nvmutil: simplify setWord() with word() macroLeah Rowe
There is nothing cooler than a macro. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-06-01util/nvmutil: do xor swap in a macroLeah Rowe
eventually, everything will be a macro! Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-06-01util/nvmutil pledge,unveil: use correct err stringLeah Rowe
2023-06-01util/nvmutil: ensure that errno is set on err()Leah Rowe
When err() is called, it is intended that nvmutil will always exit with non-zero status, but with errno as the return value. Ensure that errno is *not* zero. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-06-01util/nvmutil: minor code cleanupLeah Rowe
Make word() a macro, simplify err_if(). Could also make setWord() a macro if I forego certain optimisations, but I'll leave it as-is. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-31util/nvmutil: simplified error handling in mainLeah Rowe
This change also reduces code indentation. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-31util/nvmutil: Use unveil, and harden pledgesLeah Rowe
After /dev/urandom (for MAC address randomisation) and the GbE file have been handled, unveil them. Unveil is a system call provided by OpenBSD that, when called, restricts access only to the files and/or directories specified, each given specific permissions. You can learn more about unveil here: https://man.openbsd.org/unveil.2 An ifdef rule makes nvmutil only use unveil on OpenBSD, because it's not available anywhere else. This is the same as with the pledge() system call. Where invalid arguments are given, and no action performed, pledge promises are also reduced to just stdio, preventing any writes to files, or reads from files. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-31util/nvmutil: Harden pledge promisesLeah Rowe
After reading a file, remove rpath. When removing rpath, also remove wpath if flags are not to O_RDONLY (read-only disk operation). When wpath is permitted, and a file was successfully written, remove wpath. In order to permit /dev/urandom access in rhex(), I call it as a void just before re-calling pledge. The rhex() function has been written in such a way that /dev/urandom only needs to be read *once*. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-31util/nvmutil: Simplify use of pledge (on OpenBSD)Leah Rowe
Define xpledge which calls pledge and handles errors. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-30util/nvmutil: Use correct pledge promise (OpenBSD)Leah Rowe
I assumed wpath was all that's needed, but this simply allows writes. rpath must be specified alongside wpath, for reads. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-05-30util/*: Properly detect OpenBSD for pledge() callLeah Rowe
The utils that are pledged checked HAVE_PLEDGE which was bogus. OpenBSD defines __OpenBSD__, which you can check for in ifdef. This change makes nvmutil and spkmodem-recv *actually* use pledge, when the utils are compiled on OpenBSD. Signed-off-by: Leah Rowe <leah@libreboot.org>
2023-04-15util/nvmutil: sort includes alphabeticallyLeah Rowe
small nitpick, but i try to use openbsd style since i like that style. upon further reading of their style guidelines today, it was revealed to me that for includes, they: * sort sys/ includes alphabetically, at the top * after sys/ includes, have an empty line * includes for networking-related headers below that * empty space below networking headers if there * after that, have the rest of the includes, sorted alphabetically at least, that is my understanding. i have to admit, it does look cleaner not really that critical but why not do it?
2023-04-11util/nvmutil: don't display errant whitespaceLeah Rowe
At the end of each line is an errant space. Fix that.
2023-04-07util/nvmutil: even crazier code size reductionLeah Rowe
2023-04-07util/nvmutil: yet more code size optimisationLeah Rowe
2023-04-07util/nvmutil: more code size optimisationsLeah Rowe
2023-04-07util/nvmutil: minor code size optimisationLeah Rowe
not just sloccount, but compiled binary size as tested with tcc on an x86_64 host
2023-04-07util/nvmutil: fix possible regressionLeah Rowe
i went too hard on the sloc reductions a check inside a for loop could cause incomplete reading of gbe images revert that
2023-04-06util/nvmutil: minor code cleanupLeah Rowe
also removed some unnecessary checks fixed the check of pwrite's return value (it should check for -1)
2023-04-06util/nvmutil: remove unnecessary else statementLeah Rowe
2023-04-06util/nvmutil: less annoying commentsLeah Rowe
added a few that were more useful deleted a few obnoxious ones
2023-04-06util/nvmutil: remove duplicated logicLeah Rowe
the byteswap() function is used for big endian host compatibility, but it can also be used to swap words in the stored mac address
2023-04-06util/nvmutil: one more commentLeah Rowe
2023-04-06util/nvmutil: add useful code commentsLeah Rowe
2023-04-06util/nvmutil: remove unnecessary debug messagesLeah Rowe
these were put in when i was testing the feature to limit read/written bytes in loading/saving of files
2023-04-06util/nvmutil: serious re-factoring, part 2Leah Rowe
the code is smaller
2023-04-06util/nvmutil: consolidated error messageLeah Rowe
2023-04-06util/nvmutil: serious re-factoring (speed boost!)Leah Rowe
word/setWord no longer mitigates endianness. instead, all bytes are swapped after reading and before writing the file, and only if the host is big endian this improves performance on little endian hosts, which is most machines, and the code is much simpler, so it's more robust and less likely to break mac address endianness made more clear in code, including with a comment that explains it (the nvm section contains little endian words, *except* the mac address whose words are stored big endian)
2023-04-05util/nvmutil: fix faulty zeroes-mac-address checkLeah Rowe
it was resetting the total for each nibble. absolute epic fail on my part. fixed now.
2023-04-05util/nvmutil: minor code cleanupLeah Rowe
2023-04-05util/nvmutil: move mac address parsing to functionLeah Rowe
2023-03-06util/nvmutil: optimise rhex() furtherLeah Rowe
reduce the number of calls to read() by using bit shifts. when rnum is zero, read again. in most cases, a nibble will not be zero, so this will usually result in about 13-15 of of 16 nibbles being used. this is in comparison to 8 nibbles being used before, which means that the number of calls to read() are roughly halved. at the same time, the extra amount of logic is minimal (and probably less) when compiled, outside of calls to read(), because shifting is better optimised (on 64-bit machines, the uint64_t will be shifted with just a single instruction, if the compiler is decent), whereas the alternative would be to always precisely use exactly 16 nibbles by counting up to 16, which would involve the use of an and mask and still need a shift, plus... you get the point. this is probably the most efficient code ever written, for generating random numbers between the value of 0 and 15
2023-01-28util/nvmutil: tidy up variable declarationsLeah Rowe
2023-01-28util/nvmutil: setWord(): declare variables firstLeah Rowe
2023-01-28util/nvmutil: reset errno if any write attemptedLeah Rowe
the way nvmutil is designed, setWord() is only ever called under non-error conditions. however, if one part is valid but the other one isn't, and a command is run that touches both parts, errno is non-zero write writeGbeFile is called in situations where one part is valid, but the other isn't, AND the writes to gbe (in memory) results in a non-change, writeGbeFile is not called; in this situation, errno is not being reset, despite non-error condition this patch fixed the bug, resulting in zero status upon exit under such conditions
2023-01-28util/nvmutil: do not write non-changes to diskLeah Rowe
2023-01-28util/nvmutil: cmd_swap(): write sequentuallyLeah Rowe
the current code writes part 1 first, and part 0 next, on the disk, due to the way the swap works. with this change, swap still swaps the two parts of the file, on disk, but writes the new file sequentially. this change might speed up i/o on the file system, on HDDs. on SSDs, this change likely makes no difference at all.
2023-01-28util/nvmutil: don't use malloc()Leah Rowe
2023-01-28util/nvmutil: fix clang build errorsLeah Rowe
2023-01-28util/nvmutil: simplify rhex()Leah Rowe
don't use malloc(). instead, just load random bytes into a uint64_t
2023-01-27util/nvmutil: use gbe[] in word() and setword()Leah Rowe
this will make the code more flexible, if (when) i add changes that allow multiple commands to be used in a single run, on any given number of files
2023-01-27util/nvmutil: code cleanupLeah Rowe
2023-01-27util/nvmutil: call pledge() earlier, in main()Leah Rowe
2023-01-27util/nvmutil: remove unused #defineLeah Rowe
2023-01-27util/nvmutil: optimised disk readsLeah Rowe
only read the required number of bytes, per command
2023-01-27util/nvmutil: optimise cmd_swap()Leah Rowe
On many Lenovo GbE regions (in factory firmware), part 0 is invalid but part 1 is valid. This change means part 1 is checked first. If part 1 is valid, part 0 won't be checked at all (due to how most C compilers optimise). Most people are just going to extract the factory GbE file, modify it and re-insert it into the ROM image, so this causes a nice speedup.
2023-01-27util/nvmutil: optimise rhex() for speedLeah Rowe
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