blob: ccc8b31475afdd0a7c082b1979304d52c505d78f [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;
Nico Huber1d7727f2023-11-30 15:58:46 +010014
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000015with FILO.Blockdev;
16with FILO.FS.VFS;
Nico Huber8ec45a12023-12-04 17:11:08 +010017
18use Interfaces.C;
Nico Huber1d7727f2023-11-30 15:58:46 +010019
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000020package body FILO.FS.Ext2 is
Nico Huber1d7727f2023-11-30 15:58:46 +010021
Nico Huber57d3a852023-12-04 15:42:40 +010022 function Is_Mounted (State : T) return Boolean is (State.S >= Mounted);
23 function Is_Open (State : T) return Boolean is (State.S = File_Opened);
24
Nico Huber26f71832023-12-05 16:26:56 +010025 --------------------------------------------------------------------------
26
27 SUPERBLOCK_SIZE : constant := 1024;
28 SUPERBLOCK_BLOCKS : constant := SUPERBLOCK_SIZE / BLOCK_SIZE;
29
30 SUPERBLOCK_MAGIC : constant := 16#ef53#;
31 OLD_REV : constant := 0;
32 DYNAMIC_REV : constant := 1;
33 FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#;
34 FEATURE_INCOMPAT_64BIT : constant := 16#0080#;
35
Nico Hubercdc03512023-12-13 23:32:54 +010036 EXT4_EXTENTS_FL : constant := 16#8_0000#;
37
Nico Huber57d3a852023-12-04 15:42:40 +010038 procedure Mount
39 (State : in out T;
40 Part_Len : in Partition_Length;
41 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +010042 is
Nico Huber26f71832023-12-05 16:26:56 +010043 Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +010044 begin
Nico Huber26f71832023-12-05 16:26:56 +010045 if Part_Len < 2 * SUPERBLOCK_SIZE then
46 Success := False;
47 return;
48 end if;
49
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000050 Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success);
Nico Huber26f71832023-12-05 16:26:56 +010051 if not Success then
52 return;
53 end if;
54
55 if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then
56 Success := False;
57 return;
58 end if;
59
60 State.Part_Len := Part_Len;
Nico Hubercd1d5f72023-12-13 16:01:43 +010061 State.First_Data_Block := FSBlock_Offset (Read_LE32 (Super_Block, 5 * 4));
Nico Huber26f71832023-12-05 16:26:56 +010062
63 declare
64 S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4);
65 begin
66 if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then
67 State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10);
68 else
69 Success := False;
70 return;
71 end if;
72 end;
73
74 declare
75 S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4);
76 begin
Nico Huber21d42022023-12-16 02:50:22 +010077 if S_Inodes_Per_Group in 2 .. Unsigned_32'Last then
Nico Hubercdc03512023-12-13 23:32:54 +010078 State.Inodes_Per_Group := Inode_Index (S_Inodes_Per_Group);
Nico Huber26f71832023-12-05 16:26:56 +010079 else
80 Success := False;
81 return;
82 end if;
83 end;
84
85 declare
86 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
87 begin
88 if S_Rev_Level >= DYNAMIC_REV then
89 declare
90 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
91 begin
92 if S_Inode_Size in
93 Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last)
94 then
95 State.Inode_Size := Inode_Size (S_Inode_Size);
96 else
97 Success := False;
98 return;
99 end if;
100 end;
101 else
102 State.Inode_Size := Inode_Size'First;
103 end if;
104 end;
105
106 declare
107 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
108 begin
109 State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
110 State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
111 if State.Feature_64Bit then
112 declare
113 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
114 begin
Nico Hubercd1d5f72023-12-13 16:01:43 +0100115 if Is_Power_Of_2 (S_Desc_Size) and then
116 S_Desc_Size in Unsigned_16 (Desc_Size'First) ..
117 Unsigned_16 (Positive'Min (Desc_Size'Last, 2 ** State.Block_Size_Bits))
Nico Huber26f71832023-12-05 16:26:56 +0100118 then
119 State.Desc_Size := Desc_Size (S_Desc_Size);
120 else
121 Success := False;
122 return;
123 end if;
124 end;
Nico Huber77a04d72023-12-13 23:11:40 +0100125 State.Feature_64Bit := State.Feature_64Bit and State.Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100126 else
127 State.Desc_Size := Desc_Size'First;
128 end if;
129 end;
130
131 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100132 end Mount;
133
Nico Huberf5d99d02023-12-12 13:42:55 +0100134 procedure Read_FSBlock
135 (State : in T;
136 Buf : out Buffer_Type;
137 FSBlock : in FSBlock_Offset;
138 Success : out Boolean)
139 with
140 Pre =>
141 Is_Mounted (State) and
142 Buf'Length = 2 ** State.Block_Size_Bits
143 is
144 Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits;
145 Max_Block_Offset : constant FSBlock_Offset :=
146 FSBlock_Offset (State.Part_Len / Block_Size - 1);
147 begin
148 if FSBlock > Max_Block_Offset then
149 Success := False;
150 return;
151 end if;
152 Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success);
153 end Read_FSBlock;
154
Nico Huber68c86932023-12-13 11:03:11 +0100155 procedure Cache_FSBlock
156 (State : in out T;
157 Phys : in FSBlock_Offset;
158 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100159 Label : in Cache_Label;
160 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100161 Cache_Start : out Max_Block_Index;
162 Cache_End : out Max_Block_Index;
163 Success : out Boolean)
164 with
165 Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits
166 is
167 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
168 -- Limit cache usage depending on block size:
169 Max_Level : constant Block_Cache_Index :=
170 2 ** (Log_Block_Size'Last - State.Block_Size_Bits) - 1;
171 Cache_Level : constant Block_Cache_Index :=
172 Block_Cache_Index'Min (Level, Max_Level);
173 begin
174 Cache_Start := Cache_Level * Block_Size;
175 Cache_End := Cache_Start + Block_Size - 1;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100176 if State.Block_Cache_Logical (Cache_Level) = Logical and
177 State.Block_Cache_Label (Cache_Level) = Label
178 then
Nico Huber68c86932023-12-13 11:03:11 +0100179 Success := True;
180 else
181 Read_FSBlock
182 (State => State,
183 Buf => State.Block_Cache (Cache_Start .. Cache_End),
184 FSBlock => Phys,
185 Success => Success);
Nico Huber57dfbfb2023-12-13 23:24:16 +0100186 State.Block_Cache_Logical (Cache_Level) := Logical;
187 State.Block_Cache_Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100188 end if;
189 end Cache_FSBlock;
190
Nico Huber57dfbfb2023-12-13 23:24:16 +0100191 procedure Cache_FSBlock
192 (State : in out T;
193 Phys : in FSBlock_Offset;
194 Level : in Block_Cache_Index;
195 Cache_Start : out Max_Block_Index;
196 Cache_End : out Max_Block_Index;
197 Success : out Boolean)
198 with
199 Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits
200 is
201 begin
202 Cache_FSBlock (State, Phys, Level, Cache_Label (Phys),
203 False, Cache_Start, Cache_End, Success);
204 end Cache_FSBlock;
205
206 procedure Reset_Cache_Logical (State : in out T) is
207 begin
208 for I in Block_Cache_Index loop
209 if State.Block_Cache_Logical (I) then
210 State.Block_Cache_Logical (I) := False;
211 State.Block_Cache_Label (I) := 0;
212 end if;
213 end loop;
214 end Reset_Cache_Logical;
215
Nico Huber6623c982023-12-12 16:35:46 +0100216 procedure Ext2_Block_Map
217 (State : in out T;
218 Logical : in FSBlock_Logical;
219 Physical : out FSBlock_Offset;
220 Success : out Boolean)
221 is
Nico Huber33f6d952023-12-13 23:16:42 +0100222 Direct_Blocks : constant := 12;
223 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
224 type Inode_Blocks is record
225 Direct_Blocks : Direct_Blocks_Array;
226 Indirect_Block : Unsigned_32;
227 Double_Indirect : Unsigned_32;
228 Triple_Indirect : Unsigned_32;
229 end record
230 with Size => Inode_Extents'Length * 8;
231
232 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100233 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100234
Nico Huber6623c982023-12-12 16:35:46 +0100235 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
236 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4);
237 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4);
238 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
239
240 procedure Indirect_Block_Lookup
241 (Indirect_Block_Phys : in FSBlock_Offset;
242 Addr_In_Block : in Addr_In_Block_Range;
243 Level : in Block_Cache_Index;
244 Logical_Off : in FSBlock_Logical;
245 Next_Physical : out FSBlock_Offset;
246 Success : out Boolean)
247 with
248 Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
249 is
Nico Huber68c86932023-12-13 11:03:11 +0100250 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100251 begin
Nico Huber68c86932023-12-13 11:03:11 +0100252 Cache_FSBlock
253 (State => State,
254 Phys => Indirect_Block_Phys,
255 Level => Level,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100256 Label => Cache_Label (Logical_Off),
Nico Huber68c86932023-12-13 11:03:11 +0100257 Cache_Start => Cache_Start,
258 Cache_End => Cache_End,
259 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100260 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huberfe897122023-12-12 21:33:36 +0100261 (Buf => State.Block_Cache (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100262 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100263 end Indirect_Block_Lookup;
264
265 Logical_Rest : FSBlock_Logical := Logical;
266 begin
267 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100268 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100269 Success := True;
270 return;
271 end if;
272
273 Logical_Rest := Logical_Rest - Direct_Blocks;
274 if Logical_Rest < Addr_Per_Block then
275 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100276 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100277 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100278 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100279 Logical_Off => Logical - Logical_Rest,
280 Next_Physical => Physical,
281 Success => Success);
282 return;
283 end if;
284
285 Logical_Rest := Logical_Rest - Addr_Per_Block;
286 if Logical_Rest < Addr_Per_Block ** 2 then
287 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100288 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber6623c982023-12-12 16:35:46 +0100289 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100290 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100291 Logical_Off => Logical - Logical_Rest,
292 Next_Physical => Physical,
293 Success => Success);
294 if not Success then
295 return;
296 end if;
297
298 Indirect_Block_Lookup
299 (Indirect_Block_Phys => Physical,
300 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100301 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100302 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
303 Next_Physical => Physical,
304 Success => Success);
305 return;
306 end if;
307
308 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
309 if Logical_Rest < Addr_Per_Block ** 3 then
310 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100311 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber6623c982023-12-12 16:35:46 +0100312 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block ** 2),
Nico Huberfe897122023-12-12 21:33:36 +0100313 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100314 Logical_Off => Logical - Logical_Rest,
315 Next_Physical => Physical,
316 Success => Success);
317 if not Success then
318 return;
319 end if;
320
321 Indirect_Block_Lookup
322 (Indirect_Block_Phys => Physical,
323 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100324 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100325 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
326 Next_Physical => Physical,
327 Success => Success);
328 if not Success then
329 return;
330 end if;
331
332 Indirect_Block_Lookup
333 (Indirect_Block_Phys => Physical,
334 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100335 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100336 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
337 Next_Physical => Physical,
338 Success => Success);
339 return;
340 end if;
341
342 -- Logical address was just too high.
343 Success := False;
344 end Ext2_Block_Map;
345
Nico Huberfffc8c12023-12-13 12:44:12 +0100346 procedure Extent_Block_Map
347 (State : in out T;
348 Logical : in FSBlock_Logical;
349 Physical : out FSBlock_Offset;
350 Success : out Boolean)
351 is
352 -- Extent blocks always start with a 12B header and contain 12B entries.
353 -- Every entry starts with the number of the first logical block it
354 -- covers. Entries are sorted by this number.
355 -- Depth > 0 blocks have index entries, referencing further extent blocks.
356 -- Depth = 0 blocks have extent entries, referencing a contiguous range
357 -- of data blocks.
358 --
359 -- +-----------------+
360 -- .-> | Hdr depth=0 |
361 -- | +-----------------+
362 -- | | Extent 0.. 12 |
363 -- +-------------+ | +-----------------+
364 -- | Hdr depth=1 | | | Extent 13.. 13 |
365 -- +-------------+ | +-----------------+
366 -- | Index 0 | --' | Extent 14..122 |
367 -- +-------------+ +-----------------+
368 -- | Index 123 | --.
369 -- +-------------+ | +-----------------+
370 -- `-> | Hdr depth=0 |
371 -- +-----------------+
372 -- | Extent 123..125 |
373 -- +-----------------+
374 -- | Extent 126..234 |
375 -- +-----------------+
376 --
377
378 Extent_Header_Size : constant := 12;
379 Extent_Header_Magic : constant := 16#f03a#;
380 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
381 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
382
383 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
384 is
385 (Idx * Extent_Header_Size + Off);
386
387 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
388 is
389 (Read_LE16 (Buf, 0))
390 with
391 Pre => Buf'Length >= 2;
392
393 function Header_Entries (Buf : Buffer_Type) return Natural
394 is
395 (Natural (Read_LE16 (Buf, 2)))
396 with
397 Pre => Buf'Length >= 4;
398
399 function Header_Depth (Buf : Buffer_Type) return Natural
400 is
401 (Natural (Read_LE16 (Buf, 6)))
402 with
403 Pre => Buf'Length >= 8;
404
405 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
406 is
407 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
408 with
409 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
410
411 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
412 is
413 (FSBlock_Offset
414 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
415 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
416 with
417 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
418
419 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
420 renames Index_Logical;
421
422 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
423 is
424 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
425 with
426 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
427
428 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
429 is
430 (FSBlock_Offset
431 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
432 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
433 with
434 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
435
436 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
437 with
438 Pre => Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and
439 Extent_Logical (Buf, 1) <= Logical,
440 Post => Bin_Search'Result in 1 .. Refs and
441 Extent_Logical (Buf, Bin_Search'Result) <= Logical
442 is
443 Left : Extent_Idx := 1;
444 Right : Extent_Idx := Refs;
445 begin
446 while Left <= Right loop
447 declare
448 Mid : constant Extent_Idx := (Left + Right) / 2;
449 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
450 begin
451 if Logical < Ext_Logical then
452 Right := Mid - 1;
453 else
454 Left := Mid + 1;
455 end if;
456 end;
457 end loop;
458 return Left - 1;
459 end Bin_Search;
460
461 procedure Next_Ref
462 (Current : in FSBlock_Offset;
463 Logical_Off : in FSBlock_Logical;
464 Depth : in Natural;
465 Next : out Extent_Idx;
466 Cache_Start : out Max_Block_Index;
467 Cache_End : out Max_Block_Index;
468 Success : out Boolean)
469 with
470 Pre => Logical_Off <= Logical,
471 Post => (if Success then
472 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical)
473 is
474 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
475 Dynamic_Max_Index : constant Natural := Block_Size / Extent_Header_Size - 1;
476 begin
477 Cache_FSBlock
478 (State => State,
479 Phys => Current,
480 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100481 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100482 Cache_Start => Cache_Start,
483 Cache_End => Cache_End,
484 Success => Success);
485 if not Success then
486 Next := 1;
487 return;
488 end if;
489
490 declare
491 Hdr_Magic : constant Unsigned_16 :=
492 Header_Magic (State.Block_Cache (Cache_Start .. Cache_End));
493 Hdr_Entries : constant Natural :=
494 Header_Entries (State.Block_Cache (Cache_Start .. Cache_End));
495 Hdr_Depth : constant Natural :=
496 Header_Depth (State.Block_Cache (Cache_Start .. Cache_End));
497 First_Logical : constant FSBlock_Logical :=
498 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1);
499 begin
500 Success := Success and then
501 Hdr_Magic = Extent_Header_Magic and then
502 Hdr_Depth = Depth and then
503 Hdr_Entries <= Dynamic_Max_Index and then
504 First_Logical = Logical_Off;
505 if not Success then
506 Next := 1;
507 else
508 Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries);
509 end if;
510 end;
511 end Next_Ref;
512
Nico Huber7403a542023-12-15 23:13:47 +0100513 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
514 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
515 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
516 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
517 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100518
519 Cache_Start, Cache_End : Max_Block_Index;
520 Logical_Off, Length : FSBlock_Logical;
521 Idx : Extent_Idx;
522 begin
523 Success :=
524 Inode_Magic = Extent_Header_Magic and then
525 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100526 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100527 First_Logical <= Logical;
528 if not Success then
529 Physical := 0;
530 return;
531 end if;
532
Nico Huber7403a542023-12-15 23:13:47 +0100533 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100534 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100535 Physical := Extent_Physical (Inline_Extents, Idx);
536 Logical_Off := Extent_Logical (Inline_Extents, Idx);
537 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100538 else
Nico Huber7403a542023-12-15 23:13:47 +0100539 Physical := Index_Physical (Inline_Extents, Idx);
540 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100541 loop
542 Depth := Depth - 1;
543 Next_Ref
544 (Current => Physical,
545 Logical_Off => Logical_Off,
546 Depth => Depth,
547 Next => Idx,
548 Cache_Start => Cache_Start,
549 Cache_End => Cache_End,
550 Success => Success);
551 if not Success then
552 return;
553 end if;
554
555 exit when Depth = 0;
556 Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
557 Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
558 end loop;
559
560 Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
561 Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
562 Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx);
563 end if;
564
565 Success :=
566 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100567 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100568 Logical < Logical_Off + Length;
569 if Success then
570 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
571 end if;
572 end Extent_Block_Map;
573
Nico Huber57d3a852023-12-04 15:42:40 +0100574 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100575 (State : in out T;
576 Inode : in Inode_Index;
577 Success : out Boolean)
578 with
579 Pre => Is_Mounted (State) and not Is_Open (State),
580 Post => Success = Is_Open (State)
581 is
582 type Group_Index is new Natural;
583 subtype Desc_In_Block_Index is Group_Index
584 range 0 .. Group_Index (2 ** Log_Block_Size'Last / Desc_Size'First - 1);
585
586 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
587
588 Inodes_Per_Block : constant Inode_Index := Inode_Index (Block_Size / State.Inode_Size);
589 Desc_Per_Block : constant Group_Index := Group_Index (Block_Size / State.Desc_Size);
590
Nico Huber022e2262023-12-15 23:15:17 +0100591 ------------------------
592 -- Group deserialization
593
Nico Hubercdc03512023-12-13 23:32:54 +0100594 subtype Group_Off is Natural range 0 .. Desc_Size'Last;
595 function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural
596 is
597 (Natural (Idx) * State.Desc_Size + Off);
598
599 function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset
600 is
601 (FSBlock_Offset (
602 (if State.Feature_64Bit
603 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32)
604 else 0)
605 or
606 Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8)))))
607 with
608 Pre => Buf'Length >= Group_Byte_Offset (Idx, 44);
609
Nico Huber022e2262023-12-15 23:15:17 +0100610 ------------------------
611 -- Inode deserialization
612
Nico Hubercdc03512023-12-13 23:32:54 +0100613 subtype Inode_Off is Natural range 0 .. Inode_Size'Last;
614 function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural
615 is
616 (Natural (Idx) * State.Inode_Size + Off);
617
Nico Huber022e2262023-12-15 23:15:17 +0100618 function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16
619 is
620 (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0)))
621 with
622 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2);
623
624 function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64
625 is
626 ((if Mode = Regular
627 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32)
628 else 0) or
629 Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4))))
630 with
631 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112);
632
633 function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
634 is
635 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28)))
636 with
637 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32);
638
Nico Hubercdc03512023-12-13 23:32:54 +0100639 function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
640 is
641 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32)))
642 with
643 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36);
644
Nico Huber022e2262023-12-15 23:15:17 +0100645 function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
646 is
647 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104)))
648 with
649 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108);
650
Nico Hubercdc03512023-12-13 23:32:54 +0100651 Group : constant Group_Index := Group_Index ((Inode - 1) / State.Inodes_Per_Group);
652 Desc_Block : constant FSBlock_Offset :=
653 1 + State.First_Data_Block + FSBlock_Offset (Group / Desc_Per_Block);
654 Cache_Start, Cache_End : Max_Block_Index;
655 begin
656 Cache_FSBlock
657 (State => State,
658 Phys => Desc_Block,
659 Level => Natural'Min (Natural (Group / Desc_Per_Block), Block_Cache_Index'Last),
660 Cache_Start => Cache_Start,
661 Cache_End => Cache_End,
662 Success => Success);
663 if not Success then
664 return;
665 end if;
666
667 declare
668 Desc : constant Desc_In_Block_Index := Group mod Desc_Per_Block;
669 Inode_In_Group : constant Inode_Index := (Inode - 1) mod State.Inodes_Per_Group;
670 Inode_Block : constant FSBlock_Offset :=
671 Group_Inode_Table (State.Block_Cache (Cache_Start .. Cache_End), Group) +
672 FSBlock_Offset (Inode_In_Group / Inodes_Per_Block);
673 begin
674 Cache_FSBlock
675 (State => State,
676 Phys => Inode_Block,
677 Level => Block_Cache_Index'Last,
678 Cache_Start => Cache_Start,
679 Cache_End => Cache_End,
680 Success => Success);
681 if not Success then
682 return;
683 end if;
684
685 declare
Nico Huber022e2262023-12-15 23:15:17 +0100686 S_IFMT : constant := 8#170000#;
687 S_IFDIR : constant := 8#040000#;
688 S_IFREG : constant := 8#100000#;
689 S_IFLNK : constant := 8#120000#;
690
Nico Hubercdc03512023-12-13 23:32:54 +0100691 Inode_In_Block : constant Inode_Index := Inode_In_Group mod Inodes_Per_Block;
Nico Huber022e2262023-12-15 23:15:17 +0100692 I_Mode : constant Unsigned_16 :=
693 Inode_Mode (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100694 I_Flags : constant Unsigned_32 :=
695 Inode_Flags (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
696 begin
Nico Huber022e2262023-12-15 23:15:17 +0100697 case I_Mode and S_IFMT is
698 when S_IFDIR => State.Inode.Mode := Dir;
699 when S_IFREG => State.Inode.Mode := Regular;
700 when S_IFLNK => State.Inode.Mode := Link;
701 when others => Success := False; return;
702 end case;
703 if State.Inode.Mode = Link then
704 declare
705 I_File_ACL : constant Unsigned_32 := Inode_File_ACL (
706 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
707 I_Blocks : constant Unsigned_32 := Inode_Blocks (
708 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
709 begin
710 if (I_File_ACL = 0 and I_Blocks = 0) or
711 (I_File_ACL /= 0 and I_Blocks = 2 ** (State.Block_Size_Bits - 9))
712 then
713 State.Inode.Mode := Fast_Link;
714 end if;
715 end;
716 end if;
717 declare
718 I_Size : constant Unsigned_64 := Inode_Size (
719 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode);
720 begin
721 if I_Size <= Unsigned_64 (Inode_Length'Last) then
722 State.Inode.Size := Inode_Length (I_Size);
723 else
724 Success := False;
725 end if;
726 end;
Nico Hubercdc03512023-12-13 23:32:54 +0100727 State.Inode.Use_Extents := State.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
Nico Huber7403a542023-12-15 23:13:47 +0100728 State.Inode.Inline := State.Block_Cache (Cache_Start + 40 .. Cache_Start + 100 - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100729 State.Inode.I := Inode;
Nico Hubercdc03512023-12-13 23:32:54 +0100730 Reset_Cache_Logical (State);
731 State.S := File_Opened;
732 end;
733 end;
734 end Open;
735
736 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100737 (State : in out T;
738 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100739 File_Type : out FS.File_Type;
740 File_Name : in String;
741 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100742 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100743 is
Nico Huber21d42022023-12-16 02:50:22 +0100744 File_Name_Max : constant := 255;
745 Root_Inode : constant := 2;
746
747 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is
748 begin
749 for I in Str'Range loop
750 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
751 return False;
752 end if;
753 end loop;
754 return True;
755 end Str_Buf_Equal;
756
757 File_Inode : Inode_Index;
758 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100759 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100760 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100761 File_Type := FS.File_Type'First;
762
763 if File_Name'Length > File_Name_Max then
764 Success := False;
765 return;
766 end if;
767
768 -- Ensure dir is opened:
769 --
770 if State.S = File_Opened then
771 if State.Cur_Dir /= State.Inode.I then
772 Success := False;
773 return;
774 end if;
775 else
776 if In_Root then
777 State.Cur_Dir := Root_Inode;
778 end if;
779 Open (State, State.Cur_Dir, Success);
780 if not Success then
781 return;
782 end if;
783 end if;
784
785 -- Lookup file in opened dir:
786 --
787 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100788 Success := False;
Nico Huber21d42022023-12-16 02:50:22 +0100789 while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop
790 declare
791 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
792 subtype Dir_Entry_Index is Natural
793 range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1;
794 Dir_Entry : Buffer_Type (Dir_Entry_Index);
795 Entry_File_Pos : File_Offset := File_Pos;
796 Len : Natural;
797 begin
798 Read
799 (State => State,
800 File_Pos => Entry_File_Pos,
801 Buf => Dir_Entry (0 .. 7),
802 Len => Len);
803 if Len < Dir_Entry_Header_Length then
804 return;
805 end if;
806
807 -- Only check filenames of exact same length
808 if Read_LE32 (Dir_Entry, 0) > Root_Inode and then
809 File_Name'Length = Natural (Dir_Entry (6))
810 then
811 Read
812 (State => State,
813 File_Pos => Entry_File_Pos,
814 Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1),
815 Len => Len);
816 if Len < File_Name'Length then
817 return;
818 end if;
819
820 File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0));
821 Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1));
822 exit when Success;
823 end if;
824
825 File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4));
826 end;
827 end loop;
828
829 if Success then
830 Open (State, File_Inode, Success);
831 if not Success then
832 return;
833 end if;
834
835 if State.Inode.Mode = Dir then
836 State.Cur_Dir := File_Inode;
837 end if;
838 end if;
839
840 Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last);
841 if Success then
842 File_Len := File_Length (State.Inode.Size);
843 File_Type := (case State.Inode.Mode is
844 when Dir => FS.Dir,
845 when Regular => FS.Regular,
846 when Link .. Fast_Link => FS.Link);
847 else
848 Close (State);
849 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100850 end Open;
851
Nico Huber57d3a852023-12-04 15:42:40 +0100852 procedure Close (State : in out T) is
853 begin
854 State.S := Mounted;
855 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100856
Nico Huber57d3a852023-12-04 15:42:40 +0100857 procedure Read
858 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100859 File_Pos : in out File_Offset;
860 Buf : out Buffer_Type;
861 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100862 is
Nico Huberd3644be2023-12-16 01:43:00 +0100863 Pos : Natural;
Nico Huber1d7727f2023-11-30 15:58:46 +0100864 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100865 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100866 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
867 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100868 Len := 0;
869 else
Nico Huber549a1b82023-12-17 01:51:59 +0100870 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100871 end if;
872 Buf (Buf'First .. Buf'First + Len - 1) :=
873 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
874 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
875 File_Pos := File_Pos + File_Length (Len);
876 return;
877 end if;
878
Nico Huber1d7727f2023-11-30 15:58:46 +0100879 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100880 Pos := Buf'First;
881 while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop
882 declare
883 Block_Size : constant File_Length := 2 ** State.Block_Size_Bits;
884 In_Block : constant Max_Block_Index := Natural (File_Pos mod Block_Size);
885 Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / Block_Size);
886 In_Block_Space : constant Natural := Natural (Block_Size) - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100887 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huberd3644be2023-12-16 01:43:00 +0100888 In_Buf_Space : constant Natural := Buf'Last - Pos + 1;
889 Len_Here : Natural;
890 begin
891 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100892 if In_File_Space < Inode_Length (Len_Here) then
893 Len_Here := Natural (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100894 end if;
895 if In_Buf_Space < Len_Here then
896 Len_Here := In_Buf_Space;
897 end if;
898
899 declare
900 Last : constant Index_Type := Pos + Len_Here - 1;
901 Cache_Start, Cache_End : Max_Block_Index;
902 Physical : FSBlock_Offset;
903 Success : Boolean;
904 begin
905 if State.Inode.Use_Extents then
906 Extent_Block_Map (State, Logical, Physical, Success);
907 else
908 Ext2_Block_Map (State, Logical, Physical, Success);
909 end if;
910 if Success then
911 Cache_FSBlock
912 (State => State,
913 Phys => Physical,
914 Level => Block_Cache_Index'Last,
915 Cache_Start => Cache_Start,
916 Cache_End => Cache_End,
917 Success => Success);
918 end if;
919 exit when not Success;
920
921 Buf (Pos .. Last) := State.Block_Cache (
922 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
923 File_Pos := File_Pos + File_Length (Len_Here);
924 Pos := Pos + Len_Here;
925 Len := Len + Len_Here;
926 end;
927 end;
928 end loop;
929 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +0100930 end Read;
931
Nico Huber26f71832023-12-05 16:26:56 +0100932 --------------------------------------------------------------------------
933
934 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100935
936 function C_Mount return int
937 with
938 Export,
939 Convention => C,
940 External_Name => "ext2fs_mount";
941 function C_Mount return int
942 with
943 SPARK_Mode => Off
944 is
945 begin
946 return C.C_Mount;
947 end C_Mount;
948
Nico Huber3da21472023-12-18 15:43:35 +0100949 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100950 with
951 Export,
952 Convention => C,
953 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +0100954 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100955 with
956 SPARK_Mode => Off
957 is
958 begin
959 return C.C_Open (File_Path);
960 end C_Open;
961
962 function C_Read (Buf : System.Address; Len : int) return int
963 with
964 Export,
965 Convention => C,
966 External_Name => "ext2fs_read";
967 function C_Read (Buf : System.Address; Len : int) return int
968 with
969 SPARK_Mode => Off
970 is
971 begin
972 return C.C_Read (Buf, Len);
973 end C_Read;
974
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000975end FILO.FS.Ext2;