1 /* 2 * Copyright 2007-2013, Axel Dörfler, axeld@pinc-software.de. 3 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch. All rights reserved. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 #ifndef GPT_HEADER_H 8 #define GPT_HEADER_H 9 10 11 #include "efi_gpt.h" 12 13 14 namespace EFI { 15 16 17 class Header { 18 public: 19 Header(int fd, uint64 lastBlock, 20 uint32 blockSize); 21 #ifndef _BOOT_MODE 22 // constructor for empty header 23 Header(uint64 lastBlock, uint32 blockSize); 24 #endif 25 ~Header(); 26 27 status_t InitCheck() const; 28 29 uint64 FirstUsableBlock() const 30 { return fHeader.FirstUsableBlock(); } 31 uint64 LastUsableBlock() const 32 { return fHeader.LastUsableBlock(); } 33 34 uint32 EntryCount() const 35 { return fHeader.EntryCount(); } 36 efi_partition_entry& EntryAt(int32 index) const 37 { return *(efi_partition_entry*)(fEntries 38 + fHeader.EntrySize() * index); } 39 40 #ifndef _BOOT_MODE 41 status_t WriteEntry(int fd, uint32 entryIndex); 42 status_t Write(int fd); 43 #endif 44 45 private: 46 #ifndef _BOOT_MODE 47 status_t _WriteHeader(int fd); 48 status_t _Write(int fd, off_t offset, const void* data, 49 size_t size) const; 50 void _UpdateCRC(); 51 void _UpdateCRC(efi_table_header& header); 52 #endif 53 54 status_t _Read(int fd, off_t offset, void* data, 55 size_t size) const; 56 bool _IsHeaderValid(const efi_table_header& header, 57 uint64 block); 58 bool _ValidateHeaderCRC(); 59 bool _ValidateEntriesCRC() const; 60 void _SetBackupHeaderFromPrimary(uint64 lastBlock); 61 size_t _EntryArraySize() const 62 { return fHeader.EntrySize() 63 * fHeader.EntryCount(); } 64 65 const char* _PrintGUID(const guid_t& id); 66 void _Dump(const efi_table_header& header); 67 void _DumpPartitions(); 68 69 private: 70 uint32 fBlockSize; 71 status_t fStatus; 72 efi_table_header fHeader; 73 efi_table_header fBackupHeader; 74 uint8* fEntries; 75 }; 76 77 78 } // namespace EFI 79 80 81 #endif // GPT_HEADER_H 82