blob: 0d3df607d29c7cf423408dc0e06e63d86ea194bf [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 Huberecafb8f2024-01-09 15:45:49 +010074 pragma Assert_And_Cut (Success);
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 Huber21d42022023-12-16 02:50:22 +010079 if S_Inodes_Per_Group in 2 .. Unsigned_32'Last then
Nico Huber52f4c8d2024-01-09 13:57:33 +010080 Static.Inodes_Per_Group := Inode_Index (S_Inodes_Per_Group);
Nico Huber26f71832023-12-05 16:26:56 +010081 else
82 Success := False;
83 return;
84 end if;
85 end;
Nico Huberecafb8f2024-01-09 15:45:49 +010086 pragma Assert_And_Cut (Success);
Nico Huber26f71832023-12-05 16:26:56 +010087
88 declare
89 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
90 begin
91 if S_Rev_Level >= DYNAMIC_REV then
92 declare
93 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
94 begin
Nico Huberecafb8f2024-01-09 15:45:49 +010095 if Natural (S_Inode_Size) in Inode_Size and then
96 Natural (S_Inode_Size) <= Static.Block_Size
97 then
Nico Huber52f4c8d2024-01-09 13:57:33 +010098 Static.Inode_Size := Inode_Size (S_Inode_Size);
Nico Huber26f71832023-12-05 16:26:56 +010099 else
100 Success := False;
101 return;
102 end if;
Nico Huberecafb8f2024-01-09 15:45:49 +0100103 pragma Assert (Static.Block_Size / Static.Inode_Size <= Natural (Inode_In_Block_Count'Last));
Nico Huber26f71832023-12-05 16:26:56 +0100104 end;
105 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100106 Static.Inode_Size := Inode_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100107 end if;
Nico Huberecafb8f2024-01-09 15:45:49 +0100108 Static.Inodes_Per_Block := Inode_In_Block_Count (Static.Block_Size / Static.Inode_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100109 end;
Nico Huberecafb8f2024-01-09 15:45:49 +0100110 pragma Assert_And_Cut (Success);
Nico Huber26f71832023-12-05 16:26:56 +0100111
112 declare
113 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
114 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100115 Static.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
116 Static.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
117 if Static.Feature_64Bit then
Nico Huber26f71832023-12-05 16:26:56 +0100118 declare
119 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
120 begin
Nico Huber700a4112024-01-08 15:50:09 +0100121 if Natural (S_Desc_Size) in Desc_Size and
Nico Huber52f4c8d2024-01-09 13:57:33 +0100122 Natural (S_Desc_Size) <= Static.Block_Size and
Nico Huber700a4112024-01-08 15:50:09 +0100123 Is_Power_Of_2 (S_Desc_Size)
Nico Huber26f71832023-12-05 16:26:56 +0100124 then
Nico Huber52f4c8d2024-01-09 13:57:33 +0100125 Static.Desc_Size := Desc_Size (S_Desc_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100126 else
127 Success := False;
128 return;
129 end if;
130 end;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100131 Static.Feature_64Bit := Static.Feature_64Bit and Static.Desc_Size >= 64;
Nico Huber26f71832023-12-05 16:26:56 +0100132 else
Nico Huber52f4c8d2024-01-09 13:57:33 +0100133 Static.Desc_Size := Desc_Size'First;
Nico Huber26f71832023-12-05 16:26:56 +0100134 end if;
Nico Huberecafb8f2024-01-09 15:45:49 +0100135 Static.Desc_Per_Block := Group_Index (Static.Block_Size / Static.Desc_Size);
Nico Huber26f71832023-12-05 16:26:56 +0100136 end;
Nico Huberecafb8f2024-01-09 15:45:49 +0100137 pragma Assert_And_Cut (Success);
Nico Huber26f71832023-12-05 16:26:56 +0100138
139 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100140 end Mount;
141
Nico Huberf5d99d02023-12-12 13:42:55 +0100142 procedure Read_FSBlock
Nico Huber700a4112024-01-08 15:50:09 +0100143 (Buf : in out Buffer_Type;
Nico Huberf5d99d02023-12-12 13:42:55 +0100144 FSBlock : in FSBlock_Offset;
Nico Huber700a4112024-01-08 15:50:09 +0100145 Part_Len : in Partition_Length;
Nico Huberf5d99d02023-12-12 13:42:55 +0100146 Success : out Boolean)
147 with
Nico Huber700a4112024-01-08 15:50:09 +0100148 Pre => Buf'Length in Block_Size
Nico Huberf5d99d02023-12-12 13:42:55 +0100149 is
Nico Huber700a4112024-01-08 15:50:09 +0100150 FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock);
151 Block_Size : constant Integer_64 := Integer_64 (Buf'Length);
152 Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1;
Nico Huberf5d99d02023-12-12 13:42:55 +0100153 begin
Nico Huber700a4112024-01-08 15:50:09 +0100154 if FSBlock_64 > Max_Block_Offset then
Nico Huberf5d99d02023-12-12 13:42:55 +0100155 Success := False;
156 return;
157 end if;
Nico Huber700a4112024-01-08 15:50:09 +0100158 Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success);
Nico Huberf5d99d02023-12-12 13:42:55 +0100159 end Read_FSBlock;
160
Nico Huber68c86932023-12-13 11:03:11 +0100161 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100162 (Static : in Mount_State;
163 Cache : in out Block_Cache;
Nico Huber68c86932023-12-13 11:03:11 +0100164 Phys : in FSBlock_Offset;
165 Level : in Block_Cache_Index;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100166 Label : in Cache_Label;
167 Logical : in Boolean := True;
Nico Huber68c86932023-12-13 11:03:11 +0100168 Cache_Start : out Max_Block_Index;
169 Cache_End : out Max_Block_Index;
170 Success : out Boolean)
171 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100172 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber68c86932023-12-13 11:03:11 +0100173 is
Nico Huber68c86932023-12-13 11:03:11 +0100174 -- Limit cache usage depending on block size:
Nico Huber52f4c8d2024-01-09 13:57:33 +0100175 Max_Level : constant Block_Cache_Index := Block_Size'Last / Static.Block_Size - 1;
Nico Huber700a4112024-01-08 15:50:09 +0100176 Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level);
Nico Huber68c86932023-12-13 11:03:11 +0100177 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100178 Cache_Start := Cache_Level * Static.Block_Size;
179 Cache_End := Cache_Start + Static.Block_Size - 1;
180 if Cache.Logical (Cache_Level) = Logical and
181 Cache.Label (Cache_Level) = Label
Nico Huber57dfbfb2023-12-13 23:24:16 +0100182 then
Nico Huber68c86932023-12-13 11:03:11 +0100183 Success := True;
184 else
185 Read_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100186 (Buf => Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber68c86932023-12-13 11:03:11 +0100187 FSBlock => Phys,
Nico Huber52f4c8d2024-01-09 13:57:33 +0100188 Part_Len => Static.Part_Len,
Nico Huber68c86932023-12-13 11:03:11 +0100189 Success => Success);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100190 Cache.Logical (Cache_Level) := Logical; -- FIXME: Level needs to be part of Label
191 Cache.Label (Cache_Level) := Label;
Nico Huber68c86932023-12-13 11:03:11 +0100192 end if;
193 end Cache_FSBlock;
194
Nico Huber57dfbfb2023-12-13 23:24:16 +0100195 procedure Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100196 (Static : in Mount_State;
197 Cache : in out Block_Cache;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100198 Phys : in FSBlock_Offset;
199 Level : in Block_Cache_Index;
200 Cache_Start : out Max_Block_Index;
201 Cache_End : out Max_Block_Index;
202 Success : out Boolean)
203 with
Nico Huber52f4c8d2024-01-09 13:57:33 +0100204 Post => Cache_End = Cache_Start + Static.Block_Size - 1
Nico Huber57dfbfb2023-12-13 23:24:16 +0100205 is
206 begin
Nico Huber52f4c8d2024-01-09 13:57:33 +0100207 Cache_FSBlock (Static, Cache, Phys, Level, Cache_Label (Phys),
Nico Huber57dfbfb2023-12-13 23:24:16 +0100208 False, Cache_Start, Cache_End, Success);
209 end Cache_FSBlock;
210
Nico Huber52f4c8d2024-01-09 13:57:33 +0100211 procedure Reset_Cache_Logical (Cache : in out Block_Cache) is
Nico Huber57dfbfb2023-12-13 23:24:16 +0100212 begin
213 for I in Block_Cache_Index loop
Nico Huber52f4c8d2024-01-09 13:57:33 +0100214 if Cache.Logical (I) then
215 Cache.Logical (I) := False;
216 Cache.Label (I) := 0;
Nico Huber57dfbfb2023-12-13 23:24:16 +0100217 end if;
218 end loop;
219 end Reset_Cache_Logical;
220
Nico Huber6623c982023-12-12 16:35:46 +0100221 procedure Ext2_Block_Map
222 (State : in out T;
223 Logical : in FSBlock_Logical;
224 Physical : out FSBlock_Offset;
225 Success : out Boolean)
226 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100227 Static : constant Mount_State := State.Static;
228
Nico Huber33f6d952023-12-13 23:16:42 +0100229 Direct_Blocks : constant := 12;
230 type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32;
231 type Inode_Blocks is record
232 Direct_Blocks : Direct_Blocks_Array;
233 Indirect_Block : Unsigned_32;
234 Double_Indirect : Unsigned_32;
235 Triple_Indirect : Unsigned_32;
236 end record
Nico Huber5a042fd2024-01-08 15:54:57 +0100237 with Object_Size => Inode_Extents'Length * 8;
Nico Huber33f6d952023-12-13 23:16:42 +0100238
239 function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks);
Nico Huber7403a542023-12-15 23:13:47 +0100240 function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline));
Nico Huber33f6d952023-12-13 23:16:42 +0100241
Nico Huber52f4c8d2024-01-09 13:57:33 +0100242 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Static.Block_Size / 4);
Nico Huber700a4112024-01-08 15:50:09 +0100243 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4);
Nico Huber6623c982023-12-12 16:35:46 +0100244 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
245
246 procedure Indirect_Block_Lookup
247 (Indirect_Block_Phys : in FSBlock_Offset;
248 Addr_In_Block : in Addr_In_Block_Range;
249 Level : in Block_Cache_Index;
250 Logical_Off : in FSBlock_Logical;
251 Next_Physical : out FSBlock_Offset;
252 Success : out Boolean)
253 with
Nico Huber5a042fd2024-01-08 15:54:57 +0100254 Pre =>
Nico Huber52f4c8d2024-01-09 13:57:33 +0100255 Addr_Per_Block = FSBlock_Logical (Static.Block_Size / 4) and
Nico Huber5a042fd2024-01-08 15:54:57 +0100256 FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
Nico Huber6623c982023-12-12 16:35:46 +0100257 is
Nico Huber68c86932023-12-13 11:03:11 +0100258 Cache_Start, Cache_End : Max_Block_Index;
Nico Huber6623c982023-12-12 16:35:46 +0100259 begin
Nico Huber68c86932023-12-13 11:03:11 +0100260 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100261 (Static => Static,
262 Cache => State.Cache,
Nico Huber68c86932023-12-13 11:03:11 +0100263 Phys => Indirect_Block_Phys,
264 Level => Level,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100265 Label => Cache_Label (Logical_Off),
Nico Huber68c86932023-12-13 11:03:11 +0100266 Cache_Start => Cache_Start,
267 Cache_End => Cache_End,
268 Success => Success);
Nico Huber6623c982023-12-12 16:35:46 +0100269 Next_Physical := FSBlock_Offset (Read_LE32
Nico Huber52f4c8d2024-01-09 13:57:33 +0100270 (Buf => State.Cache.Buffer (Cache_Start .. Cache_End),
Nico Huber6623c982023-12-12 16:35:46 +0100271 Off => Natural (Addr_In_Block) * 4));
Nico Huber6623c982023-12-12 16:35:46 +0100272 end Indirect_Block_Lookup;
273
274 Logical_Rest : FSBlock_Logical := Logical;
Nico Huber5a042fd2024-01-08 15:54:57 +0100275
276 pragma Assert (Addr_Per_Block <= 2 ** 14);
Nico Huber6623c982023-12-12 16:35:46 +0100277 begin
278 if Logical_Rest < Direct_Blocks then
Nico Huber33f6d952023-12-13 23:16:42 +0100279 Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical)));
Nico Huber6623c982023-12-12 16:35:46 +0100280 Success := True;
281 return;
282 end if;
283
284 Logical_Rest := Logical_Rest - Direct_Blocks;
285 if Logical_Rest < Addr_Per_Block then
286 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100287 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block),
Nico Huber6623c982023-12-12 16:35:46 +0100288 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
Nico Huberfe897122023-12-12 21:33:36 +0100289 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100290 Logical_Off => Logical - Logical_Rest,
291 Next_Physical => Physical,
292 Success => Success);
293 return;
294 end if;
295
296 Logical_Rest := Logical_Rest - Addr_Per_Block;
297 if Logical_Rest < Addr_Per_Block ** 2 then
298 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100299 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100300 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100301 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100302 Logical_Off => Logical - Logical_Rest,
303 Next_Physical => Physical,
304 Success => Success);
305 if not Success then
306 return;
307 end if;
308
309 Indirect_Block_Lookup
310 (Indirect_Block_Phys => Physical,
311 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100312 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100313 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
314 Next_Physical => Physical,
315 Success => Success);
316 return;
317 end if;
318
319 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
320 if Logical_Rest < Addr_Per_Block ** 3 then
321 Indirect_Block_Lookup
Nico Huber33f6d952023-12-13 23:16:42 +0100322 (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect),
Nico Huber5a042fd2024-01-08 15:54:57 +0100323 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block ** 2) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100324 Level => 2,
Nico Huber6623c982023-12-12 16:35:46 +0100325 Logical_Off => Logical - Logical_Rest,
326 Next_Physical => Physical,
327 Success => Success);
328 if not Success then
329 return;
330 end if;
331
332 Indirect_Block_Lookup
333 (Indirect_Block_Phys => Physical,
Nico Huber5a042fd2024-01-08 15:54:57 +0100334 Addr_In_Block => Addr_In_Block_Range ((Logical_Rest / Addr_Per_Block) mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100335 Level => 1,
Nico Huber6623c982023-12-12 16:35:46 +0100336 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
337 Next_Physical => Physical,
338 Success => Success);
339 if not Success then
340 return;
341 end if;
342
343 Indirect_Block_Lookup
344 (Indirect_Block_Phys => Physical,
345 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
Nico Huberfe897122023-12-12 21:33:36 +0100346 Level => 0,
Nico Huber6623c982023-12-12 16:35:46 +0100347 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
348 Next_Physical => Physical,
349 Success => Success);
350 return;
351 end if;
352
353 -- Logical address was just too high.
Nico Huber5a042fd2024-01-08 15:54:57 +0100354 Physical := 0;
Nico Huber6623c982023-12-12 16:35:46 +0100355 Success := False;
356 end Ext2_Block_Map;
357
Nico Huberfffc8c12023-12-13 12:44:12 +0100358 procedure Extent_Block_Map
359 (State : in out T;
360 Logical : in FSBlock_Logical;
361 Physical : out FSBlock_Offset;
362 Success : out Boolean)
363 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100364 Cache : Cache_Buffer renames State.Cache.Buffer;
365
Nico Huberfffc8c12023-12-13 12:44:12 +0100366 -- Extent blocks always start with a 12B header and contain 12B entries.
367 -- Every entry starts with the number of the first logical block it
368 -- covers. Entries are sorted by this number.
369 -- Depth > 0 blocks have index entries, referencing further extent blocks.
370 -- Depth = 0 blocks have extent entries, referencing a contiguous range
371 -- of data blocks.
372 --
373 -- +-----------------+
374 -- .-> | Hdr depth=0 |
375 -- | +-----------------+
376 -- | | Extent 0.. 12 |
377 -- +-------------+ | +-----------------+
378 -- | Hdr depth=1 | | | Extent 13.. 13 |
379 -- +-------------+ | +-----------------+
380 -- | Index 0 | --' | Extent 14..122 |
381 -- +-------------+ +-----------------+
382 -- | Index 123 | --.
383 -- +-------------+ | +-----------------+
384 -- `-> | Hdr depth=0 |
385 -- +-----------------+
386 -- | Extent 123..125 |
387 -- +-----------------+
388 -- | Extent 126..234 |
389 -- +-----------------+
390 --
391
392 Extent_Header_Size : constant := 12;
393 Extent_Header_Magic : constant := 16#f03a#;
394 subtype Extent_Off is Natural range 0 .. Extent_Header_Size;
395 subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100396 Dynamic_Max_Index : constant Extent_Idx := State.Static.Block_Size / Extent_Header_Size - 1;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100397 subtype Extent_Depth is Natural range 0 .. 32;
Nico Huberfffc8c12023-12-13 12:44:12 +0100398
399 function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural
400 is
401 (Idx * Extent_Header_Size + Off);
402
403 function Header_Magic (Buf : Buffer_Type) return Unsigned_16
404 is
405 (Read_LE16 (Buf, 0))
406 with
407 Pre => Buf'Length >= 2;
408
409 function Header_Entries (Buf : Buffer_Type) return Natural
410 is
411 (Natural (Read_LE16 (Buf, 2)))
412 with
413 Pre => Buf'Length >= 4;
414
415 function Header_Depth (Buf : Buffer_Type) return Natural
416 is
417 (Natural (Read_LE16 (Buf, 6)))
418 with
419 Pre => Buf'Length >= 8;
420
421 function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
422 is
423 (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0))))
424 with
425 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4);
426
427 function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
428 is
429 (FSBlock_Offset
430 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or
431 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4)))))
432 with
433 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10);
434
435 function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
436 renames Index_Logical;
437
438 function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical
439 is
440 (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4))))
441 with
442 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6);
443
444 function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset
445 is
446 (FSBlock_Offset
447 (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or
448 Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8)))))
449 with
450 Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12);
451
452 function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx
453 with
Nico Huber6710c0e2024-01-08 15:56:24 +0100454 Pre =>
455 Buf'Length in 2 * Extent_Header_Size .. Max_Block_Index'Last and then
456 Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and then
457 Extent_Logical (Buf, 1) <= Logical,
458 Post =>
459 Bin_Search'Result in 1 .. Refs and
460 Extent_Logical (Buf, Bin_Search'Result) <= Logical
Nico Huberfffc8c12023-12-13 12:44:12 +0100461 is
Nico Huber6710c0e2024-01-08 15:56:24 +0100462 Left : Positive := 2;
Nico Huberfffc8c12023-12-13 12:44:12 +0100463 Right : Extent_Idx := Refs;
464 begin
465 while Left <= Right loop
466 declare
467 Mid : constant Extent_Idx := (Left + Right) / 2;
468 Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid);
469 begin
470 if Logical < Ext_Logical then
471 Right := Mid - 1;
472 else
473 Left := Mid + 1;
474 end if;
Nico Huber6710c0e2024-01-08 15:56:24 +0100475 pragma Loop_Invariant
476 (Right <= Refs and then
477 Left in 2 .. Refs + 1 and then
478 Extent_Logical (Buf, Left - 1) <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100479 end;
480 end loop;
481 return Left - 1;
482 end Bin_Search;
483
484 procedure Next_Ref
485 (Current : in FSBlock_Offset;
486 Logical_Off : in FSBlock_Logical;
Nico Huber96a0b0e2024-01-08 15:57:09 +0100487 Depth : in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100488 Next : out Extent_Idx;
489 Cache_Start : out Max_Block_Index;
490 Cache_End : out Max_Block_Index;
491 Success : out Boolean)
492 with
Nico Huber1db91932024-01-09 16:58:26 +0100493 Pre =>
494 Logical_Off <= Logical and
495 Dynamic_Max_Index = State.Static.Block_Size / Extent_Header_Size - 1,
496 Post =>
497 State.Static = State.Static'Old and
498 (if Success then
499 Next <= Dynamic_Max_Index and then
500 Cache_End = Cache_Start + State.Static.Block_Size - 1 and then
501 Extent_Logical (Cache (Cache_Start .. Cache_End), Next) <= Logical)
Nico Huberfffc8c12023-12-13 12:44:12 +0100502 is
Nico Huberfffc8c12023-12-13 12:44:12 +0100503 begin
504 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100505 (Static => State.Static,
506 Cache => State.Cache,
Nico Huberfffc8c12023-12-13 12:44:12 +0100507 Phys => Current,
508 Level => Depth,
Nico Huber57dfbfb2023-12-13 23:24:16 +0100509 Label => Cache_Label (Logical_Off),
Nico Huberfffc8c12023-12-13 12:44:12 +0100510 Cache_Start => Cache_Start,
511 Cache_End => Cache_End,
512 Success => Success);
513 if not Success then
514 Next := 1;
515 return;
516 end if;
517
518 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100519 Hdr_Magic : constant Unsigned_16 := Header_Magic (Cache (Cache_Start .. Cache_End));
520 Hdr_Entries : constant Natural := Header_Entries (Cache (Cache_Start .. Cache_End));
521 Hdr_Depth : constant Natural := Header_Depth (Cache (Cache_Start .. Cache_End));
Nico Huberfffc8c12023-12-13 12:44:12 +0100522 First_Logical : constant FSBlock_Logical :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100523 Extent_Logical (Cache (Cache_Start .. Cache_End), 1);
Nico Huberfffc8c12023-12-13 12:44:12 +0100524 begin
525 Success := Success and then
526 Hdr_Magic = Extent_Header_Magic and then
527 Hdr_Depth = Depth and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100528 Hdr_Entries in Extent_Idx and then
Nico Huberfffc8c12023-12-13 12:44:12 +0100529 Hdr_Entries <= Dynamic_Max_Index and then
530 First_Logical = Logical_Off;
531 if not Success then
532 Next := 1;
533 else
Nico Huber1db91932024-01-09 16:58:26 +0100534 pragma Assert (Cache_End - Cache_Start + 1 = State.Static.Block_Size);
Nico Huber52f4c8d2024-01-09 13:57:33 +0100535 Next := Bin_Search (Cache (Cache_Start .. Cache_End), Hdr_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100536 end if;
537 end;
538 end Next_Ref;
539
Nico Huber7403a542023-12-15 23:13:47 +0100540 Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline;
541 Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents);
542 Inode_Entries : constant Natural := Header_Entries (Inline_Extents);
543 First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1);
544 Depth : Natural := Header_Depth (Inline_Extents);
Nico Huberfffc8c12023-12-13 12:44:12 +0100545
546 Cache_Start, Cache_End : Max_Block_Index;
547 Logical_Off, Length : FSBlock_Logical;
548 Idx : Extent_Idx;
549 begin
550 Success :=
551 Inode_Magic = Extent_Header_Magic and then
552 Inode_Entries > 0 and then
Nico Huber7403a542023-12-15 23:13:47 +0100553 Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100554 First_Logical <= Logical and then
555 Depth in Extent_Depth;
Nico Huberfffc8c12023-12-13 12:44:12 +0100556 if not Success then
557 Physical := 0;
558 return;
559 end if;
560
Nico Huber7403a542023-12-15 23:13:47 +0100561 Idx := Bin_Search (Inline_Extents, Inode_Entries);
Nico Huberfffc8c12023-12-13 12:44:12 +0100562 if Depth = 0 then
Nico Huber7403a542023-12-15 23:13:47 +0100563 Physical := Extent_Physical (Inline_Extents, Idx);
564 Logical_Off := Extent_Logical (Inline_Extents, Idx);
565 Length := Extent_Length (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100566 else
Nico Huber7403a542023-12-15 23:13:47 +0100567 Physical := Index_Physical (Inline_Extents, Idx);
568 Logical_Off := Index_Logical (Inline_Extents, Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100569 loop
Nico Huber96a0b0e2024-01-08 15:57:09 +0100570 pragma Loop_Invariant
Nico Huber1db91932024-01-09 16:58:26 +0100571 (State.Static = State.Static'Loop_Entry and then
572 Depth > 0 and then
Nico Huber96a0b0e2024-01-08 15:57:09 +0100573 Depth in Extent_Depth and then
574 Logical_Off <= Logical);
Nico Huberfffc8c12023-12-13 12:44:12 +0100575 Depth := Depth - 1;
576 Next_Ref
577 (Current => Physical,
578 Logical_Off => Logical_Off,
579 Depth => Depth,
580 Next => Idx,
581 Cache_Start => Cache_Start,
582 Cache_End => Cache_End,
583 Success => Success);
584 if not Success then
585 return;
586 end if;
587
588 exit when Depth = 0;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100589 Physical := Index_Physical (Cache (Cache_Start .. Cache_End), Idx);
590 Logical_Off := Index_Logical (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100591 end loop;
592
Nico Huber52f4c8d2024-01-09 13:57:33 +0100593 Physical := Extent_Physical (Cache (Cache_Start .. Cache_End), Idx);
594 Logical_Off := Extent_Logical (Cache (Cache_Start .. Cache_End), Idx);
595 Length := Extent_Length (Cache (Cache_Start .. Cache_End), Idx);
Nico Huberfffc8c12023-12-13 12:44:12 +0100596 end if;
597
598 Success :=
599 Length > 0 and then
Nico Huberc4c7a5e2023-12-14 00:08:56 +0100600 Logical_Off <= FSBlock_Logical'Last - Length and then
Nico Huber1db91932024-01-09 16:58:26 +0100601 Logical < Logical_Off + Length and then
602 FSBlock_Offset (Logical - Logical_Off) <= FSBlock_Offset'Last - Physical;
Nico Huberfffc8c12023-12-13 12:44:12 +0100603 if Success then
604 Physical := Physical + FSBlock_Offset (Logical - Logical_Off);
605 end if;
606 end Extent_Block_Map;
607
Nico Huber57d3a852023-12-04 15:42:40 +0100608 procedure Open
Nico Hubercdc03512023-12-13 23:32:54 +0100609 (State : in out T;
610 Inode : in Inode_Index;
611 Success : out Boolean)
612 with
613 Pre => Is_Mounted (State) and not Is_Open (State),
614 Post => Success = Is_Open (State)
615 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100616 Static : Mount_State renames State.Static;
617 Cache : Cache_Buffer renames State.Cache.Buffer;
618
Nico Huber022e2262023-12-15 23:15:17 +0100619 ------------------------
620 -- Group deserialization
621
Nico Hubercdc03512023-12-13 23:32:54 +0100622 subtype Group_Off is Natural range 0 .. Desc_Size'Last;
623 function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural
624 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100625 (Natural (Idx) * Static.Desc_Size + Off);
Nico Hubercdc03512023-12-13 23:32:54 +0100626
627 function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset
628 is
629 (FSBlock_Offset (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100630 (if Static.Feature_64Bit
Nico Hubercdc03512023-12-13 23:32:54 +0100631 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32)
632 else 0)
633 or
634 Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8)))))
635 with
636 Pre => Buf'Length >= Group_Byte_Offset (Idx, 44);
637
Nico Huber022e2262023-12-15 23:15:17 +0100638 ------------------------
639 -- Inode deserialization
640
Nico Hubercdc03512023-12-13 23:32:54 +0100641 subtype Inode_Off is Natural range 0 .. Inode_Size'Last;
642 function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural
643 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100644 (Natural (Idx) * Static.Inode_Size + Off);
Nico Hubercdc03512023-12-13 23:32:54 +0100645
Nico Huber022e2262023-12-15 23:15:17 +0100646 function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16
647 is
648 (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0)))
649 with
650 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2);
651
652 function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64
653 is
654 ((if Mode = Regular
655 then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32)
656 else 0) or
657 Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4))))
658 with
659 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112);
660
661 function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
662 is
663 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28)))
664 with
665 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32);
666
Nico Hubercdc03512023-12-13 23:32:54 +0100667 function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
668 is
669 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32)))
670 with
671 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36);
672
Nico Huber022e2262023-12-15 23:15:17 +0100673 function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32
674 is
675 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104)))
676 with
677 Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108);
678
Nico Huber52f4c8d2024-01-09 13:57:33 +0100679 Group : constant Group_Index := Group_Index ((Inode - 1) / Static.Inodes_Per_Group);
Nico Hubercdc03512023-12-13 23:32:54 +0100680 Desc_Block : constant FSBlock_Offset :=
Nico Huber39f086c2024-01-09 17:43:06 +0100681 1 + FSBlock_Offset (Static.First_Data_Block) + FSBlock_Offset (Group / Static.Desc_Per_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100682 Cache_Start, Cache_End : Max_Block_Index;
683 begin
684 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100685 (Static => Static,
686 Cache => State.Cache,
Nico Hubercdc03512023-12-13 23:32:54 +0100687 Phys => Desc_Block,
Nico Huberecafb8f2024-01-09 15:45:49 +0100688 Level => Natural'Min (Natural (Group / Static.Desc_Per_Block), Block_Cache_Index'Last),
Nico Hubercdc03512023-12-13 23:32:54 +0100689 Cache_Start => Cache_Start,
690 Cache_End => Cache_End,
691 Success => Success);
692 if not Success then
693 return;
694 end if;
695
696 declare
Nico Huberecafb8f2024-01-09 15:45:49 +0100697 Desc : constant Desc_In_Block_Index := Desc_In_Block_Index (Group mod Static.Desc_Per_Block);
698 Inode_In_Group : constant Inode_In_Group_Index :=
699 Inode_In_Group_Index ((Inode - 1) mod Static.Inodes_Per_Group);
Nico Hubercdc03512023-12-13 23:32:54 +0100700 Inode_Block : constant FSBlock_Offset :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100701 Group_Inode_Table (Cache (Cache_Start .. Cache_End), Group) +
Nico Huberecafb8f2024-01-09 15:45:49 +0100702 FSBlock_Offset (Inode_In_Group / Static.Inodes_Per_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100703 begin
704 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100705 (Static => Static,
706 Cache => State.Cache,
Nico Hubercdc03512023-12-13 23:32:54 +0100707 Phys => Inode_Block,
708 Level => Block_Cache_Index'Last,
709 Cache_Start => Cache_Start,
710 Cache_End => Cache_End,
711 Success => Success);
712 if not Success then
713 return;
714 end if;
715
716 declare
Nico Huber022e2262023-12-15 23:15:17 +0100717 S_IFMT : constant := 8#170000#;
718 S_IFDIR : constant := 8#040000#;
719 S_IFREG : constant := 8#100000#;
720 S_IFLNK : constant := 8#120000#;
721
Nico Huberecafb8f2024-01-09 15:45:49 +0100722 Inode_In_Block : constant Inode_Index := Inode_Index (Inode_In_Group mod Static.Inodes_Per_Block);
Nico Huber022e2262023-12-15 23:15:17 +0100723 I_Mode : constant Unsigned_16 :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100724 Inode_Mode (Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100725 I_Flags : constant Unsigned_32 :=
Nico Huber52f4c8d2024-01-09 13:57:33 +0100726 Inode_Flags (Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Hubercdc03512023-12-13 23:32:54 +0100727 begin
Nico Huber022e2262023-12-15 23:15:17 +0100728 case I_Mode and S_IFMT is
729 when S_IFDIR => State.Inode.Mode := Dir;
730 when S_IFREG => State.Inode.Mode := Regular;
731 when S_IFLNK => State.Inode.Mode := Link;
732 when others => Success := False; return;
733 end case;
734 if State.Inode.Mode = Link then
735 declare
736 I_File_ACL : constant Unsigned_32 := Inode_File_ACL (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100737 Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Huber022e2262023-12-15 23:15:17 +0100738 I_Blocks : constant Unsigned_32 := Inode_Blocks (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100739 Cache (Cache_Start .. Cache_End), Inode_In_Block);
Nico Huber022e2262023-12-15 23:15:17 +0100740 begin
741 if (I_File_ACL = 0 and I_Blocks = 0) or
Nico Huber52f4c8d2024-01-09 13:57:33 +0100742 (I_File_ACL /= 0 and I_Blocks = 2 ** (Static.Block_Size_Bits - 9))
Nico Huber022e2262023-12-15 23:15:17 +0100743 then
744 State.Inode.Mode := Fast_Link;
745 end if;
746 end;
747 end if;
748 declare
749 I_Size : constant Unsigned_64 := Inode_Size (
Nico Huber52f4c8d2024-01-09 13:57:33 +0100750 Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode);
Nico Huber022e2262023-12-15 23:15:17 +0100751 begin
752 if I_Size <= Unsigned_64 (Inode_Length'Last) then
753 State.Inode.Size := Inode_Length (I_Size);
754 else
755 Success := False;
Nico Huberecafb8f2024-01-09 15:45:49 +0100756 return;
Nico Huber022e2262023-12-15 23:15:17 +0100757 end if;
758 end;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100759 State.Inode.Use_Extents := Static.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0;
760 State.Inode.Inline := Cache (Cache_Start + 40 .. Cache_Start + 100 - 1);
Nico Huber21d42022023-12-16 02:50:22 +0100761 State.Inode.I := Inode;
Nico Huber52f4c8d2024-01-09 13:57:33 +0100762 Reset_Cache_Logical (State.Cache);
Nico Hubercdc03512023-12-13 23:32:54 +0100763 State.S := File_Opened;
764 end;
765 end;
766 end Open;
767
768 procedure Open
Nico Huber57d3a852023-12-04 15:42:40 +0100769 (State : in out T;
770 File_Len : out File_Length;
Nico Huberb1cb2d32023-12-17 01:45:47 +0100771 File_Type : out FS.File_Type;
772 File_Name : in String;
773 In_Root : in Boolean;
Nico Huber57d3a852023-12-04 15:42:40 +0100774 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100775 is
Nico Huber21d42022023-12-16 02:50:22 +0100776 File_Name_Max : constant := 255;
777 Root_Inode : constant := 2;
778
779 function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is
780 begin
781 for I in Str'Range loop
782 if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then
783 return False;
784 end if;
785 end loop;
786 return True;
787 end Str_Buf_Equal;
788
789 File_Inode : Inode_Index;
790 File_Pos : File_Length;
Nico Huber1d7727f2023-11-30 15:58:46 +0100791 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100792 File_Len := 0;
Nico Huber21d42022023-12-16 02:50:22 +0100793 File_Type := FS.File_Type'First;
794
795 if File_Name'Length > File_Name_Max then
796 Success := False;
797 return;
798 end if;
799
800 -- Ensure dir is opened:
801 --
802 if State.S = File_Opened then
803 if State.Cur_Dir /= State.Inode.I then
804 Success := False;
805 return;
806 end if;
807 else
808 if In_Root then
809 State.Cur_Dir := Root_Inode;
810 end if;
811 Open (State, State.Cur_Dir, Success);
812 if not Success then
813 return;
814 end if;
815 end if;
816
817 -- Lookup file in opened dir:
818 --
819 File_Pos := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100820 Success := False;
Nico Huber21d42022023-12-16 02:50:22 +0100821 while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop
822 declare
823 Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1;
824 subtype Dir_Entry_Index is Natural
825 range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1;
826 Dir_Entry : Buffer_Type (Dir_Entry_Index);
827 Entry_File_Pos : File_Offset := File_Pos;
828 Len : Natural;
829 begin
830 Read
831 (State => State,
832 File_Pos => Entry_File_Pos,
833 Buf => Dir_Entry (0 .. 7),
834 Len => Len);
835 if Len < Dir_Entry_Header_Length then
836 return;
837 end if;
838
839 -- Only check filenames of exact same length
840 if Read_LE32 (Dir_Entry, 0) > Root_Inode and then
841 File_Name'Length = Natural (Dir_Entry (6))
842 then
843 Read
844 (State => State,
845 File_Pos => Entry_File_Pos,
846 Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1),
847 Len => Len);
848 if Len < File_Name'Length then
849 return;
850 end if;
851
852 File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0));
853 Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1));
854 exit when Success;
855 end if;
856
857 File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4));
858 end;
859 end loop;
860
861 if Success then
862 Open (State, File_Inode, Success);
863 if not Success then
864 return;
865 end if;
866
867 if State.Inode.Mode = Dir then
868 State.Cur_Dir := File_Inode;
869 end if;
870 end if;
871
872 Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last);
873 if Success then
874 File_Len := File_Length (State.Inode.Size);
875 File_Type := (case State.Inode.Mode is
876 when Dir => FS.Dir,
877 when Regular => FS.Regular,
878 when Link .. Fast_Link => FS.Link);
879 else
880 Close (State);
881 end if;
Nico Huber1d7727f2023-11-30 15:58:46 +0100882 end Open;
883
Nico Huber57d3a852023-12-04 15:42:40 +0100884 procedure Close (State : in out T) is
885 begin
886 State.S := Mounted;
887 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100888
Nico Huber57d3a852023-12-04 15:42:40 +0100889 procedure Read
890 (State : in out T;
Nico Huber57d3a852023-12-04 15:42:40 +0100891 File_Pos : in out File_Offset;
892 Buf : out Buffer_Type;
893 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100894 is
Nico Huber52f4c8d2024-01-09 13:57:33 +0100895 Static : Mount_State renames State.Static;
896 Cache : Cache_Buffer renames State.Cache.Buffer;
897
Nico Huberd3644be2023-12-16 01:43:00 +0100898 Pos : Natural;
Nico Huber1d7727f2023-11-30 15:58:46 +0100899 begin
Nico Huberd3644be2023-12-16 01:43:00 +0100900 if State.Inode.Mode = Fast_Link then
Nico Huber549a1b82023-12-17 01:51:59 +0100901 if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or
902 Inode_Length (File_Pos) >= State.Inode.Size then
Nico Huberd3644be2023-12-16 01:43:00 +0100903 Len := 0;
904 else
Nico Huber549a1b82023-12-17 01:51:59 +0100905 Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos));
Nico Huberd3644be2023-12-16 01:43:00 +0100906 end if;
907 Buf (Buf'First .. Buf'First + Len - 1) :=
908 State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1);
909 Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#);
910 File_Pos := File_Pos + File_Length (Len);
911 return;
912 end if;
913
Nico Huber1d7727f2023-11-30 15:58:46 +0100914 Len := 0;
Nico Huberd3644be2023-12-16 01:43:00 +0100915 Pos := Buf'First;
916 while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop
917 declare
Nico Huber52f4c8d2024-01-09 13:57:33 +0100918 In_Block : constant Max_Block_Index := Natural (File_Pos) mod Static.Block_Size;
919 Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / File_Offset (Static.Block_Size));
920 In_Block_Space : constant Natural := Natural (Static.Block_Size) - In_Block;
Nico Huber549a1b82023-12-17 01:51:59 +0100921 In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos);
Nico Huberd3644be2023-12-16 01:43:00 +0100922 In_Buf_Space : constant Natural := Buf'Last - Pos + 1;
923 Len_Here : Natural;
924 begin
925 Len_Here := In_Block_Space;
Nico Huber549a1b82023-12-17 01:51:59 +0100926 if In_File_Space < Inode_Length (Len_Here) then
927 Len_Here := Natural (In_File_Space);
Nico Huberd3644be2023-12-16 01:43:00 +0100928 end if;
929 if In_Buf_Space < Len_Here then
930 Len_Here := In_Buf_Space;
931 end if;
932
933 declare
934 Last : constant Index_Type := Pos + Len_Here - 1;
935 Cache_Start, Cache_End : Max_Block_Index;
936 Physical : FSBlock_Offset;
937 Success : Boolean;
938 begin
939 if State.Inode.Use_Extents then
940 Extent_Block_Map (State, Logical, Physical, Success);
941 else
942 Ext2_Block_Map (State, Logical, Physical, Success);
943 end if;
944 if Success then
945 Cache_FSBlock
Nico Huber52f4c8d2024-01-09 13:57:33 +0100946 (Static => Static,
947 Cache => State.Cache,
Nico Huberd3644be2023-12-16 01:43:00 +0100948 Phys => Physical,
949 Level => Block_Cache_Index'Last,
950 Cache_Start => Cache_Start,
951 Cache_End => Cache_End,
952 Success => Success);
953 end if;
954 exit when not Success;
955
Nico Huber52f4c8d2024-01-09 13:57:33 +0100956 Buf (Pos .. Last) := Cache (
Nico Huberd3644be2023-12-16 01:43:00 +0100957 Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1);
958 File_Pos := File_Pos + File_Length (Len_Here);
959 Pos := Pos + Len_Here;
960 Len := Len + Len_Here;
961 end;
962 end;
963 end loop;
964 Buf (Pos .. Buf'Last) := (others => 16#00#);
Nico Huber1d7727f2023-11-30 15:58:46 +0100965 end Read;
966
Nico Huber26f71832023-12-05 16:26:56 +0100967 --------------------------------------------------------------------------
968
969 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100970
971 function C_Mount return int
972 with
973 Export,
974 Convention => C,
975 External_Name => "ext2fs_mount";
976 function C_Mount return int
977 with
978 SPARK_Mode => Off
979 is
980 begin
981 return C.C_Mount;
982 end C_Mount;
983
Nico Huber3da21472023-12-18 15:43:35 +0100984 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100985 with
986 Export,
987 Convention => C,
988 External_Name => "ext2fs_dir";
Nico Huber3da21472023-12-18 15:43:35 +0100989 function C_Open (File_Path : System.Address) return int
Nico Huber8ec45a12023-12-04 17:11:08 +0100990 with
991 SPARK_Mode => Off
992 is
993 begin
994 return C.C_Open (File_Path);
995 end C_Open;
996
997 function C_Read (Buf : System.Address; Len : int) return int
998 with
999 Export,
1000 Convention => C,
1001 External_Name => "ext2fs_read";
1002 function C_Read (Buf : System.Address; Len : int) return int
1003 with
1004 SPARK_Mode => Off
1005 is
1006 begin
1007 return C.C_Read (Buf, Len);
1008 end C_Read;
1009
Thomas Heijligen5c43abc2023-12-11 15:24:36 +00001010end FILO.FS.Ext2;