Commit Graph

27 Commits

Author SHA1 Message Date
Michael Marineau f19f5cc49d gpt: include backup-in-sync check in revalidation 2018-03-29 22:18:54 -04:00
Michael Marineau 285368e375 gpt: always revalidate when recomputing checksums
This ensures all code modifying GPT data include the same sanity check
that repair does. If revalidation fails the status flags are left in the
appropriate state.
2018-03-29 22:18:54 -04:00
Michael Marineau f1f618740d gpt: selectively update fields during repair
Just a little cleanup/refactor to skip touching data we don't need to.
2018-03-29 22:18:54 -04:00
Michael Marineau 753dd92013 gpt: be more careful about relocating backup header
The header was being relocated without checking the new location is
actually safe. If the BIOS thinks the disk is smaller than the OS then
repair may relocate the header into allocated space, failing the final
validation check. So only move it if the disk has grown.

Additionally, if the backup is valid then we can assume its current
location is good enough and leave it as-is.
2018-03-29 22:18:54 -04:00
Michael Marineau 34652e500d gpt: check header and entries status bits together
Use the new status function which checks *_HEADER_VALID and
*_ENTRIES_VALID bits together. It doesn't make sense for the header and
entries bits to mismatch so don't allow for it.
2018-03-29 22:18:54 -04:00
Michael Marineau 38cc185319 gpt: allow repair function to noop
Simplifies usage a little.
2018-03-29 22:18:54 -04:00
Michael Marineau a794435ae9 gpt: use inline functions for checking status bits
This should prevent bugs like 6078f836 and 4268f3da.
2018-03-29 22:18:54 -04:00
Vito Caputo 2ed905dc03 gpt: add helper for picking a valid header
Eliminate some repetition in primary vs. backup header acquisition.
2018-03-29 22:18:54 -04:00
Michael Marineau 1d358a2061 gpt: prefer disk size from header over firmware
The firmware and the OS may disagree on the disk configuration and size.
Although such a setup should be avoided users are unlikely to know about
the problem, assuming everything behaves like the OS. Tolerate this as
best we can and trust the reported on-disk location over the firmware
when looking for the backup GPT. If the location is inaccessible report
the error as best we can and move on.
2018-03-29 22:18:54 -04:00
Michael Marineau 72b178950d gpt: fix partition table indexing and validation
Portions of the code attempted to handle the fact that GPT entries on
disk may be larger than the currently defined struct while others
assumed the data could be indexed by the struct size directly. This
never came up because no utility uses a size larger than 128 bytes but
for the sake of safety we need to do this by the spec.
2018-03-29 22:18:54 -04:00
Michael Marineau 3a3e45823d fix checking alternate_lba 2018-03-29 22:18:54 -04:00
Michael Marineau b1ef48849c gpt: properly detect and repair invalid tables
GPT_BOTH_VALID is 4 bits so simple a boolean check is not sufficient.
This broken condition allowed gptprio to trust bogus disk locations in
headers that were marked invalid causing arbitrary disk corruption.
2018-03-29 22:18:54 -04:00
Michael Marineau fa18d3a292 gpt: refuse to write to sector 0 2018-03-29 22:18:53 -04:00
Michael Marineau f6b89ec315 gpt: improve validation of GPT headers
Adds basic validation of all the disk locations in the headers, reducing
the chance of corrupting weird locations on disk.
2018-03-29 22:18:53 -04:00
Michael Marineau 99959fa2fb gpt: add verbose debug logging 2018-03-29 22:18:53 -04:00
Michael Marineau 6d4ea47541 gpt: do not use disk sizes GRUB will reject as invalid later on
GRUB assumes that no disk is ever larger than 1EiB and rejects
reads/writes to such locations. Unfortunately this is not conveyed in
the usual way with the special GRUB_DISK_SIZE_UNKNOWN value.
2018-03-29 22:18:53 -04:00
Alex Crawford 1545295ad4 gpt: add search by disk uuid command 2018-03-29 22:18:53 -04:00
Michael Marineau d1270a2ba3 gpt: clean up little-endian crc32 computation
- Remove problematic cast from *uint8_t to *uint32_t (alignment issue).
 - Remove dynamic allocation and associated error handling paths.
 - Match parameter ordering to existing grub_crypto_hash function.
2018-03-29 22:18:53 -04:00
Michael Marineau 67475f53e0 gpt: add search by partition label and uuid commands
Builds on the existing filesystem search code. Only for GPT right now.
2018-03-29 22:18:53 -04:00
Michael Marineau d9bdbc1048 gpt: move gpt guid printing function to common library 2018-03-29 22:18:52 -04:00
Michael Marineau f8f6f790aa gpt: split out checksum recomputation
For basic data modifications the full repair function is overkill.
2018-03-29 22:18:52 -04:00
Michael Marineau 5e1829d414 gpt: add write function and gptrepair command
The first hint of something practical, a command that can restore any of
the GPT structures from the alternate location. New test case must run
under QEMU because the loopback device used by the other unit tests does
not support writing.
2018-03-29 22:18:52 -04:00
Michael Marineau dab6fac705 gpt: add new repair function to sync up primary and backup tables. 2018-03-29 22:18:52 -04:00
Michael Marineau 3d066264ac gpt: consolidate crc32 computation code
The gcrypt API is overly verbose, wrap it up in a helper function to
keep this rather common operation easy to use.
2018-03-29 22:18:52 -04:00
Michael Marineau 94f04a532d gpt: record size of of the entries table
The size of the entries table will be needed later when writing it back
to disk. Restructure the entries reading code to flow a little better.
2018-03-29 22:18:52 -04:00
Michael Marineau c26743a145 gpt: rename misnamed header location fields
The header location fields refer to 'this header' and 'alternate header'
respectively, not 'primary header' and 'backup header'. The previous
field names are backwards for the backup header.
2018-03-29 22:18:52 -04:00
Michael Marineau f69a9e0fdc gpt: start new GPT module
This module is a new implementation for reading GUID Partition Tables
which is much stricter than the existing part_gpt module and exports GPT
data directly instead of the generic grub_partition structure. It will
be the basis for modules that need to read/write/update GPT data.

The current code does nothing more than read and verify the table.
2018-03-29 22:18:52 -04:00