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