blob: deb2d2116063c28eb1fc39b20a90beae84d436c5 [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 Huber52f4c8d2024-01-09 13:57:33 +010042 Static : Mount_State renames State.Static;
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
Nico Huber52f4c8d2024-01-09 13:57:33 +010060 Static.Part_Len := Part_Len;
61 Static.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
Nico Huber52f4c8d2024-01-09 13:57:33 +010067 Static.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10);
68 Static.Block_Size := 2 ** Log_Block_Size (S_Log_Block_Size + 10);
Nico Huber26f71832023-12-05 16:26:56 +010069 else
70 Success := False;
71 return;
72 end if;
73 end;
74
75 declare
76 S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4);
77 begin
Nico Huber21d42022023-12-16 02:50:22 +010078 if S_Inodes_Per_Group in 2 .. Unsigned_32'Last then
Nico Huber52f4c8d2024-01-09 13:57:33 +010079 Static.Inodes_Per_Group := Inode_Index (S_Inodes_Per_Group);
Nico Huber26f71832023-12-05 16:26:56 +010080 else
81 Success := False;
82 return;
83 end if;
84 end;
85
86 declare
87 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
88 begin
89 if S_Rev_Level >= DYNAMIC_REV then
90 declare
91 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
92 begin
Nico Huber5a042fd2024-01-08 15:54:57 +010093 if Natural (S_Inode_Size) in Inode_Size then
Nico Huber52f4c8d2024-01-09 13:57:33 +010094 Static.Inode_Size := Inode_Size (S_Inode_Size);
Nico Huber26f71832023-12-05 16:26:56 +010095 else
96 Success := False;
97 return;
98 end if;
99 end;
100 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100101 Static.Inode_Size := Inode_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100102 end if;
103 end;
104
105 declare
106 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
107 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100108 Static.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
109 Static.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
110 if Static.Feature_64Bit then
Nico Huber26f71832023-12-05 16:26:56 +0100111 declare
112 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
113 begin
Nico Huber700a4112024-01-08 15:50:09 +0100114 if Natural (S_Desc_Size) in Desc_Size and
Nico Huber52f4c8d2024-01-09 13:57:33 +0100115 Natural (S_Desc_Size) <= Static.Block_Size and
Nico Huber700a4112024-01-08 15:50:09 +0100116 Is_Power_Of_2 (S_Desc_Size)
Nico Huber26f71832023-12-05 16:26:56 +0100117 then
Nico Huber52f4c8d2024-01-09 13:57:33 +0100118 Static.Desc_Size := Desc_Size (S_Desc_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100119 else
120 Success := False;
121 return;
122 end if;
123 end;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100124 Static.Feature_64Bit := Static.Feature_64Bit and Static.Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100125 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100126 Static.Desc_Size := Desc_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100127 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
Nico Huber700a4112024-01-08 15:50:09 +0100134 (Buf : in out Buffer_Type;
Nico Huberf5d99d02023-12-12 13:42:55 +0100135 FSBlock : in FSBlock_Offset;
Nico Huber700a4112024-01-08 15:50:09 +0100136 Part_Len : in Partition_Length;
Nico Huberf5d99d02023-12-12 13:42:55 +0100137 Success : out Boolean)
138 with
Nico Huber700a4112024-01-08 15:50:09 +0100139 Pre => Buf'Length in Block_Size
Nico Huberf5d99d02023-12-12 13:42:55 +0100140 is
Nico Huber700a4112024-01-08 15:50:09 +0100141 FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock);
142 Block_Size : constant Integer_64 := Integer_64 (Buf'Length);
143 Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1;
Nico Huberf5d99d02023-12-12 13:42:55 +0100144 begin
Nico Huber700a4112024-01-08 15:50:09 +0100145 if FSBlock_64 > Max_Block_Offset then
Nico Huberf5d99d02023-12-12 13:42:55 +0100146 Success := False;
147 return;
148 end if;
Nico Huber700a4112024-01-08 15:50:09 +0100149 Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success);
Nico Huberf5d99d02023-12-12 13:42:55 +0100150 end Read_FSBlock;
151
Nico Huber68c86932023-12-13 11:03:11 +0100152 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100153 (Static : in Mount_State;
154 Cache : in out Block_Cache;
Nico Huber68c86932023-12-13 11:03:11 +0100155 Phys : in FSBlock_Offset;
156 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100157 Label : in Cache_Label;
158 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100159 Cache_Start : out Max_Block_Index;
160 Cache_End : out Max_Block_Index;
161 Success : out Boolean)
162 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100163 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber68c86932023-12-13 11:03:11 +0100164 is
Nico Huber68c86932023-12-13 11:03:11 +0100165 -- Limit cache usage depending on block size:
Nico Huber52f4c8d2024-01-09 13:57:33 +0100166 Max_Level : constant Block_Cache_Index := Block_Size'Last / Static.Block_Size - 1;
Nico Huber700a4112024-01-08 15:50:09 +0100167 Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level);
Nico Huber68c86932023-12-13 11:03:11 +0100168 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100169 Cache_Start := Cache_Level * Static.Block_Size;
170 Cache_End := Cache_Start + Static.Block_Size - 1;
171 if Cache.Logical (Cache_Level) = Logical and
172 Cache.Label (Cache_Level) = Label
Nico Huber57dfbfb2023-12-13 23:24:16 +0100173 then
Nico Huber68c86932023-12-13 11:03:11 +0100174 Success := True;
175 else
176 Read_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100177 (Buf => Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber68c86932023-12-13 11:03:11 +0100178 FSBlock => Phys,
Nico Huber52f4c8d2024-01-09 13:57:33 +0100179 Part_Len => Static.Part_Len,
Nico Huber68c86932023-12-13 11:03:11 +0100180 Success => Success);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100181 Cache.Logical (Cache_Level) := Logical; -- FIXME: Level needs to be part of Label
182 Cache.Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100183 end if;
184 end Cache_FSBlock;
185
Nico Huber57dfbfb2023-12-13 23:24:16 +0100186 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100187 (Static : in Mount_State;
188 Cache : in out Block_Cache;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100189 Phys : in FSBlock_Offset;
190 Level : in Block_Cache_Index;
191 Cache_Start : out Max_Block_Index;
192 Cache_End : out Max_Block_Index;
193 Success : out Boolean)
194 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100195 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber57dfbfb2023-12-13 23:24:16 +0100196 is
197 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100198 Cache_FSBlock (Static, Cache, Phys, Level, Cache_Label (Phys),
Nico Huber57dfbfb2023-12-13 23:24:16 +0100199 False, Cache_Start, Cache_End, Success);
200 end Cache_FSBlock;
201
Nico Huber52f4c8d2024-01-09 13:57:33 +0100202 procedure Reset_Cache_Logical (Cache : in out Block_Cache) is
Nico Huber57dfbfb2023-12-13 23:24:16 +0100203 begin
204 for I in Block_Cache_Index loop
Nico Huber52f4c8d2024-01-09 13:57:33 +0100205 if Cache.Logical (I) then
206 Cache.Logical (I) := False;
207 Cache.Label (I) := 0;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100208 end if;
209 end loop;
210 end Reset_Cache_Logical;
211
Nico Huber6623c982023-12-12 16:35:46 +0100212 procedure Ext2_Block_Map
213 (State : in out T;
214 Logical : in FSBlock_Logical;
215 Physical : out FSBlock_Offset;
216 Success : out Boolean)
217 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100218 Static : constant Mount_State := State.Static;
219
Nico Huber33f6d952023-12-13 23:16:42 +0100220 Direct_Blocks : constant := 12;
221 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
222 type Inode_Blocks is record
223 Direct_Blocks : Direct_Blocks_Array;
224 Indirect_Block : Unsigned_32;
225 Double_Indirect : Unsigned_32;
226 Triple_Indirect : Unsigned_32;
227 end record
Nico Huber5a042fd2024-01-08 15:54:57 +0100228 with Object_Size => Inode_Extents'Length * 8;
Nico Huber33f6d952023-12-13 23:16:42 +0100229
230 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100231 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100232
Nico Huber52f4c8d2024-01-09 13:57:33 +0100233 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Static.Block_Size / 4);
Nico Huber700a4112024-01-08 15:50:09 +0100234 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4);
Nico Huber6623c982023-12-12 16:35:46 +0100235 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
236
237 procedure Indirect_Block_Lookup
238 (Indirect_Block_Phys : in FSBlock_Offset;
239 Addr_In_Block : in Addr_In_Block_Range;
240 Level : in Block_Cache_Index;
241 Logical_Off : in FSBlock_Logical;
242 Next_Physical : out FSBlock_Offset;
243 Success : out Boolean)
244 with
Nico Huber5a042fd2024-01-08 15:54:57 +0100245 Pre =>
Nico Huber52f4c8d2024-01-09 13:57:33 +0100246 Addr_Per_Block = FSBlock_Logical (Static.Block_Size / 4) and
Nico Huber5a042fd2024-01-08 15:54:57 +0100247 FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
Nico Huber6623c982023-12-12 16:35:46 +0100248 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
Nico Huber52f4c8d2024-01-09 13:57:33 +0100252 (Static => Static,
253 Cache => State.Cache,
Nico Huber68c86932023-12-13 11:03:11 +0100254 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 Huber52f4c8d2024-01-09 13:57:33 +0100261 (Buf => State.Cache.Buffer (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;
Nico Huber5a042fd2024-01-08 15:54:57 +0100266
267 pragma Assert (Addr_Per_Block <= 2 ** 14);
Nico Huber6623c982023-12-12 16:35:46 +0100268 begin
269 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100270 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100271 Success := True;
272 return;
273 end if;
274
275 Logical_Rest := Logical_Rest - Direct_Blocks;
276 if Logical_Rest < Addr_Per_Block then
277 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100278 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100279 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100280 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100281 Logical_Off => Logical - Logical_Rest,
282 Next_Physical => Physical,
283 Success => Success);
284 return;
285 end if;
286
287 Logical_Rest := Logical_Rest - Addr_Per_Block;
288 if Logical_Rest < Addr_Per_Block ** 2 then
289 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100290 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100291 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100292 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100293 Logical_Off => Logical - Logical_Rest,
294 Next_Physical => Physical,
295 Success => Success);
296 if not Success then
297 return;
298 end if;
299
300 Indirect_Block_Lookup
301 (Indirect_Block_Phys => Physical,
302 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100303 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100304 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
305 Next_Physical => Physical,
306 Success => Success);
307 return;
308 end if;
309
310 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
311 if Logical_Rest < Addr_Per_Block ** 3 then
312 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100313 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100314 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100315 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100316 Logical_Off => Logical - Logical_Rest,
317 Next_Physical => Physical,
318 Success => Success);
319 if not Success then
320 return;
321 end if;
322
323 Indirect_Block_Lookup
324 (Indirect_Block_Phys => Physical,
Nico Huber5a042fd2024-01-08 15:54:57 +0100325 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100326 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100327 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
328 Next_Physical => Physical,
329 Success => Success);
330 if not Success then
331 return;
332 end if;
333
334 Indirect_Block_Lookup
335 (Indirect_Block_Phys => Physical,
336 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100337 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100338 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
339 Next_Physical => Physical,
340 Success => Success);
341 return;
342 end if;
343
344 -- Logical address was just too high.
Nico Huber5a042fd2024-01-08 15:54:57 +0100345 Physical := 0;
Nico Huber6623c982023-12-12 16:35:46 +0100346 Success := False;
347 end Ext2_Block_Map;
348
Nico Huberfffc8c12023-12-13 12:44:12 +0100349 procedure Extent_Block_Map
350 (State : in out T;
351 Logical : in FSBlock_Logical;
352 Physical : out FSBlock_Offset;
353 Success : out Boolean)
354 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100355 Cache : Cache_Buffer renames State.Cache.Buffer;
356
Nico Huberfffc8c12023-12-13 12:44:12 +0100357 -- Extent blocks always start with a 12B header and contain 12B entries.
358 -- Every entry starts with the number of the first logical block it
359 -- covers. Entries are sorted by this number.
360 -- Depth > 0 blocks have index entries, referencing further extent blocks.
361 -- Depth = 0 blocks have extent entries, referencing a contiguous range
362 -- of data blocks.
363 --
364 -- +-----------------+
365 -- .-> | Hdr depth=0 |
366 -- | +-----------------+
367 -- | | Extent 0.. 12 |
368 -- +-------------+ | +-----------------+
369 -- | Hdr depth=1 | | | Extent 13.. 13 |
370 -- +-------------+ | +-----------------+
371 -- | Index 0 | --' | Extent 14..122 |
372 -- +-------------+ +-----------------+
373 -- | Index 123 | --.
374 -- +-------------+ | +-----------------+
375 -- `-> | Hdr depth=0 |
376 -- +-----------------+
377 -- | Extent 123..125 |
378 -- +-----------------+
379 -- | Extent 126..234 |
380 -- +-----------------+
381 --
382
383 Extent_Header_Size : constant := 12;
384 Extent_Header_Magic : constant := 16#f03a#;
385 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
386 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100387 Dynamic_Max_Index : constant Extent_Idx := State.Static.Block_Size / Extent_Header_Size - 1;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100388 subtype Extent_Depth is Natural range 0 .. 32;
Nico Huberfffc8c12023-12-13 12:44:12 +0100389
390 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
391 is
392 (Idx * Extent_Header_Size + Off);
393
394 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
395 is
396 (Read_LE16 (Buf, 0))
397 with
398 Pre => Buf'Length >= 2;
399
400 function Header_Entries (Buf : Buffer_Type) return Natural
401 is
402 (Natural (Read_LE16 (Buf, 2)))
403 with
404 Pre => Buf'Length >= 4;
405
406 function Header_Depth (Buf : Buffer_Type) return Natural
407 is
408 (Natural (Read_LE16 (Buf, 6)))
409 with
410 Pre => Buf'Length >= 8;
411
412 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
413 is
414 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
415 with
416 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
417
418 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
419 is
420 (FSBlock_Offset
421 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
422 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
423 with
424 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
425
426 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
427 renames Index_Logical;
428
429 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
430 is
431 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
432 with
433 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
434
435 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
436 is
437 (FSBlock_Offset
438 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
439 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
440 with
441 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
442
443 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
444 with
Nico Huber6710c0e2024-01-08 15:56:24 +0100445 Pre =>
446 Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then
447 Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then
448 Extent_Logical (Buf, 1) <= Logical,
449 Post =>
450 Bin_Search'Result in 1 .. Refs and
451 Extent_Logical (Buf, Bin_Search'Result) <= Logical
Nico Huberfffc8c12023-12-13 12:44:12 +0100452 is
Nico Huber6710c0e2024-01-08 15:56:24 +0100453 Left : Positive := 2;
Nico Huberfffc8c12023-12-13 12:44:12 +0100454 Right : Extent_Idx := Refs;
455 begin
456 while Left <= Right loop
457 declare
458 Mid : constant Extent_Idx := (Left + Right) / 2;
459 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
460 begin
461 if Logical < Ext_Logical then
462 Right := Mid - 1;
463 else
464 Left := Mid + 1;
465 end if;
Nico Huber6710c0e2024-01-08 15:56:24 +0100466 pragma Loop_Invariant
467 (Right <= Refs and then
468 Left in 2 .. Refs + 1 and then
469 Extent_Logical (Buf, Left - 1) <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100470 end;
471 end loop;
472 return Left - 1;
473 end Bin_Search;
474
475 procedure Next_Ref
476 (Current : in FSBlock_Offset;
477 Logical_Off : in FSBlock_Logical;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100478 Depth : in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100479 Next : out Extent_Idx;
480 Cache_Start : out Max_Block_Index;
481 Cache_End : out Max_Block_Index;
482 Success : out Boolean)
483 with
484 Pre => Logical_Off <= Logical,
485 Post => (if Success then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100486 Next <= Dynamic_Max_Index and then
Nico Huber52f4c8d2024-01-09 13:57:33 +0100487 Cache_End = Cache_Start + State.Static.Block_Size - 1 and then
488 Extent_Logical (Cache (Cache_Start .. Cache_End), Next) <= Logical)
Nico Huberfffc8c12023-12-13 12:44:12 +0100489 is
Nico Huberfffc8c12023-12-13 12:44:12 +0100490 begin
491 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100492 (Static => State.Static,
493 Cache => State.Cache,
Nico Huberfffc8c12023-12-13 12:44:12 +0100494 Phys => Current,
495 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100496 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100497 Cache_Start => Cache_Start,
498 Cache_End => Cache_End,
499 Success => Success);
500 if not Success then
501 Next := 1;
502 return;
503 end if;
504
505 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100506 Hdr_Magic : constant Unsigned_16 := Header_Magic (Cache (Cache_Start .. Cache_End));
507 Hdr_Entries : constant Natural := Header_Entries (Cache (Cache_Start .. Cache_End));
508 Hdr_Depth : constant Natural := Header_Depth (Cache (Cache_Start .. Cache_End));
Nico Huberfffc8c12023-12-13 12:44:12 +0100509 First_Logical : constant FSBlock_Logical :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100510 Extent_Logical (Cache (Cache_Start .. Cache_End), 1);
Nico Huberfffc8c12023-12-13 12:44:12 +0100511 begin
512 Success := Success and then
513 Hdr_Magic = Extent_Header_Magic and then
514 Hdr_Depth = Depth and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100515 Hdr_Entries in Extent_Idx and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100516 Hdr_Entries <= Dynamic_Max_Index and then
517 First_Logical = Logical_Off;
518 if not Success then
519 Next := 1;
520 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100521 Next := Bin_Search (Cache (Cache_Start .. Cache_End), Hdr_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100522 end if;
523 end;
524 end Next_Ref;
525
Nico Huber7403a542023-12-15 23:13:47 +0100526 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
527 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
528 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
529 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
530 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100531
532 Cache_Start, Cache_End : Max_Block_Index;
533 Logical_Off, Length : FSBlock_Logical;
534 Idx : Extent_Idx;
535 begin
536 Success :=
537 Inode_Magic = Extent_Header_Magic and then
538 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100539 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100540 First_Logical <= Logical and then
541 Depth in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100542 if not Success then
543 Physical := 0;
544 return;
545 end if;
546
Nico Huber7403a542023-12-15 23:13:47 +0100547 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100548 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100549 Physical := Extent_Physical (Inline_Extents, Idx);
550 Logical_Off := Extent_Logical (Inline_Extents, Idx);
551 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100552 else
Nico Huber7403a542023-12-15 23:13:47 +0100553 Physical := Index_Physical (Inline_Extents, Idx);
554 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100555 loop
Nico Huber96a0b0e2024-01-08 15:57:09 +0100556 pragma Loop_Invariant
557 (Depth > 0 and then
558 Depth in Extent_Depth and then
559 Logical_Off <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100560 Depth := Depth - 1;
561 Next_Ref
562 (Current => Physical,
563 Logical_Off => Logical_Off,
564 Depth => Depth,
565 Next => Idx,
566 Cache_Start => Cache_Start,
567 Cache_End => Cache_End,
568 Success => Success);
569 if not Success then
570 return;
571 end if;
572
573 exit when Depth = 0;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100574 Physical := Index_Physical (Cache (Cache_Start .. Cache_End), Idx);
575 Logical_Off := Index_Logical (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100576 end loop;
577
Nico Huber52f4c8d2024-01-09 13:57:33 +0100578 Physical := Extent_Physical (Cache (Cache_Start .. Cache_End), Idx);
579 Logical_Off := Extent_Logical (Cache (Cache_Start .. Cache_End), Idx);
580 Length := Extent_Length (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100581 end if;
582
583 Success :=
584 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100585 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100586 Logical < Logical_Off + Length;
587 if Success then
588 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
589 end if;
590 end Extent_Block_Map;
591
Nico Huber57d3a852023-12-04 15:42:40 +0100592 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100593 (State : in out T;
594 Inode : in Inode_Index;
595 Success : out Boolean)
596 with
597 Pre => Is_Mounted (State) and not Is_Open (State),
598 Post => Success = Is_Open (State)
599 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100600 Static : Mount_State renames State.Static;
601 Cache : Cache_Buffer renames State.Cache.Buffer;
602
Nico Hubercdc03512023-12-13 23:32:54 +0100603 type Group_Index is new Natural;
604 subtype Desc_In_Block_Index is Group_Index
605 range 0 .. Group_Index (2 ** Log_Block_Size'Last / Desc_Size'First - 1);
606
Nico Huber52f4c8d2024-01-09 13:57:33 +0100607 Inodes_Per_Block : constant Inode_Index := Inode_Index (Static.Block_Size / Static.Inode_Size);
608 Desc_Per_Block : constant Group_Index := Group_Index (Static.Block_Size / Static.Desc_Size);
Nico Hubercdc03512023-12-13 23:32:54 +0100609
Nico Huber022e2262023-12-15 23:15:17 +0100610 ------------------------
611 -- Group deserialization
612
Nico Hubercdc03512023-12-13 23:32:54 +0100613 subtype Group_Off is Natural range 0 .. Desc_Size'Last;
614 function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural
615 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100616 (Natural (Idx) * Static.Desc_Size + Off);
Nico Hubercdc03512023-12-13 23:32:54 +0100617
618 function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset
619 is
620 (FSBlock_Offset (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100621 (if Static.Feature_64Bit
Nico Hubercdc03512023-12-13 23:32:54 +0100622 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32)
623 else 0)
624 or
625 Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8)))))
626 with
627 Pre => Buf'Length >= Group_Byte_Offset (Idx, 44);
628
Nico Huber022e2262023-12-15 23:15:17 +0100629 ------------------------
630 -- Inode deserialization
631
Nico Hubercdc03512023-12-13 23:32:54 +0100632 subtype Inode_Off is Natural range 0 .. Inode_Size'Last;
633 function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural
634 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100635 (Natural (Idx) * Static.Inode_Size + Off);
Nico Hubercdc03512023-12-13 23:32:54 +0100636
Nico Huber022e2262023-12-15 23:15:17 +0100637 function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16
638 is
639 (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0)))
640 with
641 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2);
642
643 function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64
644 is
645 ((if Mode = Regular
646 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32)
647 else 0) or
648 Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4))))
649 with
650 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112);
651
652 function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
653 is
654 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28)))
655 with
656 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32);
657
Nico Hubercdc03512023-12-13 23:32:54 +0100658 function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
659 is
660 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32)))
661 with
662 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36);
663
Nico Huber022e2262023-12-15 23:15:17 +0100664 function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
665 is
666 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104)))
667 with
668 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108);
669
Nico Huber52f4c8d2024-01-09 13:57:33 +0100670 Group : constant Group_Index := Group_Index ((Inode - 1) / Static.Inodes_Per_Group);
Nico Hubercdc03512023-12-13 23:32:54 +0100671 Desc_Block : constant FSBlock_Offset :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100672 1 + Static.First_Data_Block + FSBlock_Offset (Group / Desc_Per_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100673 Cache_Start, Cache_End : Max_Block_Index;
674 begin
675 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100676 (Static => Static,
677 Cache => State.Cache,
Nico Hubercdc03512023-12-13 23:32:54 +0100678 Phys => Desc_Block,
679 Level => Natural'Min (Natural (Group / Desc_Per_Block), Block_Cache_Index'Last),
680 Cache_Start => Cache_Start,
681 Cache_End => Cache_End,
682 Success => Success);
683 if not Success then
684 return;
685 end if;
686
687 declare
688 Desc : constant Desc_In_Block_Index := Group mod Desc_Per_Block;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100689 Inode_In_Group : constant Inode_Index := (Inode - 1) mod Static.Inodes_Per_Group;
Nico Hubercdc03512023-12-13 23:32:54 +0100690 Inode_Block : constant FSBlock_Offset :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100691 Group_Inode_Table (Cache (Cache_Start .. Cache_End), Group) +
Nico Hubercdc03512023-12-13 23:32:54 +0100692 FSBlock_Offset (Inode_In_Group / Inodes_Per_Block);
693 begin
694 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100695 (Static => Static,
696 Cache => State.Cache,
Nico Hubercdc03512023-12-13 23:32:54 +0100697 Phys => Inode_Block,
698 Level => Block_Cache_Index'Last,
699 Cache_Start => Cache_Start,
700 Cache_End => Cache_End,
701 Success => Success);
702 if not Success then
703 return;
704 end if;
705
706 declare
Nico Huber022e2262023-12-15 23:15:17 +0100707 S_IFMT : constant := 8#170000#;
708 S_IFDIR : constant := 8#040000#;
709 S_IFREG : constant := 8#100000#;
710 S_IFLNK : constant := 8#120000#;
711
Nico Hubercdc03512023-12-13 23:32:54 +0100712 Inode_In_Block : constant Inode_Index := Inode_In_Group mod Inodes_Per_Block;
Nico Huber022e2262023-12-15 23:15:17 +0100713 I_Mode : constant Unsigned_16 :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100714 Inode_Mode (Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100715 I_Flags : constant Unsigned_32 :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100716 Inode_Flags (Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100717 begin
Nico Huber022e2262023-12-15 23:15:17 +0100718 case I_Mode and S_IFMT is
719 when S_IFDIR => State.Inode.Mode := Dir;
720 when S_IFREG => State.Inode.Mode := Regular;
721 when S_IFLNK => State.Inode.Mode := Link;
722 when others => Success := False; return;
723 end case;
724 if State.Inode.Mode = Link then
725 declare
726 I_File_ACL : constant Unsigned_32 := Inode_File_ACL (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100727 Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Huber022e2262023-12-15 23:15:17 +0100728 I_Blocks : constant Unsigned_32 := Inode_Blocks (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100729 Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Huber022e2262023-12-15 23:15:17 +0100730 begin
731 if (I_File_ACL = 0 and I_Blocks = 0) or
Nico Huber52f4c8d2024-01-09 13:57:33 +0100732 (I_File_ACL /= 0 and I_Blocks = 2 ** (Static.Block_Size_Bits - 9))
Nico Huber022e2262023-12-15 23:15:17 +0100733 then
734 State.Inode.Mode := Fast_Link;
735 end if;
736 end;
737 end if;
738 declare
739 I_Size : constant Unsigned_64 := Inode_Size (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100740 Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode);
Nico Huber022e2262023-12-15 23:15:17 +0100741 begin
742 if I_Size <= Unsigned_64 (Inode_Length'Last) then
743 State.Inode.Size := Inode_Length (I_Size);
744 else
745 Success := False;
746 end if;
747 end;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100748 State.Inode.Use_Extents := Static.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
749 State.Inode.Inline := Cache (Cache_Start + 40 .. Cache_Start + 100 - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100750 State.Inode.I := Inode;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100751 Reset_Cache_Logical (State.Cache);
Nico Hubercdc03512023-12-13 23:32:54 +0100752 State.S := File_Opened;
753 end;
754 end;
755 end Open;
756
757 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100758 (State : in out T;
759 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100760 File_Type : out FS.File_Type;
761 File_Name : in String;
762 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100763 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100764 is
Nico Huber21d42022023-12-16 02:50:22 +0100765 File_Name_Max : constant := 255;
766 Root_Inode : constant := 2;
767
768 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is
769 begin
770 for I in Str'Range loop
771 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
772 return False;
773 end if;
774 end loop;
775 return True;
776 end Str_Buf_Equal;
777
778 File_Inode : Inode_Index;
779 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100780 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100781 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100782 File_Type := FS.File_Type'First;
783
784 if File_Name'Length > File_Name_Max then
785 Success := False;
786 return;
787 end if;
788
789 -- Ensure dir is opened:
790 --
791 if State.S = File_Opened then
792 if State.Cur_Dir /= State.Inode.I then
793 Success := False;
794 return;
795 end if;
796 else
797 if In_Root then
798 State.Cur_Dir := Root_Inode;
799 end if;
800 Open (State, State.Cur_Dir, Success);
801 if not Success then
802 return;
803 end if;
804 end if;
805
806 -- Lookup file in opened dir:
807 --
808 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100809 Success := False;
Nico Huber21d42022023-12-16 02:50:22 +0100810 while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop
811 declare
812 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
813 subtype Dir_Entry_Index is Natural
814 range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1;
815 Dir_Entry : Buffer_Type (Dir_Entry_Index);
816 Entry_File_Pos : File_Offset := File_Pos;
817 Len : Natural;
818 begin
819 Read
820 (State => State,
821 File_Pos => Entry_File_Pos,
822 Buf => Dir_Entry (0 .. 7),
823 Len => Len);
824 if Len < Dir_Entry_Header_Length then
825 return;
826 end if;
827
828 -- Only check filenames of exact same length
829 if Read_LE32 (Dir_Entry, 0) > Root_Inode and then
830 File_Name'Length = Natural (Dir_Entry (6))
831 then
832 Read
833 (State => State,
834 File_Pos => Entry_File_Pos,
835 Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1),
836 Len => Len);
837 if Len < File_Name'Length then
838 return;
839 end if;
840
841 File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0));
842 Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1));
843 exit when Success;
844 end if;
845
846 File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4));
847 end;
848 end loop;
849
850 if Success then
851 Open (State, File_Inode, Success);
852 if not Success then
853 return;
854 end if;
855
856 if State.Inode.Mode = Dir then
857 State.Cur_Dir := File_Inode;
858 end if;
859 end if;
860
861 Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last);
862 if Success then
863 File_Len := File_Length (State.Inode.Size);
864 File_Type := (case State.Inode.Mode is
865 when Dir => FS.Dir,
866 when Regular => FS.Regular,
867 when Link .. Fast_Link => FS.Link);
868 else
869 Close (State);
870 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100871 end Open;
872
Nico Huber57d3a852023-12-04 15:42:40 +0100873 procedure Close (State : in out T) is
874 begin
875 State.S := Mounted;
876 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100877
Nico Huber57d3a852023-12-04 15:42:40 +0100878 procedure Read
879 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100880 File_Pos : in out File_Offset;
881 Buf : out Buffer_Type;
882 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100883 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100884 Static : Mount_State renames State.Static;
885 Cache : Cache_Buffer renames State.Cache.Buffer;
886
Nico Huberd3644be2023-12-16 01:43:00 +0100887 Pos : Natural;
Nico Huber1d7727f2023-11-30 15:58:46 +0100888 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100889 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100890 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
891 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100892 Len := 0;
893 else
Nico Huber549a1b82023-12-17 01:51:59 +0100894 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100895 end if;
896 Buf (Buf'First .. Buf'First + Len - 1) :=
897 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
898 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
899 File_Pos := File_Pos + File_Length (Len);
900 return;
901 end if;
902
Nico Huber1d7727f2023-11-30 15:58:46 +0100903 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100904 Pos := Buf'First;
905 while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop
906 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100907 In_Block : constant Max_Block_Index := Natural (File_Pos) mod Static.Block_Size;
908 Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / File_Offset (Static.Block_Size));
909 In_Block_Space : constant Natural := Natural (Static.Block_Size) - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100910 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huberd3644be2023-12-16 01:43:00 +0100911 In_Buf_Space : constant Natural := Buf'Last - Pos + 1;
912 Len_Here : Natural;
913 begin
914 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100915 if In_File_Space < Inode_Length (Len_Here) then
916 Len_Here := Natural (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100917 end if;
918 if In_Buf_Space < Len_Here then
919 Len_Here := In_Buf_Space;
920 end if;
921
922 declare
923 Last : constant Index_Type := Pos + Len_Here - 1;
924 Cache_Start, Cache_End : Max_Block_Index;
925 Physical : FSBlock_Offset;
926 Success : Boolean;
927 begin
928 if State.Inode.Use_Extents then
929 Extent_Block_Map (State, Logical, Physical, Success);
930 else
931 Ext2_Block_Map (State, Logical, Physical, Success);
932 end if;
933 if Success then
934 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100935 (Static => Static,
936 Cache => State.Cache,
Nico Huberd3644be2023-12-16 01:43:00 +0100937 Phys => Physical,
938 Level => Block_Cache_Index'Last,
939 Cache_Start => Cache_Start,
940 Cache_End => Cache_End,
941 Success => Success);
942 end if;
943 exit when not Success;
944
Nico Huber52f4c8d2024-01-09 13:57:33 +0100945 Buf (Pos .. Last) := Cache (
Nico Huberd3644be2023-12-16 01:43:00 +0100946 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
947 File_Pos := File_Pos + File_Length (Len_Here);
948 Pos := Pos + Len_Here;
949 Len := Len + Len_Here;
950 end;
951 end;
952 end loop;
953 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +0100954 end Read;
955
Nico Huber26f71832023-12-05 16:26:56 +0100956 --------------------------------------------------------------------------
957
958 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100959
960 function C_Mount return int
961 with
962 Export,
963 Convention => C,
964 External_Name => "ext2fs_mount";
965 function C_Mount return int
966 with
967 SPARK_Mode => Off
968 is
969 begin
970 return C.C_Mount;
971 end C_Mount;
972
Nico Huber3da21472023-12-18 15:43:35 +0100973 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100974 with
975 Export,
976 Convention => C,
977 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +0100978 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100979 with
980 SPARK_Mode => Off
981 is
982 begin
983 return C.C_Open (File_Path);
984 end C_Open;
985
986 function C_Read (Buf : System.Address; Len : int) return int
987 with
988 Export,
989 Convention => C,
990 External_Name => "ext2fs_read";
991 function C_Read (Buf : System.Address; Len : int) return int
992 with
993 SPARK_Mode => Off
994 is
995 begin
996 return C.C_Read (Buf, Len);
997 end C_Read;
998
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000999end FILO.FS.Ext2;