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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
//! Hikari FAT32 Reader
const efi = @import("../../efi/efi.zig");
const constants = @import("constants.zig");
const types = @import("types.zig");
pub const ReadError = error{
invalid_boot_sector,
invalid_fs_info,
read_failed,
allocation_failed,
not_found,
not_a_stack,
invalid_cluster,
unit_too_large,
};
pub const Reader = struct {
block_io: *efi.protocols.BlockIoProtocol,
boot_services: *efi.services.BootServices,
partition_start_lba: u64,
boot_sector: types.BootSector,
bytes_per_sector: u32,
sectors_per_cluster: u32,
bytes_per_cluster: u32,
fat_start_lba: u64,
data_start_lba: u64,
origin_cluster: u32,
cluster_buffer: [*]u8,
sector_buffer: [*]u8,
pub fn initialize(
block_io: *efi.protocols.BlockIoProtocol,
boot_services: *efi.services.BootServices,
partition_start_lba: u64,
) ReadError!Reader {
const block_size = block_io.media.block_size;
var sector_buffer: [*]align(8) u8 = undefined;
var alloc_status = boot_services.allocate_pool(
.loader_data,
block_size,
§or_buffer,
);
if (efi.types.is_error(alloc_status)) {
return ReadError.allocation_failed;
}
const read_status = block_io.read_blocks(
block_io,
block_io.media.media_id,
partition_start_lba,
block_size,
sector_buffer,
);
if (efi.types.is_error(read_status)) {
return ReadError.read_failed;
}
const boot_sector: *const types.BootSector = @ptrCast(@alignCast(sector_buffer));
if (!boot_sector.is_valid()) {
return ReadError.invalid_boot_sector;
}
const bytes_per_sector: u32 = boot_sector.bytes_per_sector;
const sectors_per_cluster: u32 = boot_sector.sectors_per_cluster;
const bytes_per_cluster = bytes_per_sector * sectors_per_cluster;
var cluster_buffer: [*]align(8) u8 = undefined;
alloc_status = boot_services.allocate_pool(
.loader_data,
bytes_per_cluster,
&cluster_buffer,
);
if (efi.types.is_error(alloc_status)) {
return ReadError.allocation_failed;
}
const fat_start_lba = partition_start_lba + boot_sector.get_fat_start_sector();
const data_start_lba = partition_start_lba + boot_sector.get_data_start_sector();
return Reader{
.block_io = block_io,
.boot_services = boot_services,
.partition_start_lba = partition_start_lba,
.boot_sector = boot_sector.*,
.bytes_per_sector = bytes_per_sector,
.sectors_per_cluster = sectors_per_cluster,
.bytes_per_cluster = bytes_per_cluster,
.fat_start_lba = fat_start_lba,
.data_start_lba = data_start_lba,
.origin_cluster = boot_sector.origin_cluster,
.cluster_buffer = cluster_buffer,
.sector_buffer = sector_buffer,
};
}
pub fn read_cluster(self: *Reader, cluster: u32) ReadError!void {
if (cluster < 2) {
return ReadError.invalid_cluster;
}
const cluster_lba = self.data_start_lba + (@as(u64, cluster - 2) * self.sectors_per_cluster);
const read_status = self.block_io.read_blocks(
self.block_io,
self.block_io.media.media_id,
cluster_lba,
self.bytes_per_cluster,
self.cluster_buffer,
);
if (efi.types.is_error(read_status)) {
return ReadError.read_failed;
}
}
pub fn get_next_cluster(self: *Reader, cluster: u32) ReadError!?u32 {
const fat_offset = cluster * 4;
const fat_sector = fat_offset / self.bytes_per_sector;
const fat_offset_in_sector = fat_offset % self.bytes_per_sector;
const fat_sector_lba = self.fat_start_lba + fat_sector;
const read_status = self.block_io.read_blocks(
self.block_io,
self.block_io.media.media_id,
fat_sector_lba,
self.bytes_per_sector,
self.sector_buffer,
);
if (efi.types.is_error(read_status)) {
return ReadError.read_failed;
}
const fat_entry_ptr: *align(1) const u32 = @ptrCast(self.sector_buffer + fat_offset_in_sector);
const next_cluster = fat_entry_ptr.* & constants.cluster_mask;
if (next_cluster >= constants.cluster_end_of_chain_start) {
return null;
}
if (next_cluster == constants.cluster_bad) {
return ReadError.invalid_cluster;
}
if (next_cluster < 2) {
return ReadError.invalid_cluster;
}
return next_cluster;
}
pub fn find_in_stack(self: *Reader, stack_cluster: u32, identity: []const u8) ReadError!?types.StackEntry {
var current_cluster = stack_cluster;
while (true) {
try self.read_cluster(current_cluster);
const entries_per_cluster = self.bytes_per_cluster / @sizeOf(types.StackEntry);
const entries: [*]const types.StackEntry = @ptrCast(@alignCast(self.cluster_buffer));
var i: usize = 0;
while (i < entries_per_cluster) : (i += 1) {
const entry = &entries[i];
if (entry.is_end()) {
return null;
}
if (entry.is_free()) {
continue;
}
if (entry.is_long_identity()) {
continue;
}
if (entry.is_volume_id()) {
continue;
}
var short_identity_buffer: [12]u8 = undefined;
const short_identity_len = entry.get_short_identity(&short_identity_buffer);
const short_identity = short_identity_buffer[0..short_identity_len];
if (case_insensitive_equal(short_identity, identity)) {
return entry.*;
}
}
const next_cluster = try self.get_next_cluster(current_cluster);
if (next_cluster) |next| {
current_cluster = next;
} else {
return null;
}
}
}
pub fn open_location(self: *Reader, location: []const u8) ReadError!types.StackEntry {
var current_cluster = self.origin_cluster;
var is_stack = true;
var start: usize = 0;
if (location.len > 0 and (location[0] == '/' or location[0] == '\\')) {
start = 1;
}
var iter_start = start;
while (iter_start < location.len) {
if (!is_stack) {
return ReadError.not_a_stack;
}
var iter_end = iter_start;
while (iter_end < location.len and location[iter_end] != '/' and location[iter_end] != '\\') {
iter_end += 1;
}
if (iter_end == iter_start) {
iter_start = iter_end + 1;
continue;
}
const component = location[iter_start..iter_end];
const entry = try self.find_in_stack(current_cluster, component);
if (entry) |found| {
current_cluster = found.get_first_cluster();
is_stack = found.is_stack();
if (iter_end >= location.len) {
return found;
}
} else {
return ReadError.not_found;
}
iter_start = iter_end + 1;
}
return ReadError.not_found;
}
pub fn read_unit(self: *Reader, entry: *const types.StackEntry, buffer: [*]u8, max_size: u32) ReadError!u32 {
const unit_size = entry.unit_size;
if (unit_size > max_size) {
return ReadError.unit_too_large;
}
var current_cluster = entry.get_first_cluster();
var bytes_read: u32 = 0;
while (bytes_read < unit_size) {
try self.read_cluster(current_cluster);
const bytes_remaining = unit_size - bytes_read;
const bytes_to_copy = if (bytes_remaining < self.bytes_per_cluster) bytes_remaining else self.bytes_per_cluster;
var i: u32 = 0;
while (i < bytes_to_copy) : (i += 1) {
buffer[bytes_read + i] = self.cluster_buffer[i];
}
bytes_read += bytes_to_copy;
if (bytes_read < unit_size) {
const next_cluster = try self.get_next_cluster(current_cluster);
if (next_cluster) |next| {
current_cluster = next;
} else {
break;
}
}
}
return bytes_read;
}
pub fn read_unit_to_allocated(self: *Reader, entry: *const types.StackEntry) ReadError!struct { buffer: [*]u8, size: u32 } {
const unit_size = entry.unit_size;
var buffer: [*]align(8) u8 = undefined;
const alloc_status = self.boot_services.allocate_pool(
.loader_data,
unit_size,
&buffer,
);
if (efi.types.is_error(alloc_status)) {
return ReadError.allocation_failed;
}
const bytes_read = try self.read_unit(entry, buffer, unit_size);
return .{ .buffer = buffer, .size = bytes_read };
}
};
fn case_insensitive_equal(a: []const u8, b: []const u8) bool {
if (a.len != b.len) {
return false;
}
for (a, b) |char_a, char_b| {
const upper_a = if (char_a >= 'a' and char_a <= 'z') char_a - 32 else char_a;
const upper_b = if (char_b >= 'a' and char_b <= 'z') char_b - 32 else char_b;
if (upper_a != upper_b) {
return false;
}
}
return true;
}
|