blob: aeff958d0b478aa08bce00c85b09794d55be3b2b [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 Huber8ec45a12023-12-04 17:11:08 +010010with System;
Nico Huber1d7727f2023-11-30 15:58:46 +010011with Interfaces;
Nico Huber8ec45a12023-12-04 17:11:08 +010012with Interfaces.C;
13with Interfaces.C.Strings;
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;
28 SUPERBLOCK_BLOCKS : constant := SUPERBLOCK_SIZE / BLOCK_SIZE;
29
30 SUPERBLOCK_MAGIC : constant := 16#ef53#;
31 OLD_REV : constant := 0;
32 DYNAMIC_REV : constant := 1;
33 FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#;
34 FEATURE_INCOMPAT_64BIT : constant := 16#0080#;
35
Nico Huber57d3a852023-12-04 15:42:40 +010036 procedure Mount
37 (State : in out T;
38 Part_Len : in Partition_Length;
39 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +010040 is
Nico Huber26f71832023-12-05 16:26:56 +010041 Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +010042 begin
Nico Huber26f71832023-12-05 16:26:56 +010043 if Part_Len < 2 * SUPERBLOCK_SIZE then
44 Success := False;
45 return;
46 end if;
47
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000048 Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success);
Nico Huber26f71832023-12-05 16:26:56 +010049 if not Success then
50 return;
51 end if;
52
53 if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then
54 Success := False;
55 return;
56 end if;
57
58 State.Part_Len := Part_Len;
59 State.First_Data_Block := Block_Offset (Read_LE32 (Super_Block, 5 * 4));
60
61 declare
62 S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4);
63 begin
64 if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then
65 State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10);
66 else
67 Success := False;
68 return;
69 end if;
70 end;
71
72 declare
73 S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4);
74 begin
75 if S_Inodes_Per_Group in 1 .. Unsigned_32 (Positive'Last) then
76 State.Inodes_Per_Group := Positive (S_Inodes_Per_Group);
77 else
78 Success := False;
79 return;
80 end if;
81 end;
82
83 declare
84 S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4);
85 begin
86 if S_Rev_Level >= DYNAMIC_REV then
87 declare
88 S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4);
89 begin
90 if S_Inode_Size in
91 Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last)
92 then
93 State.Inode_Size := Inode_Size (S_Inode_Size);
94 else
95 Success := False;
96 return;
97 end if;
98 end;
99 else
100 State.Inode_Size := Inode_Size'First;
101 end if;
102 end;
103
104 declare
105 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
106 begin
107 State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
108 State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
109 if State.Feature_64Bit then
110 declare
111 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
112 begin
113 if S_Desc_Size in
114 Unsigned_16 (Desc_Size'First) .. Unsigned_16 (Desc_Size'Last)
115 then
116 State.Desc_Size := Desc_Size (S_Desc_Size);
117 else
118 Success := False;
119 return;
120 end if;
121 end;
122 else
123 State.Desc_Size := Desc_Size'First;
124 end if;
125 end;
126
127 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100128 end Mount;
129
Nico Huberf5d99d02023-12-12 13:42:55 +0100130 procedure Read_FSBlock
131 (State : in T;
132 Buf : out Buffer_Type;
133 FSBlock : in FSBlock_Offset;
134 Success : out Boolean)
135 with
136 Pre =>
137 Is_Mounted (State) and
138 Buf'Length = 2 ** State.Block_Size_Bits
139 is
140 Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits;
141 Max_Block_Offset : constant FSBlock_Offset :=
142 FSBlock_Offset (State.Part_Len / Block_Size - 1);
143 begin
144 if FSBlock > Max_Block_Offset then
145 Success := False;
146 return;
147 end if;
148 Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success);
149 end Read_FSBlock;
150
Nico Huber6623c982023-12-12 16:35:46 +0100151 procedure Ext2_Block_Map
152 (State : in out T;
153 Logical : in FSBlock_Logical;
154 Physical : out FSBlock_Offset;
155 Success : out Boolean)
156 is
157 Block_Size : constant Natural := 2 ** State.Block_Size_Bits;
158 Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4);
159 Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4);
160 type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1;
161
162 procedure Indirect_Block_Lookup
163 (Indirect_Block_Phys : in FSBlock_Offset;
164 Addr_In_Block : in Addr_In_Block_Range;
165 Level : in Block_Cache_Index;
166 Logical_Off : in FSBlock_Logical;
167 Next_Physical : out FSBlock_Offset;
168 Success : out Boolean)
169 with
170 Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block
171 is
172 -- Limit cache usage in case of incredibly huge block size:
173 Cache_Level : constant Block_Cache_Index :=
174 (if State.Block_Size_Bits > 15 then Any
175 elsif State.Block_Size_Bits > 14 and Level > Any then Last_Level
176 else Level);
177 Cache_Index : constant Index_Type :=
178 Block_Cache_Index'Pos (Cache_Level) * Block_Size;
179 Cache_End : constant Index_Type := Cache_Index + Block_Size - 1;
180 begin
181 if State.Block_Cache_Index (Cache_Level) /= Logical_Off then
182 Read_FSBlock
183 (State => State,
184 Buf => State.Block_Cache (Cache_Index .. Cache_End),
185 FSBlock => Indirect_Block_Phys,
186 Success => Success);
187 State.Block_Cache_Index (Cache_Level) := Logical_Off;
188 if not Success then
189 return;
190 end if;
191 end if;
192
193 Next_Physical := FSBlock_Offset (Read_LE32
194 (Buf => State.Block_Cache (Cache_Index .. Cache_End),
195 Off => Natural (Addr_In_Block) * 4));
196 Success := True;
197 end Indirect_Block_Lookup;
198
199 Logical_Rest : FSBlock_Logical := Logical;
200 begin
201 if Logical_Rest < Direct_Blocks then
202 Physical := FSBlock_Offset (State.Direct_Blocks (Natural (Logical)));
203 Success := True;
204 return;
205 end if;
206
207 Logical_Rest := Logical_Rest - Direct_Blocks;
208 if Logical_Rest < Addr_Per_Block then
209 Indirect_Block_Lookup
210 (Indirect_Block_Phys => FSBlock_Offset (State.Indirect_Block),
211 Addr_In_Block => Addr_In_Block_Range (Logical_Rest),
212 Level => Last_Level,
213 Logical_Off => Logical - Logical_Rest,
214 Next_Physical => Physical,
215 Success => Success);
216 return;
217 end if;
218
219 Logical_Rest := Logical_Rest - Addr_Per_Block;
220 if Logical_Rest < Addr_Per_Block ** 2 then
221 Indirect_Block_Lookup
222 (Indirect_Block_Phys => FSBlock_Offset (State.Double_Indirect),
223 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block),
224 Level => Second_Last_Level,
225 Logical_Off => Logical - Logical_Rest,
226 Next_Physical => Physical,
227 Success => Success);
228 if not Success then
229 return;
230 end if;
231
232 Indirect_Block_Lookup
233 (Indirect_Block_Phys => Physical,
234 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
235 Level => Last_Level,
236 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
237 Next_Physical => Physical,
238 Success => Success);
239 return;
240 end if;
241
242 Logical_Rest := Logical_Rest - Addr_Per_Block ** 2;
243 if Logical_Rest < Addr_Per_Block ** 3 then
244 Indirect_Block_Lookup
245 (Indirect_Block_Phys => FSBlock_Offset (State.Triple_Indirect),
246 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block ** 2),
247 Level => First_Level,
248 Logical_Off => Logical - Logical_Rest,
249 Next_Physical => Physical,
250 Success => Success);
251 if not Success then
252 return;
253 end if;
254
255 Indirect_Block_Lookup
256 (Indirect_Block_Phys => Physical,
257 Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block mod Addr_Per_Block),
258 Level => Second_Last_Level,
259 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2),
260 Next_Physical => Physical,
261 Success => Success);
262 if not Success then
263 return;
264 end if;
265
266 Indirect_Block_Lookup
267 (Indirect_Block_Phys => Physical,
268 Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block),
269 Level => Last_Level,
270 Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block),
271 Next_Physical => Physical,
272 Success => Success);
273 return;
274 end if;
275
276 -- Logical address was just too high.
277 Success := False;
278 end Ext2_Block_Map;
279
Nico Huber57d3a852023-12-04 15:42:40 +0100280 procedure Open
281 (State : in out T;
282 File_Len : out File_Length;
283 File_Path : in String;
284 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100285 is
286 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100287 File_Len := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100288 Success := False;
289 end Open;
290
Nico Huber57d3a852023-12-04 15:42:40 +0100291 procedure Close (State : in out T) is
292 begin
293 State.S := Mounted;
294 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100295
Nico Huber57d3a852023-12-04 15:42:40 +0100296 procedure Read
297 (State : in out T;
298 File_Len : in File_Length;
299 File_Pos : in out File_Offset;
300 Buf : out Buffer_Type;
301 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100302 is
303 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100304 Buf := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +0100305 Len := 0;
306 end Read;
307
Nico Huber26f71832023-12-05 16:26:56 +0100308 --------------------------------------------------------------------------
309
310 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100311
312 function C_Mount return int
313 with
314 Export,
315 Convention => C,
316 External_Name => "ext2fs_mount";
317 function C_Mount return int
318 with
319 SPARK_Mode => Off
320 is
321 begin
322 return C.C_Mount;
323 end C_Mount;
324
325 function C_Open (File_Path : Strings.chars_ptr) return int
326 with
327 Export,
328 Convention => C,
329 External_Name => "ext2fs_dir";
330 function C_Open (File_Path : Strings.chars_ptr) return int
331 with
332 SPARK_Mode => Off
333 is
334 begin
335 return C.C_Open (File_Path);
336 end C_Open;
337
338 function C_Read (Buf : System.Address; Len : int) return int
339 with
340 Export,
341 Convention => C,
342 External_Name => "ext2fs_read";
343 function C_Read (Buf : System.Address; Len : int) return int
344 with
345 SPARK_Mode => Off
346 is
347 begin
348 return C.C_Read (Buf, Len);
349 end C_Read;
350
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000351end FILO.FS.Ext2;