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 "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 bool IsDirty() const; 29 FirstUsableBlock()30 uint64 FirstUsableBlock() const 31 { return fHeader.FirstUsableBlock(); } LastUsableBlock()32 uint64 LastUsableBlock() const 33 { return fHeader.LastUsableBlock(); } TableHeader()34 const gpt_table_header& TableHeader() const 35 { return fHeader; } 36 EntryCount()37 uint32 EntryCount() const 38 { return fHeader.EntryCount(); } EntryAt(int32 index)39 gpt_partition_entry& EntryAt(int32 index) const 40 { return *(gpt_partition_entry*)(fEntries 41 + fHeader.EntrySize() * index); } 42 43 #ifndef _BOOT_MODE 44 status_t WriteEntry(int fd, uint32 entryIndex); 45 status_t Write(int fd); 46 #endif 47 48 private: 49 #ifndef _BOOT_MODE 50 status_t _WriteHeader(int fd); 51 status_t _Write(int fd, off_t offset, const void* data, 52 size_t size) const; 53 void _UpdateCRC(); 54 void _UpdateCRC(gpt_table_header& header); 55 #endif 56 57 status_t _Read(int fd, off_t offset, void* data, 58 size_t size) const; 59 static bool _IsHeaderValid(gpt_table_header& header, 60 uint64 block); 61 static bool _ValidateHeaderCRC(gpt_table_header& header); 62 bool _ValidateEntriesCRC() const; 63 void _SetBackupHeaderFromPrimary(uint64 lastBlock); _EntryArraySize()64 size_t _EntryArraySize() const 65 { return fHeader.EntrySize() 66 * fHeader.EntryCount(); } 67 68 const char* _PrintGUID(const guid_t& id); 69 void _Dump(const gpt_table_header& header); 70 void _DumpPartitions(); 71 72 private: 73 uint32 fBlockSize; 74 status_t fStatus; 75 gpt_table_header fHeader; 76 gpt_table_header fBackupHeader; 77 uint8* fEntries; 78 bool fDirty; 79 }; 80 81 82 } // namespace EFI 83 84 85 #endif // GPT_HEADER_H 86