blob: 96328dd3051ea2b17dc0e8699470d16002cd48c0 [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;
Nico Huber39f086c2024-01-09 17:43:06 +010061 Static.First_Data_Block := 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;
Nico Huber53df7852024-01-15 18:36:04 +010074 pragma Assert_And_Cut (Success and not Is_Mounted (State));
Nico Huber26f71832023-12-05 16:26:56 +010075
76 declare
77 S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4);
78 begin
Nico Huber925326e2024-01-09 18:46:26 +010079 if S_Inodes_Per_Group in 1 .. Unsigned_32'Last then
80 Static.Inodes_Per_Group := Inode_In_Group_Count (S_Inodes_Per_Group);
Nico Huber26f71832023-12-05 16:26:56 +010081 else
82 Success := False;
83 return;
84 end if;
85 end;
86
87 declare
88 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
89 begin
90 if S_Rev_Level >= DYNAMIC_REV then
91 declare
92 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
93 begin
Nico Huberecafb8f2024-01-09 15:45:49 +010094 if Natural (S_Inode_Size) in Inode_Size and then
95 Natural (S_Inode_Size) <= Static.Block_Size
96 then
Nico Huber52f4c8d2024-01-09 13:57:33 +010097 Static.Inode_Size := Inode_Size (S_Inode_Size);
Nico Huber26f71832023-12-05 16:26:56 +010098 else
99 Success := False;
100 return;
101 end if;
Nico Huberecafb8f2024-01-09 15:45:49 +0100102 pragma Assert (Static.Block_Size / Static.Inode_Size <= Natural (Inode_In_Block_Count'Last));
Nico Huber26f71832023-12-05 16:26:56 +0100103 end;
104 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100105 Static.Inode_Size := Inode_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100106 end if;
Nico Huberecafb8f2024-01-09 15:45:49 +0100107 Static.Inodes_Per_Block := Inode_In_Block_Count (Static.Block_Size / Static.Inode_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100108 end;
Nico Huber53df7852024-01-15 18:36:04 +0100109 pragma Assert_And_Cut (Success and not Is_Mounted (State));
Nico Huber26f71832023-12-05 16:26:56 +0100110
111 declare
112 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
113 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100114 Static.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
115 Static.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
116 if Static.Feature_64Bit then
Nico Huber26f71832023-12-05 16:26:56 +0100117 declare
118 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
119 begin
Nico Huber7eb56922024-01-10 17:22:54 +0100120 if Natural (S_Desc_Size) in Group_Desc_Size and
Nico Huber52f4c8d2024-01-09 13:57:33 +0100121 Natural (S_Desc_Size) <= Static.Block_Size and
Nico Huber700a4112024-01-08 15:50:09 +0100122 Is_Power_Of_2 (S_Desc_Size)
Nico Huber26f71832023-12-05 16:26:56 +0100123 then
Nico Huber7eb56922024-01-10 17:22:54 +0100124 Static.Group_Desc_Size := Group_Desc_Size (S_Desc_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100125 else
126 Success := False;
127 return;
128 end if;
Nico Huber7eb56922024-01-10 17:22:54 +0100129 pragma Assert (Static.Block_Size / Static.Group_Desc_Size <= Natural (Group_In_Block_Count'Last));
Nico Huber26f71832023-12-05 16:26:56 +0100130 end;
Nico Huber7eb56922024-01-10 17:22:54 +0100131 Static.Feature_64Bit := Static.Feature_64Bit and Static.Group_Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100132 else
Nico Huber7eb56922024-01-10 17:22:54 +0100133 Static.Group_Desc_Size := Group_Desc_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100134 end if;
Nico Huber7eb56922024-01-10 17:22:54 +0100135 Static.Group_Desc_Per_Block := Group_In_Block_Count (Static.Block_Size / Static.Group_Desc_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100136 end;
137
138 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100139 end Mount;
140
Nico Huberf5d99d02023-12-12 13:42:55 +0100141 procedure Read_FSBlock
Nico Huber700a4112024-01-08 15:50:09 +0100142 (Buf : in out Buffer_Type;
Nico Huberf5d99d02023-12-12 13:42:55 +0100143 FSBlock : in FSBlock_Offset;
Nico Huber700a4112024-01-08 15:50:09 +0100144 Part_Len : in Partition_Length;
Nico Huberf5d99d02023-12-12 13:42:55 +0100145 Success : out Boolean)
146 with
Nico Huber700a4112024-01-08 15:50:09 +0100147 Pre => Buf'Length in Block_Size
Nico Huberf5d99d02023-12-12 13:42:55 +0100148 is
Nico Huber700a4112024-01-08 15:50:09 +0100149 FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock);
150 Block_Size : constant Integer_64 := Integer_64 (Buf'Length);
151 Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1;
Nico Huberf5d99d02023-12-12 13:42:55 +0100152 begin
Nico Huber700a4112024-01-08 15:50:09 +0100153 if FSBlock_64 > Max_Block_Offset then
Nico Huberf5d99d02023-12-12 13:42:55 +0100154 Success := False;
155 return;
156 end if;
Nico Huber700a4112024-01-08 15:50:09 +0100157 Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success);
Nico Huberf5d99d02023-12-12 13:42:55 +0100158 end Read_FSBlock;
159
Nico Huber68c86932023-12-13 11:03:11 +0100160 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100161 (Static : in Mount_State;
162 Cache : in out Block_Cache;
Nico Huber68c86932023-12-13 11:03:11 +0100163 Phys : in FSBlock_Offset;
164 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100165 Label : in Cache_Label;
166 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100167 Cache_Start : out Max_Block_Index;
168 Cache_End : out Max_Block_Index;
169 Success : out Boolean)
170 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100171 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber68c86932023-12-13 11:03:11 +0100172 is
Nico Huber68c86932023-12-13 11:03:11 +0100173 -- Limit cache usage depending on block size:
Nico Huber52f4c8d2024-01-09 13:57:33 +0100174 Max_Level : constant Block_Cache_Index := Block_Size'Last / Static.Block_Size - 1;
Nico Huber700a4112024-01-08 15:50:09 +0100175 Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level);
Nico Huber68c86932023-12-13 11:03:11 +0100176 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100177 Cache_Start := Cache_Level * Static.Block_Size;
178 Cache_End := Cache_Start + Static.Block_Size - 1;
179 if Cache.Logical (Cache_Level) = Logical and
180 Cache.Label (Cache_Level) = Label
Nico Huber57dfbfb2023-12-13 23:24:16 +0100181 then
Nico Huber68c86932023-12-13 11:03:11 +0100182 Success := True;
183 else
184 Read_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100185 (Buf => Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber68c86932023-12-13 11:03:11 +0100186 FSBlock => Phys,
Nico Huber52f4c8d2024-01-09 13:57:33 +0100187 Part_Len => Static.Part_Len,
Nico Huber68c86932023-12-13 11:03:11 +0100188 Success => Success);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100189 Cache.Logical (Cache_Level) := Logical; -- FIXME: Level needs to be part of Label
190 Cache.Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100191 end if;
192 end Cache_FSBlock;
193
Nico Huber57dfbfb2023-12-13 23:24:16 +0100194 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100195 (Static : in Mount_State;
196 Cache : in out Block_Cache;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100197 Phys : in FSBlock_Offset;
198 Level : in Block_Cache_Index;
199 Cache_Start : out Max_Block_Index;
200 Cache_End : out Max_Block_Index;
201 Success : out Boolean)
202 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100203 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber57dfbfb2023-12-13 23:24:16 +0100204 is
205 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100206 Cache_FSBlock (Static, Cache, Phys, Level, Cache_Label (Phys),
Nico Huber57dfbfb2023-12-13 23:24:16 +0100207 False, Cache_Start, Cache_End, Success);
208 end Cache_FSBlock;
209
Nico Huber52f4c8d2024-01-09 13:57:33 +0100210 procedure Reset_Cache_Logical (Cache : in out Block_Cache) is
Nico Huber57dfbfb2023-12-13 23:24:16 +0100211 begin
212 for I in Block_Cache_Index loop
Nico Huber52f4c8d2024-01-09 13:57:33 +0100213 if Cache.Logical (I) then
214 Cache.Logical (I) := False;
215 Cache.Label (I) := 0;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100216 end if;
217 end loop;
218 end Reset_Cache_Logical;
219
Nico Huber6623c982023-12-12 16:35:46 +0100220 procedure Ext2_Block_Map
221 (State : in out T;
222 Logical : in FSBlock_Logical;
223 Physical : out FSBlock_Offset;
224 Success : out Boolean)
Nico Huber53df7852024-01-15 18:36:04 +0100225 with
226 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huber6623c982023-12-12 16:35:46 +0100227 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100228 Static : constant Mount_State := State.Static;
229
Nico Huber33f6d952023-12-13 23:16:42 +0100230 Direct_Blocks : constant := 12;
231 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
232 type Inode_Blocks is record
233 Direct_Blocks : Direct_Blocks_Array;
234 Indirect_Block : Unsigned_32;
235 Double_Indirect : Unsigned_32;
236 Triple_Indirect : Unsigned_32;
237 end record
Nico Huber5a042fd2024-01-08 15:54:57 +0100238 with Object_Size => Inode_Extents'Length * 8;
Nico Huber33f6d952023-12-13 23:16:42 +0100239
240 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100241 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100242
Nico Huber52f4c8d2024-01-09 13:57:33 +0100243 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Static.Block_Size / 4);
Nico Huber700a4112024-01-08 15:50:09 +0100244 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4);
Nico Huber6623c982023-12-12 16:35:46 +0100245 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
246
247 procedure Indirect_Block_Lookup
248 (Indirect_Block_Phys : in FSBlock_Offset;
249 Addr_In_Block : in Addr_In_Block_Range;
250 Level : in Block_Cache_Index;
251 Logical_Off : in FSBlock_Logical;
252 Next_Physical : out FSBlock_Offset;
253 Success : out Boolean)
254 with
Nico Huber5a042fd2024-01-08 15:54:57 +0100255 Pre =>
Nico Huber52f4c8d2024-01-09 13:57:33 +0100256 Addr_Per_Block = FSBlock_Logical (Static.Block_Size / 4) and
Nico Huber53df7852024-01-15 18:36:04 +0100257 FSBlock_Logical (Addr_In_Block) < Addr_Per_Block,
258 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huber6623c982023-12-12 16:35:46 +0100259 is
Nico Huber68c86932023-12-13 11:03:11 +0100260 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100261 begin
Nico Huber68c86932023-12-13 11:03:11 +0100262 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100263 (Static => Static,
264 Cache => State.Cache,
Nico Huber68c86932023-12-13 11:03:11 +0100265 Phys => Indirect_Block_Phys,
266 Level => Level,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100267 Label => Cache_Label (Logical_Off),
Nico Huber68c86932023-12-13 11:03:11 +0100268 Cache_Start => Cache_Start,
269 Cache_End => Cache_End,
270 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100271 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huber52f4c8d2024-01-09 13:57:33 +0100272 (Buf => State.Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100273 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100274 end Indirect_Block_Lookup;
275
276 Logical_Rest : FSBlock_Logical := Logical;
Nico Huber5a042fd2024-01-08 15:54:57 +0100277
278 pragma Assert (Addr_Per_Block <= 2 ** 14);
Nico Huber6623c982023-12-12 16:35:46 +0100279 begin
280 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100281 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100282 Success := True;
283 return;
284 end if;
285
286 Logical_Rest := Logical_Rest - Direct_Blocks;
287 if Logical_Rest < Addr_Per_Block then
288 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100289 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100290 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100291 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100292 Logical_Off => Logical - Logical_Rest,
293 Next_Physical => Physical,
294 Success => Success);
295 return;
296 end if;
297
298 Logical_Rest := Logical_Rest - Addr_Per_Block;
299 if Logical_Rest < Addr_Per_Block ** 2 then
300 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100301 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100302 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100303 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100304 Logical_Off => Logical - Logical_Rest,
305 Next_Physical => Physical,
306 Success => Success);
307 if not Success then
308 return;
309 end if;
310
311 Indirect_Block_Lookup
312 (Indirect_Block_Phys => Physical,
313 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100314 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100315 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
316 Next_Physical => Physical,
317 Success => Success);
318 return;
319 end if;
320
321 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
322 if Logical_Rest < Addr_Per_Block ** 3 then
323 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100324 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100325 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100326 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100327 Logical_Off => Logical - Logical_Rest,
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,
Nico Huber5a042fd2024-01-08 15:54:57 +0100336 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100337 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100338 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
339 Next_Physical => Physical,
340 Success => Success);
341 if not Success then
342 return;
343 end if;
344
345 Indirect_Block_Lookup
346 (Indirect_Block_Phys => Physical,
347 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100348 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100349 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
350 Next_Physical => Physical,
351 Success => Success);
352 return;
353 end if;
354
355 -- Logical address was just too high.
Nico Huber5a042fd2024-01-08 15:54:57 +0100356 Physical := 0;
Nico Huber6623c982023-12-12 16:35:46 +0100357 Success := False;
358 end Ext2_Block_Map;
359
Nico Huberfffc8c12023-12-13 12:44:12 +0100360 procedure Extent_Block_Map
361 (State : in out T;
362 Logical : in FSBlock_Logical;
363 Physical : out FSBlock_Offset;
364 Success : out Boolean)
Nico Huber53df7852024-01-15 18:36:04 +0100365 with
366 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huberfffc8c12023-12-13 12:44:12 +0100367 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100368 Cache : Cache_Buffer renames State.Cache.Buffer;
369
Nico Huberfffc8c12023-12-13 12:44:12 +0100370 -- Extent blocks always start with a 12B header and contain 12B entries.
371 -- Every entry starts with the number of the first logical block it
372 -- covers. Entries are sorted by this number.
373 -- Depth > 0 blocks have index entries, referencing further extent blocks.
374 -- Depth = 0 blocks have extent entries, referencing a contiguous range
375 -- of data blocks.
376 --
377 -- +-----------------+
378 -- .-> | Hdr depth=0 |
379 -- | +-----------------+
380 -- | | Extent 0.. 12 |
381 -- +-------------+ | +-----------------+
382 -- | Hdr depth=1 | | | Extent 13.. 13 |
383 -- +-------------+ | +-----------------+
384 -- | Index 0 | --' | Extent 14..122 |
385 -- +-------------+ +-----------------+
386 -- | Index 123 | --.
387 -- +-------------+ | +-----------------+
388 -- `-> | Hdr depth=0 |
389 -- +-----------------+
390 -- | Extent 123..125 |
391 -- +-----------------+
392 -- | Extent 126..234 |
393 -- +-----------------+
394 --
395
396 Extent_Header_Size : constant := 12;
397 Extent_Header_Magic : constant := 16#f03a#;
398 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
399 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100400 Dynamic_Max_Index : constant Extent_Idx := State.Static.Block_Size / Extent_Header_Size - 1;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100401 subtype Extent_Depth is Natural range 0 .. 32;
Nico Huberfffc8c12023-12-13 12:44:12 +0100402
403 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
404 is
405 (Idx * Extent_Header_Size + Off);
406
407 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
408 is
409 (Read_LE16 (Buf, 0))
410 with
411 Pre => Buf'Length >= 2;
412
413 function Header_Entries (Buf : Buffer_Type) return Natural
414 is
415 (Natural (Read_LE16 (Buf, 2)))
416 with
417 Pre => Buf'Length >= 4;
418
419 function Header_Depth (Buf : Buffer_Type) return Natural
420 is
421 (Natural (Read_LE16 (Buf, 6)))
422 with
423 Pre => Buf'Length >= 8;
424
425 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
426 is
427 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
428 with
429 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
430
431 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
432 is
433 (FSBlock_Offset
434 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
435 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
436 with
437 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
438
439 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
440 renames Index_Logical;
441
442 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
443 is
444 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
445 with
446 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
447
448 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
449 is
450 (FSBlock_Offset
451 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
452 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
453 with
454 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
455
456 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
457 with
Nico Huber6710c0e2024-01-08 15:56:24 +0100458 Pre =>
459 Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then
460 Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then
461 Extent_Logical (Buf, 1) <= Logical,
462 Post =>
463 Bin_Search'Result in 1 .. Refs and
464 Extent_Logical (Buf, Bin_Search'Result) <= Logical
Nico Huberfffc8c12023-12-13 12:44:12 +0100465 is
Nico Huber6710c0e2024-01-08 15:56:24 +0100466 Left : Positive := 2;
Nico Huberfffc8c12023-12-13 12:44:12 +0100467 Right : Extent_Idx := Refs;
468 begin
469 while Left <= Right loop
470 declare
471 Mid : constant Extent_Idx := (Left + Right) / 2;
472 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
473 begin
474 if Logical < Ext_Logical then
475 Right := Mid - 1;
476 else
477 Left := Mid + 1;
478 end if;
Nico Huber6710c0e2024-01-08 15:56:24 +0100479 pragma Loop_Invariant
480 (Right <= Refs and then
481 Left in 2 .. Refs + 1 and then
482 Extent_Logical (Buf, Left - 1) <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100483 end;
484 end loop;
485 return Left - 1;
486 end Bin_Search;
487
488 procedure Next_Ref
489 (Current : in FSBlock_Offset;
490 Logical_Off : in FSBlock_Logical;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100491 Depth : in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100492 Next : out Extent_Idx;
493 Cache_Start : out Max_Block_Index;
494 Cache_End : out Max_Block_Index;
495 Success : out Boolean)
496 with
Nico Huber1db91932024-01-09 16:58:26 +0100497 Pre =>
498 Logical_Off <= Logical and
499 Dynamic_Max_Index = State.Static.Block_Size / Extent_Header_Size - 1,
500 Post =>
Nico Huber53df7852024-01-15 18:36:04 +0100501 State.Static = State.Static'Old and State.S = State.S'Old and
Nico Huber1db91932024-01-09 16:58:26 +0100502 (if Success then
503 Next <= Dynamic_Max_Index and then
504 Cache_End = Cache_Start + State.Static.Block_Size - 1 and then
505 Extent_Logical (Cache (Cache_Start .. Cache_End), Next) <= Logical)
Nico Huberfffc8c12023-12-13 12:44:12 +0100506 is
Nico Huberfffc8c12023-12-13 12:44:12 +0100507 begin
508 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100509 (Static => State.Static,
510 Cache => State.Cache,
Nico Huberfffc8c12023-12-13 12:44:12 +0100511 Phys => Current,
512 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100513 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100514 Cache_Start => Cache_Start,
515 Cache_End => Cache_End,
516 Success => Success);
517 if not Success then
518 Next := 1;
519 return;
520 end if;
521
522 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100523 Hdr_Magic : constant Unsigned_16 := Header_Magic (Cache (Cache_Start .. Cache_End));
524 Hdr_Entries : constant Natural := Header_Entries (Cache (Cache_Start .. Cache_End));
525 Hdr_Depth : constant Natural := Header_Depth (Cache (Cache_Start .. Cache_End));
Nico Huberfffc8c12023-12-13 12:44:12 +0100526 First_Logical : constant FSBlock_Logical :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100527 Extent_Logical (Cache (Cache_Start .. Cache_End), 1);
Nico Huberfffc8c12023-12-13 12:44:12 +0100528 begin
529 Success := Success and then
530 Hdr_Magic = Extent_Header_Magic and then
531 Hdr_Depth = Depth and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100532 Hdr_Entries in Extent_Idx and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100533 Hdr_Entries <= Dynamic_Max_Index and then
534 First_Logical = Logical_Off;
535 if not Success then
536 Next := 1;
537 else
Nico Huber1db91932024-01-09 16:58:26 +0100538 pragma Assert (Cache_End - Cache_Start + 1 = State.Static.Block_Size);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100539 Next := Bin_Search (Cache (Cache_Start .. Cache_End), Hdr_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100540 end if;
541 end;
542 end Next_Ref;
543
Nico Huber7403a542023-12-15 23:13:47 +0100544 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
545 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
546 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
547 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
548 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100549
550 Cache_Start, Cache_End : Max_Block_Index;
551 Logical_Off, Length : FSBlock_Logical;
552 Idx : Extent_Idx;
553 begin
554 Success :=
555 Inode_Magic = Extent_Header_Magic and then
556 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100557 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100558 First_Logical <= Logical and then
559 Depth in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100560 if not Success then
561 Physical := 0;
562 return;
563 end if;
564
Nico Huber7403a542023-12-15 23:13:47 +0100565 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100566 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100567 Physical := Extent_Physical (Inline_Extents, Idx);
568 Logical_Off := Extent_Logical (Inline_Extents, Idx);
569 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100570 else
Nico Huber7403a542023-12-15 23:13:47 +0100571 Physical := Index_Physical (Inline_Extents, Idx);
572 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100573 loop
Nico Huber96a0b0e2024-01-08 15:57:09 +0100574 pragma Loop_Invariant
Nico Huber1db91932024-01-09 16:58:26 +0100575 (State.Static = State.Static'Loop_Entry and then
Nico Huber53df7852024-01-15 18:36:04 +0100576 State.S = State.S'Loop_Entry and then
Nico Huber1db91932024-01-09 16:58:26 +0100577 Depth > 0 and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100578 Depth in Extent_Depth and then
579 Logical_Off <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100580 Depth := Depth - 1;
581 Next_Ref
582 (Current => Physical,
583 Logical_Off => Logical_Off,
584 Depth => Depth,
585 Next => Idx,
586 Cache_Start => Cache_Start,
587 Cache_End => Cache_End,
588 Success => Success);
589 if not Success then
590 return;
591 end if;
592
593 exit when Depth = 0;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100594 Physical := Index_Physical (Cache (Cache_Start .. Cache_End), Idx);
595 Logical_Off := Index_Logical (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100596 end loop;
597
Nico Huber52f4c8d2024-01-09 13:57:33 +0100598 Physical := Extent_Physical (Cache (Cache_Start .. Cache_End), Idx);
599 Logical_Off := Extent_Logical (Cache (Cache_Start .. Cache_End), Idx);
600 Length := Extent_Length (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100601 end if;
602
603 Success :=
604 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100605 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huber1db91932024-01-09 16:58:26 +0100606 Logical < Logical_Off + Length and then
607 FSBlock_Offset (Logical - Logical_Off) <= FSBlock_Offset'Last - Physical;
Nico Huberfffc8c12023-12-13 12:44:12 +0100608 if Success then
609 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
610 end if;
611 end Extent_Block_Map;
612
Nico Huber7eb56922024-01-10 17:22:54 +0100613 procedure Parse_Group
614 (Static : in Mount_State;
615 Table_Start : out FSBlock_Offset;
616 Buf : in Buffer_Type;
617 Success : out Boolean)
618 with
619 Pre =>
620 Buf'Length >= Group_Desc_Size'First and
621 (if Static.Feature_64Bit then Buf'Length >= 64)
622 is
623 Inode_Table_Block : constant Unsigned_64 :=
624 (if Static.Feature_64Bit
625 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, 40)), 32)
626 else 0)
627 or
628 Unsigned_64 (Read_LE32 (Buf, 8));
629 begin
630 if Inode_Table_Block >= Unsigned_64 (Static.First_Data_Block) and
631 Inode_Table_Block <= Unsigned_64 (FSBlock_Offset'Last)
632 then
633 Table_Start := FSBlock_Offset (Inode_Table_Block);
634 Success := True;
635 else
636 Table_Start := 0;
637 Success := False;
638 end if;
639 end Parse_Group;
640
Nico Huber71f9ca02024-01-10 15:38:49 +0100641 procedure Parse_Inode
642 (Static : in Mount_State;
643 Inode : in out Inode_Info;
644 Buf : in Buffer_Type;
645 Success : out Boolean)
646 with
647 Pre => Buf'Length >= Inode_Size'First
648 is
649 S_IFMT : constant := 8#170000#;
650 S_IFDIR : constant := 8#040000#;
651 S_IFREG : constant := 8#100000#;
652 S_IFLNK : constant := 8#120000#;
653
654 I_Mode : constant Unsigned_16 := Read_LE16 (Buf, 0);
655 I_Blocks : constant Unsigned_32 := Read_LE32 (Buf, 28);
656 I_Flags : constant Unsigned_32 := Read_LE32 (Buf, 32);
657 I_File_ACL : constant Unsigned_32 := Read_LE32 (Buf, 104);
658 begin
659 case I_Mode and S_IFMT is
660 when S_IFDIR => Inode.Mode := Dir;
661 when S_IFREG => Inode.Mode := Regular;
662 when S_IFLNK => Inode.Mode := Link;
663 when others => Success := False; return;
664 end case;
665
666 if Inode.Mode = Link and
667 ((I_File_ACL = 0 and I_Blocks = 0) or
668 (I_File_ACL /= 0 and I_Blocks = 2 ** (Static.Block_Size_Bits - 9)))
669 then
670 Inode.Mode := Fast_Link;
671 end if;
672
673 declare
674 I_Size : constant Unsigned_64 :=
675 (if Inode.Mode = Regular
676 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, 108)), 32)
677 else 0) or
678 Unsigned_64 (Read_LE32 (Buf, 4));
679 begin
680 if I_Size > Unsigned_64 (Inode_Length'Last) then
681 Success := False;
682 return;
683 end if;
684 Inode.Size := Inode_Length (I_Size);
685 end;
686
687 Inode.Use_Extents := Static.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
688 Inode.Inline := Buf (Buf'First + 40 .. Buf'First + 100 - 1);
689
690 Success := True;
691 end Parse_Inode;
692
Nico Huber57d3a852023-12-04 15:42:40 +0100693 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100694 (State : in out T;
695 Inode : in Inode_Index;
696 Success : out Boolean)
697 with
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100698 Pre => Is_Mounted (State),
Nico Huber53df7852024-01-15 18:36:04 +0100699 Post => Is_Mounted (State) and (Success = Is_Open (State))
Nico Hubercdc03512023-12-13 23:32:54 +0100700 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100701 Static : Mount_State renames State.Static;
702 Cache : Cache_Buffer renames State.Cache.Buffer;
703
Nico Huber52f4c8d2024-01-09 13:57:33 +0100704 Group : constant Group_Index := Group_Index ((Inode - 1) / Static.Inodes_Per_Group);
Nico Huber7eb56922024-01-10 17:22:54 +0100705 Group_Block : constant FSBlock_Offset :=
706 1 + FSBlock_Offset (Static.First_Data_Block) + FSBlock_Offset (Group / Static.Group_Desc_Per_Block);
707 Group_In_Block : constant Group_In_Block_Index := Group_In_Block_Index (Group mod Static.Group_Desc_Per_Block);
708 Group_In_Block_Offset : constant Natural := Natural (Group_In_Block) * Static.Group_Desc_Size;
709
Nico Hubercdc03512023-12-13 23:32:54 +0100710 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber7eb56922024-01-10 17:22:54 +0100711 First_Table_Block : FSBlock_Offset;
Nico Hubercdc03512023-12-13 23:32:54 +0100712 begin
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100713 State.S := Mounted;
714
Nico Hubercdc03512023-12-13 23:32:54 +0100715 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100716 (Static => Static,
717 Cache => State.Cache,
Nico Huber7eb56922024-01-10 17:22:54 +0100718 Phys => Group_Block,
719 Level => Natural (Group_Index'Min (Group / Static.Group_Desc_Per_Block,
Nico Huber925326e2024-01-09 18:46:26 +0100720 Group_Index (Block_Cache_Index'Last))),
Nico Hubercdc03512023-12-13 23:32:54 +0100721 Cache_Start => Cache_Start,
722 Cache_End => Cache_End,
723 Success => Success);
Nico Hubereb012072024-01-10 17:56:24 +0100724 Success := Success and then
725 -- TODO: Prove this using a predicate on Mount_State:
726 Cache_Start <= Cache_End - Group_In_Block_Offset - Static.Group_Desc_Size + 1;
Nico Hubercdc03512023-12-13 23:32:54 +0100727 if not Success then
728 return;
729 end if;
730
Nico Huber7eb56922024-01-10 17:22:54 +0100731 Parse_Group
732 (Static => Static,
733 Table_Start => First_Table_Block,
734 Buf => Cache (Cache_Start + Group_In_Block_Offset ..
735 Cache_Start + Group_In_Block_Offset + Static.Group_Desc_Size - 1),
736 Success => Success);
737 if not Success then
738 return;
739 end if;
Nico Huber7eb56922024-01-10 17:22:54 +0100740
Nico Hubercdc03512023-12-13 23:32:54 +0100741 declare
Nico Huberecafb8f2024-01-09 15:45:49 +0100742 Inode_In_Group : constant Inode_In_Group_Index :=
743 Inode_In_Group_Index ((Inode - 1) mod Static.Inodes_Per_Group);
Nico Huber71f9ca02024-01-10 15:38:49 +0100744
Nico Huber7eb56922024-01-10 17:22:54 +0100745 Inode_Block : constant FSBlock_Offset := FSBlock_Offset (Inode_In_Group / Static.Inodes_Per_Block);
Nico Huber71f9ca02024-01-10 15:38:49 +0100746 Inode_In_Block : constant Inode_In_Block_Index :=
747 Inode_In_Block_Index (Inode_In_Group mod Static.Inodes_Per_Block);
748 Inode_In_Block_Offset : constant Natural :=
749 Natural (Inode_In_Block) * Static.Inode_Size;
Nico Hubercdc03512023-12-13 23:32:54 +0100750 begin
Nico Huber7eb56922024-01-10 17:22:54 +0100751 if First_Table_Block > FSBlock_Offset'Last - Inode_Block then
Nico Huber925326e2024-01-09 18:46:26 +0100752 Success := False;
753 return;
754 end if;
Nico Huber925326e2024-01-09 18:46:26 +0100755
Nico Hubercdc03512023-12-13 23:32:54 +0100756 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100757 (Static => Static,
758 Cache => State.Cache,
Nico Huber7eb56922024-01-10 17:22:54 +0100759 Phys => FSBlock_Offset (First_Table_Block + Inode_Block),
Nico Hubercdc03512023-12-13 23:32:54 +0100760 Level => Block_Cache_Index'Last,
761 Cache_Start => Cache_Start,
762 Cache_End => Cache_End,
763 Success => Success);
Nico Hubereb012072024-01-10 17:56:24 +0100764 Success := Success and then
765 -- TODO: Prove this using a predicate on Mount_State:
766 Cache_Start <= Cache_End - Inode_In_Block_Offset - Static.Inode_Size + 1;
Nico Hubercdc03512023-12-13 23:32:54 +0100767 if not Success then
768 return;
769 end if;
770
Nico Huber71f9ca02024-01-10 15:38:49 +0100771 Parse_Inode
772 (Static => State.Static,
773 Inode => State.Inode,
774 Buf => Cache (Cache_Start + Inode_In_Block_Offset ..
775 Cache_Start + Inode_In_Block_Offset + Static.Inode_Size - 1),
776 Success => Success);
Nico Huber022e2262023-12-15 23:15:17 +0100777
Nico Huber71f9ca02024-01-10 15:38:49 +0100778 if Success then
Nico Huber21d42022023-12-16 02:50:22 +0100779 State.Inode.I := Inode;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100780 Reset_Cache_Logical (State.Cache);
Nico Hubercdc03512023-12-13 23:32:54 +0100781 State.S := File_Opened;
Nico Huber71f9ca02024-01-10 15:38:49 +0100782 end if;
Nico Hubercdc03512023-12-13 23:32:54 +0100783 end;
784 end Open;
785
786 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100787 (State : in out T;
788 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100789 File_Type : out FS.File_Type;
790 File_Name : in String;
791 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100792 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100793 is
Nico Huber21d42022023-12-16 02:50:22 +0100794 File_Name_Max : constant := 255;
795 Root_Inode : constant := 2;
796
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100797 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean
798 with
799 Pre => Str'Length <= Buf'Length
800 is
Nico Huber21d42022023-12-16 02:50:22 +0100801 begin
802 for I in Str'Range loop
803 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
804 return False;
805 end if;
806 end loop;
807 return True;
808 end Str_Buf_Equal;
809
810 File_Inode : Inode_Index;
811 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100812 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100813 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100814 File_Type := FS.File_Type'First;
815
816 if File_Name'Length > File_Name_Max then
817 Success := False;
818 return;
819 end if;
820
821 -- Ensure dir is opened:
822 --
823 if State.S = File_Opened then
824 if State.Cur_Dir /= State.Inode.I then
825 Success := False;
826 return;
827 end if;
828 else
829 if In_Root then
830 State.Cur_Dir := Root_Inode;
831 end if;
832 Open (State, State.Cur_Dir, Success);
833 if not Success then
834 return;
835 end if;
836 end if;
837
838 -- Lookup file in opened dir:
839 --
840 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100841 Success := False;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100842 loop
843 pragma Loop_Invariant (Is_Open (State) and not Success);
Nico Huber21d42022023-12-16 02:50:22 +0100844 declare
845 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100846 Dir_Entry_Header : Buffer_Type (0 .. Dir_Entry_Header_Length - 1);
847 Dir_Entry_Name : Buffer_Type (0 .. File_Name_Max - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100848 Entry_File_Pos : File_Offset := File_Pos;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100849 Dir_Entry_Length : File_Length;
850 Inode : Inode_0_Index;
Nico Huber21d42022023-12-16 02:50:22 +0100851 Len : Natural;
852 begin
853 Read
854 (State => State,
855 File_Pos => Entry_File_Pos,
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100856 Buf => Dir_Entry_Header,
Nico Huber21d42022023-12-16 02:50:22 +0100857 Len => Len);
858 if Len < Dir_Entry_Header_Length then
859 return;
860 end if;
861
862 -- Only check filenames of exact same length
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100863 Inode := Inode_0_Index (Read_LE32 (Dir_Entry_Header, 0));
864 if Inode > Root_Inode and then
865 File_Name'Length = Natural (Dir_Entry_Header (6))
Nico Huber21d42022023-12-16 02:50:22 +0100866 then
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100867 pragma Warnings
868 (GNATprove, Off, """Entry_File_Pos"" is set*but not used",
869 Reason => "We only care about intermedidate values.");
Nico Huber21d42022023-12-16 02:50:22 +0100870 Read
871 (State => State,
872 File_Pos => Entry_File_Pos,
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100873 Buf => Dir_Entry_Name,
Nico Huber21d42022023-12-16 02:50:22 +0100874 Len => Len);
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100875 pragma Warnings (GNATprove, On, """Entry_File_Pos"" is set*but not used");
Nico Huber21d42022023-12-16 02:50:22 +0100876 if Len < File_Name'Length then
877 return;
878 end if;
879
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100880 Success := Str_Buf_Equal (File_Name, Dir_Entry_Name);
881 if Success then
882 File_Inode := Inode;
883 exit;
884 end if;
Nico Huber21d42022023-12-16 02:50:22 +0100885 end if;
886
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100887 Dir_Entry_Length := File_Length (Read_LE16 (Dir_Entry_Header, 4));
888 if File_Pos > File_Length'Last - Dir_Entry_Length or
889 Unsigned_64 (File_Pos) >= Unsigned_64 (State.Inode.Size) - Unsigned_64 (Dir_Entry_Length)
890 then
891 return;
892 end if;
893 File_Pos := File_Pos + Dir_Entry_Length;
Nico Huber21d42022023-12-16 02:50:22 +0100894 end;
895 end loop;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100896 pragma Assert_And_Cut (Success and Is_Mounted (State));
Nico Huber21d42022023-12-16 02:50:22 +0100897
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100898 Open (State, File_Inode, Success);
899 if not Success then
900 return;
Nico Huber21d42022023-12-16 02:50:22 +0100901 end if;
902
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100903 if State.Inode.Mode = Dir then
904 State.Cur_Dir := File_Inode;
905 end if;
906
907 Success := State.Inode.Size <= Inode_Length (File_Length'Last);
Nico Huber21d42022023-12-16 02:50:22 +0100908 if Success then
909 File_Len := File_Length (State.Inode.Size);
910 File_Type := (case State.Inode.Mode is
911 when Dir => FS.Dir,
912 when Regular => FS.Regular,
913 when Link .. Fast_Link => FS.Link);
914 else
915 Close (State);
916 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100917 end Open;
918
Nico Huber57d3a852023-12-04 15:42:40 +0100919 procedure Close (State : in out T) is
920 begin
921 State.S := Mounted;
922 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100923
Nico Huber57d3a852023-12-04 15:42:40 +0100924 procedure Read
925 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100926 File_Pos : in out File_Offset;
927 Buf : out Buffer_Type;
928 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100929 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100930 Static : Mount_State renames State.Static;
931 Cache : Cache_Buffer renames State.Cache.Buffer;
932
Nico Huber53df7852024-01-15 18:36:04 +0100933 Pos : Natural range Buf'First .. Buf'Last + 1;
Nico Huber1d7727f2023-11-30 15:58:46 +0100934 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100935 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100936 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
937 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100938 Len := 0;
939 else
Nico Huber549a1b82023-12-17 01:51:59 +0100940 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100941 end if;
942 Buf (Buf'First .. Buf'First + Len - 1) :=
943 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
944 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
945 File_Pos := File_Pos + File_Length (Len);
946 return;
947 end if;
948
Nico Huber1d7727f2023-11-30 15:58:46 +0100949 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100950 Pos := Buf'First;
Nico Huber53df7852024-01-15 18:36:04 +0100951 while Pos <= Buf'Last and
952 File_Pos < File_Offset'Last and
953 Inode_Length (File_Pos) < State.Inode.Size
954 loop
955 pragma Loop_Invariant (Is_Open (State));
Nico Huberd3644be2023-12-16 01:43:00 +0100956 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100957 In_Block : constant Max_Block_Index := Natural (File_Pos) mod Static.Block_Size;
Nico Huber53df7852024-01-15 18:36:04 +0100958 Logical : constant FSBlock_Logical :=
959 FSBlock_Logical (File_Pos / File_Offset (Static.Block_Size));
960 In_Block_Space : constant Positive := Static.Block_Size - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100961 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huber53df7852024-01-15 18:36:04 +0100962 In_Buf_Space : constant Positive := Buf'Last - Pos + 1;
963 Len_Here : Positive;
Nico Huberd3644be2023-12-16 01:43:00 +0100964 begin
965 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100966 if In_File_Space < Inode_Length (Len_Here) then
Nico Huber53df7852024-01-15 18:36:04 +0100967 Len_Here := Positive (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100968 end if;
969 if In_Buf_Space < Len_Here then
970 Len_Here := In_Buf_Space;
971 end if;
Nico Huber53df7852024-01-15 18:36:04 +0100972 if File_Offset'Last - File_Pos < File_Length (Len_Here) then
973 Len_Here := Positive (File_Offset'Last - File_Pos);
974 end if;
Nico Huberd3644be2023-12-16 01:43:00 +0100975
976 declare
977 Last : constant Index_Type := Pos + Len_Here - 1;
978 Cache_Start, Cache_End : Max_Block_Index;
979 Physical : FSBlock_Offset;
980 Success : Boolean;
981 begin
982 if State.Inode.Use_Extents then
983 Extent_Block_Map (State, Logical, Physical, Success);
984 else
985 Ext2_Block_Map (State, Logical, Physical, Success);
986 end if;
Nico Huber53df7852024-01-15 18:36:04 +0100987 exit when not Success;
988
989 Cache_FSBlock
990 (Static => Static,
991 Cache => State.Cache,
992 Phys => Physical,
993 Level => Block_Cache_Index'Last,
994 Cache_Start => Cache_Start,
995 Cache_End => Cache_End,
996 Success => Success);
997 pragma Assert (Cache_Start <= Cache_End - (In_Block + Len_Here - 1));
Nico Huberd3644be2023-12-16 01:43:00 +0100998 exit when not Success;
999
Nico Huber52f4c8d2024-01-09 13:57:33 +01001000 Buf (Pos .. Last) := Cache (
Nico Huberd3644be2023-12-16 01:43:00 +01001001 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
1002 File_Pos := File_Pos + File_Length (Len_Here);
1003 Pos := Pos + Len_Here;
Nico Huber53df7852024-01-15 18:36:04 +01001004 Len := Pos - Buf'First;
Nico Huberd3644be2023-12-16 01:43:00 +01001005 end;
1006 end;
1007 end loop;
1008 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +01001009 end Read;
1010
Nico Huber26f71832023-12-05 16:26:56 +01001011 --------------------------------------------------------------------------
1012
1013 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +01001014
1015 function C_Mount return int
1016 with
1017 Export,
1018 Convention => C,
1019 External_Name => "ext2fs_mount";
1020 function C_Mount return int
1021 with
1022 SPARK_Mode => Off
1023 is
1024 begin
1025 return C.C_Mount;
1026 end C_Mount;
1027
Nico Huber3da21472023-12-18 15:43:35 +01001028 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +01001029 with
1030 Export,
1031 Convention => C,
1032 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +01001033 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +01001034 with
1035 SPARK_Mode => Off
1036 is
1037 begin
1038 return C.C_Open (File_Path);
1039 end C_Open;
1040
1041 function C_Read (Buf : System.Address; Len : int) return int
1042 with
1043 Export,
1044 Convention => C,
1045 External_Name => "ext2fs_read";
1046 function C_Read (Buf : System.Address; Len : int) return int
1047 with
1048 SPARK_Mode => Off
1049 is
1050 begin
1051 return C.C_Read (Buf, Len);
1052 end C_Read;
1053
Thomas Heijligen5c43abc2023-12-11 15:24:36 +00001054end FILO.FS.Ext2;