blob: 1a151b97850b40e687d449caec6d03a2769ef5bd [file] [log] [blame]
Nico Huber26f71832023-12-05 16:26:56 +01001-- Derived from GRUB -- GRand Unified Bootloader
2-- Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc.
3-- Copyright (C) 2023 secunet Security Networks AG
4--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
7-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
9
Nico Huber33f6d952023-12-13 23:16:42 +010010with Ada.Unchecked_Conversion;
Nico Huber8ec45a12023-12-04 17:11:08 +010011with System;
Nico Huber1d7727f2023-11-30 15:58:46 +010012with Interfaces;
Nico Huber8ec45a12023-12-04 17:11:08 +010013with Interfaces.C;
14with Interfaces.C.Strings;
Nico Huber1d7727f2023-11-30 15:58:46 +010015
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000016with FILO.Blockdev;
17with FILO.FS.VFS;
Nico Huber8ec45a12023-12-04 17:11:08 +010018
19use Interfaces.C;
Nico Huber1d7727f2023-11-30 15:58:46 +010020
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000021package body FILO.FS.Ext2 is
Nico Huber1d7727f2023-11-30 15:58:46 +010022
Nico Huber57d3a852023-12-04 15:42:40 +010023 function Is_Mounted (State : T) return Boolean is (State.S >= Mounted);
24 function Is_Open (State : T) return Boolean is (State.S = File_Opened);
25
Nico Huber26f71832023-12-05 16:26:56 +010026 --------------------------------------------------------------------------
27
28 SUPERBLOCK_SIZE : constant := 1024;
29 SUPERBLOCK_BLOCKS : constant := SUPERBLOCK_SIZE / BLOCK_SIZE;
30
31 SUPERBLOCK_MAGIC : constant := 16#ef53#;
32 OLD_REV : constant := 0;
33 DYNAMIC_REV : constant := 1;
34 FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#;
35 FEATURE_INCOMPAT_64BIT : constant := 16#0080#;
36
Nico Huber57d3a852023-12-04 15:42:40 +010037 procedure Mount
38 (State : in out T;
39 Part_Len : in Partition_Length;
40 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +010041 is
Nico Huber26f71832023-12-05 16:26:56 +010042 Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +010043 begin
Nico Huber26f71832023-12-05 16:26:56 +010044 if Part_Len < 2 * SUPERBLOCK_SIZE then
45 Success := False;
46 return;
47 end if;
48
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000049 Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success);
Nico Huber26f71832023-12-05 16:26:56 +010050 if not Success then
51 return;
52 end if;
53
54 if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then
55 Success := False;
56 return;
57 end if;
58
59 State.Part_Len := Part_Len;
Nico Hubercd1d5f72023-12-13 16:01:43 +010060 State.First_Data_Block := FSBlock_Offset (Read_LE32 (Super_Block, 5 * 4));
Nico Huber26f71832023-12-05 16:26:56 +010061
62 declare
63 S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4);
64 begin
65 if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then
66 State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10);
67 else
68 Success := False;
69 return;
70 end if;
71 end;
72
73 declare
74 S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4);
75 begin
76 if S_Inodes_Per_Group in 1 .. Unsigned_32 (Positive'Last) then
77 State.Inodes_Per_Group := Positive (S_Inodes_Per_Group);
78 else
79 Success := False;
80 return;
81 end if;
82 end;
83
84 declare
85 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
86 begin
87 if S_Rev_Level >= DYNAMIC_REV then
88 declare
89 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
90 begin
91 if S_Inode_Size in
92 Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last)
93 then
94 State.Inode_Size := Inode_Size (S_Inode_Size);
95 else
96 Success := False;
97 return;
98 end if;
99 end;
100 else
101 State.Inode_Size := Inode_Size'First;
102 end if;
103 end;
104
105 declare
106 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
107 begin
108 State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
109 State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
110 if State.Feature_64Bit then
111 declare
112 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
113 begin
Nico Hubercd1d5f72023-12-13 16:01:43 +0100114 if Is_Power_Of_2 (S_Desc_Size) and then
115 S_Desc_Size in Unsigned_16 (Desc_Size'First) ..
116 Unsigned_16 (Positive'Min (Desc_Size'Last, 2 ** State.Block_Size_Bits))
Nico Huber26f71832023-12-05 16:26:56 +0100117 then
118 State.Desc_Size := Desc_Size (S_Desc_Size);
119 else
120 Success := False;
121 return;
122 end if;
123 end;
Nico Huber77a04d72023-12-13 23:11:40 +0100124 State.Feature_64Bit := State.Feature_64Bit and State.Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100125 else
126 State.Desc_Size := Desc_Size'First;
127 end if;
128 end;
129
130 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100131 end Mount;
132
Nico Huberf5d99d02023-12-12 13:42:55 +0100133 procedure Read_FSBlock
134 (State : in T;
135 Buf : out Buffer_Type;
136 FSBlock : in FSBlock_Offset;
137 Success : out Boolean)
138 with
139 Pre =>
140 Is_Mounted (State) and
141 Buf'Length = 2 ** State.Block_Size_Bits
142 is
143 Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits;
144 Max_Block_Offset : constant FSBlock_Offset :=
145 FSBlock_Offset (State.Part_Len / Block_Size - 1);
146 begin
147 if FSBlock > Max_Block_Offset then
148 Success := False;
149 return;
150 end if;
151 Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success);
152 end Read_FSBlock;
153
Nico Huber68c86932023-12-13 11:03:11 +0100154 procedure Cache_FSBlock
155 (State : in out T;
156 Phys : in FSBlock_Offset;
157 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100158 Label : in Cache_Label;
159 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100160 Cache_Start : out Max_Block_Index;
161 Cache_End : out Max_Block_Index;
162 Success : out Boolean)
163 with
164 Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits
165 is
166 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
167 -- Limit cache usage depending on block size:
168 Max_Level : constant Block_Cache_Index :=
169 2 ** (Log_Block_Size'Last - State.Block_Size_Bits) - 1;
170 Cache_Level : constant Block_Cache_Index :=
171 Block_Cache_Index'Min (Level, Max_Level);
172 begin
173 Cache_Start := Cache_Level * Block_Size;
174 Cache_End := Cache_Start + Block_Size - 1;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100175 if State.Block_Cache_Logical (Cache_Level) = Logical and
176 State.Block_Cache_Label (Cache_Level) = Label
177 then
Nico Huber68c86932023-12-13 11:03:11 +0100178 Success := True;
179 else
180 Read_FSBlock
181 (State => State,
182 Buf => State.Block_Cache (Cache_Start .. Cache_End),
183 FSBlock => Phys,
184 Success => Success);
Nico Huber57dfbfb2023-12-13 23:24:16 +0100185 State.Block_Cache_Logical (Cache_Level) := Logical;
186 State.Block_Cache_Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100187 end if;
188 end Cache_FSBlock;
189
Nico Huber57dfbfb2023-12-13 23:24:16 +0100190 procedure Cache_FSBlock
191 (State : in out T;
192 Phys : in FSBlock_Offset;
193 Level : in Block_Cache_Index;
194 Cache_Start : out Max_Block_Index;
195 Cache_End : out Max_Block_Index;
196 Success : out Boolean)
197 with
198 Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits
199 is
200 begin
201 Cache_FSBlock (State, Phys, Level, Cache_Label (Phys),
202 False, Cache_Start, Cache_End, Success);
203 end Cache_FSBlock;
204
205 procedure Reset_Cache_Logical (State : in out T) is
206 begin
207 for I in Block_Cache_Index loop
208 if State.Block_Cache_Logical (I) then
209 State.Block_Cache_Logical (I) := False;
210 State.Block_Cache_Label (I) := 0;
211 end if;
212 end loop;
213 end Reset_Cache_Logical;
214
Nico Huber6623c982023-12-12 16:35:46 +0100215 procedure Ext2_Block_Map
216 (State : in out T;
217 Logical : in FSBlock_Logical;
218 Physical : out FSBlock_Offset;
219 Success : out Boolean)
220 is
Nico Huber33f6d952023-12-13 23:16:42 +0100221 Direct_Blocks : constant := 12;
222 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
223 type Inode_Blocks is record
224 Direct_Blocks : Direct_Blocks_Array;
225 Indirect_Block : Unsigned_32;
226 Double_Indirect : Unsigned_32;
227 Triple_Indirect : Unsigned_32;
228 end record
229 with Size => Inode_Extents'Length * 8;
230
231 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
232 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Extents));
233
Nico Huber6623c982023-12-12 16:35:46 +0100234 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
235 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4);
236 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4);
237 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
238
239 procedure Indirect_Block_Lookup
240 (Indirect_Block_Phys : in FSBlock_Offset;
241 Addr_In_Block : in Addr_In_Block_Range;
242 Level : in Block_Cache_Index;
243 Logical_Off : in FSBlock_Logical;
244 Next_Physical : out FSBlock_Offset;
245 Success : out Boolean)
246 with
247 Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
248 is
Nico Huber68c86932023-12-13 11:03:11 +0100249 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100250 begin
Nico Huber68c86932023-12-13 11:03:11 +0100251 Cache_FSBlock
252 (State => State,
253 Phys => Indirect_Block_Phys,
254 Level => Level,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100255 Label => Cache_Label (Logical_Off),
Nico Huber68c86932023-12-13 11:03:11 +0100256 Cache_Start => Cache_Start,
257 Cache_End => Cache_End,
258 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100259 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huberfe897122023-12-12 21:33:36 +0100260 (Buf => State.Block_Cache (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100261 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100262 end Indirect_Block_Lookup;
263
264 Logical_Rest : FSBlock_Logical := Logical;
265 begin
266 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100267 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100268 Success := True;
269 return;
270 end if;
271
272 Logical_Rest := Logical_Rest - Direct_Blocks;
273 if Logical_Rest < Addr_Per_Block then
274 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100275 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100276 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100277 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100278 Logical_Off => Logical - Logical_Rest,
279 Next_Physical => Physical,
280 Success => Success);
281 return;
282 end if;
283
284 Logical_Rest := Logical_Rest - Addr_Per_Block;
285 if Logical_Rest < Addr_Per_Block ** 2 then
286 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100287 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber6623c982023-12-12 16:35:46 +0100288 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100289 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100290 Logical_Off => Logical - Logical_Rest,
291 Next_Physical => Physical,
292 Success => Success);
293 if not Success then
294 return;
295 end if;
296
297 Indirect_Block_Lookup
298 (Indirect_Block_Phys => Physical,
299 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100300 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100301 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
302 Next_Physical => Physical,
303 Success => Success);
304 return;
305 end if;
306
307 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
308 if Logical_Rest < Addr_Per_Block ** 3 then
309 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100310 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber6623c982023-12-12 16:35:46 +0100311 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block ** 2),
Nico Huberfe897122023-12-12 21:33:36 +0100312 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100313 Logical_Off => Logical - Logical_Rest,
314 Next_Physical => Physical,
315 Success => Success);
316 if not Success then
317 return;
318 end if;
319
320 Indirect_Block_Lookup
321 (Indirect_Block_Phys => Physical,
322 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100323 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100324 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
325 Next_Physical => Physical,
326 Success => Success);
327 if not Success then
328 return;
329 end if;
330
331 Indirect_Block_Lookup
332 (Indirect_Block_Phys => Physical,
333 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100334 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100335 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
336 Next_Physical => Physical,
337 Success => Success);
338 return;
339 end if;
340
341 -- Logical address was just too high.
342 Success := False;
343 end Ext2_Block_Map;
344
Nico Huberfffc8c12023-12-13 12:44:12 +0100345 procedure Extent_Block_Map
346 (State : in out T;
347 Logical : in FSBlock_Logical;
348 Physical : out FSBlock_Offset;
349 Success : out Boolean)
350 is
351 -- Extent blocks always start with a 12B header and contain 12B entries.
352 -- Every entry starts with the number of the first logical block it
353 -- covers. Entries are sorted by this number.
354 -- Depth > 0 blocks have index entries, referencing further extent blocks.
355 -- Depth = 0 blocks have extent entries, referencing a contiguous range
356 -- of data blocks.
357 --
358 -- +-----------------+
359 -- .-> | Hdr depth=0 |
360 -- | +-----------------+
361 -- | | Extent 0.. 12 |
362 -- +-------------+ | +-----------------+
363 -- | Hdr depth=1 | | | Extent 13.. 13 |
364 -- +-------------+ | +-----------------+
365 -- | Index 0 | --' | Extent 14..122 |
366 -- +-------------+ +-----------------+
367 -- | Index 123 | --.
368 -- +-------------+ | +-----------------+
369 -- `-> | Hdr depth=0 |
370 -- +-----------------+
371 -- | Extent 123..125 |
372 -- +-----------------+
373 -- | Extent 126..234 |
374 -- +-----------------+
375 --
376
377 Extent_Header_Size : constant := 12;
378 Extent_Header_Magic : constant := 16#f03a#;
379 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
380 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
381
382 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
383 is
384 (Idx * Extent_Header_Size + Off);
385
386 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
387 is
388 (Read_LE16 (Buf, 0))
389 with
390 Pre => Buf'Length >= 2;
391
392 function Header_Entries (Buf : Buffer_Type) return Natural
393 is
394 (Natural (Read_LE16 (Buf, 2)))
395 with
396 Pre => Buf'Length >= 4;
397
398 function Header_Depth (Buf : Buffer_Type) return Natural
399 is
400 (Natural (Read_LE16 (Buf, 6)))
401 with
402 Pre => Buf'Length >= 8;
403
404 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
405 is
406 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
407 with
408 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
409
410 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
411 is
412 (FSBlock_Offset
413 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
414 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
415 with
416 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
417
418 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
419 renames Index_Logical;
420
421 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
422 is
423 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
424 with
425 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
426
427 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
428 is
429 (FSBlock_Offset
430 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
431 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
432 with
433 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
434
435 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
436 with
437 Pre => Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and
438 Extent_Logical (Buf, 1) <= Logical,
439 Post => Bin_Search'Result in 1 .. Refs and
440 Extent_Logical (Buf, Bin_Search'Result) <= Logical
441 is
442 Left : Extent_Idx := 1;
443 Right : Extent_Idx := Refs;
444 begin
445 while Left <= Right loop
446 declare
447 Mid : constant Extent_Idx := (Left + Right) / 2;
448 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
449 begin
450 if Logical < Ext_Logical then
451 Right := Mid - 1;
452 else
453 Left := Mid + 1;
454 end if;
455 end;
456 end loop;
457 return Left - 1;
458 end Bin_Search;
459
460 procedure Next_Ref
461 (Current : in FSBlock_Offset;
462 Logical_Off : in FSBlock_Logical;
463 Depth : in Natural;
464 Next : out Extent_Idx;
465 Cache_Start : out Max_Block_Index;
466 Cache_End : out Max_Block_Index;
467 Success : out Boolean)
468 with
469 Pre => Logical_Off <= Logical,
470 Post => (if Success then
471 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical)
472 is
473 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
474 Dynamic_Max_Index : constant Natural := Block_Size / Extent_Header_Size - 1;
475 begin
476 Cache_FSBlock
477 (State => State,
478 Phys => Current,
479 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100480 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100481 Cache_Start => Cache_Start,
482 Cache_End => Cache_End,
483 Success => Success);
484 if not Success then
485 Next := 1;
486 return;
487 end if;
488
489 declare
490 Hdr_Magic : constant Unsigned_16 :=
491 Header_Magic (State.Block_Cache (Cache_Start .. Cache_End));
492 Hdr_Entries : constant Natural :=
493 Header_Entries (State.Block_Cache (Cache_Start .. Cache_End));
494 Hdr_Depth : constant Natural :=
495 Header_Depth (State.Block_Cache (Cache_Start .. Cache_End));
496 First_Logical : constant FSBlock_Logical :=
497 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1);
498 begin
499 Success := Success and then
500 Hdr_Magic = Extent_Header_Magic and then
501 Hdr_Depth = Depth and then
502 Hdr_Entries <= Dynamic_Max_Index and then
503 First_Logical = Logical_Off;
504 if not Success then
505 Next := 1;
506 else
507 Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries);
508 end if;
509 end;
510 end Next_Ref;
511
Nico Huber33f6d952023-12-13 23:16:42 +0100512 Inode_Magic : constant Unsigned_16 := Header_Magic (State.Inode.Extents);
513 Inode_Entries : constant Natural := Header_Entries (State.Inode.Extents);
514 First_Logical : constant FSBlock_Logical := Extent_Logical (State.Inode.Extents, 1);
515 Depth : Natural := Header_Depth (State.Inode.Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100516
517 Cache_Start, Cache_End : Max_Block_Index;
518 Logical_Off, Length : FSBlock_Logical;
519 Idx : Extent_Idx;
520 begin
521 Success :=
522 Inode_Magic = Extent_Header_Magic and then
523 Inode_Entries > 0 and then
Nico Huber33f6d952023-12-13 23:16:42 +0100524 Inode_Entries < State.Inode.Extents'Length / Extent_Header_Size and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100525 First_Logical <= Logical;
526 if not Success then
527 Physical := 0;
528 return;
529 end if;
530
Nico Huber33f6d952023-12-13 23:16:42 +0100531 Idx := Bin_Search (State.Inode.Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100532 if Depth = 0 then
Nico Huber33f6d952023-12-13 23:16:42 +0100533 Physical := Extent_Physical (State.Inode.Extents, Idx);
534 Logical_Off := Extent_Logical (State.Inode.Extents, Idx);
535 Length := Extent_Length (State.Inode.Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100536 else
Nico Huber33f6d952023-12-13 23:16:42 +0100537 Physical := Index_Physical (State.Inode.Extents, Idx);
538 Logical_Off := Index_Logical (State.Inode.Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100539 loop
540 Depth := Depth - 1;
541 Next_Ref
542 (Current => Physical,
543 Logical_Off => Logical_Off,
544 Depth => Depth,
545 Next => Idx,
546 Cache_Start => Cache_Start,
547 Cache_End => Cache_End,
548 Success => Success);
549 if not Success then
550 return;
551 end if;
552
553 exit when Depth = 0;
554 Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
555 Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
556 end loop;
557
558 Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
559 Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
560 Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx);
561 end if;
562
563 Success :=
564 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100565 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100566 Logical < Logical_Off + Length;
567 if Success then
568 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
569 end if;
570 end Extent_Block_Map;
571
Nico Huber57d3a852023-12-04 15:42:40 +0100572 procedure Open
573 (State : in out T;
574 File_Len : out File_Length;
575 File_Path : in String;
576 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100577 is
578 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100579 File_Len := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100580 Success := False;
581 end Open;
582
Nico Huber57d3a852023-12-04 15:42:40 +0100583 procedure Close (State : in out T) is
584 begin
585 State.S := Mounted;
586 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100587
Nico Huber57d3a852023-12-04 15:42:40 +0100588 procedure Read
589 (State : in out T;
590 File_Len : in File_Length;
591 File_Pos : in out File_Offset;
592 Buf : out Buffer_Type;
593 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100594 is
595 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100596 Buf := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +0100597 Len := 0;
598 end Read;
599
Nico Huber26f71832023-12-05 16:26:56 +0100600 --------------------------------------------------------------------------
601
602 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100603
604 function C_Mount return int
605 with
606 Export,
607 Convention => C,
608 External_Name => "ext2fs_mount";
609 function C_Mount return int
610 with
611 SPARK_Mode => Off
612 is
613 begin
614 return C.C_Mount;
615 end C_Mount;
616
617 function C_Open (File_Path : Strings.chars_ptr) return int
618 with
619 Export,
620 Convention => C,
621 External_Name => "ext2fs_dir";
622 function C_Open (File_Path : Strings.chars_ptr) return int
623 with
624 SPARK_Mode => Off
625 is
626 begin
627 return C.C_Open (File_Path);
628 end C_Open;
629
630 function C_Read (Buf : System.Address; Len : int) return int
631 with
632 Export,
633 Convention => C,
634 External_Name => "ext2fs_read";
635 function C_Read (Buf : System.Address; Len : int) return int
636 with
637 SPARK_Mode => Off
638 is
639 begin
640 return C.C_Read (Buf, Len);
641 end C_Read;
642
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000643end FILO.FS.Ext2;