GPT.h
Go to the documentation of this file.
1 /****
2  * GPT.h
3  *
4  * Copyright 2022 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the DiskStorage Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "PartInfo.h"
23 #include "Error.h"
24 #include "BlockDevice.h"
25 
26 namespace Storage::Disk
27 {
28 namespace GPT
29 {
30 #define EFI_PARTITION_TYPE_GUID_MAP(XX) \
31  XX(PARTITION_SYSTEM, 0xC12A7328, 0xF81F, 0x11d2, 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B) \
32  XX(LEGACY_MBR_PARTITION, 0x024DEE41, 0x33E7, 0x11d3, 0x9D, 0x69, 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F) \
33  XX(PARTITION_MSFT_RESERVED, 0xE3C9E316, 0x0B5C, 0x4DB8, 0x81, 0x7D, 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE) \
34  XX(PARTITION_BASIC_DATA, 0xEBD0A0A2, 0xB9E5, 0x4433, 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7) \
35  XX(PARTITION_LINUX_RAID, 0xa19d880f, 0x05fc, 0x4d3b, 0xa0, 0x06, 0x74, 0x3f, 0x0f, 0x84, 0x91, 0x1e) \
36  XX(PARTITION_LINUX_SWAP, 0x0657fd6d, 0xa4ab, 0x43c4, 0x84, 0xe5, 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f) \
37  XX(PARTITION_LINUX_LVM, 0xe6d6d379, 0xf507, 0x44c2, 0xa2, 0x3c, 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28) \
38  XX(PARTITION_LINUX_DATA, 0x0fc63daf, 0x8483, 0x4772, 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4)
39 
40 // Base GUID type for regular Sming partition types; node[0..1] indicate type and subtype
41 struct SmingTypeGuid : public Uuid {
42  static constexpr Uuid PROGMEM baseguid{0x3cd54234, 0xcb54, 0x4ed5, 0xbc, 0x8b, 0x00, 0x00, 0x7d, 0x42, 0x84, 0x70};
43 
45  {
46  }
47 
49  {
50  node[0] = uint8_t(fulltype.type);
51  node[1] = fulltype.subtype;
52  }
53 
54  static Partition::FullType match(const Uuid& guid)
55  {
56  auto tmp = guid;
57  Partition::FullType ft{Partition::Type(tmp.node[0]), tmp.node[1]};
58  tmp.node[0] = 0;
59  tmp.node[1] = 0;
60  if(baseguid != tmp) {
61  ft = Partition::FullType{};
62  }
63  return ft;
64  }
65 };
66 
67 #define XX(name, ...) extern const Uuid name##_GUID;
69 #undef XX
70 
71 class PartitionTable : public BasePartitionTable
72 {
73 public:
85  bool add(const String& name, SysType sysType, storage_size_t offset, storage_size_t size,
86  const Uuid& uniqueGuid = {}, const Uuid& typeGuid = {}, Partition::Flags flags = 0)
87  {
88  auto part = new PartInfo(
89  name, fatTypes[sysType] ? Partition::SubType::Data::fat : Partition::SubType::Data::any, offset, size, 0);
90  if(part == nullptr) {
91  return false;
92  }
93  part->systype = sysType;
94  part->typeGuid = typeGuid ?: GPT::PARTITION_BASIC_DATA_GUID;
95  if(uniqueGuid) {
96  part->uniqueGuid = uniqueGuid;
97  } else {
98  part->uniqueGuid.generate();
99  }
100  return BasePartitionTable::add(part);
101  }
102 
114  bool add(const String& name, Partition::FullType type, storage_size_t offset, storage_size_t size,
115  const Uuid& uniqueGuid = {}, Partition::Flags flags = 0)
116  {
117  auto part = new PartInfo(name, type, offset, size, 0);
118  if(part == nullptr) {
119  return false;
120  }
121 
122  if(type == Partition::SubType::Data::fat) {
123  part->typeGuid = GPT::PARTITION_BASIC_DATA_GUID;
124  } else {
125  part->typeGuid = SmingTypeGuid(type);
126  }
127  if(uniqueGuid) {
128  part->uniqueGuid = uniqueGuid;
129  } else {
130  part->uniqueGuid.generate();
131  }
132  return BasePartitionTable::add(part);
133  }
134 };
135 
139 String getTypeName(const Uuid& typeGuid);
140 
141 } // namespace GPT
142 
149 Error formatDisk(BlockDevice& device, GPT::PartitionTable& table, const Uuid& diskGuid = {});
150 
151 } // namespace Storage::Disk
#define PROGMEM
Place entity into flash memory.
Definition: Arch/Esp8266/Components/libc/include/sys/pgmspace.h:36
Type type
Definition: Partition.h:140
Error
Definition: Libraries/DiskStorage/src/include/Storage/Disk/Error.h:54
Error formatDisk(BlockDevice &device, GPT::PartitionTable &table, const Uuid &diskGuid={})
Partition a device using the GPT scheme.
static Partition::FullType match(const Uuid &guid)
Definition: GPT.h:88
The String class.
Definition: WString.h:136
BitSet< uint32_t, Flag > Flags
Definition: Partition.h:134
bool add(ObjectType *object)
Definition: LinkedObjectList.h:134
Definition: Partition.h:77
SysType
Identifies exact disk volume type.
Definition: PartInfo.h:57
Express both partition type and subtype together.
Definition: Partition.h:139
SmingTypeGuid()
Definition: GPT.h:78
uint8_t subtype
Definition: Partition.h:141
static constexpr SysTypes fatTypes
Definition: PartInfo.h:67
uint8_t node[6]
Definition: Uuid.h:42
String getTypeName(const Uuid &typeGuid)
Get string for known GPT type GUIDs.
static constexpr Uuid PROGMEM baseguid
Definition: GPT.h:76
Class for manipulating UUID (aka GUID) entities.
Definition: Uuid.h:26
#define EFI_PARTITION_TYPE_GUID_MAP(XX)
Definition: GPT.h:64
Type
Definition: Partition.h:88
bool generate(MacAddress mac)
Generate a UUID using a MAC node address.
In-memory partition information.
Definition: PartInfo.h:119
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
bool add(const String &name, SysType sysType, storage_size_t offset, storage_size_t size, const Uuid &uniqueGuid={}, const Uuid &typeGuid={}, Partition::Flags flags=0)
Add a new standard GPT partition definition.
Definition: GPT.h:119