blob: 4dcde30016bbff46c7f5a88810c6ece806437d2b [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;
Nico Huber26f71832023-12-05 16:26:56 +010028 SUPERBLOCK_MAGIC : constant := 16#ef53#;
29 OLD_REV : constant := 0;
30 DYNAMIC_REV : constant := 1;
Nico Huber700a4112024-01-08 15:50:09 +010031
Nico Huber26f71832023-12-05 16:26:56 +010032 FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#;
33 FEATURE_INCOMPAT_64BIT : constant := 16#0080#;
34
Nico Hubercdc03512023-12-13 23:32:54 +010035 EXT4_EXTENTS_FL : constant := 16#8_0000#;
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);
Nico Huber700a4112024-01-08 15:50:09 +010067 State.Block_Size := 2 ** Log_Block_Size (S_Log_Block_Size + 10);
Nico Huber26f71832023-12-05 16:26:56 +010068 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
Nico Huber5a042fd2024-01-08 15:54:57 +010092 if Natural (S_Inode_Size) in Inode_Size then
Nico Huber26f71832023-12-05 16:26:56 +010093 State.Inode_Size := Inode_Size (S_Inode_Size);
94 else
95 Success := False;
96 return;
97 end if;
98 end;
99 else
100 State.Inode_Size := Inode_Size'First;
101 end if;
102 end;
103
104 declare
105 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
106 begin
107 State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
108 State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
109 if State.Feature_64Bit then
110 declare
111 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
112 begin
Nico Huber700a4112024-01-08 15:50:09 +0100113 if Natural (S_Desc_Size) in Desc_Size and
114 Natural (S_Desc_Size) <= State.Block_Size and
115 Is_Power_Of_2 (S_Desc_Size)
Nico Huber26f71832023-12-05 16:26:56 +0100116 then
117 State.Desc_Size := Desc_Size (S_Desc_Size);
118 else
119 Success := False;
120 return;
121 end if;
122 end;
Nico Huber77a04d72023-12-13 23:11:40 +0100123 State.Feature_64Bit := State.Feature_64Bit and State.Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100124 else
125 State.Desc_Size := Desc_Size'First;
126 end if;
127 end;
128
129 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100130 end Mount;
131
Nico Huberf5d99d02023-12-12 13:42:55 +0100132 procedure Read_FSBlock
Nico Huber700a4112024-01-08 15:50:09 +0100133 (Buf : in out Buffer_Type;
Nico Huberf5d99d02023-12-12 13:42:55 +0100134 FSBlock : in FSBlock_Offset;
Nico Huber700a4112024-01-08 15:50:09 +0100135 Part_Len : in Partition_Length;
Nico Huberf5d99d02023-12-12 13:42:55 +0100136 Success : out Boolean)
137 with
Nico Huber700a4112024-01-08 15:50:09 +0100138 Pre => Buf'Length in Block_Size
Nico Huberf5d99d02023-12-12 13:42:55 +0100139 is
Nico Huber700a4112024-01-08 15:50:09 +0100140 FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock);
141 Block_Size : constant Integer_64 := Integer_64 (Buf'Length);
142 Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1;
Nico Huberf5d99d02023-12-12 13:42:55 +0100143 begin
Nico Huber700a4112024-01-08 15:50:09 +0100144 if FSBlock_64 > Max_Block_Offset then
Nico Huberf5d99d02023-12-12 13:42:55 +0100145 Success := False;
146 return;
147 end if;
Nico Huber700a4112024-01-08 15:50:09 +0100148 Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success);
Nico Huberf5d99d02023-12-12 13:42:55 +0100149 end Read_FSBlock;
150
Nico Huber68c86932023-12-13 11:03:11 +0100151 procedure Cache_FSBlock
152 (State : in out T;
153 Phys : in FSBlock_Offset;
154 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100155 Label : in Cache_Label;
156 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100157 Cache_Start : out Max_Block_Index;
158 Cache_End : out Max_Block_Index;
159 Success : out Boolean)
160 with
Nico Huber700a4112024-01-08 15:50:09 +0100161 Post => Cache_End = Cache_Start + State.Block_Size - 1
Nico Huber68c86932023-12-13 11:03:11 +0100162 is
Nico Huber68c86932023-12-13 11:03:11 +0100163 -- Limit cache usage depending on block size:
Nico Huber700a4112024-01-08 15:50:09 +0100164 Max_Level : constant Block_Cache_Index := Block_Size'Last / State.Block_Size - 1;
165 Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level);
Nico Huber68c86932023-12-13 11:03:11 +0100166 begin
Nico Huber700a4112024-01-08 15:50:09 +0100167 Cache_Start := Cache_Level * State.Block_Size;
168 Cache_End := Cache_Start + State.Block_Size - 1;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100169 if State.Block_Cache_Logical (Cache_Level) = Logical and
170 State.Block_Cache_Label (Cache_Level) = Label
171 then
Nico Huber68c86932023-12-13 11:03:11 +0100172 Success := True;
173 else
174 Read_FSBlock
Nico Huber700a4112024-01-08 15:50:09 +0100175 (Buf => State.Block_Cache (Cache_Start .. Cache_End),
Nico Huber68c86932023-12-13 11:03:11 +0100176 FSBlock => Phys,
Nico Huber700a4112024-01-08 15:50:09 +0100177 Part_Len => State.Part_Len,
Nico Huber68c86932023-12-13 11:03:11 +0100178 Success => Success);
Nico Huber57dfbfb2023-12-13 23:24:16 +0100179 State.Block_Cache_Logical (Cache_Level) := Logical;
180 State.Block_Cache_Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100181 end if;
182 end Cache_FSBlock;
183
Nico Huber57dfbfb2023-12-13 23:24:16 +0100184 procedure Cache_FSBlock
185 (State : in out T;
186 Phys : in FSBlock_Offset;
187 Level : in Block_Cache_Index;
188 Cache_Start : out Max_Block_Index;
189 Cache_End : out Max_Block_Index;
190 Success : out Boolean)
191 with
Nico Huber700a4112024-01-08 15:50:09 +0100192 Post => Cache_End = Cache_Start + State.Block_Size - 1
Nico Huber57dfbfb2023-12-13 23:24:16 +0100193 is
194 begin
195 Cache_FSBlock (State, Phys, Level, Cache_Label (Phys),
196 False, Cache_Start, Cache_End, Success);
197 end Cache_FSBlock;
198
199 procedure Reset_Cache_Logical (State : in out T) is
200 begin
201 for I in Block_Cache_Index loop
202 if State.Block_Cache_Logical (I) then
203 State.Block_Cache_Logical (I) := False;
204 State.Block_Cache_Label (I) := 0;
205 end if;
206 end loop;
207 end Reset_Cache_Logical;
208
Nico Huber6623c982023-12-12 16:35:46 +0100209 procedure Ext2_Block_Map
210 (State : in out T;
211 Logical : in FSBlock_Logical;
212 Physical : out FSBlock_Offset;
213 Success : out Boolean)
214 is
Nico Huber33f6d952023-12-13 23:16:42 +0100215 Direct_Blocks : constant := 12;
216 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
217 type Inode_Blocks is record
218 Direct_Blocks : Direct_Blocks_Array;
219 Indirect_Block : Unsigned_32;
220 Double_Indirect : Unsigned_32;
221 Triple_Indirect : Unsigned_32;
222 end record
Nico Huber5a042fd2024-01-08 15:54:57 +0100223 with Object_Size => Inode_Extents'Length * 8;
Nico Huber33f6d952023-12-13 23:16:42 +0100224
225 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100226 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100227
Nico Huber700a4112024-01-08 15:50:09 +0100228 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (State.Block_Size / 4);
229 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4);
Nico Huber6623c982023-12-12 16:35:46 +0100230 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
231
232 procedure Indirect_Block_Lookup
233 (Indirect_Block_Phys : in FSBlock_Offset;
234 Addr_In_Block : in Addr_In_Block_Range;
235 Level : in Block_Cache_Index;
236 Logical_Off : in FSBlock_Logical;
237 Next_Physical : out FSBlock_Offset;
238 Success : out Boolean)
239 with
Nico Huber5a042fd2024-01-08 15:54:57 +0100240 Pre =>
241 Addr_Per_Block = FSBlock_Logical (State.Block_Size / 4) and
242 FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
Nico Huber6623c982023-12-12 16:35:46 +0100243 is
Nico Huber68c86932023-12-13 11:03:11 +0100244 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100245 begin
Nico Huber68c86932023-12-13 11:03:11 +0100246 Cache_FSBlock
247 (State => State,
248 Phys => Indirect_Block_Phys,
249 Level => Level,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100250 Label => Cache_Label (Logical_Off),
Nico Huber68c86932023-12-13 11:03:11 +0100251 Cache_Start => Cache_Start,
252 Cache_End => Cache_End,
253 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100254 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huberfe897122023-12-12 21:33:36 +0100255 (Buf => State.Block_Cache (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100256 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100257 end Indirect_Block_Lookup;
258
259 Logical_Rest : FSBlock_Logical := Logical;
Nico Huber5a042fd2024-01-08 15:54:57 +0100260
261 pragma Assert (Addr_Per_Block <= 2 ** 14);
Nico Huber6623c982023-12-12 16:35:46 +0100262 begin
263 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100264 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100265 Success := True;
266 return;
267 end if;
268
269 Logical_Rest := Logical_Rest - Direct_Blocks;
270 if Logical_Rest < Addr_Per_Block then
271 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100272 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100273 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100274 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100275 Logical_Off => Logical - Logical_Rest,
276 Next_Physical => Physical,
277 Success => Success);
278 return;
279 end if;
280
281 Logical_Rest := Logical_Rest - Addr_Per_Block;
282 if Logical_Rest < Addr_Per_Block ** 2 then
283 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100284 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100285 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100286 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100287 Logical_Off => Logical - Logical_Rest,
288 Next_Physical => Physical,
289 Success => Success);
290 if not Success then
291 return;
292 end if;
293
294 Indirect_Block_Lookup
295 (Indirect_Block_Phys => Physical,
296 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100297 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100298 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
299 Next_Physical => Physical,
300 Success => Success);
301 return;
302 end if;
303
304 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
305 if Logical_Rest < Addr_Per_Block ** 3 then
306 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100307 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100308 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100309 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100310 Logical_Off => Logical - Logical_Rest,
311 Next_Physical => Physical,
312 Success => Success);
313 if not Success then
314 return;
315 end if;
316
317 Indirect_Block_Lookup
318 (Indirect_Block_Phys => Physical,
Nico Huber5a042fd2024-01-08 15:54:57 +0100319 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100320 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100321 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
322 Next_Physical => Physical,
323 Success => Success);
324 if not Success then
325 return;
326 end if;
327
328 Indirect_Block_Lookup
329 (Indirect_Block_Phys => Physical,
330 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100331 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100332 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
333 Next_Physical => Physical,
334 Success => Success);
335 return;
336 end if;
337
338 -- Logical address was just too high.
Nico Huber5a042fd2024-01-08 15:54:57 +0100339 Physical := 0;
Nico Huber6623c982023-12-12 16:35:46 +0100340 Success := False;
341 end Ext2_Block_Map;
342
Nico Huberfffc8c12023-12-13 12:44:12 +0100343 procedure Extent_Block_Map
344 (State : in out T;
345 Logical : in FSBlock_Logical;
346 Physical : out FSBlock_Offset;
347 Success : out Boolean)
348 is
349 -- Extent blocks always start with a 12B header and contain 12B entries.
350 -- Every entry starts with the number of the first logical block it
351 -- covers. Entries are sorted by this number.
352 -- Depth > 0 blocks have index entries, referencing further extent blocks.
353 -- Depth = 0 blocks have extent entries, referencing a contiguous range
354 -- of data blocks.
355 --
356 -- +-----------------+
357 -- .-> | Hdr depth=0 |
358 -- | +-----------------+
359 -- | | Extent 0.. 12 |
360 -- +-------------+ | +-----------------+
361 -- | Hdr depth=1 | | | Extent 13.. 13 |
362 -- +-------------+ | +-----------------+
363 -- | Index 0 | --' | Extent 14..122 |
364 -- +-------------+ +-----------------+
365 -- | Index 123 | --.
366 -- +-------------+ | +-----------------+
367 -- `-> | Hdr depth=0 |
368 -- +-----------------+
369 -- | Extent 123..125 |
370 -- +-----------------+
371 -- | Extent 126..234 |
372 -- +-----------------+
373 --
374
375 Extent_Header_Size : constant := 12;
376 Extent_Header_Magic : constant := 16#f03a#;
377 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
378 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
Nico Huber700a4112024-01-08 15:50:09 +0100379 Dynamic_Max_Index : constant Extent_Idx := State.Block_Size / Extent_Header_Size - 1;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100380 subtype Extent_Depth is Natural range 0 .. 32;
Nico Huberfffc8c12023-12-13 12:44:12 +0100381
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
Nico Huber6710c0e2024-01-08 15:56:24 +0100437 Pre =>
438 Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then
439 Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then
440 Extent_Logical (Buf, 1) <= Logical,
441 Post =>
442 Bin_Search'Result in 1 .. Refs and
443 Extent_Logical (Buf, Bin_Search'Result) <= Logical
Nico Huberfffc8c12023-12-13 12:44:12 +0100444 is
Nico Huber6710c0e2024-01-08 15:56:24 +0100445 Left : Positive := 2;
Nico Huberfffc8c12023-12-13 12:44:12 +0100446 Right : Extent_Idx := Refs;
447 begin
448 while Left <= Right loop
449 declare
450 Mid : constant Extent_Idx := (Left + Right) / 2;
451 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
452 begin
453 if Logical < Ext_Logical then
454 Right := Mid - 1;
455 else
456 Left := Mid + 1;
457 end if;
Nico Huber6710c0e2024-01-08 15:56:24 +0100458 pragma Loop_Invariant
459 (Right <= Refs and then
460 Left in 2 .. Refs + 1 and then
461 Extent_Logical (Buf, Left - 1) <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100462 end;
463 end loop;
464 return Left - 1;
465 end Bin_Search;
466
467 procedure Next_Ref
468 (Current : in FSBlock_Offset;
469 Logical_Off : in FSBlock_Logical;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100470 Depth : in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100471 Next : out Extent_Idx;
472 Cache_Start : out Max_Block_Index;
473 Cache_End : out Max_Block_Index;
474 Success : out Boolean)
475 with
476 Pre => Logical_Off <= Logical,
477 Post => (if Success then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100478 Next <= Dynamic_Max_Index and then
Nico Huber700a4112024-01-08 15:50:09 +0100479 Cache_End = Cache_Start + State.Block_Size - 1 and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100480 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical)
481 is
Nico Huberfffc8c12023-12-13 12:44:12 +0100482 begin
483 Cache_FSBlock
484 (State => State,
485 Phys => Current,
486 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100487 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100488 Cache_Start => Cache_Start,
489 Cache_End => Cache_End,
490 Success => Success);
491 if not Success then
492 Next := 1;
493 return;
494 end if;
495
496 declare
497 Hdr_Magic : constant Unsigned_16 :=
498 Header_Magic (State.Block_Cache (Cache_Start .. Cache_End));
499 Hdr_Entries : constant Natural :=
500 Header_Entries (State.Block_Cache (Cache_Start .. Cache_End));
501 Hdr_Depth : constant Natural :=
502 Header_Depth (State.Block_Cache (Cache_Start .. Cache_End));
503 First_Logical : constant FSBlock_Logical :=
504 Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1);
505 begin
506 Success := Success and then
507 Hdr_Magic = Extent_Header_Magic and then
508 Hdr_Depth = Depth and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100509 Hdr_Entries in Extent_Idx and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100510 Hdr_Entries <= Dynamic_Max_Index and then
511 First_Logical = Logical_Off;
512 if not Success then
513 Next := 1;
514 else
515 Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries);
516 end if;
517 end;
518 end Next_Ref;
519
Nico Huber7403a542023-12-15 23:13:47 +0100520 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
521 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
522 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
523 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
524 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100525
526 Cache_Start, Cache_End : Max_Block_Index;
527 Logical_Off, Length : FSBlock_Logical;
528 Idx : Extent_Idx;
529 begin
530 Success :=
531 Inode_Magic = Extent_Header_Magic and then
532 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100533 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100534 First_Logical <= Logical and then
535 Depth in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100536 if not Success then
537 Physical := 0;
538 return;
539 end if;
540
Nico Huber7403a542023-12-15 23:13:47 +0100541 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100542 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100543 Physical := Extent_Physical (Inline_Extents, Idx);
544 Logical_Off := Extent_Logical (Inline_Extents, Idx);
545 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100546 else
Nico Huber7403a542023-12-15 23:13:47 +0100547 Physical := Index_Physical (Inline_Extents, Idx);
548 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100549 loop
Nico Huber96a0b0e2024-01-08 15:57:09 +0100550 pragma Loop_Invariant
551 (Depth > 0 and then
552 Depth in Extent_Depth and then
553 Logical_Off <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100554 Depth := Depth - 1;
555 Next_Ref
556 (Current => Physical,
557 Logical_Off => Logical_Off,
558 Depth => Depth,
559 Next => Idx,
560 Cache_Start => Cache_Start,
561 Cache_End => Cache_End,
562 Success => Success);
563 if not Success then
564 return;
565 end if;
566
567 exit when Depth = 0;
568 Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
569 Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
570 end loop;
571
572 Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
573 Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx);
574 Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx);
575 end if;
576
577 Success :=
578 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100579 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100580 Logical < Logical_Off + Length;
581 if Success then
582 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
583 end if;
584 end Extent_Block_Map;
585
Nico Huber57d3a852023-12-04 15:42:40 +0100586 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100587 (State : in out T;
588 Inode : in Inode_Index;
589 Success : out Boolean)
590 with
591 Pre => Is_Mounted (State) and not Is_Open (State),
592 Post => Success = Is_Open (State)
593 is
594 type Group_Index is new Natural;
595 subtype Desc_In_Block_Index is Group_Index
596 range 0 .. Group_Index (2 ** Log_Block_Size'Last / Desc_Size'First - 1);
597
Nico Huber700a4112024-01-08 15:50:09 +0100598 Inodes_Per_Block : constant Inode_Index := Inode_Index (State.Block_Size / State.Inode_Size);
599 Desc_Per_Block : constant Group_Index := Group_Index (State.Block_Size / State.Desc_Size);
Nico Hubercdc03512023-12-13 23:32:54 +0100600
Nico Huber022e2262023-12-15 23:15:17 +0100601 ------------------------
602 -- Group deserialization
603
Nico Hubercdc03512023-12-13 23:32:54 +0100604 subtype Group_Off is Natural range 0 .. Desc_Size'Last;
605 function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural
606 is
607 (Natural (Idx) * State.Desc_Size + Off);
608
609 function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset
610 is
611 (FSBlock_Offset (
612 (if State.Feature_64Bit
613 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32)
614 else 0)
615 or
616 Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8)))))
617 with
618 Pre => Buf'Length >= Group_Byte_Offset (Idx, 44);
619
Nico Huber022e2262023-12-15 23:15:17 +0100620 ------------------------
621 -- Inode deserialization
622
Nico Hubercdc03512023-12-13 23:32:54 +0100623 subtype Inode_Off is Natural range 0 .. Inode_Size'Last;
624 function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural
625 is
626 (Natural (Idx) * State.Inode_Size + Off);
627
Nico Huber022e2262023-12-15 23:15:17 +0100628 function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16
629 is
630 (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0)))
631 with
632 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2);
633
634 function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64
635 is
636 ((if Mode = Regular
637 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32)
638 else 0) or
639 Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4))))
640 with
641 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112);
642
643 function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
644 is
645 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28)))
646 with
647 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32);
648
Nico Hubercdc03512023-12-13 23:32:54 +0100649 function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
650 is
651 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32)))
652 with
653 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36);
654
Nico Huber022e2262023-12-15 23:15:17 +0100655 function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
656 is
657 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104)))
658 with
659 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108);
660
Nico Hubercdc03512023-12-13 23:32:54 +0100661 Group : constant Group_Index := Group_Index ((Inode - 1) / State.Inodes_Per_Group);
662 Desc_Block : constant FSBlock_Offset :=
663 1 + State.First_Data_Block + FSBlock_Offset (Group / Desc_Per_Block);
664 Cache_Start, Cache_End : Max_Block_Index;
665 begin
666 Cache_FSBlock
667 (State => State,
668 Phys => Desc_Block,
669 Level => Natural'Min (Natural (Group / Desc_Per_Block), Block_Cache_Index'Last),
670 Cache_Start => Cache_Start,
671 Cache_End => Cache_End,
672 Success => Success);
673 if not Success then
674 return;
675 end if;
676
677 declare
678 Desc : constant Desc_In_Block_Index := Group mod Desc_Per_Block;
679 Inode_In_Group : constant Inode_Index := (Inode - 1) mod State.Inodes_Per_Group;
680 Inode_Block : constant FSBlock_Offset :=
681 Group_Inode_Table (State.Block_Cache (Cache_Start .. Cache_End), Group) +
682 FSBlock_Offset (Inode_In_Group / Inodes_Per_Block);
683 begin
684 Cache_FSBlock
685 (State => State,
686 Phys => Inode_Block,
687 Level => Block_Cache_Index'Last,
688 Cache_Start => Cache_Start,
689 Cache_End => Cache_End,
690 Success => Success);
691 if not Success then
692 return;
693 end if;
694
695 declare
Nico Huber022e2262023-12-15 23:15:17 +0100696 S_IFMT : constant := 8#170000#;
697 S_IFDIR : constant := 8#040000#;
698 S_IFREG : constant := 8#100000#;
699 S_IFLNK : constant := 8#120000#;
700
Nico Hubercdc03512023-12-13 23:32:54 +0100701 Inode_In_Block : constant Inode_Index := Inode_In_Group mod Inodes_Per_Block;
Nico Huber022e2262023-12-15 23:15:17 +0100702 I_Mode : constant Unsigned_16 :=
703 Inode_Mode (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100704 I_Flags : constant Unsigned_32 :=
705 Inode_Flags (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
706 begin
Nico Huber022e2262023-12-15 23:15:17 +0100707 case I_Mode and S_IFMT is
708 when S_IFDIR => State.Inode.Mode := Dir;
709 when S_IFREG => State.Inode.Mode := Regular;
710 when S_IFLNK => State.Inode.Mode := Link;
711 when others => Success := False; return;
712 end case;
713 if State.Inode.Mode = Link then
714 declare
715 I_File_ACL : constant Unsigned_32 := Inode_File_ACL (
716 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
717 I_Blocks : constant Unsigned_32 := Inode_Blocks (
718 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block);
719 begin
720 if (I_File_ACL = 0 and I_Blocks = 0) or
721 (I_File_ACL /= 0 and I_Blocks = 2 ** (State.Block_Size_Bits - 9))
722 then
723 State.Inode.Mode := Fast_Link;
724 end if;
725 end;
726 end if;
727 declare
728 I_Size : constant Unsigned_64 := Inode_Size (
729 State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode);
730 begin
731 if I_Size <= Unsigned_64 (Inode_Length'Last) then
732 State.Inode.Size := Inode_Length (I_Size);
733 else
734 Success := False;
735 end if;
736 end;
Nico Hubercdc03512023-12-13 23:32:54 +0100737 State.Inode.Use_Extents := State.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
Nico Huber7403a542023-12-15 23:13:47 +0100738 State.Inode.Inline := State.Block_Cache (Cache_Start + 40 .. Cache_Start + 100 - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100739 State.Inode.I := Inode;
Nico Hubercdc03512023-12-13 23:32:54 +0100740 Reset_Cache_Logical (State);
741 State.S := File_Opened;
742 end;
743 end;
744 end Open;
745
746 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100747 (State : in out T;
748 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100749 File_Type : out FS.File_Type;
750 File_Name : in String;
751 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100752 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100753 is
Nico Huber21d42022023-12-16 02:50:22 +0100754 File_Name_Max : constant := 255;
755 Root_Inode : constant := 2;
756
757 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is
758 begin
759 for I in Str'Range loop
760 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
761 return False;
762 end if;
763 end loop;
764 return True;
765 end Str_Buf_Equal;
766
767 File_Inode : Inode_Index;
768 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100769 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100770 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100771 File_Type := FS.File_Type'First;
772
773 if File_Name'Length > File_Name_Max then
774 Success := False;
775 return;
776 end if;
777
778 -- Ensure dir is opened:
779 --
780 if State.S = File_Opened then
781 if State.Cur_Dir /= State.Inode.I then
782 Success := False;
783 return;
784 end if;
785 else
786 if In_Root then
787 State.Cur_Dir := Root_Inode;
788 end if;
789 Open (State, State.Cur_Dir, Success);
790 if not Success then
791 return;
792 end if;
793 end if;
794
795 -- Lookup file in opened dir:
796 --
797 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100798 Success := False;
Nico Huber21d42022023-12-16 02:50:22 +0100799 while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop
800 declare
801 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
802 subtype Dir_Entry_Index is Natural
803 range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1;
804 Dir_Entry : Buffer_Type (Dir_Entry_Index);
805 Entry_File_Pos : File_Offset := File_Pos;
806 Len : Natural;
807 begin
808 Read
809 (State => State,
810 File_Pos => Entry_File_Pos,
811 Buf => Dir_Entry (0 .. 7),
812 Len => Len);
813 if Len < Dir_Entry_Header_Length then
814 return;
815 end if;
816
817 -- Only check filenames of exact same length
818 if Read_LE32 (Dir_Entry, 0) > Root_Inode and then
819 File_Name'Length = Natural (Dir_Entry (6))
820 then
821 Read
822 (State => State,
823 File_Pos => Entry_File_Pos,
824 Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1),
825 Len => Len);
826 if Len < File_Name'Length then
827 return;
828 end if;
829
830 File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0));
831 Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1));
832 exit when Success;
833 end if;
834
835 File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4));
836 end;
837 end loop;
838
839 if Success then
840 Open (State, File_Inode, Success);
841 if not Success then
842 return;
843 end if;
844
845 if State.Inode.Mode = Dir then
846 State.Cur_Dir := File_Inode;
847 end if;
848 end if;
849
850 Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last);
851 if Success then
852 File_Len := File_Length (State.Inode.Size);
853 File_Type := (case State.Inode.Mode is
854 when Dir => FS.Dir,
855 when Regular => FS.Regular,
856 when Link .. Fast_Link => FS.Link);
857 else
858 Close (State);
859 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100860 end Open;
861
Nico Huber57d3a852023-12-04 15:42:40 +0100862 procedure Close (State : in out T) is
863 begin
864 State.S := Mounted;
865 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100866
Nico Huber57d3a852023-12-04 15:42:40 +0100867 procedure Read
868 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100869 File_Pos : in out File_Offset;
870 Buf : out Buffer_Type;
871 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100872 is
Nico Huberd3644be2023-12-16 01:43:00 +0100873 Pos : Natural;
Nico Huber1d7727f2023-11-30 15:58:46 +0100874 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100875 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100876 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
877 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100878 Len := 0;
879 else
Nico Huber549a1b82023-12-17 01:51:59 +0100880 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100881 end if;
882 Buf (Buf'First .. Buf'First + Len - 1) :=
883 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
884 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
885 File_Pos := File_Pos + File_Length (Len);
886 return;
887 end if;
888
Nico Huber1d7727f2023-11-30 15:58:46 +0100889 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100890 Pos := Buf'First;
891 while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop
892 declare
Nico Huber700a4112024-01-08 15:50:09 +0100893 In_Block : constant Max_Block_Index := Natural (File_Pos) mod State.Block_Size;
894 Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / File_Offset (State.Block_Size));
895 In_Block_Space : constant Natural := Natural (State.Block_Size) - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100896 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huberd3644be2023-12-16 01:43:00 +0100897 In_Buf_Space : constant Natural := Buf'Last - Pos + 1;
898 Len_Here : Natural;
899 begin
900 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100901 if In_File_Space < Inode_Length (Len_Here) then
902 Len_Here := Natural (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100903 end if;
904 if In_Buf_Space < Len_Here then
905 Len_Here := In_Buf_Space;
906 end if;
907
908 declare
909 Last : constant Index_Type := Pos + Len_Here - 1;
910 Cache_Start, Cache_End : Max_Block_Index;
911 Physical : FSBlock_Offset;
912 Success : Boolean;
913 begin
914 if State.Inode.Use_Extents then
915 Extent_Block_Map (State, Logical, Physical, Success);
916 else
917 Ext2_Block_Map (State, Logical, Physical, Success);
918 end if;
919 if Success then
920 Cache_FSBlock
921 (State => State,
922 Phys => Physical,
923 Level => Block_Cache_Index'Last,
924 Cache_Start => Cache_Start,
925 Cache_End => Cache_End,
926 Success => Success);
927 end if;
928 exit when not Success;
929
930 Buf (Pos .. Last) := State.Block_Cache (
931 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
932 File_Pos := File_Pos + File_Length (Len_Here);
933 Pos := Pos + Len_Here;
934 Len := Len + Len_Here;
935 end;
936 end;
937 end loop;
938 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +0100939 end Read;
940
Nico Huber26f71832023-12-05 16:26:56 +0100941 --------------------------------------------------------------------------
942
943 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100944
945 function C_Mount return int
946 with
947 Export,
948 Convention => C,
949 External_Name => "ext2fs_mount";
950 function C_Mount return int
951 with
952 SPARK_Mode => Off
953 is
954 begin
955 return C.C_Mount;
956 end C_Mount;
957
Nico Huber3da21472023-12-18 15:43:35 +0100958 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100959 with
960 Export,
961 Convention => C,
962 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +0100963 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100964 with
965 SPARK_Mode => Off
966 is
967 begin
968 return C.C_Open (File_Path);
969 end C_Open;
970
971 function C_Read (Buf : System.Address; Len : int) return int
972 with
973 Export,
974 Convention => C,
975 External_Name => "ext2fs_read";
976 function C_Read (Buf : System.Address; Len : int) return int
977 with
978 SPARK_Mode => Off
979 is
980 begin
981 return C.C_Read (Buf, Len);
982 end C_Read;
983
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000984end FILO.FS.Ext2;