1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "proto/blockdev.pb.h"
6
7#include <cstddef>
8#include <mos/types.h>
9#include <string>
10#include <vector>
11
12using blockdev_handle = mosrpc_blockdev_blockdev;
13
14namespace GPT
15{
16 struct Header
17 {
18 u64 signature;
19 u32 revision;
20 u32 header_size;
21 u32 header_crc32;
22 u32 reserved;
23 u64 current_lba;
24 u64 backup_lba;
25 u64 first_usable_lba;
26 u64 last_usable_lba;
27 u8 disk_guid[16];
28 u64 partition_table_lba;
29 u32 partition_count;
30 u32 partition_entry_size;
31 u32 partition_table_crc32;
32 u8 reserved2[420];
33 } __packed;
34
35 struct PartitionEntry
36 {
37 u8 type_guid[16];
38 u8 partition_guid[16];
39 u64 first_lba;
40 u64 last_lba;
41 u64 attributes;
42 // char name[0]; // UTF-16LE
43 } __packed;
44} // namespace GPT
45
46class GPTDisk
47{
48 public:
49 explicit GPTDisk(const blockdev_handle handle, const std::string &disk_name);
50 ~GPTDisk() = default;
51
52 bool initialise_gpt();
53
54 u32 get_partition_count() const;
55 GPT::PartitionEntry get_partition(size_t index) const;
56
57 size_t read_partition_block(size_t partition_index, u64 blockoffset, u8 *buffer, u32 nblocks);
58 size_t write_partition_block(size_t partition_index, u64 blockoffset, const u8 *buffer, u32 nblocks);
59
60 size_t get_block_size() const
61 {
62 return 512;
63 }
64
65 std::string name() const
66 {
67 return disk_name;
68 }
69
70 private:
71 bool disk_read_header();
72 bool disk_read_partitions();
73
74 private:
75 const blockdev_handle device_handle;
76 std::string disk_name;
77
78 bool ready = false;
79 GPT::Header header;
80 std::vector<GPT::PartitionEntry> partitions;
81};
82