Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 1 | -- 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 Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 10 | with Ada.Unchecked_Conversion; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 11 | with System; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 12 | with Interfaces; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 13 | with Interfaces.C; |
| 14 | with Interfaces.C.Strings; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 15 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 16 | with FILO.Blockdev; |
| 17 | with FILO.FS.VFS; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 18 | |
| 19 | use Interfaces.C; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 20 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 21 | package body FILO.FS.Ext2 is |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 22 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 23 | function Is_Mounted (State : T) return Boolean is (State.S >= Mounted); |
| 24 | function Is_Open (State : T) return Boolean is (State.S = File_Opened); |
| 25 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 26 | -------------------------------------------------------------------------- |
| 27 | |
| 28 | SUPERBLOCK_SIZE : constant := 1024; |
| 29 | SUPERBLOCK_BLOCKS : constant := SUPERBLOCK_SIZE / BLOCK_SIZE; |
| 30 | |
| 31 | SUPERBLOCK_MAGIC : constant := 16#ef53#; |
| 32 | OLD_REV : constant := 0; |
| 33 | DYNAMIC_REV : constant := 1; |
| 34 | FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#; |
| 35 | FEATURE_INCOMPAT_64BIT : constant := 16#0080#; |
| 36 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 37 | procedure Mount |
| 38 | (State : in out T; |
| 39 | Part_Len : in Partition_Length; |
| 40 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 41 | is |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 42 | Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 43 | begin |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 44 | if Part_Len < 2 * SUPERBLOCK_SIZE then |
| 45 | Success := False; |
| 46 | return; |
| 47 | end if; |
| 48 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 49 | Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 50 | if not Success then |
| 51 | return; |
| 52 | end if; |
| 53 | |
| 54 | if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then |
| 55 | Success := False; |
| 56 | return; |
| 57 | end if; |
| 58 | |
| 59 | State.Part_Len := Part_Len; |
Nico Huber | cd1d5f7 | 2023-12-13 16:01:43 +0100 | [diff] [blame] | 60 | State.First_Data_Block := FSBlock_Offset (Read_LE32 (Super_Block, 5 * 4)); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 61 | |
| 62 | declare |
| 63 | S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4); |
| 64 | begin |
| 65 | if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then |
| 66 | State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10); |
| 67 | else |
| 68 | Success := False; |
| 69 | return; |
| 70 | end if; |
| 71 | end; |
| 72 | |
| 73 | declare |
| 74 | S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4); |
| 75 | begin |
| 76 | if S_Inodes_Per_Group in 1 .. Unsigned_32 (Positive'Last) then |
| 77 | State.Inodes_Per_Group := Positive (S_Inodes_Per_Group); |
| 78 | else |
| 79 | Success := False; |
| 80 | return; |
| 81 | end if; |
| 82 | end; |
| 83 | |
| 84 | declare |
| 85 | S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4); |
| 86 | begin |
| 87 | if S_Rev_Level >= DYNAMIC_REV then |
| 88 | declare |
| 89 | S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4); |
| 90 | begin |
| 91 | if S_Inode_Size in |
| 92 | Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last) |
| 93 | then |
| 94 | State.Inode_Size := Inode_Size (S_Inode_Size); |
| 95 | else |
| 96 | Success := False; |
| 97 | return; |
| 98 | end if; |
| 99 | end; |
| 100 | else |
| 101 | State.Inode_Size := Inode_Size'First; |
| 102 | end if; |
| 103 | end; |
| 104 | |
| 105 | declare |
| 106 | S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4); |
| 107 | begin |
| 108 | State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0; |
| 109 | State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0; |
| 110 | if State.Feature_64Bit then |
| 111 | declare |
| 112 | S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2); |
| 113 | begin |
Nico Huber | cd1d5f7 | 2023-12-13 16:01:43 +0100 | [diff] [blame] | 114 | if Is_Power_Of_2 (S_Desc_Size) and then |
| 115 | S_Desc_Size in Unsigned_16 (Desc_Size'First) .. |
| 116 | Unsigned_16 (Positive'Min (Desc_Size'Last, 2 ** State.Block_Size_Bits)) |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 117 | then |
| 118 | State.Desc_Size := Desc_Size (S_Desc_Size); |
| 119 | else |
| 120 | Success := False; |
| 121 | return; |
| 122 | end if; |
| 123 | end; |
Nico Huber | 77a04d7 | 2023-12-13 23:11:40 +0100 | [diff] [blame] | 124 | State.Feature_64Bit := State.Feature_64Bit and State.Desc_Size >= 64; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 125 | else |
| 126 | State.Desc_Size := Desc_Size'First; |
| 127 | end if; |
| 128 | end; |
| 129 | |
| 130 | State.S := Mounted; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 131 | end Mount; |
| 132 | |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 133 | procedure Read_FSBlock |
| 134 | (State : in T; |
| 135 | Buf : out Buffer_Type; |
| 136 | FSBlock : in FSBlock_Offset; |
| 137 | Success : out Boolean) |
| 138 | with |
| 139 | Pre => |
| 140 | Is_Mounted (State) and |
| 141 | Buf'Length = 2 ** State.Block_Size_Bits |
| 142 | is |
| 143 | Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits; |
| 144 | Max_Block_Offset : constant FSBlock_Offset := |
| 145 | FSBlock_Offset (State.Part_Len / Block_Size - 1); |
| 146 | begin |
| 147 | if FSBlock > Max_Block_Offset then |
| 148 | Success := False; |
| 149 | return; |
| 150 | end if; |
| 151 | Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success); |
| 152 | end Read_FSBlock; |
| 153 | |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 154 | procedure Cache_FSBlock |
| 155 | (State : in out T; |
| 156 | Phys : in FSBlock_Offset; |
| 157 | Level : in Block_Cache_Index; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 158 | Label : in Cache_Label; |
| 159 | Logical : in Boolean := True; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 160 | Cache_Start : out Max_Block_Index; |
| 161 | Cache_End : out Max_Block_Index; |
| 162 | Success : out Boolean) |
| 163 | with |
| 164 | Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits |
| 165 | is |
| 166 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 167 | -- Limit cache usage depending on block size: |
| 168 | Max_Level : constant Block_Cache_Index := |
| 169 | 2 ** (Log_Block_Size'Last - State.Block_Size_Bits) - 1; |
| 170 | Cache_Level : constant Block_Cache_Index := |
| 171 | Block_Cache_Index'Min (Level, Max_Level); |
| 172 | begin |
| 173 | Cache_Start := Cache_Level * Block_Size; |
| 174 | Cache_End := Cache_Start + Block_Size - 1; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 175 | if State.Block_Cache_Logical (Cache_Level) = Logical and |
| 176 | State.Block_Cache_Label (Cache_Level) = Label |
| 177 | then |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 178 | Success := True; |
| 179 | else |
| 180 | Read_FSBlock |
| 181 | (State => State, |
| 182 | Buf => State.Block_Cache (Cache_Start .. Cache_End), |
| 183 | FSBlock => Phys, |
| 184 | Success => Success); |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 185 | State.Block_Cache_Logical (Cache_Level) := Logical; |
| 186 | State.Block_Cache_Label (Cache_Level) := Label; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 187 | end if; |
| 188 | end Cache_FSBlock; |
| 189 | |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 190 | procedure Cache_FSBlock |
| 191 | (State : in out T; |
| 192 | Phys : in FSBlock_Offset; |
| 193 | Level : in Block_Cache_Index; |
| 194 | Cache_Start : out Max_Block_Index; |
| 195 | Cache_End : out Max_Block_Index; |
| 196 | Success : out Boolean) |
| 197 | with |
| 198 | Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits |
| 199 | is |
| 200 | begin |
| 201 | Cache_FSBlock (State, Phys, Level, Cache_Label (Phys), |
| 202 | False, Cache_Start, Cache_End, Success); |
| 203 | end Cache_FSBlock; |
| 204 | |
| 205 | procedure Reset_Cache_Logical (State : in out T) is |
| 206 | begin |
| 207 | for I in Block_Cache_Index loop |
| 208 | if State.Block_Cache_Logical (I) then |
| 209 | State.Block_Cache_Logical (I) := False; |
| 210 | State.Block_Cache_Label (I) := 0; |
| 211 | end if; |
| 212 | end loop; |
| 213 | end Reset_Cache_Logical; |
| 214 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 215 | procedure Ext2_Block_Map |
| 216 | (State : in out T; |
| 217 | Logical : in FSBlock_Logical; |
| 218 | Physical : out FSBlock_Offset; |
| 219 | Success : out Boolean) |
| 220 | is |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 221 | Direct_Blocks : constant := 12; |
| 222 | type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32; |
| 223 | type Inode_Blocks is record |
| 224 | Direct_Blocks : Direct_Blocks_Array; |
| 225 | Indirect_Block : Unsigned_32; |
| 226 | Double_Indirect : Unsigned_32; |
| 227 | Triple_Indirect : Unsigned_32; |
| 228 | end record |
| 229 | with Size => Inode_Extents'Length * 8; |
| 230 | |
| 231 | function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks); |
| 232 | function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Extents)); |
| 233 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 234 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 235 | Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4); |
| 236 | Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4); |
| 237 | type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1; |
| 238 | |
| 239 | procedure Indirect_Block_Lookup |
| 240 | (Indirect_Block_Phys : in FSBlock_Offset; |
| 241 | Addr_In_Block : in Addr_In_Block_Range; |
| 242 | Level : in Block_Cache_Index; |
| 243 | Logical_Off : in FSBlock_Logical; |
| 244 | Next_Physical : out FSBlock_Offset; |
| 245 | Success : out Boolean) |
| 246 | with |
| 247 | Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block |
| 248 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 249 | Cache_Start, Cache_End : Max_Block_Index; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 250 | begin |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 251 | Cache_FSBlock |
| 252 | (State => State, |
| 253 | Phys => Indirect_Block_Phys, |
| 254 | Level => Level, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 255 | Label => Cache_Label (Logical_Off), |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 256 | Cache_Start => Cache_Start, |
| 257 | Cache_End => Cache_End, |
| 258 | Success => Success); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 259 | Next_Physical := FSBlock_Offset (Read_LE32 |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 260 | (Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 261 | Off => Natural (Addr_In_Block) * 4)); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 262 | end Indirect_Block_Lookup; |
| 263 | |
| 264 | Logical_Rest : FSBlock_Logical := Logical; |
| 265 | begin |
| 266 | if Logical_Rest < Direct_Blocks then |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 267 | Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical))); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 268 | Success := True; |
| 269 | return; |
| 270 | end if; |
| 271 | |
| 272 | Logical_Rest := Logical_Rest - Direct_Blocks; |
| 273 | if Logical_Rest < Addr_Per_Block then |
| 274 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 275 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 276 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 277 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 278 | Logical_Off => Logical - Logical_Rest, |
| 279 | Next_Physical => Physical, |
| 280 | Success => Success); |
| 281 | return; |
| 282 | end if; |
| 283 | |
| 284 | Logical_Rest := Logical_Rest - Addr_Per_Block; |
| 285 | if Logical_Rest < Addr_Per_Block ** 2 then |
| 286 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 287 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 288 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 289 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 290 | Logical_Off => Logical - Logical_Rest, |
| 291 | 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, |
| 299 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 300 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 301 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 302 | Next_Physical => Physical, |
| 303 | Success => Success); |
| 304 | return; |
| 305 | end if; |
| 306 | |
| 307 | Logical_Rest := Logical_Rest - Addr_Per_Block ** 2; |
| 308 | if Logical_Rest < Addr_Per_Block ** 3 then |
| 309 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 310 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 311 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block ** 2), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 312 | Level => 2, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 313 | Logical_Off => Logical - Logical_Rest, |
| 314 | Next_Physical => Physical, |
| 315 | Success => Success); |
| 316 | if not Success then |
| 317 | return; |
| 318 | end if; |
| 319 | |
| 320 | Indirect_Block_Lookup |
| 321 | (Indirect_Block_Phys => Physical, |
| 322 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 323 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 324 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2), |
| 325 | Next_Physical => Physical, |
| 326 | Success => Success); |
| 327 | if not Success then |
| 328 | return; |
| 329 | end if; |
| 330 | |
| 331 | Indirect_Block_Lookup |
| 332 | (Indirect_Block_Phys => Physical, |
| 333 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 334 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 335 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 336 | Next_Physical => Physical, |
| 337 | Success => Success); |
| 338 | return; |
| 339 | end if; |
| 340 | |
| 341 | -- Logical address was just too high. |
| 342 | Success := False; |
| 343 | end Ext2_Block_Map; |
| 344 | |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 345 | procedure Extent_Block_Map |
| 346 | (State : in out T; |
| 347 | Logical : in FSBlock_Logical; |
| 348 | Physical : out FSBlock_Offset; |
| 349 | Success : out Boolean) |
| 350 | is |
| 351 | -- Extent blocks always start with a 12B header and contain 12B entries. |
| 352 | -- Every entry starts with the number of the first logical block it |
| 353 | -- covers. Entries are sorted by this number. |
| 354 | -- Depth > 0 blocks have index entries, referencing further extent blocks. |
| 355 | -- Depth = 0 blocks have extent entries, referencing a contiguous range |
| 356 | -- of data blocks. |
| 357 | -- |
| 358 | -- +-----------------+ |
| 359 | -- .-> | Hdr depth=0 | |
| 360 | -- | +-----------------+ |
| 361 | -- | | Extent 0.. 12 | |
| 362 | -- +-------------+ | +-----------------+ |
| 363 | -- | Hdr depth=1 | | | Extent 13.. 13 | |
| 364 | -- +-------------+ | +-----------------+ |
| 365 | -- | Index 0 | --' | Extent 14..122 | |
| 366 | -- +-------------+ +-----------------+ |
| 367 | -- | Index 123 | --. |
| 368 | -- +-------------+ | +-----------------+ |
| 369 | -- `-> | Hdr depth=0 | |
| 370 | -- +-----------------+ |
| 371 | -- | Extent 123..125 | |
| 372 | -- +-----------------+ |
| 373 | -- | Extent 126..234 | |
| 374 | -- +-----------------+ |
| 375 | -- |
| 376 | |
| 377 | Extent_Header_Size : constant := 12; |
| 378 | Extent_Header_Magic : constant := 16#f03a#; |
| 379 | subtype Extent_Off is Natural range 0 .. Extent_Header_Size; |
| 380 | subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1; |
| 381 | |
| 382 | function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural |
| 383 | is |
| 384 | (Idx * Extent_Header_Size + Off); |
| 385 | |
| 386 | function Header_Magic (Buf : Buffer_Type) return Unsigned_16 |
| 387 | is |
| 388 | (Read_LE16 (Buf, 0)) |
| 389 | with |
| 390 | Pre => Buf'Length >= 2; |
| 391 | |
| 392 | function Header_Entries (Buf : Buffer_Type) return Natural |
| 393 | is |
| 394 | (Natural (Read_LE16 (Buf, 2))) |
| 395 | with |
| 396 | Pre => Buf'Length >= 4; |
| 397 | |
| 398 | function Header_Depth (Buf : Buffer_Type) return Natural |
| 399 | is |
| 400 | (Natural (Read_LE16 (Buf, 6))) |
| 401 | with |
| 402 | Pre => Buf'Length >= 8; |
| 403 | |
| 404 | function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 405 | is |
| 406 | (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0)))) |
| 407 | with |
| 408 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4); |
| 409 | |
| 410 | function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 411 | is |
| 412 | (FSBlock_Offset |
| 413 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or |
| 414 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4))))) |
| 415 | with |
| 416 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10); |
| 417 | |
| 418 | function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 419 | renames Index_Logical; |
| 420 | |
| 421 | function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 422 | is |
| 423 | (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4)))) |
| 424 | with |
| 425 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6); |
| 426 | |
| 427 | function Extent_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, 6))), 32) or |
| 431 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8))))) |
| 432 | with |
| 433 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12); |
| 434 | |
| 435 | function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx |
| 436 | with |
| 437 | Pre => Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and |
| 438 | Extent_Logical (Buf, 1) <= Logical, |
| 439 | Post => Bin_Search'Result in 1 .. Refs and |
| 440 | Extent_Logical (Buf, Bin_Search'Result) <= Logical |
| 441 | is |
| 442 | Left : Extent_Idx := 1; |
| 443 | Right : Extent_Idx := Refs; |
| 444 | begin |
| 445 | while Left <= Right loop |
| 446 | declare |
| 447 | Mid : constant Extent_Idx := (Left + Right) / 2; |
| 448 | Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid); |
| 449 | begin |
| 450 | if Logical < Ext_Logical then |
| 451 | Right := Mid - 1; |
| 452 | else |
| 453 | Left := Mid + 1; |
| 454 | end if; |
| 455 | end; |
| 456 | end loop; |
| 457 | return Left - 1; |
| 458 | end Bin_Search; |
| 459 | |
| 460 | procedure Next_Ref |
| 461 | (Current : in FSBlock_Offset; |
| 462 | Logical_Off : in FSBlock_Logical; |
| 463 | Depth : in Natural; |
| 464 | Next : out Extent_Idx; |
| 465 | Cache_Start : out Max_Block_Index; |
| 466 | Cache_End : out Max_Block_Index; |
| 467 | Success : out Boolean) |
| 468 | with |
| 469 | Pre => Logical_Off <= Logical, |
| 470 | Post => (if Success then |
| 471 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical) |
| 472 | is |
| 473 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 474 | Dynamic_Max_Index : constant Natural := Block_Size / Extent_Header_Size - 1; |
| 475 | begin |
| 476 | Cache_FSBlock |
| 477 | (State => State, |
| 478 | Phys => Current, |
| 479 | Level => Depth, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame^] | 480 | Label => Cache_Label (Logical_Off), |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 481 | Cache_Start => Cache_Start, |
| 482 | Cache_End => Cache_End, |
| 483 | Success => Success); |
| 484 | if not Success then |
| 485 | Next := 1; |
| 486 | return; |
| 487 | end if; |
| 488 | |
| 489 | declare |
| 490 | Hdr_Magic : constant Unsigned_16 := |
| 491 | Header_Magic (State.Block_Cache (Cache_Start .. Cache_End)); |
| 492 | Hdr_Entries : constant Natural := |
| 493 | Header_Entries (State.Block_Cache (Cache_Start .. Cache_End)); |
| 494 | Hdr_Depth : constant Natural := |
| 495 | Header_Depth (State.Block_Cache (Cache_Start .. Cache_End)); |
| 496 | First_Logical : constant FSBlock_Logical := |
| 497 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1); |
| 498 | begin |
| 499 | Success := Success and then |
| 500 | Hdr_Magic = Extent_Header_Magic and then |
| 501 | Hdr_Depth = Depth and then |
| 502 | Hdr_Entries <= Dynamic_Max_Index and then |
| 503 | First_Logical = Logical_Off; |
| 504 | if not Success then |
| 505 | Next := 1; |
| 506 | else |
| 507 | Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries); |
| 508 | end if; |
| 509 | end; |
| 510 | end Next_Ref; |
| 511 | |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 512 | Inode_Magic : constant Unsigned_16 := Header_Magic (State.Inode.Extents); |
| 513 | Inode_Entries : constant Natural := Header_Entries (State.Inode.Extents); |
| 514 | First_Logical : constant FSBlock_Logical := Extent_Logical (State.Inode.Extents, 1); |
| 515 | Depth : Natural := Header_Depth (State.Inode.Extents); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 516 | |
| 517 | Cache_Start, Cache_End : Max_Block_Index; |
| 518 | Logical_Off, Length : FSBlock_Logical; |
| 519 | Idx : Extent_Idx; |
| 520 | begin |
| 521 | Success := |
| 522 | Inode_Magic = Extent_Header_Magic and then |
| 523 | Inode_Entries > 0 and then |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 524 | Inode_Entries < State.Inode.Extents'Length / Extent_Header_Size and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 525 | First_Logical <= Logical; |
| 526 | if not Success then |
| 527 | Physical := 0; |
| 528 | return; |
| 529 | end if; |
| 530 | |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 531 | Idx := Bin_Search (State.Inode.Extents, Inode_Entries); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 532 | if Depth = 0 then |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 533 | Physical := Extent_Physical (State.Inode.Extents, Idx); |
| 534 | Logical_Off := Extent_Logical (State.Inode.Extents, Idx); |
| 535 | Length := Extent_Length (State.Inode.Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 536 | else |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 537 | Physical := Index_Physical (State.Inode.Extents, Idx); |
| 538 | Logical_Off := Index_Logical (State.Inode.Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 539 | loop |
| 540 | 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; |
| 554 | Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 555 | Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 556 | end loop; |
| 557 | |
| 558 | Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 559 | Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 560 | Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 561 | end if; |
| 562 | |
| 563 | Success := |
| 564 | Length > 0 and then |
Nico Huber | c4c7a5e | 2023-12-14 00:08:56 +0100 | [diff] [blame] | 565 | Logical_Off <= FSBlock_Logical'Last - Length and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 566 | Logical < Logical_Off + Length; |
| 567 | if Success then |
| 568 | Physical := Physical + FSBlock_Offset (Logical - Logical_Off); |
| 569 | end if; |
| 570 | end Extent_Block_Map; |
| 571 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 572 | procedure Open |
| 573 | (State : in out T; |
| 574 | File_Len : out File_Length; |
| 575 | File_Path : in String; |
| 576 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 577 | is |
| 578 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 579 | File_Len := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 580 | Success := False; |
| 581 | end Open; |
| 582 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 583 | procedure Close (State : in out T) is |
| 584 | begin |
| 585 | State.S := Mounted; |
| 586 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 587 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 588 | procedure Read |
| 589 | (State : in out T; |
| 590 | File_Len : in File_Length; |
| 591 | File_Pos : in out File_Offset; |
| 592 | Buf : out Buffer_Type; |
| 593 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 594 | is |
| 595 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 596 | Buf := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 597 | Len := 0; |
| 598 | end Read; |
| 599 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 600 | -------------------------------------------------------------------------- |
| 601 | |
| 602 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 603 | |
| 604 | function C_Mount return int |
| 605 | with |
| 606 | Export, |
| 607 | Convention => C, |
| 608 | External_Name => "ext2fs_mount"; |
| 609 | function C_Mount return int |
| 610 | with |
| 611 | SPARK_Mode => Off |
| 612 | is |
| 613 | begin |
| 614 | return C.C_Mount; |
| 615 | end C_Mount; |
| 616 | |
| 617 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 618 | with |
| 619 | Export, |
| 620 | Convention => C, |
| 621 | External_Name => "ext2fs_dir"; |
| 622 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 623 | with |
| 624 | SPARK_Mode => Off |
| 625 | is |
| 626 | begin |
| 627 | return C.C_Open (File_Path); |
| 628 | end C_Open; |
| 629 | |
| 630 | function C_Read (Buf : System.Address; Len : int) return int |
| 631 | with |
| 632 | Export, |
| 633 | Convention => C, |
| 634 | External_Name => "ext2fs_read"; |
| 635 | function C_Read (Buf : System.Address; Len : int) return int |
| 636 | with |
| 637 | SPARK_Mode => Off |
| 638 | is |
| 639 | begin |
| 640 | return C.C_Read (Buf, Len); |
| 641 | end C_Read; |
| 642 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 643 | end FILO.FS.Ext2; |