blob: 819773388153a9a629b32dec597bea03ce020e09 [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 Huber43af31f2024-01-26 22:33:47 +010043 Super_Block : Buffer_Type (0 .. 64 * 4 - 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 Huber68c86932023-12-13 11:03:11 +0100165 Cache_Start : out Max_Block_Index;
166 Cache_End : out Max_Block_Index;
167 Success : out Boolean)
168 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100169 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber68c86932023-12-13 11:03:11 +0100170 is
Nico Huber68c86932023-12-13 11:03:11 +0100171 -- Limit cache usage depending on block size:
Nico Huber52f4c8d2024-01-09 13:57:33 +0100172 Max_Level : constant Block_Cache_Index := Block_Size'Last / Static.Block_Size - 1;
Nico Huber700a4112024-01-08 15:50:09 +0100173 Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level);
Nico Huber68c86932023-12-13 11:03:11 +0100174 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100175 Cache_Start := Cache_Level * Static.Block_Size;
176 Cache_End := Cache_Start + Static.Block_Size - 1;
Nico Huber6bef9242024-01-26 23:47:49 +0100177 if Cache.Phys (Cache_Level) = Phys then
Nico Huber68c86932023-12-13 11:03:11 +0100178 Success := True;
179 else
180 Read_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100181 (Buf => Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber68c86932023-12-13 11:03:11 +0100182 FSBlock => Phys,
Nico Huber52f4c8d2024-01-09 13:57:33 +0100183 Part_Len => Static.Part_Len,
Nico Huber68c86932023-12-13 11:03:11 +0100184 Success => Success);
Nico Huber6bef9242024-01-26 23:47:49 +0100185 Cache.Phys (Cache_Level) := Phys;
Nico Huber68c86932023-12-13 11:03:11 +0100186 end if;
187 end Cache_FSBlock;
188
Nico Huber6623c982023-12-12 16:35:46 +0100189 procedure Ext2_Block_Map
190 (State : in out T;
191 Logical : in FSBlock_Logical;
192 Physical : out FSBlock_Offset;
193 Success : out Boolean)
Nico Huber53df7852024-01-15 18:36:04 +0100194 with
195 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huber6623c982023-12-12 16:35:46 +0100196 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100197 Static : constant Mount_State := State.Static;
198
Nico Huber33f6d952023-12-13 23:16:42 +0100199 Direct_Blocks : constant := 12;
200 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
201 type Inode_Blocks is record
202 Direct_Blocks : Direct_Blocks_Array;
203 Indirect_Block : Unsigned_32;
204 Double_Indirect : Unsigned_32;
205 Triple_Indirect : Unsigned_32;
206 end record
Nico Huber5a042fd2024-01-08 15:54:57 +0100207 with Object_Size => Inode_Extents'Length * 8;
Nico Huber33f6d952023-12-13 23:16:42 +0100208
209 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100210 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100211
Nico Huber52f4c8d2024-01-09 13:57:33 +0100212 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Static.Block_Size / 4);
Nico Huber700a4112024-01-08 15:50:09 +0100213 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4);
Nico Huber6623c982023-12-12 16:35:46 +0100214 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
215
216 procedure Indirect_Block_Lookup
217 (Indirect_Block_Phys : in FSBlock_Offset;
218 Addr_In_Block : in Addr_In_Block_Range;
219 Level : in Block_Cache_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100220 Next_Physical : out FSBlock_Offset;
221 Success : out Boolean)
222 with
Nico Huber5a042fd2024-01-08 15:54:57 +0100223 Pre =>
Nico Huber52f4c8d2024-01-09 13:57:33 +0100224 Addr_Per_Block = FSBlock_Logical (Static.Block_Size / 4) and
Nico Huber53df7852024-01-15 18:36:04 +0100225 FSBlock_Logical (Addr_In_Block) < Addr_Per_Block,
226 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huber6623c982023-12-12 16:35:46 +0100227 is
Nico Huber68c86932023-12-13 11:03:11 +0100228 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100229 begin
Nico Huber68c86932023-12-13 11:03:11 +0100230 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100231 (Static => Static,
232 Cache => State.Cache,
Nico Huber68c86932023-12-13 11:03:11 +0100233 Phys => Indirect_Block_Phys,
234 Level => Level,
Nico Huber68c86932023-12-13 11:03:11 +0100235 Cache_Start => Cache_Start,
236 Cache_End => Cache_End,
237 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100238 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huber52f4c8d2024-01-09 13:57:33 +0100239 (Buf => State.Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100240 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100241 end Indirect_Block_Lookup;
242
243 Logical_Rest : FSBlock_Logical := Logical;
Nico Huber5a042fd2024-01-08 15:54:57 +0100244
245 pragma Assert (Addr_Per_Block <= 2 ** 14);
Nico Huber6623c982023-12-12 16:35:46 +0100246 begin
247 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100248 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100249 Success := True;
250 return;
251 end if;
252
253 Logical_Rest := Logical_Rest - Direct_Blocks;
254 if Logical_Rest < Addr_Per_Block then
255 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100256 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100257 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huber6bef9242024-01-26 23:47:49 +0100258 Level => Block_Map_Cache_Level,
Nico Huber6623c982023-12-12 16:35:46 +0100259 Next_Physical => Physical,
260 Success => Success);
261 return;
262 end if;
263
264 Logical_Rest := Logical_Rest - Addr_Per_Block;
265 if Logical_Rest < Addr_Per_Block ** 2 then
266 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100267 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100268 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100269 Level => Block_Map_Cache_Level + 1,
Nico Huber6623c982023-12-12 16:35:46 +0100270 Next_Physical => Physical,
271 Success => Success);
272 if not Success then
273 return;
274 end if;
275
276 Indirect_Block_Lookup
277 (Indirect_Block_Phys => Physical,
278 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100279 Level => Block_Map_Cache_Level,
Nico Huber6623c982023-12-12 16:35:46 +0100280 Next_Physical => Physical,
281 Success => Success);
282 return;
283 end if;
284
285 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
286 if Logical_Rest < Addr_Per_Block ** 3 then
287 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100288 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100289 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100290 Level => Block_Map_Cache_Level + 2,
Nico Huber6623c982023-12-12 16:35:46 +0100291 Next_Physical => Physical,
292 Success => Success);
293 if not Success then
294 return;
295 end if;
296
297 Indirect_Block_Lookup
298 (Indirect_Block_Phys => Physical,
Nico Huber5a042fd2024-01-08 15:54:57 +0100299 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100300 Level => Block_Map_Cache_Level + 1,
Nico Huber6623c982023-12-12 16:35:46 +0100301 Next_Physical => Physical,
302 Success => Success);
303 if not Success then
304 return;
305 end if;
306
307 Indirect_Block_Lookup
308 (Indirect_Block_Phys => Physical,
309 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100310 Level => Block_Map_Cache_Level,
Nico Huber6623c982023-12-12 16:35:46 +0100311 Next_Physical => Physical,
312 Success => Success);
313 return;
314 end if;
315
316 -- Logical address was just too high.
Nico Huber5a042fd2024-01-08 15:54:57 +0100317 Physical := 0;
Nico Huber6623c982023-12-12 16:35:46 +0100318 Success := False;
319 end Ext2_Block_Map;
320
Nico Huberfffc8c12023-12-13 12:44:12 +0100321 procedure Extent_Block_Map
322 (State : in out T;
323 Logical : in FSBlock_Logical;
324 Physical : out FSBlock_Offset;
325 Success : out Boolean)
Nico Huber53df7852024-01-15 18:36:04 +0100326 with
327 Post => State.Static = State.Static'Old and State.S = State.S'Old
Nico Huberfffc8c12023-12-13 12:44:12 +0100328 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100329 Cache : Cache_Buffer renames State.Cache.Buffer;
330
Nico Huberfffc8c12023-12-13 12:44:12 +0100331 -- Extent blocks always start with a 12B header and contain 12B entries.
332 -- Every entry starts with the number of the first logical block it
333 -- covers. Entries are sorted by this number.
334 -- Depth > 0 blocks have index entries, referencing further extent blocks.
335 -- Depth = 0 blocks have extent entries, referencing a contiguous range
336 -- of data blocks.
337 --
338 -- +-----------------+
339 -- .-> | Hdr depth=0 |
340 -- | +-----------------+
341 -- | | Extent 0.. 12 |
342 -- +-------------+ | +-----------------+
343 -- | Hdr depth=1 | | | Extent 13.. 13 |
344 -- +-------------+ | +-----------------+
345 -- | Index 0 | --' | Extent 14..122 |
346 -- +-------------+ +-----------------+
347 -- | Index 123 | --.
348 -- +-------------+ | +-----------------+
349 -- `-> | Hdr depth=0 |
350 -- +-----------------+
351 -- | Extent 123..125 |
352 -- +-----------------+
353 -- | Extent 126..234 |
354 -- +-----------------+
355 --
356
357 Extent_Header_Size : constant := 12;
Nico Huberbc4f7e92024-01-27 01:59:54 +0100358 Extent_Header_Magic : constant := 16#f30a#;
Nico Huberfffc8c12023-12-13 12:44:12 +0100359 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
360 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100361 Dynamic_Max_Index : constant Extent_Idx := State.Static.Block_Size / Extent_Header_Size - 1;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100362 subtype Extent_Depth is Natural range 0 .. 32;
Nico Huberfffc8c12023-12-13 12:44:12 +0100363
364 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
365 is
366 (Idx * Extent_Header_Size + Off);
367
368 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
369 is
370 (Read_LE16 (Buf, 0))
371 with
372 Pre => Buf'Length >= 2;
373
374 function Header_Entries (Buf : Buffer_Type) return Natural
375 is
376 (Natural (Read_LE16 (Buf, 2)))
377 with
378 Pre => Buf'Length >= 4;
379
380 function Header_Depth (Buf : Buffer_Type) return Natural
381 is
382 (Natural (Read_LE16 (Buf, 6)))
383 with
384 Pre => Buf'Length >= 8;
385
386 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
387 is
388 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
389 with
390 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
391
392 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
393 is
394 (FSBlock_Offset
395 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
396 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
397 with
398 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
399
400 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
401 renames Index_Logical;
402
403 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
404 is
405 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
406 with
407 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
408
409 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
410 is
411 (FSBlock_Offset
412 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
413 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
414 with
415 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
416
417 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
418 with
Nico Huber6710c0e2024-01-08 15:56:24 +0100419 Pre =>
420 Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then
421 Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then
422 Extent_Logical (Buf, 1) <= Logical,
423 Post =>
424 Bin_Search'Result in 1 .. Refs and
425 Extent_Logical (Buf, Bin_Search'Result) <= Logical
Nico Huberfffc8c12023-12-13 12:44:12 +0100426 is
Nico Huber6710c0e2024-01-08 15:56:24 +0100427 Left : Positive := 2;
Nico Huberfffc8c12023-12-13 12:44:12 +0100428 Right : Extent_Idx := Refs;
429 begin
430 while Left <= Right loop
431 declare
432 Mid : constant Extent_Idx := (Left + Right) / 2;
433 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
434 begin
435 if Logical < Ext_Logical then
436 Right := Mid - 1;
437 else
438 Left := Mid + 1;
439 end if;
Nico Huber6710c0e2024-01-08 15:56:24 +0100440 pragma Loop_Invariant
441 (Right <= Refs and then
442 Left in 2 .. Refs + 1 and then
443 Extent_Logical (Buf, Left - 1) <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100444 end;
445 end loop;
446 return Left - 1;
447 end Bin_Search;
448
449 procedure Next_Ref
450 (Current : in FSBlock_Offset;
451 Logical_Off : in FSBlock_Logical;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100452 Depth : in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100453 Next : out Extent_Idx;
454 Cache_Start : out Max_Block_Index;
455 Cache_End : out Max_Block_Index;
456 Success : out Boolean)
457 with
Nico Huber1db91932024-01-09 16:58:26 +0100458 Pre =>
459 Logical_Off <= Logical and
460 Dynamic_Max_Index = State.Static.Block_Size / Extent_Header_Size - 1,
461 Post =>
Nico Huber53df7852024-01-15 18:36:04 +0100462 State.Static = State.Static'Old and State.S = State.S'Old and
Nico Huber1db91932024-01-09 16:58:26 +0100463 (if Success then
464 Next <= Dynamic_Max_Index and then
465 Cache_End = Cache_Start + State.Static.Block_Size - 1 and then
466 Extent_Logical (Cache (Cache_Start .. Cache_End), Next) <= Logical)
Nico Huberfffc8c12023-12-13 12:44:12 +0100467 is
Nico Huberfffc8c12023-12-13 12:44:12 +0100468 begin
469 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100470 (Static => State.Static,
471 Cache => State.Cache,
Nico Huberfffc8c12023-12-13 12:44:12 +0100472 Phys => Current,
Nico Huber6bef9242024-01-26 23:47:49 +0100473 Level => Block_Map_Cache_Level + Depth,
Nico Huberfffc8c12023-12-13 12:44:12 +0100474 Cache_Start => Cache_Start,
475 Cache_End => Cache_End,
476 Success => Success);
477 if not Success then
478 Next := 1;
479 return;
480 end if;
481
482 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100483 Hdr_Magic : constant Unsigned_16 := Header_Magic (Cache (Cache_Start .. Cache_End));
484 Hdr_Entries : constant Natural := Header_Entries (Cache (Cache_Start .. Cache_End));
485 Hdr_Depth : constant Natural := Header_Depth (Cache (Cache_Start .. Cache_End));
Nico Huberfffc8c12023-12-13 12:44:12 +0100486 First_Logical : constant FSBlock_Logical :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100487 Extent_Logical (Cache (Cache_Start .. Cache_End), 1);
Nico Huberfffc8c12023-12-13 12:44:12 +0100488 begin
489 Success := Success and then
490 Hdr_Magic = Extent_Header_Magic and then
491 Hdr_Depth = Depth and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100492 Hdr_Entries in Extent_Idx and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100493 Hdr_Entries <= Dynamic_Max_Index and then
494 First_Logical = Logical_Off;
495 if not Success then
496 Next := 1;
497 else
Nico Huber1db91932024-01-09 16:58:26 +0100498 pragma Assert (Cache_End - Cache_Start + 1 = State.Static.Block_Size);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100499 Next := Bin_Search (Cache (Cache_Start .. Cache_End), Hdr_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100500 end if;
501 end;
502 end Next_Ref;
503
Nico Huber7403a542023-12-15 23:13:47 +0100504 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
505 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
506 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
507 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
508 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100509
510 Cache_Start, Cache_End : Max_Block_Index;
511 Logical_Off, Length : FSBlock_Logical;
512 Idx : Extent_Idx;
513 begin
514 Success :=
515 Inode_Magic = Extent_Header_Magic and then
516 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100517 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100518 First_Logical <= Logical and then
519 Depth in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100520 if not Success then
521 Physical := 0;
522 return;
523 end if;
524
Nico Huber7403a542023-12-15 23:13:47 +0100525 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100526 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100527 Physical := Extent_Physical (Inline_Extents, Idx);
528 Logical_Off := Extent_Logical (Inline_Extents, Idx);
529 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100530 else
Nico Huber7403a542023-12-15 23:13:47 +0100531 Physical := Index_Physical (Inline_Extents, Idx);
532 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100533 loop
Nico Huber96a0b0e2024-01-08 15:57:09 +0100534 pragma Loop_Invariant
Nico Huber1db91932024-01-09 16:58:26 +0100535 (State.Static = State.Static'Loop_Entry and then
Nico Huber53df7852024-01-15 18:36:04 +0100536 State.S = State.S'Loop_Entry and then
Nico Huber1db91932024-01-09 16:58:26 +0100537 Depth > 0 and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100538 Depth in Extent_Depth and then
539 Logical_Off <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100540 Depth := Depth - 1;
541 Next_Ref
542 (Current => Physical,
543 Logical_Off => Logical_Off,
544 Depth => Depth,
545 Next => Idx,
546 Cache_Start => Cache_Start,
547 Cache_End => Cache_End,
548 Success => Success);
549 if not Success then
550 return;
551 end if;
552
553 exit when Depth = 0;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100554 Physical := Index_Physical (Cache (Cache_Start .. Cache_End), Idx);
555 Logical_Off := Index_Logical (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100556 end loop;
557
Nico Huber52f4c8d2024-01-09 13:57:33 +0100558 Physical := Extent_Physical (Cache (Cache_Start .. Cache_End), Idx);
559 Logical_Off := Extent_Logical (Cache (Cache_Start .. Cache_End), Idx);
560 Length := Extent_Length (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100561 end if;
562
563 Success :=
564 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100565 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huber1db91932024-01-09 16:58:26 +0100566 Logical < Logical_Off + Length and then
567 FSBlock_Offset (Logical - Logical_Off) <= FSBlock_Offset'Last - Physical;
Nico Huberfffc8c12023-12-13 12:44:12 +0100568 if Success then
569 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
570 end if;
571 end Extent_Block_Map;
572
Nico Huber7eb56922024-01-10 17:22:54 +0100573 procedure Parse_Group
574 (Static : in Mount_State;
575 Table_Start : out FSBlock_Offset;
576 Buf : in Buffer_Type;
577 Success : out Boolean)
578 with
579 Pre =>
580 Buf'Length >= Group_Desc_Size'First and
581 (if Static.Feature_64Bit then Buf'Length >= 64)
582 is
583 Inode_Table_Block : constant Unsigned_64 :=
584 (if Static.Feature_64Bit
585 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, 40)), 32)
586 else 0)
587 or
588 Unsigned_64 (Read_LE32 (Buf, 8));
589 begin
590 if Inode_Table_Block >= Unsigned_64 (Static.First_Data_Block) and
591 Inode_Table_Block <= Unsigned_64 (FSBlock_Offset'Last)
592 then
593 Table_Start := FSBlock_Offset (Inode_Table_Block);
594 Success := True;
595 else
596 Table_Start := 0;
597 Success := False;
598 end if;
599 end Parse_Group;
600
Nico Huber71f9ca02024-01-10 15:38:49 +0100601 procedure Parse_Inode
602 (Static : in Mount_State;
603 Inode : in out Inode_Info;
604 Buf : in Buffer_Type;
605 Success : out Boolean)
606 with
607 Pre => Buf'Length >= Inode_Size'First
608 is
609 S_IFMT : constant := 8#170000#;
610 S_IFDIR : constant := 8#040000#;
611 S_IFREG : constant := 8#100000#;
612 S_IFLNK : constant := 8#120000#;
613
614 I_Mode : constant Unsigned_16 := Read_LE16 (Buf, 0);
615 I_Blocks : constant Unsigned_32 := Read_LE32 (Buf, 28);
616 I_Flags : constant Unsigned_32 := Read_LE32 (Buf, 32);
617 I_File_ACL : constant Unsigned_32 := Read_LE32 (Buf, 104);
618 begin
619 case I_Mode and S_IFMT is
620 when S_IFDIR => Inode.Mode := Dir;
621 when S_IFREG => Inode.Mode := Regular;
622 when S_IFLNK => Inode.Mode := Link;
623 when others => Success := False; return;
624 end case;
625
626 if Inode.Mode = Link and
627 ((I_File_ACL = 0 and I_Blocks = 0) or
628 (I_File_ACL /= 0 and I_Blocks = 2 ** (Static.Block_Size_Bits - 9)))
629 then
630 Inode.Mode := Fast_Link;
631 end if;
632
633 declare
634 I_Size : constant Unsigned_64 :=
635 (if Inode.Mode = Regular
636 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, 108)), 32)
637 else 0) or
638 Unsigned_64 (Read_LE32 (Buf, 4));
639 begin
640 if I_Size > Unsigned_64 (Inode_Length'Last) then
641 Success := False;
642 return;
643 end if;
644 Inode.Size := Inode_Length (I_Size);
645 end;
646
647 Inode.Use_Extents := Static.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
648 Inode.Inline := Buf (Buf'First + 40 .. Buf'First + 100 - 1);
649
650 Success := True;
651 end Parse_Inode;
652
Nico Huber57d3a852023-12-04 15:42:40 +0100653 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100654 (State : in out T;
655 Inode : in Inode_Index;
656 Success : out Boolean)
657 with
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100658 Pre => Is_Mounted (State),
Nico Huber53df7852024-01-15 18:36:04 +0100659 Post => Is_Mounted (State) and (Success = Is_Open (State))
Nico Hubercdc03512023-12-13 23:32:54 +0100660 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100661 Static : Mount_State renames State.Static;
662 Cache : Cache_Buffer renames State.Cache.Buffer;
663
Nico Huber52f4c8d2024-01-09 13:57:33 +0100664 Group : constant Group_Index := Group_Index ((Inode - 1) / Static.Inodes_Per_Group);
Nico Huber7eb56922024-01-10 17:22:54 +0100665 Group_Block : constant FSBlock_Offset :=
666 1 + FSBlock_Offset (Static.First_Data_Block) + FSBlock_Offset (Group / Static.Group_Desc_Per_Block);
667 Group_In_Block : constant Group_In_Block_Index := Group_In_Block_Index (Group mod Static.Group_Desc_Per_Block);
668 Group_In_Block_Offset : constant Natural := Natural (Group_In_Block) * Static.Group_Desc_Size;
669
Nico Hubercdc03512023-12-13 23:32:54 +0100670 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber7eb56922024-01-10 17:22:54 +0100671 First_Table_Block : FSBlock_Offset;
Nico Hubercdc03512023-12-13 23:32:54 +0100672 begin
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100673 State.S := Mounted;
674
Nico Hubercdc03512023-12-13 23:32:54 +0100675 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100676 (Static => Static,
677 Cache => State.Cache,
Nico Huber7eb56922024-01-10 17:22:54 +0100678 Phys => Group_Block,
Nico Huber6bef9242024-01-26 23:47:49 +0100679 Level => Group_Cache_Level,
Nico Hubercdc03512023-12-13 23:32:54 +0100680 Cache_Start => Cache_Start,
681 Cache_End => Cache_End,
682 Success => Success);
Nico Hubereb012072024-01-10 17:56:24 +0100683 Success := Success and then
684 -- TODO: Prove this using a predicate on Mount_State:
685 Cache_Start <= Cache_End - Group_In_Block_Offset - Static.Group_Desc_Size + 1;
Nico Hubercdc03512023-12-13 23:32:54 +0100686 if not Success then
687 return;
688 end if;
689
Nico Huber7eb56922024-01-10 17:22:54 +0100690 Parse_Group
691 (Static => Static,
692 Table_Start => First_Table_Block,
693 Buf => Cache (Cache_Start + Group_In_Block_Offset ..
694 Cache_Start + Group_In_Block_Offset + Static.Group_Desc_Size - 1),
695 Success => Success);
696 if not Success then
697 return;
698 end if;
Nico Huber7eb56922024-01-10 17:22:54 +0100699
Nico Hubercdc03512023-12-13 23:32:54 +0100700 declare
Nico Huberecafb8f2024-01-09 15:45:49 +0100701 Inode_In_Group : constant Inode_In_Group_Index :=
702 Inode_In_Group_Index ((Inode - 1) mod Static.Inodes_Per_Group);
Nico Huber71f9ca02024-01-10 15:38:49 +0100703
Nico Huber7eb56922024-01-10 17:22:54 +0100704 Inode_Block : constant FSBlock_Offset := FSBlock_Offset (Inode_In_Group / Static.Inodes_Per_Block);
Nico Huber71f9ca02024-01-10 15:38:49 +0100705 Inode_In_Block : constant Inode_In_Block_Index :=
706 Inode_In_Block_Index (Inode_In_Group mod Static.Inodes_Per_Block);
707 Inode_In_Block_Offset : constant Natural :=
708 Natural (Inode_In_Block) * Static.Inode_Size;
Nico Hubercdc03512023-12-13 23:32:54 +0100709 begin
Nico Huber7eb56922024-01-10 17:22:54 +0100710 if First_Table_Block > FSBlock_Offset'Last - Inode_Block then
Nico Huber925326e2024-01-09 18:46:26 +0100711 Success := False;
712 return;
713 end if;
Nico Huber925326e2024-01-09 18:46:26 +0100714
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 => FSBlock_Offset (First_Table_Block + Inode_Block),
Nico Huber6bef9242024-01-26 23:47:49 +0100719 Level => Inode_Cache_Level,
Nico Hubercdc03512023-12-13 23:32:54 +0100720 Cache_Start => Cache_Start,
721 Cache_End => Cache_End,
722 Success => Success);
Nico Hubereb012072024-01-10 17:56:24 +0100723 Success := Success and then
724 -- TODO: Prove this using a predicate on Mount_State:
725 Cache_Start <= Cache_End - Inode_In_Block_Offset - Static.Inode_Size + 1;
Nico Hubercdc03512023-12-13 23:32:54 +0100726 if not Success then
727 return;
728 end if;
729
Nico Huber71f9ca02024-01-10 15:38:49 +0100730 Parse_Inode
731 (Static => State.Static,
732 Inode => State.Inode,
733 Buf => Cache (Cache_Start + Inode_In_Block_Offset ..
734 Cache_Start + Inode_In_Block_Offset + Static.Inode_Size - 1),
735 Success => Success);
Nico Huber022e2262023-12-15 23:15:17 +0100736
Nico Huber71f9ca02024-01-10 15:38:49 +0100737 if Success then
Nico Huber21d42022023-12-16 02:50:22 +0100738 State.Inode.I := Inode;
Nico Hubercdc03512023-12-13 23:32:54 +0100739 State.S := File_Opened;
Nico Huber71f9ca02024-01-10 15:38:49 +0100740 end if;
Nico Hubercdc03512023-12-13 23:32:54 +0100741 end;
742 end Open;
743
744 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100745 (State : in out T;
746 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100747 File_Type : out FS.File_Type;
748 File_Name : in String;
749 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100750 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100751 is
Nico Huber21d42022023-12-16 02:50:22 +0100752 File_Name_Max : constant := 255;
753 Root_Inode : constant := 2;
754
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100755 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean
756 with
757 Pre => Str'Length <= Buf'Length
758 is
Nico Huber21d42022023-12-16 02:50:22 +0100759 begin
760 for I in Str'Range loop
761 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
762 return False;
763 end if;
764 end loop;
765 return True;
766 end Str_Buf_Equal;
767
768 File_Inode : Inode_Index;
769 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100770 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100771 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100772 File_Type := FS.File_Type'First;
773
774 if File_Name'Length > File_Name_Max then
775 Success := False;
776 return;
777 end if;
778
779 -- Ensure dir is opened:
780 --
781 if State.S = File_Opened then
782 if State.Cur_Dir /= State.Inode.I then
783 Success := False;
784 return;
785 end if;
786 else
787 if In_Root then
788 State.Cur_Dir := Root_Inode;
789 end if;
790 Open (State, State.Cur_Dir, Success);
791 if not Success then
792 return;
793 end if;
794 end if;
795
796 -- Lookup file in opened dir:
797 --
798 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100799 Success := False;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100800 loop
801 pragma Loop_Invariant (Is_Open (State) and not Success);
Nico Huber21d42022023-12-16 02:50:22 +0100802 declare
803 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100804 Dir_Entry_Header : Buffer_Type (0 .. Dir_Entry_Header_Length - 1);
805 Dir_Entry_Name : Buffer_Type (0 .. File_Name_Max - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100806 Entry_File_Pos : File_Offset := File_Pos;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100807 Dir_Entry_Length : File_Length;
808 Inode : Inode_0_Index;
Nico Huber21d42022023-12-16 02:50:22 +0100809 Len : Natural;
810 begin
811 Read
812 (State => State,
813 File_Pos => Entry_File_Pos,
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100814 Buf => Dir_Entry_Header,
Nico Huber21d42022023-12-16 02:50:22 +0100815 Len => Len);
816 if Len < Dir_Entry_Header_Length then
817 return;
818 end if;
819
820 -- Only check filenames of exact same length
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100821 Inode := Inode_0_Index (Read_LE32 (Dir_Entry_Header, 0));
822 if Inode > Root_Inode and then
823 File_Name'Length = Natural (Dir_Entry_Header (6))
Nico Huber21d42022023-12-16 02:50:22 +0100824 then
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100825 pragma Warnings
826 (GNATprove, Off, """Entry_File_Pos"" is set*but not used",
827 Reason => "We only care about intermedidate values.");
Nico Huber21d42022023-12-16 02:50:22 +0100828 Read
829 (State => State,
830 File_Pos => Entry_File_Pos,
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100831 Buf => Dir_Entry_Name,
Nico Huber21d42022023-12-16 02:50:22 +0100832 Len => Len);
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100833 pragma Warnings (GNATprove, On, """Entry_File_Pos"" is set*but not used");
Nico Huber21d42022023-12-16 02:50:22 +0100834 if Len < File_Name'Length then
835 return;
836 end if;
837
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100838 Success := Str_Buf_Equal (File_Name, Dir_Entry_Name);
839 if Success then
840 File_Inode := Inode;
841 exit;
842 end if;
Nico Huber21d42022023-12-16 02:50:22 +0100843 end if;
844
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100845 Dir_Entry_Length := File_Length (Read_LE16 (Dir_Entry_Header, 4));
846 if File_Pos > File_Length'Last - Dir_Entry_Length or
847 Unsigned_64 (File_Pos) >= Unsigned_64 (State.Inode.Size) - Unsigned_64 (Dir_Entry_Length)
848 then
849 return;
850 end if;
851 File_Pos := File_Pos + Dir_Entry_Length;
Nico Huber21d42022023-12-16 02:50:22 +0100852 end;
853 end loop;
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100854 pragma Assert_And_Cut (Success and Is_Mounted (State));
Nico Huber21d42022023-12-16 02:50:22 +0100855
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100856 Open (State, File_Inode, Success);
857 if not Success then
858 return;
Nico Huber21d42022023-12-16 02:50:22 +0100859 end if;
860
Nico Hubere5d7c0e2024-01-10 18:53:13 +0100861 if State.Inode.Mode = Dir then
862 State.Cur_Dir := File_Inode;
863 end if;
864
865 Success := State.Inode.Size <= Inode_Length (File_Length'Last);
Nico Huber21d42022023-12-16 02:50:22 +0100866 if Success then
867 File_Len := File_Length (State.Inode.Size);
868 File_Type := (case State.Inode.Mode is
869 when Dir => FS.Dir,
870 when Regular => FS.Regular,
871 when Link .. Fast_Link => FS.Link);
872 else
873 Close (State);
874 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100875 end Open;
876
Nico Huber57d3a852023-12-04 15:42:40 +0100877 procedure Close (State : in out T) is
878 begin
879 State.S := Mounted;
880 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100881
Nico Huber57d3a852023-12-04 15:42:40 +0100882 procedure Read
883 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100884 File_Pos : in out File_Offset;
885 Buf : out Buffer_Type;
886 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100887 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100888 Static : Mount_State renames State.Static;
889 Cache : Cache_Buffer renames State.Cache.Buffer;
890
Nico Huber53df7852024-01-15 18:36:04 +0100891 Pos : Natural range Buf'First .. Buf'Last + 1;
Nico Huber1d7727f2023-11-30 15:58:46 +0100892 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100893 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100894 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
895 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100896 Len := 0;
897 else
Nico Huber549a1b82023-12-17 01:51:59 +0100898 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100899 end if;
900 Buf (Buf'First .. Buf'First + Len - 1) :=
901 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
902 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
903 File_Pos := File_Pos + File_Length (Len);
904 return;
905 end if;
906
Nico Huber1d7727f2023-11-30 15:58:46 +0100907 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100908 Pos := Buf'First;
Nico Huber53df7852024-01-15 18:36:04 +0100909 while Pos <= Buf'Last and
910 File_Pos < File_Offset'Last and
911 Inode_Length (File_Pos) < State.Inode.Size
912 loop
913 pragma Loop_Invariant (Is_Open (State));
Nico Huberd3644be2023-12-16 01:43:00 +0100914 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100915 In_Block : constant Max_Block_Index := Natural (File_Pos) mod Static.Block_Size;
Nico Huber53df7852024-01-15 18:36:04 +0100916 Logical : constant FSBlock_Logical :=
917 FSBlock_Logical (File_Pos / File_Offset (Static.Block_Size));
918 In_Block_Space : constant Positive := Static.Block_Size - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100919 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huber53df7852024-01-15 18:36:04 +0100920 In_Buf_Space : constant Positive := Buf'Last - Pos + 1;
921 Len_Here : Positive;
Nico Huberd3644be2023-12-16 01:43:00 +0100922 begin
923 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100924 if In_File_Space < Inode_Length (Len_Here) then
Nico Huber53df7852024-01-15 18:36:04 +0100925 Len_Here := Positive (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100926 end if;
927 if In_Buf_Space < Len_Here then
928 Len_Here := In_Buf_Space;
929 end if;
Nico Huber53df7852024-01-15 18:36:04 +0100930 if File_Offset'Last - File_Pos < File_Length (Len_Here) then
931 Len_Here := Positive (File_Offset'Last - File_Pos);
932 end if;
Nico Huberd3644be2023-12-16 01:43:00 +0100933
934 declare
935 Last : constant Index_Type := Pos + Len_Here - 1;
936 Cache_Start, Cache_End : Max_Block_Index;
937 Physical : FSBlock_Offset;
938 Success : Boolean;
939 begin
940 if State.Inode.Use_Extents then
941 Extent_Block_Map (State, Logical, Physical, Success);
942 else
943 Ext2_Block_Map (State, Logical, Physical, Success);
944 end if;
Nico Huber53df7852024-01-15 18:36:04 +0100945 exit when not Success;
946
947 Cache_FSBlock
948 (Static => Static,
949 Cache => State.Cache,
950 Phys => Physical,
Nico Huber6bef9242024-01-26 23:47:49 +0100951 Level => File_Cache_Level,
Nico Huber53df7852024-01-15 18:36:04 +0100952 Cache_Start => Cache_Start,
953 Cache_End => Cache_End,
954 Success => Success);
955 pragma Assert (Cache_Start <= Cache_End - (In_Block + Len_Here - 1));
Nico Huberd3644be2023-12-16 01:43:00 +0100956 exit when not Success;
957
Nico Huber52f4c8d2024-01-09 13:57:33 +0100958 Buf (Pos .. Last) := Cache (
Nico Huberd3644be2023-12-16 01:43:00 +0100959 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
960 File_Pos := File_Pos + File_Length (Len_Here);
961 Pos := Pos + Len_Here;
Nico Huber53df7852024-01-15 18:36:04 +0100962 Len := Pos - Buf'First;
Nico Huberd3644be2023-12-16 01:43:00 +0100963 end;
964 end;
965 end loop;
966 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +0100967 end Read;
968
Nico Huber26f71832023-12-05 16:26:56 +0100969 --------------------------------------------------------------------------
970
971 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100972
973 function C_Mount return int
974 with
975 Export,
976 Convention => C,
977 External_Name => "ext2fs_mount";
978 function C_Mount return int
979 with
980 SPARK_Mode => Off
981 is
982 begin
983 return C.C_Mount;
984 end C_Mount;
985
Nico Huber3da21472023-12-18 15:43:35 +0100986 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100987 with
988 Export,
989 Convention => C,
990 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +0100991 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100992 with
993 SPARK_Mode => Off
994 is
995 begin
996 return C.C_Open (File_Path);
997 end C_Open;
998
999 function C_Read (Buf : System.Address; Len : int) return int
1000 with
1001 Export,
1002 Convention => C,
1003 External_Name => "ext2fs_read";
1004 function C_Read (Buf : System.Address; Len : int) return int
1005 with
1006 SPARK_Mode => Off
1007 is
1008 begin
1009 return C.C_Read (Buf, Len);
1010 end C_Read;
1011
Thomas Heijligen5c43abc2023-12-11 15:24:36 +00001012end FILO.FS.Ext2;