Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/RefPtr.h>
30#include <AK/Vector.h>
31#include <Kernel/Devices/DiskPartition.h>
32
33namespace Kernel {
34
35#define MBR_SIGNATURE 0xaa55
36#define MBR_PROTECTIVE 0xEE
37#define EBR_CHS_CONTAINER 0x05
38#define EBR_LBA_CONTAINER 0x0F
39
40struct [[gnu::packed]] MBRPartitionEntry
41{
42 u8 status;
43 u8 chs1[3];
44 u8 type;
45 u8 chs2[3];
46 u32 offset;
47 u32 length;
48};
49
50struct [[gnu::packed]] MBRPartitionHeader
51{
52 u8 code1[218];
53 u16 ts_zero;
54 u8 ts_drive, ts_seconds, ts_minutes, ts_hours;
55 u8 code2[216];
56 u32 disk_signature;
57 u16 disk_signature_zero;
58 MBRPartitionEntry entry[4];
59 u16 mbr_signature;
60};
61
62class MBRPartitionTable {
63 AK_MAKE_ETERNAL
64
65public:
66 explicit MBRPartitionTable(NonnullRefPtr<BlockDevice>);
67 ~MBRPartitionTable();
68
69 bool initialize();
70 bool is_protective_mbr() const;
71 bool contains_ebr() const;
72 RefPtr<DiskPartition> partition(unsigned index);
73
74private:
75 NonnullRefPtr<BlockDevice> m_device;
76
77 const MBRPartitionHeader& header() const;
78
79 u8 m_cached_header[512];
80};
81
82}