| 1 | #ifndef LIB__PART_H__ |
| 2 | #define LIB__PART_H__ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <lib/guid.h> |
| 8 | #if defined (UEFI) |
| 9 | # include <efi.h> |
| 10 | # include <crypt/blake2b.h> |
| 11 | #endif |
| 12 | |
| 13 | #define NO_PARTITION (-1) |
| 14 | #define INVALID_TABLE (-2) |
| 15 | #define END_OF_TABLE (-3) |
| 16 | |
| 17 | struct volume { |
| 18 | #if defined (UEFI) |
| 19 | EFI_HANDLE efi_handle; |
| 20 | |
| 21 | // Block storage |
| 22 | EFI_HANDLE efi_part_handle; |
| 23 | EFI_BLOCK_IO *block_io; |
| 24 | |
| 25 | // PXE |
| 26 | EFI_PXE_BASE_CODE_PROTOCOL *pxe_base_code; |
| 27 | |
| 28 | bool unique_sector_valid; |
| 29 | uint8_t unique_sector_b2b[BLAKE2B_OUT_BYTES]; |
| 30 | #elif defined (BIOS) |
| 31 | int drive; |
| 32 | #endif |
| 33 | |
| 34 | size_t fastest_xfer_size; |
| 35 | |
| 36 | int index; |
| 37 | |
| 38 | bool is_optical; |
| 39 | bool pxe; |
| 40 | |
| 41 | int partition; |
| 42 | int sector_size; |
| 43 | struct volume *backing_dev; |
| 44 | |
| 45 | int max_partition; |
| 46 | |
| 47 | int cache_status; |
| 48 | uint8_t *cache; |
| 49 | uint64_t cached_block; |
| 50 | |
| 51 | uint64_t first_sect; |
| 52 | uint64_t sect_count; |
| 53 | |
| 54 | bool guid_valid; |
| 55 | struct guid guid; |
| 56 | bool part_guid_valid; |
| 57 | struct guid part_guid; |
| 58 | bool fslabel_valid; |
| 59 | char *fslabel; |
| 60 | }; |
| 61 | |
| 62 | bool is_valid_mbr(struct volume *volume); |
| 63 | |
| 64 | extern struct volume **volume_index; |
| 65 | extern size_t volume_index_i; |
| 66 | |
| 67 | bool gpt_get_guid(struct guid *guid, struct volume *volume); |
| 68 | uint32_t mbr_get_id(struct volume *volume); |
| 69 | |
| 70 | int part_get(struct volume *part, struct volume *volume, int partition); |
| 71 | |
| 72 | struct volume *volume_get_by_guid(struct guid *guid); |
| 73 | struct volume *volume_get_by_fslabel(char *fslabel); |
| 74 | struct volume *volume_get_by_coord(bool optical, int drive, int partition); |
| 75 | #if defined (BIOS) |
| 76 | struct volume *volume_get_by_bios_drive(int drive); |
| 77 | #endif |
| 78 | |
| 79 | bool volume_read(struct volume *part, void *buffer, uint64_t loc, uint64_t count); |
| 80 | |
| 81 | #define volume_iterate_parts(_VOLUME_, _BODY_) do { \ |
| 82 | struct volume *_VOLUME = _VOLUME_; \ |
| 83 | if (_VOLUME->pxe) { \ |
| 84 | do { \ |
| 85 | struct volume *_PART = _VOLUME; \ |
| 86 | _BODY_ \ |
| 87 | } while (0); \ |
| 88 | } else { \ |
| 89 | while (_VOLUME->backing_dev != NULL) { \ |
| 90 | _VOLUME = _VOLUME->backing_dev; \ |
| 91 | } \ |
| 92 | \ |
| 93 | int _PART_CNT = -1; \ |
| 94 | for (size_t _PARTNO = 0; ; _PARTNO++) { \ |
| 95 | if (_PART_CNT > _VOLUME->max_partition) \ |
| 96 | break; \ |
| 97 | \ |
| 98 | struct volume *_PART = volume_get_by_coord(_VOLUME->is_optical, \ |
| 99 | _VOLUME->index, _PARTNO); \ |
| 100 | if (_PART == NULL) \ |
| 101 | continue; \ |
| 102 | \ |
| 103 | _PART_CNT++; \ |
| 104 | \ |
| 105 | _BODY_ \ |
| 106 | } \ |
| 107 | } \ |
| 108 | } while (0) |
| 109 | |
| 110 | #endif |