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 | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 10 | with System; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 11 | with Interfaces; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 12 | with Interfaces.C; |
| 13 | with Interfaces.C.Strings; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 14 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 15 | with FILO.Blockdev; |
| 16 | with FILO.FS.VFS; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 17 | |
| 18 | use Interfaces.C; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 19 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 20 | package body FILO.FS.Ext2 is |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 21 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 22 | 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 Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 25 | -------------------------------------------------------------------------- |
| 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 Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 36 | procedure Mount |
| 37 | (State : in out T; |
| 38 | Part_Len : in Partition_Length; |
| 39 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 40 | is |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 41 | Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 42 | begin |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 43 | if Part_Len < 2 * SUPERBLOCK_SIZE then |
| 44 | Success := False; |
| 45 | return; |
| 46 | end if; |
| 47 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 48 | Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 49 | 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 Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 128 | end Mount; |
| 129 | |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 130 | 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 Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 151 | procedure Cache_FSBlock |
| 152 | (State : in out T; |
| 153 | Phys : in FSBlock_Offset; |
| 154 | Level : in Block_Cache_Index; |
| 155 | Logical_Off : in FSBlock_Logical; |
| 156 | Cache_Start : out Max_Block_Index; |
| 157 | Cache_End : out Max_Block_Index; |
| 158 | Success : out Boolean) |
| 159 | with |
| 160 | Post => Cache_End = Cache_Start + 2 ** State.Block_Size_Bits |
| 161 | is |
| 162 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 163 | -- Limit cache usage depending on block size: |
| 164 | Max_Level : constant Block_Cache_Index := |
| 165 | 2 ** (Log_Block_Size'Last - State.Block_Size_Bits) - 1; |
| 166 | Cache_Level : constant Block_Cache_Index := |
| 167 | Block_Cache_Index'Min (Level, Max_Level); |
| 168 | begin |
| 169 | Cache_Start := Cache_Level * Block_Size; |
| 170 | Cache_End := Cache_Start + Block_Size - 1; |
| 171 | if State.Block_Cache_Index (Cache_Level) = Logical_Off then |
| 172 | Success := True; |
| 173 | else |
| 174 | Read_FSBlock |
| 175 | (State => State, |
| 176 | Buf => State.Block_Cache (Cache_Start .. Cache_End), |
| 177 | FSBlock => Phys, |
| 178 | Success => Success); |
| 179 | State.Block_Cache_Index (Cache_Level) := Logical_Off; |
| 180 | end if; |
| 181 | end Cache_FSBlock; |
| 182 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 183 | procedure Ext2_Block_Map |
| 184 | (State : in out T; |
| 185 | Logical : in FSBlock_Logical; |
| 186 | Physical : out FSBlock_Offset; |
| 187 | Success : out Boolean) |
| 188 | is |
| 189 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 190 | Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4); |
| 191 | Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4); |
| 192 | type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1; |
| 193 | |
| 194 | procedure Indirect_Block_Lookup |
| 195 | (Indirect_Block_Phys : in FSBlock_Offset; |
| 196 | Addr_In_Block : in Addr_In_Block_Range; |
| 197 | Level : in Block_Cache_Index; |
| 198 | Logical_Off : in FSBlock_Logical; |
| 199 | Next_Physical : out FSBlock_Offset; |
| 200 | Success : out Boolean) |
| 201 | with |
| 202 | Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block |
| 203 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 204 | Cache_Start, Cache_End : Max_Block_Index; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 205 | begin |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 206 | Cache_FSBlock |
| 207 | (State => State, |
| 208 | Phys => Indirect_Block_Phys, |
| 209 | Level => Level, |
| 210 | Logical_Off => Logical_Off, |
| 211 | Cache_Start => Cache_Start, |
| 212 | Cache_End => Cache_End, |
| 213 | Success => Success); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 214 | Next_Physical := FSBlock_Offset (Read_LE32 |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 215 | (Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 216 | Off => Natural (Addr_In_Block) * 4)); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 217 | end Indirect_Block_Lookup; |
| 218 | |
| 219 | Logical_Rest : FSBlock_Logical := Logical; |
| 220 | begin |
| 221 | if Logical_Rest < Direct_Blocks then |
| 222 | Physical := FSBlock_Offset (State.Direct_Blocks (Natural (Logical))); |
| 223 | Success := True; |
| 224 | return; |
| 225 | end if; |
| 226 | |
| 227 | Logical_Rest := Logical_Rest - Direct_Blocks; |
| 228 | if Logical_Rest < Addr_Per_Block then |
| 229 | Indirect_Block_Lookup |
| 230 | (Indirect_Block_Phys => FSBlock_Offset (State.Indirect_Block), |
| 231 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 232 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 233 | Logical_Off => Logical - Logical_Rest, |
| 234 | Next_Physical => Physical, |
| 235 | Success => Success); |
| 236 | return; |
| 237 | end if; |
| 238 | |
| 239 | Logical_Rest := Logical_Rest - Addr_Per_Block; |
| 240 | if Logical_Rest < Addr_Per_Block ** 2 then |
| 241 | Indirect_Block_Lookup |
| 242 | (Indirect_Block_Phys => FSBlock_Offset (State.Double_Indirect), |
| 243 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 244 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 245 | Logical_Off => Logical - Logical_Rest, |
| 246 | Next_Physical => Physical, |
| 247 | Success => Success); |
| 248 | if not Success then |
| 249 | return; |
| 250 | end if; |
| 251 | |
| 252 | Indirect_Block_Lookup |
| 253 | (Indirect_Block_Phys => Physical, |
| 254 | 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] | 255 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 256 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 257 | Next_Physical => Physical, |
| 258 | Success => Success); |
| 259 | return; |
| 260 | end if; |
| 261 | |
| 262 | Logical_Rest := Logical_Rest - Addr_Per_Block ** 2; |
| 263 | if Logical_Rest < Addr_Per_Block ** 3 then |
| 264 | Indirect_Block_Lookup |
| 265 | (Indirect_Block_Phys => FSBlock_Offset (State.Triple_Indirect), |
| 266 | 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] | 267 | Level => 2, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 268 | Logical_Off => Logical - Logical_Rest, |
| 269 | Next_Physical => Physical, |
| 270 | Success => Success); |
| 271 | if not Success then |
| 272 | return; |
| 273 | end if; |
| 274 | |
| 275 | Indirect_Block_Lookup |
| 276 | (Indirect_Block_Phys => Physical, |
| 277 | 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] | 278 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 279 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2), |
| 280 | Next_Physical => Physical, |
| 281 | Success => Success); |
| 282 | if not Success then |
| 283 | return; |
| 284 | end if; |
| 285 | |
| 286 | Indirect_Block_Lookup |
| 287 | (Indirect_Block_Phys => Physical, |
| 288 | 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] | 289 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 290 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 291 | Next_Physical => Physical, |
| 292 | Success => Success); |
| 293 | return; |
| 294 | end if; |
| 295 | |
| 296 | -- Logical address was just too high. |
| 297 | Success := False; |
| 298 | end Ext2_Block_Map; |
| 299 | |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame^] | 300 | procedure Extent_Block_Map |
| 301 | (State : in out T; |
| 302 | Logical : in FSBlock_Logical; |
| 303 | Physical : out FSBlock_Offset; |
| 304 | Success : out Boolean) |
| 305 | is |
| 306 | -- Extent blocks always start with a 12B header and contain 12B entries. |
| 307 | -- Every entry starts with the number of the first logical block it |
| 308 | -- covers. Entries are sorted by this number. |
| 309 | -- Depth > 0 blocks have index entries, referencing further extent blocks. |
| 310 | -- Depth = 0 blocks have extent entries, referencing a contiguous range |
| 311 | -- of data blocks. |
| 312 | -- |
| 313 | -- +-----------------+ |
| 314 | -- .-> | Hdr depth=0 | |
| 315 | -- | +-----------------+ |
| 316 | -- | | Extent 0.. 12 | |
| 317 | -- +-------------+ | +-----------------+ |
| 318 | -- | Hdr depth=1 | | | Extent 13.. 13 | |
| 319 | -- +-------------+ | +-----------------+ |
| 320 | -- | Index 0 | --' | Extent 14..122 | |
| 321 | -- +-------------+ +-----------------+ |
| 322 | -- | Index 123 | --. |
| 323 | -- +-------------+ | +-----------------+ |
| 324 | -- `-> | Hdr depth=0 | |
| 325 | -- +-----------------+ |
| 326 | -- | Extent 123..125 | |
| 327 | -- +-----------------+ |
| 328 | -- | Extent 126..234 | |
| 329 | -- +-----------------+ |
| 330 | -- |
| 331 | |
| 332 | Extent_Header_Size : constant := 12; |
| 333 | Extent_Header_Magic : constant := 16#f03a#; |
| 334 | subtype Extent_Off is Natural range 0 .. Extent_Header_Size; |
| 335 | subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1; |
| 336 | |
| 337 | function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural |
| 338 | is |
| 339 | (Idx * Extent_Header_Size + Off); |
| 340 | |
| 341 | function Header_Magic (Buf : Buffer_Type) return Unsigned_16 |
| 342 | is |
| 343 | (Read_LE16 (Buf, 0)) |
| 344 | with |
| 345 | Pre => Buf'Length >= 2; |
| 346 | |
| 347 | function Header_Entries (Buf : Buffer_Type) return Natural |
| 348 | is |
| 349 | (Natural (Read_LE16 (Buf, 2))) |
| 350 | with |
| 351 | Pre => Buf'Length >= 4; |
| 352 | |
| 353 | function Header_Depth (Buf : Buffer_Type) return Natural |
| 354 | is |
| 355 | (Natural (Read_LE16 (Buf, 6))) |
| 356 | with |
| 357 | Pre => Buf'Length >= 8; |
| 358 | |
| 359 | function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 360 | is |
| 361 | (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0)))) |
| 362 | with |
| 363 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4); |
| 364 | |
| 365 | function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 366 | is |
| 367 | (FSBlock_Offset |
| 368 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or |
| 369 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4))))) |
| 370 | with |
| 371 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10); |
| 372 | |
| 373 | function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 374 | renames Index_Logical; |
| 375 | |
| 376 | function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 377 | is |
| 378 | (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4)))) |
| 379 | with |
| 380 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6); |
| 381 | |
| 382 | function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 383 | is |
| 384 | (FSBlock_Offset |
| 385 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or |
| 386 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8))))) |
| 387 | with |
| 388 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12); |
| 389 | |
| 390 | function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx |
| 391 | with |
| 392 | Pre => Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and |
| 393 | Extent_Logical (Buf, 1) <= Logical, |
| 394 | Post => Bin_Search'Result in 1 .. Refs and |
| 395 | Extent_Logical (Buf, Bin_Search'Result) <= Logical |
| 396 | is |
| 397 | Left : Extent_Idx := 1; |
| 398 | Right : Extent_Idx := Refs; |
| 399 | begin |
| 400 | while Left <= Right loop |
| 401 | declare |
| 402 | Mid : constant Extent_Idx := (Left + Right) / 2; |
| 403 | Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid); |
| 404 | begin |
| 405 | if Logical < Ext_Logical then |
| 406 | Right := Mid - 1; |
| 407 | else |
| 408 | Left := Mid + 1; |
| 409 | end if; |
| 410 | end; |
| 411 | end loop; |
| 412 | return Left - 1; |
| 413 | end Bin_Search; |
| 414 | |
| 415 | procedure Next_Ref |
| 416 | (Current : in FSBlock_Offset; |
| 417 | Logical_Off : in FSBlock_Logical; |
| 418 | Depth : in Natural; |
| 419 | Next : out Extent_Idx; |
| 420 | Cache_Start : out Max_Block_Index; |
| 421 | Cache_End : out Max_Block_Index; |
| 422 | Success : out Boolean) |
| 423 | with |
| 424 | Pre => Logical_Off <= Logical, |
| 425 | Post => (if Success then |
| 426 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical) |
| 427 | is |
| 428 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 429 | Dynamic_Max_Index : constant Natural := Block_Size / Extent_Header_Size - 1; |
| 430 | begin |
| 431 | Cache_FSBlock |
| 432 | (State => State, |
| 433 | Phys => Current, |
| 434 | Level => Depth, |
| 435 | Logical_Off => Logical_Off, |
| 436 | Cache_Start => Cache_Start, |
| 437 | Cache_End => Cache_End, |
| 438 | Success => Success); |
| 439 | if not Success then |
| 440 | Next := 1; |
| 441 | return; |
| 442 | end if; |
| 443 | |
| 444 | declare |
| 445 | Hdr_Magic : constant Unsigned_16 := |
| 446 | Header_Magic (State.Block_Cache (Cache_Start .. Cache_End)); |
| 447 | Hdr_Entries : constant Natural := |
| 448 | Header_Entries (State.Block_Cache (Cache_Start .. Cache_End)); |
| 449 | Hdr_Depth : constant Natural := |
| 450 | Header_Depth (State.Block_Cache (Cache_Start .. Cache_End)); |
| 451 | First_Logical : constant FSBlock_Logical := |
| 452 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1); |
| 453 | begin |
| 454 | Success := Success and then |
| 455 | Hdr_Magic = Extent_Header_Magic and then |
| 456 | Hdr_Depth = Depth and then |
| 457 | Hdr_Entries <= Dynamic_Max_Index and then |
| 458 | First_Logical = Logical_Off; |
| 459 | if not Success then |
| 460 | Next := 1; |
| 461 | else |
| 462 | Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries); |
| 463 | end if; |
| 464 | end; |
| 465 | end Next_Ref; |
| 466 | |
| 467 | Inode_Magic : constant Unsigned_16 := Header_Magic (State.Inode_Extents); |
| 468 | Inode_Entries : constant Natural := Header_Entries (State.Inode_Extents); |
| 469 | First_Logical : constant FSBlock_Logical := Extent_Logical (State.Inode_Extents, 1); |
| 470 | Depth : Natural := Header_Depth (State.Inode_Extents); |
| 471 | |
| 472 | Cache_Start, Cache_End : Max_Block_Index; |
| 473 | Logical_Off, Length : FSBlock_Logical; |
| 474 | Idx : Extent_Idx; |
| 475 | begin |
| 476 | Success := |
| 477 | Inode_Magic = Extent_Header_Magic and then |
| 478 | Inode_Entries > 0 and then |
| 479 | Inode_Entries < State.Inode_Extents'Length / Extent_Header_Size and then |
| 480 | First_Logical <= Logical; |
| 481 | if not Success then |
| 482 | Physical := 0; |
| 483 | return; |
| 484 | end if; |
| 485 | |
| 486 | Idx := Bin_Search (State.Inode_Extents, Inode_Entries); |
| 487 | if Depth = 0 then |
| 488 | Physical := Extent_Physical (State.Inode_Extents, Idx); |
| 489 | Logical_Off := Extent_Logical (State.Inode_Extents, Idx); |
| 490 | Length := Extent_Length (State.Inode_Extents, Idx); |
| 491 | else |
| 492 | Physical := Index_Physical (State.Inode_Extents, Idx); |
| 493 | Logical_Off := Index_Logical (State.Inode_Extents, Idx); |
| 494 | loop |
| 495 | Depth := Depth - 1; |
| 496 | Next_Ref |
| 497 | (Current => Physical, |
| 498 | Logical_Off => Logical_Off, |
| 499 | Depth => Depth, |
| 500 | Next => Idx, |
| 501 | Cache_Start => Cache_Start, |
| 502 | Cache_End => Cache_End, |
| 503 | Success => Success); |
| 504 | if not Success then |
| 505 | return; |
| 506 | end if; |
| 507 | |
| 508 | exit when Depth = 0; |
| 509 | Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 510 | Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 511 | end loop; |
| 512 | |
| 513 | Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 514 | Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 515 | Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 516 | end if; |
| 517 | |
| 518 | Success := |
| 519 | Length > 0 and then |
| 520 | Logical_Off <= FSBlock_Logical'Last - Length + 1 and then |
| 521 | Logical < Logical_Off + Length; |
| 522 | if Success then |
| 523 | Physical := Physical + FSBlock_Offset (Logical - Logical_Off); |
| 524 | end if; |
| 525 | end Extent_Block_Map; |
| 526 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 527 | procedure Open |
| 528 | (State : in out T; |
| 529 | File_Len : out File_Length; |
| 530 | File_Path : in String; |
| 531 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 532 | is |
| 533 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 534 | File_Len := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 535 | Success := False; |
| 536 | end Open; |
| 537 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 538 | procedure Close (State : in out T) is |
| 539 | begin |
| 540 | State.S := Mounted; |
| 541 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 542 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 543 | procedure Read |
| 544 | (State : in out T; |
| 545 | File_Len : in File_Length; |
| 546 | File_Pos : in out File_Offset; |
| 547 | Buf : out Buffer_Type; |
| 548 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 549 | is |
| 550 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 551 | Buf := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 552 | Len := 0; |
| 553 | end Read; |
| 554 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 555 | -------------------------------------------------------------------------- |
| 556 | |
| 557 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 558 | |
| 559 | function C_Mount return int |
| 560 | with |
| 561 | Export, |
| 562 | Convention => C, |
| 563 | External_Name => "ext2fs_mount"; |
| 564 | function C_Mount return int |
| 565 | with |
| 566 | SPARK_Mode => Off |
| 567 | is |
| 568 | begin |
| 569 | return C.C_Mount; |
| 570 | end C_Mount; |
| 571 | |
| 572 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 573 | with |
| 574 | Export, |
| 575 | Convention => C, |
| 576 | External_Name => "ext2fs_dir"; |
| 577 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 578 | with |
| 579 | SPARK_Mode => Off |
| 580 | is |
| 581 | begin |
| 582 | return C.C_Open (File_Path); |
| 583 | end C_Open; |
| 584 | |
| 585 | function C_Read (Buf : System.Address; Len : int) return int |
| 586 | with |
| 587 | Export, |
| 588 | Convention => C, |
| 589 | External_Name => "ext2fs_read"; |
| 590 | function C_Read (Buf : System.Address; Len : int) return int |
| 591 | with |
| 592 | SPARK_Mode => Off |
| 593 | is |
| 594 | begin |
| 595 | return C.C_Read (Buf, Len); |
| 596 | end C_Read; |
| 597 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 598 | end FILO.FS.Ext2; |