Commit Graph

213 Commits (b0e5fc9d9c8f648b2b98c48d126cf8e6e828ddd4)

Author SHA1 Message Date
Leah Rowe 6b4a14ce4a util/nvmutil: tidy up variable declarations 2023-01-28 23:21:53 +00:00
Leah Rowe 031a0b553b util/nvmutil: setWord(): declare variables first 2023-01-28 22:40:01 +00:00
Leah Rowe 257eedca0c util/nvmutil: reset errno if any write attempted
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-28 22:14:35 +00:00
Leah Rowe adc76e3814 util/nvmutil: do not write non-changes to disk 2023-01-28 21:26:36 +00:00
Leah Rowe 3e150bf303 util/nvmutil: cmd_swap(): write sequentually
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-28 20:30:34 +00:00
Leah Rowe 7e3a73558e util/nvmutil: don't use malloc() 2023-01-28 19:39:34 +00:00
Leah Rowe a924d43bdd util/nvmutil: fix clang build errors 2023-01-28 14:11:17 +00:00
Leah Rowe c822033bee util/nvmutil: simplify rhex()
don't use malloc(). instead, just load random bytes
into a uint64_t
2023-01-28 12:24:50 +00:00
Leah Rowe 0f4852450c util/nvmutil: use gbe[] in word() and setword()
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-27 20:13:15 +00:00
Leah Rowe b1186968e8 util/nvmutil: code cleanup 2023-01-27 19:52:11 +00:00
Leah Rowe 7a98649764 util/nvmutil: call pledge() earlier, in main() 2023-01-27 15:34:09 +00:00
Leah Rowe bb6fe263e7 util/nvmutil: remove unused #define 2023-01-27 15:28:50 +00:00
Leah Rowe 5a5a8662a6 util/nvmutil: optimised disk reads
only read the required number of bytes, per command
2023-01-27 15:09:34 +00:00
Leah Rowe 24d5645676 util/nvmutil: optimise cmd_swap()
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-27 14:26:24 +00:00
Leah Rowe ef84329a81 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
2023-01-27 14:18:46 +00:00
Leah Rowe 88a51531cf util/nvmutil: code cleanup in rhex() 2023-01-27 13:54:01 +00:00
Leah Rowe afc80b89ec util/nvmutil: update copyright years 2023-01-17 12:48:14 +00:00
Leah Rowe 8242dca57b util/nvmutil: limit bytes written per command
Massive reduction in number of bytes written, if copy/swap
commands are not used.
2023-01-17 11:03:55 +00:00
Leah Rowe e398331b38 util/nvmutil: make writeGbeFile more readable 2023-01-17 10:52:45 +00:00
Leah Rowe 8dea350a62 util/nvmutil: only write parts that are modified
Old behaviour: always write both gbe sections.

New behaviour: only write back what was changed.
2023-01-17 10:23:21 +00:00
lbmkplaceholder d45b2e70dc util/nvmutil: use err() more consistently 2022-12-24 01:18:17 +00:00
Leah Rowe d726b16f5f util/nvmutil: more robust pointer handling
i didn't like the previous commits, they felt really hacky

running malloc and then changing the pointer directly just rubs
me the wrong way

fix that
2022-12-24 01:10:55 +00:00
lbmkplaceholder 448ee5105d util/nvmutil: optimise cmd_swap() further
don't do xor swap. we know gbe2 is always 4KB higher than
gbe in memory, so we can just set gbe2 to the value of gbe,
and OR the size in bytes of 4KB into gbe2

this is only a marginal speed boost, negligible even, but it's
done for the lulz
2022-12-23 10:42:19 +00:00
lbmkplaceholder effcb942ce util/nvmutil: greatly optimise cmd_copy()
similar to the last change by concept. we now write
individual 4KB blocks per part 0 and 1, at the end
of nvmutil, based on pointer values gbe and gbe2

instead of running memcpy, simply overwrite the pointer

this results in less I/O, thus more speed
2022-12-23 10:28:25 +00:00
lbmkplaceholder 6e5828e4a8 util/nvmutil: greatly optimise cmd_swap()
instead of XOR-swapping every byte, have pointers to the
two parts and *XOR swap the pointers*. at the end of the
program execution, when writing, pwrite the two parts into
the same file
2022-12-23 08:41:18 +00:00
lbmkplaceholder 6ebd178f28 util/nvmutil: simplified error handling in rhex() 2022-12-21 15:45:17 +00:00
lbmkplaceholder 04da953c71 util/nvmutil: return errno when calling err() 2022-12-21 15:31:02 +00:00
lbmkplaceholder 001878112a util/nvmutil: exit non-zero if close() fails 2022-12-21 15:28:15 +00:00
lbmkplaceholder ab2cfb8639 util/nvmutil: only mask random unicast/local macs
Without this change, arbitrary MAC addresses will always be masked.

This change restores the intended behaviour.
2022-12-14 08:15:07 +00:00
Leah Rowe b495aa0987 util/nvmutil: consistent parentheses on comparison 2022-12-08 21:34:19 +00:00
Leah Rowe 17fa25e5af util/nvmutil file reads: skip reading if errno!=0
*This condition will probably never be met, but it is theoretically
possible that the code could still fail at this point. Catch all errors,
and exit, ruthlessly.
2022-12-08 21:29:36 +00:00
Leah Rowe 27876c6421 util/nvmutil: return error when fstat() is -1
Another oversight in my error handling.
2022-12-08 21:20:53 +00:00
Leah Rowe 960af2d6e8 util/nvmutil: rhex(): fail if errno not zero
The code was only checking whether all of the bytes were read,
but there are other errors that can be caught via errno.

