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 
72 {
73 public:
84  bool add(const String& name, SysType sysType, storage_size_t offset, storage_size_t size,
85  const Uuid& uniqueGuid = {}, const Uuid& typeGuid = {})
86  {
87  auto part = new PartInfo(
88  name, fatTypes[sysType] ? Partition::SubType::Data::fat : Partition::SubType::Data::any, offset, size, 0);
89  if(part == nullptr) {
90  return false;
91  }
92  part->systype = sysType;
93  part->typeGuid = typeGuid ?: GPT::PARTITION_BASIC_DATA_GUID;
94  if(uniqueGuid) {
95  part->uniqueGuid = uniqueGuid;
96  } else {
97  part->uniqueGuid.generate();
98  }
99  return BasePartitionTable::add(part);
100  }
101 
112  bool add(const String& name, Partition::FullType type, storage_size_t offset, storage_size_t size,
113  const Uuid& uniqueGuid = {})
114  {
115  auto part = new PartInfo(name, type, offset, size, 0);
116  if(part == nullptr) {
117  return false;
118  }
119 
120  if(type == Partition::SubType::Data::fat) {
121  part->typeGuid = GPT::PARTITION_BASIC_DATA_GUID;
122  } else {
123  part->typeGuid = SmingTypeGuid(type);
124  }
125  if(uniqueGuid) {
126  part->uniqueGuid = uniqueGuid;
127  } else {
128  part->uniqueGuid.generate();
129  }
130  return BasePartitionTable::add(part);
131  }
132 };
133 
137 String getTypeName(const Uuid& typeGuid);
138 
139 } // namespace GPT
140 
147 Error formatDisk(BlockDevice& device, GPT::PartitionTable& table, const Uuid& diskGuid = {});
148 
149 } // namespace Storage::Disk
#define PROGMEM
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:26
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
#define EFI_PARTITION_TYPE_GUID_MAP(XX)
Definition: GPT.h:30
bool add(ObjectType *object)
Definition: LinkedObjectList.h:132
Common type for MBR/GPT partition table.
Definition: PartInfo.h:129
Base class for sector-addressable (block) devices.
Definition: BlockDevice.h:34
bool add(const String &name, Partition::FullType type, storage_size_t offset, storage_size_t size, const Uuid &uniqueGuid={})
Add a new GPT partition for a regular Sming filing system.
Definition: GPT.h:112
bool add(const String &name, SysType sysType, storage_size_t offset, storage_size_t size, const Uuid &uniqueGuid={}, const Uuid &typeGuid={})
Add a new standard GPT partition definition.
Definition: GPT.h:84
Type
Definition: Partition.h:88
The String class.
Definition: WString.h:133
String getTypeName(const Uuid &typeGuid)
Get string for known GPT type GUIDs.
Definition: Partition.h:78
SysType
Identifies exact disk volume type.
Definition: PartInfo.h:40
Error
Definition: Libraries/DiskStorage/src/include/Storage/Disk/Error.h:37
Error formatDisk(BlockDevice &device, GPT::PartitionTable &table, const Uuid &diskGuid={})
Partition a device using the GPT scheme.
static constexpr SysTypes fatTypes
Definition: PartInfo.h:50
static constexpr Uuid PROGMEM baseguid
Definition: GPT.h:42
SmingTypeGuid()
Definition: GPT.h:44
SmingTypeGuid(Partition::FullType fulltype)
Definition: GPT.h:48
static Partition::FullType match(const Uuid &guid)
Definition: GPT.h:54
In-memory partition information.
Definition: PartInfo.h:102
Express both partition type and subtype together.
Definition: Partition.h:139
uint8_t subtype
Definition: Partition.h:141
Type type
Definition: Partition.h:140
Class for manipulating UUID (aka GUID) entities.
Definition: Uuid.h:26
bool generate(MacAddress mac)
Generate a UUID using a MAC node address.
uint8_t node[6]
Definition: Uuid.h:32