aboutsummaryrefslogtreecommitdiff
path: root/hikari/fs/afs/btree_adapter.zig
blob: d3ad9155a512a3790ac4e6e7f054a5d1c48d7403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Hikari AFS B-Tree

const efi = @import("../../efi/efi.zig");
const shared_afs = @import("../../../shared/fs/afs/afs.zig");
const block_io = @import("block_io.zig");

const EfiBlockContext = block_io.EfiBlockContext;

// Import shared types
const BTreeNodeDescriptor = shared_afs.BTreeNodeDescriptor;
const BTreeHeaderRecord = shared_afs.BTreeHeaderRecord;
const IndexKey = shared_afs.types.IndexKey;
const StackRecord = shared_afs.StackRecord;
const UnitRecord = shared_afs.UnitRecord;
const ThreadRecord = shared_afs.types.ThreadRecord;
const SpanDescriptor = shared_afs.SpanDescriptor;

// Import shared B-tree operations
const btree_ops = shared_afs.btree;
const constants = shared_afs.constants;

pub const BTreeError = error{
    ReadFailed,
    InvalidNode,
    InvalidHeader,
    KeyNotFound,
    TreeEmpty,
    AllocationFailed,
};

pub const BTree = struct {
    block_io: *efi.protocols.BlockIoProtocol,
    boot_services: *efi.services.BootServices,
    partition_start_lba: u64,
    cell_size: u32,
    base_span: SpanDescriptor,
    node_size: u32,
    root_node: u32,
    first_leaf: u32,
    last_leaf: u32,
    depth: u16,
    node_buffer: [*]u8,

    pub fn initialize(
        block_io_proto: *efi.protocols.BlockIoProtocol,
        boot_services: *efi.services.BootServices,
        partition_start_lba: u64,
        cell_size: u32,
        base_span: SpanDescriptor,
        header: *const BTreeHeaderRecord,
    ) BTreeError!BTree {
        var node_buffer: [*]align(8) u8 = undefined;
        const alloc_status = boot_services.allocate_pool(
            .loader_data,
            header.node_size,
            &node_buffer,
        );
        if (efi.types.is_error(alloc_status)) {
            return BTreeError.AllocationFailed;
        }

        return BTree{
            .block_io = block_io_proto,
            .boot_services = boot_services,
            .partition_start_lba = partition_start_lba,
            .cell_size = cell_size,
            .base_span = base_span,
            .node_size = header.node_size,
            .root_node = header.root_node,
            .first_leaf = header.first_leaf_node,
            .last_leaf = header.last_leaf_node,
            .depth = header.depth,
            .node_buffer = node_buffer,
        };
    }

    pub fn read_node(self: *BTree, node_number: u32) BTreeError!*BTreeNodeDescriptor {
        const cell_offset = btree_ops.get_node_cell(node_number, self.cell_size, self.node_size);
        const node_offset_in_cell = btree_ops.get_node_offset_in_cell(node_number, self.cell_size, self.node_size);

        const cell_lba = self.partition_start_lba +
            ((self.base_span.start_cell + cell_offset) * self.cell_size / self.block_io.media.block_size);

        var cell_buffer: [*]align(8) u8 = undefined;
        const alloc_status = self.boot_services.allocate_pool(
            .loader_data,
            self.cell_size,
            &cell_buffer,
        );
        if (efi.types.is_error(alloc_status)) {
            return BTreeError.AllocationFailed;
        }

        const read_status = self.block_io.read_blocks(
            self.block_io,
            self.block_io.media.media_id,
            cell_lba,
            self.cell_size,
            cell_buffer,
        );
        if (efi.types.is_error(read_status)) {
            return BTreeError.ReadFailed;
        }

        const node_ptr = cell_buffer + node_offset_in_cell;
        var i: u32 = 0;
        while (i < self.node_size) : (i += 1) {
            self.node_buffer[i] = node_ptr[i];
        }

        _ = self.boot_services.free_pool(cell_buffer);

        return @ptrCast(@alignCast(self.node_buffer));
    }

    pub fn get_record_offset(self: *BTree, record_index: u16) u16 {
        return btree_ops.get_record_offset(self.node_buffer, self.node_size, record_index);
    }

    pub fn get_record_ptr(self: *BTree, record_index: u16) [*]u8 {
        return btree_ops.get_record_ptr(self.node_buffer, self.node_size, record_index);
    }

    pub fn search_index(
        self: *BTree,
        parent_node_id: u32,
        identity: []const u16,
    ) BTreeError!?*const UnitRecord {
        if (self.depth == 0) {
            return BTreeError.TreeEmpty;
        }

        var current_node = self.root_node;
        var current_depth: u16 = self.depth;

        while (current_depth > 0) {
            const node_desc = try self.read_node(current_node);

            if (node_desc.is_leaf()) {
                return btree_ops.search_leaf_for_unit(
                    self.node_buffer,
                    self.node_size,
                    node_desc.record_count,
                    parent_node_id,
                    identity,
                );
            }

            const next_node = btree_ops.search_index_node(
                self.node_buffer,
                self.node_size,
                node_desc.record_count,
                parent_node_id,
                identity,
            );
            if (next_node) |node| {
                current_node = node;
                current_depth -= 1;
            } else {
                return null;
            }
        }

        return null;
    }

    pub fn search_index_for_stack(
        self: *BTree,
        parent_node_id: u32,
        identity: []const u16,
    ) BTreeError!?*const StackRecord {
        if (self.depth == 0) {
            return BTreeError.TreeEmpty;
        }

        var current_node = self.root_node;
        var current_depth: u16 = self.depth;

        while (current_depth > 0) {
            const node_desc = try self.read_node(current_node);

            if (node_desc.is_leaf()) {
                return btree_ops.search_leaf_for_stack(
                    self.node_buffer,
                    self.node_size,
                    node_desc.record_count,
                    parent_node_id,
                    identity,
                );
            }

            const next_node = btree_ops.search_index_node(
                self.node_buffer,
                self.node_size,
                node_desc.record_count,
                parent_node_id,
                identity,
            );
            if (next_node) |node| {
                current_node = node;
                current_depth -= 1;
            } else {
                return null;
            }
        }

        return null;
    }

    pub fn get_thread_record(
        self: *BTree,
        node_id: u32,
    ) BTreeError!?*const ThreadRecord {
        var empty_identity: [0]u16 = undefined;
        return self.search_thread(node_id, &empty_identity);
    }

    fn search_thread(
        self: *BTree,
        node_id: u32,
        identity: []const u16,
    ) BTreeError!?*const ThreadRecord {
        if (self.depth == 0) {
            return BTreeError.TreeEmpty;
        }

        var current_node = self.root_node;
        var current_depth: u16 = self.depth;

        while (current_depth > 0) {
            const node_desc = try self.read_node(current_node);

            if (node_desc.is_leaf()) {
                return btree_ops.search_leaf_for_thread(
                    self.node_buffer,
                    self.node_size,
                    node_desc.record_count,
                    node_id,
                    identity,
                );
            }

            const next_node = btree_ops.search_index_node(
                self.node_buffer,
                self.node_size,
                node_desc.record_count,
                node_id,
                identity,
            );
            if (next_node) |node| {
                current_node = node;
                current_depth -= 1;
            } else {
                return null;
            }
        }

        return null;
    }
};