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