Enforce strict errno handling, when generating random
numbers for command `setmac`.
2022-12-07 22:30:55 +00:00
Leah Rowe 3d01cf28d6 util/nvmutil: minor code formatting cleanup 2022-12-05 03:26:18 +00:00
Leah Rowe a5e4416a14 util/nvmutil: remove errant line break 2022-12-03 12:43:13 +00:00
Leah Rowe c100dd1f81 util/nvmutil: missing paretheses on if statement 2022-12-03 12:30:13 +00:00
Leah Rowe 036d710776 util/nvmutil: don't initialise rbuf unless needed
previously, it was always initialised, but now it's only
initialised if '?' is used on a mac address character in
command `setmac`

this is done by simply moving mac address character
randomisation to a separate function
2022-12-03 12:28:20 +00:00
Leah Rowe 851892b464 util/nvmutil: rename variable in hextonum 2022-12-03 12:17:16 +00:00
Leah Rowe 0bf3f1ed61 util/nvmutil: don't reallocate memory in hextonum 2022-12-03 12:11:15 +00:00
Leah Rowe e5a46b464d util/nvmutil: dont report bad size if /dev/urandom 2022-12-03 12:00:25 +00:00
Leah Rowe ededa5ddda util/nvmutil: rename variables in hextonum 2022-12-03 11:58:07 +00:00
Leah Rowe e2e321fc20 util/nvmutil: use BUFSIZ for rmac size in hextonum
I will be using this function elsewhere, and in general
I want this to be usable for lots of programs.
2022-12-03 11:55:38 +00:00
Leah Rowe a6d0112d86 util/nvtutil: fix out of bounds error
the error would have never been triggered, because it never
went over 11, but if this code were to be copied elsewhere,
it would be problematic
2022-12-03 11:49:52 +00:00
Leah Rowe 85937f3f4c util/nvmutil: reset errno on cmd_swap
If one of the checksums was valid, but the other was not,
errno would be set to E_CANCELED, but then the buffer would
be modified anyway; this is acceptable behaviour, and errno
would later be reset writing the GBE file, which is done
only on the condition that the buffer was modified, but
it's also a good idea to reset it here just in case.

This is not a bugfix, and no behavioural changes will be
observed by the user, but this may *prevent* a bug in the
future, so let's pre-fix that bug now.
2022-12-01 13:16:05 +00:00
Leah Rowe e8eee6dd8a util/nvmutil: mild refactoring 2022-11-27 09:43:47 +00:00
Leah Rowe 342e5abe5e util/nvmutil: improved errno handling in main 2022-11-27 09:36:18 +00:00
Leah Rowe d7465efbb0 util/nvmutil: put hextonum in its own function 2022-11-27 09:29:37 +00:00
Leah Rowe 9e5ff5e4e6 util/nvmutil: move ENOTDIR check to function 2022-11-27 09:01:57 +00:00
Leah Rowe ff88cb1ac3 util/nvmutil: further improved errno handling 2022-11-27 00:48:37 +00:00
Leah Rowe b81b51f98b util/nvmutil: remove errant code 2022-11-27 00:39:06 +00:00
Leah Rowe a94bac81f3 util/nvmutil: improved error handling 2022-11-27 00:27:07 +00:00
Leah Rowe 55a951a718 util/nvmutil: fix off by one bug 2022-11-26 23:50:04 +00:00
Leah Rowe 0108615f37 nvmutil copy/swap: actually set nvmPartModified 2022-11-26 23:48:01 +00:00
Leah Rowe 82300f4f1e util/nvmutil: move cmd copy to own function 2022-11-26 23:42:45 +00:00
Leah Rowe ddf3b76c83 util/nvmutil: move cmd swap to own function 2022-11-26 23:34:13 +00:00
Leah Rowe c2ed251ca6 util/nvmutil: move cmd brick to own function 2022-11-26 23:29:41 +00:00
Leah Rowe eaad16edad util/nvmutil: cmd setchecksum in own function 2022-11-26 23:25:23 +00:00
Leah Rowe cea1beeac5 util/nvmutil: split "dump" into smaller functions 2022-11-26 23:19:57 +00:00
Leah Rowe 0ae00e881e util/nvmutil: re-factor to reduce code indentation 2022-11-26 11:26:07 +00:00
Leah Rowe 0bbd4f1f26 util/nvmutil: write gbe files in a function
in any C program, main() should not contain detailed logic.

ideally, the main() function should only be a skeleton, showing
the overall logic flow of the program. split writing gbe files
into a separate function, to satisfy this criteria.
2022-11-26 11:03:04 +00:00
Leah Rowe b0f9f47e9a util/nvmutil: human-friendly exit messages, part 2 2022-11-26 10:35:10 +00:00
Leah Rowe 6c12afa996 util/nvmutil: more human-friendly exit messages 2022-11-18 20:07:13 +00:00
Leah Rowe b3b3642fe2 assimilate nvmutil 2022-11-17 12:07:09 +00:00