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; |
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; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 28 | SUPERBLOCK_MAGIC : constant := 16#ef53#; |
| 29 | OLD_REV : constant := 0; |
| 30 | DYNAMIC_REV : constant := 1; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 31 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 32 | FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#; |
| 33 | FEATURE_INCOMPAT_64BIT : constant := 16#0080#; |
| 34 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 35 | EXT4_EXTENTS_FL : constant := 16#8_0000#; |
| 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); |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 67 | State.Block_Size := 2 ** Log_Block_Size (S_Log_Block_Size + 10); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 68 | else |
| 69 | Success := False; |
| 70 | return; |
| 71 | end if; |
| 72 | end; |
| 73 | |
| 74 | declare |
| 75 | S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4); |
| 76 | begin |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 77 | if S_Inodes_Per_Group in 2 .. Unsigned_32'Last then |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 78 | State.Inodes_Per_Group := Inode_Index (S_Inodes_Per_Group); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 79 | else |
| 80 | Success := False; |
| 81 | return; |
| 82 | end if; |
| 83 | end; |
| 84 | |
| 85 | declare |
| 86 | S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4); |
| 87 | begin |
| 88 | if S_Rev_Level >= DYNAMIC_REV then |
| 89 | declare |
| 90 | S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4); |
| 91 | begin |
| 92 | if S_Inode_Size in |
| 93 | Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last) |
| 94 | then |
| 95 | State.Inode_Size := Inode_Size (S_Inode_Size); |
| 96 | else |
| 97 | Success := False; |
| 98 | return; |
| 99 | end if; |
| 100 | end; |
| 101 | else |
| 102 | State.Inode_Size := Inode_Size'First; |
| 103 | end if; |
| 104 | end; |
| 105 | |
| 106 | declare |
| 107 | S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4); |
| 108 | begin |
| 109 | State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0; |
| 110 | State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0; |
| 111 | if State.Feature_64Bit then |
| 112 | declare |
| 113 | S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2); |
| 114 | begin |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 115 | if Natural (S_Desc_Size) in Desc_Size and |
| 116 | Natural (S_Desc_Size) <= State.Block_Size and |
| 117 | Is_Power_Of_2 (S_Desc_Size) |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 118 | then |
| 119 | State.Desc_Size := Desc_Size (S_Desc_Size); |
| 120 | else |
| 121 | Success := False; |
| 122 | return; |
| 123 | end if; |
| 124 | end; |
Nico Huber | 77a04d7 | 2023-12-13 23:11:40 +0100 | [diff] [blame] | 125 | State.Feature_64Bit := State.Feature_64Bit and State.Desc_Size >= 64; |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 126 | else |
| 127 | State.Desc_Size := Desc_Size'First; |
| 128 | end if; |
| 129 | end; |
| 130 | |
| 131 | State.S := Mounted; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 132 | end Mount; |
| 133 | |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 134 | procedure Read_FSBlock |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 135 | (Buf : in out Buffer_Type; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 136 | FSBlock : in FSBlock_Offset; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 137 | Part_Len : in Partition_Length; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 138 | Success : out Boolean) |
| 139 | with |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 140 | Pre => Buf'Length in Block_Size |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 141 | is |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 142 | FSBlock_64 : constant Integer_64 := Integer_64 (FSBlock); |
| 143 | Block_Size : constant Integer_64 := Integer_64 (Buf'Length); |
| 144 | Max_Block_Offset : constant Integer_64 := Integer_64 (Part_Len) / Block_Size - 1; |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 145 | begin |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 146 | if FSBlock_64 > Max_Block_Offset then |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 147 | Success := False; |
| 148 | return; |
| 149 | end if; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 150 | Blockdev.Read (Buf, Blockdev_Length (FSBlock_64 * Block_Size), Success); |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 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; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 157 | Label : in Cache_Label; |
| 158 | Logical : in Boolean := True; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 159 | Cache_Start : out Max_Block_Index; |
| 160 | Cache_End : out Max_Block_Index; |
| 161 | Success : out Boolean) |
| 162 | with |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 163 | Post => Cache_End = Cache_Start + State.Block_Size - 1 |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 164 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 165 | -- Limit cache usage depending on block size: |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 166 | Max_Level : constant Block_Cache_Index := Block_Size'Last / State.Block_Size - 1; |
| 167 | Cache_Level : constant Block_Cache_Index := Block_Cache_Index'Min (Level, Max_Level); |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 168 | begin |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 169 | Cache_Start := Cache_Level * State.Block_Size; |
| 170 | Cache_End := Cache_Start + State.Block_Size - 1; |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 171 | if State.Block_Cache_Logical (Cache_Level) = Logical and |
| 172 | State.Block_Cache_Label (Cache_Level) = Label |
| 173 | then |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 174 | Success := True; |
| 175 | else |
| 176 | Read_FSBlock |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 177 | (Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 178 | FSBlock => Phys, |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 179 | Part_Len => State.Part_Len, |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 180 | Success => Success); |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 181 | State.Block_Cache_Logical (Cache_Level) := Logical; |
| 182 | State.Block_Cache_Label (Cache_Level) := Label; |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 183 | end if; |
| 184 | end Cache_FSBlock; |
| 185 | |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 186 | procedure Cache_FSBlock |
| 187 | (State : in out T; |
| 188 | Phys : in FSBlock_Offset; |
| 189 | Level : in Block_Cache_Index; |
| 190 | Cache_Start : out Max_Block_Index; |
| 191 | Cache_End : out Max_Block_Index; |
| 192 | Success : out Boolean) |
| 193 | with |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 194 | Post => Cache_End = Cache_Start + State.Block_Size - 1 |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 195 | is |
| 196 | begin |
| 197 | Cache_FSBlock (State, Phys, Level, Cache_Label (Phys), |
| 198 | False, Cache_Start, Cache_End, Success); |
| 199 | end Cache_FSBlock; |
| 200 | |
| 201 | procedure Reset_Cache_Logical (State : in out T) is |
| 202 | begin |
| 203 | for I in Block_Cache_Index loop |
| 204 | if State.Block_Cache_Logical (I) then |
| 205 | State.Block_Cache_Logical (I) := False; |
| 206 | State.Block_Cache_Label (I) := 0; |
| 207 | end if; |
| 208 | end loop; |
| 209 | end Reset_Cache_Logical; |
| 210 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 211 | procedure Ext2_Block_Map |
| 212 | (State : in out T; |
| 213 | Logical : in FSBlock_Logical; |
| 214 | Physical : out FSBlock_Offset; |
| 215 | Success : out Boolean) |
| 216 | is |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 217 | Direct_Blocks : constant := 12; |
| 218 | type Direct_Blocks_Array is array (Natural range 0 .. Direct_Blocks - 1) of Unsigned_32; |
| 219 | type Inode_Blocks is record |
| 220 | Direct_Blocks : Direct_Blocks_Array; |
| 221 | Indirect_Block : Unsigned_32; |
| 222 | Double_Indirect : Unsigned_32; |
| 223 | Triple_Indirect : Unsigned_32; |
| 224 | end record |
| 225 | with Size => Inode_Extents'Length * 8; |
| 226 | |
| 227 | function I_Blocks is new Ada.Unchecked_Conversion (Inode_Extents, Inode_Blocks); |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 228 | function I_Blocks (State : T) return Inode_Blocks is (I_Blocks (State.Inode.Inline)); |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 229 | |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 230 | Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (State.Block_Size / 4); |
| 231 | Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size'Last / 4); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 232 | type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1; |
| 233 | |
| 234 | procedure Indirect_Block_Lookup |
| 235 | (Indirect_Block_Phys : in FSBlock_Offset; |
| 236 | Addr_In_Block : in Addr_In_Block_Range; |
| 237 | Level : in Block_Cache_Index; |
| 238 | Logical_Off : in FSBlock_Logical; |
| 239 | Next_Physical : out FSBlock_Offset; |
| 240 | Success : out Boolean) |
| 241 | with |
| 242 | Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block |
| 243 | is |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 244 | Cache_Start, Cache_End : Max_Block_Index; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 245 | begin |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 246 | Cache_FSBlock |
| 247 | (State => State, |
| 248 | Phys => Indirect_Block_Phys, |
| 249 | Level => Level, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 250 | Label => Cache_Label (Logical_Off), |
Nico Huber | 68c8693 | 2023-12-13 11:03:11 +0100 | [diff] [blame] | 251 | Cache_Start => Cache_Start, |
| 252 | Cache_End => Cache_End, |
| 253 | Success => Success); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 254 | Next_Physical := FSBlock_Offset (Read_LE32 |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 255 | (Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 256 | Off => Natural (Addr_In_Block) * 4)); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 257 | end Indirect_Block_Lookup; |
| 258 | |
| 259 | Logical_Rest : FSBlock_Logical := Logical; |
| 260 | begin |
| 261 | if Logical_Rest < Direct_Blocks then |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 262 | Physical := FSBlock_Offset (I_Blocks (State).Direct_Blocks (Natural (Logical))); |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 263 | Success := True; |
| 264 | return; |
| 265 | end if; |
| 266 | |
| 267 | Logical_Rest := Logical_Rest - Direct_Blocks; |
| 268 | if Logical_Rest < Addr_Per_Block then |
| 269 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 270 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Indirect_Block), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 271 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 272 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 273 | Logical_Off => Logical - Logical_Rest, |
| 274 | Next_Physical => Physical, |
| 275 | Success => Success); |
| 276 | return; |
| 277 | end if; |
| 278 | |
| 279 | Logical_Rest := Logical_Rest - Addr_Per_Block; |
| 280 | if Logical_Rest < Addr_Per_Block ** 2 then |
| 281 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 282 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Double_Indirect), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 283 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame] | 284 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 285 | Logical_Off => Logical - Logical_Rest, |
| 286 | Next_Physical => Physical, |
| 287 | Success => Success); |
| 288 | if not Success then |
| 289 | return; |
| 290 | end if; |
| 291 | |
| 292 | Indirect_Block_Lookup |
| 293 | (Indirect_Block_Phys => Physical, |
| 294 | 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] | 295 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 296 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 297 | Next_Physical => Physical, |
| 298 | Success => Success); |
| 299 | return; |
| 300 | end if; |
| 301 | |
| 302 | Logical_Rest := Logical_Rest - Addr_Per_Block ** 2; |
| 303 | if Logical_Rest < Addr_Per_Block ** 3 then |
| 304 | Indirect_Block_Lookup |
Nico Huber | 33f6d95 | 2023-12-13 23:16:42 +0100 | [diff] [blame] | 305 | (Indirect_Block_Phys => FSBlock_Offset (I_Blocks (State).Triple_Indirect), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 306 | 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] | 307 | Level => 2, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 308 | Logical_Off => Logical - Logical_Rest, |
| 309 | Next_Physical => Physical, |
| 310 | Success => Success); |
| 311 | if not Success then |
| 312 | return; |
| 313 | end if; |
| 314 | |
| 315 | Indirect_Block_Lookup |
| 316 | (Indirect_Block_Phys => Physical, |
| 317 | 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] | 318 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 319 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2), |
| 320 | Next_Physical => Physical, |
| 321 | Success => Success); |
| 322 | if not Success then |
| 323 | return; |
| 324 | end if; |
| 325 | |
| 326 | Indirect_Block_Lookup |
| 327 | (Indirect_Block_Phys => Physical, |
| 328 | 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] | 329 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 330 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 331 | Next_Physical => Physical, |
| 332 | Success => Success); |
| 333 | return; |
| 334 | end if; |
| 335 | |
| 336 | -- Logical address was just too high. |
| 337 | Success := False; |
| 338 | end Ext2_Block_Map; |
| 339 | |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 340 | procedure Extent_Block_Map |
| 341 | (State : in out T; |
| 342 | Logical : in FSBlock_Logical; |
| 343 | Physical : out FSBlock_Offset; |
| 344 | Success : out Boolean) |
| 345 | is |
| 346 | -- Extent blocks always start with a 12B header and contain 12B entries. |
| 347 | -- Every entry starts with the number of the first logical block it |
| 348 | -- covers. Entries are sorted by this number. |
| 349 | -- Depth > 0 blocks have index entries, referencing further extent blocks. |
| 350 | -- Depth = 0 blocks have extent entries, referencing a contiguous range |
| 351 | -- of data blocks. |
| 352 | -- |
| 353 | -- +-----------------+ |
| 354 | -- .-> | Hdr depth=0 | |
| 355 | -- | +-----------------+ |
| 356 | -- | | Extent 0.. 12 | |
| 357 | -- +-------------+ | +-----------------+ |
| 358 | -- | Hdr depth=1 | | | Extent 13.. 13 | |
| 359 | -- +-------------+ | +-----------------+ |
| 360 | -- | Index 0 | --' | Extent 14..122 | |
| 361 | -- +-------------+ +-----------------+ |
| 362 | -- | Index 123 | --. |
| 363 | -- +-------------+ | +-----------------+ |
| 364 | -- `-> | Hdr depth=0 | |
| 365 | -- +-----------------+ |
| 366 | -- | Extent 123..125 | |
| 367 | -- +-----------------+ |
| 368 | -- | Extent 126..234 | |
| 369 | -- +-----------------+ |
| 370 | -- |
| 371 | |
| 372 | Extent_Header_Size : constant := 12; |
| 373 | Extent_Header_Magic : constant := 16#f03a#; |
| 374 | subtype Extent_Off is Natural range 0 .. Extent_Header_Size; |
| 375 | subtype Extent_Idx is Natural range 1 .. (Max_Block_Index'Last + 1) / Extent_Header_Size - 1; |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 376 | Dynamic_Max_Index : constant Extent_Idx := State.Block_Size / Extent_Header_Size - 1; |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 377 | |
| 378 | function Extent_Byte_Offset (Idx : Extent_Idx; Off : Extent_Off) return Natural |
| 379 | is |
| 380 | (Idx * Extent_Header_Size + Off); |
| 381 | |
| 382 | function Header_Magic (Buf : Buffer_Type) return Unsigned_16 |
| 383 | is |
| 384 | (Read_LE16 (Buf, 0)) |
| 385 | with |
| 386 | Pre => Buf'Length >= 2; |
| 387 | |
| 388 | function Header_Entries (Buf : Buffer_Type) return Natural |
| 389 | is |
| 390 | (Natural (Read_LE16 (Buf, 2))) |
| 391 | with |
| 392 | Pre => Buf'Length >= 4; |
| 393 | |
| 394 | function Header_Depth (Buf : Buffer_Type) return Natural |
| 395 | is |
| 396 | (Natural (Read_LE16 (Buf, 6))) |
| 397 | with |
| 398 | Pre => Buf'Length >= 8; |
| 399 | |
| 400 | function Index_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 401 | is |
| 402 | (FSBlock_Logical (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 0)))) |
| 403 | with |
| 404 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 4); |
| 405 | |
| 406 | function Index_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 407 | is |
| 408 | (FSBlock_Offset |
| 409 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 8))), 32) or |
| 410 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 4))))) |
| 411 | with |
| 412 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 10); |
| 413 | |
| 414 | function Extent_Logical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 415 | renames Index_Logical; |
| 416 | |
| 417 | function Extent_Length (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Logical |
| 418 | is |
| 419 | (FSBlock_Logical (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 4)))) |
| 420 | with |
| 421 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 6); |
| 422 | |
| 423 | function Extent_Physical (Buf : Buffer_Type; Idx : Extent_Idx) return FSBlock_Offset |
| 424 | is |
| 425 | (FSBlock_Offset |
| 426 | (Shift_Left (Unsigned_64 (Read_LE16 (Buf, Extent_Byte_Offset (Idx, 6))), 32) or |
| 427 | Unsigned_64 (Read_LE32 (Buf, Extent_Byte_Offset (Idx, 8))))) |
| 428 | with |
| 429 | Pre => Buf'Length >= Extent_Byte_Offset (Idx, 12); |
| 430 | |
| 431 | function Bin_Search (Buf : Buffer_Type; Refs : Extent_Idx) return Extent_Idx |
| 432 | with |
| 433 | Pre => Refs in 1 .. Extent_Idx (Buf'Length / Extent_Header_Size - 1) and |
| 434 | Extent_Logical (Buf, 1) <= Logical, |
| 435 | Post => Bin_Search'Result in 1 .. Refs and |
| 436 | Extent_Logical (Buf, Bin_Search'Result) <= Logical |
| 437 | is |
| 438 | Left : Extent_Idx := 1; |
| 439 | Right : Extent_Idx := Refs; |
| 440 | begin |
| 441 | while Left <= Right loop |
| 442 | declare |
| 443 | Mid : constant Extent_Idx := (Left + Right) / 2; |
| 444 | Ext_Logical : constant FSBlock_Logical := Extent_Logical (Buf, Mid); |
| 445 | begin |
| 446 | if Logical < Ext_Logical then |
| 447 | Right := Mid - 1; |
| 448 | else |
| 449 | Left := Mid + 1; |
| 450 | end if; |
| 451 | end; |
| 452 | end loop; |
| 453 | return Left - 1; |
| 454 | end Bin_Search; |
| 455 | |
| 456 | procedure Next_Ref |
| 457 | (Current : in FSBlock_Offset; |
| 458 | Logical_Off : in FSBlock_Logical; |
| 459 | Depth : in Natural; |
| 460 | Next : out Extent_Idx; |
| 461 | Cache_Start : out Max_Block_Index; |
| 462 | Cache_End : out Max_Block_Index; |
| 463 | Success : out Boolean) |
| 464 | with |
| 465 | Pre => Logical_Off <= Logical, |
| 466 | Post => (if Success then |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 467 | Cache_End = Cache_Start + State.Block_Size - 1 and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 468 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Next) <= Logical) |
| 469 | is |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 470 | begin |
| 471 | Cache_FSBlock |
| 472 | (State => State, |
| 473 | Phys => Current, |
| 474 | Level => Depth, |
Nico Huber | 57dfbfb | 2023-12-13 23:24:16 +0100 | [diff] [blame] | 475 | Label => Cache_Label (Logical_Off), |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 476 | Cache_Start => Cache_Start, |
| 477 | Cache_End => Cache_End, |
| 478 | Success => Success); |
| 479 | if not Success then |
| 480 | Next := 1; |
| 481 | return; |
| 482 | end if; |
| 483 | |
| 484 | declare |
| 485 | Hdr_Magic : constant Unsigned_16 := |
| 486 | Header_Magic (State.Block_Cache (Cache_Start .. Cache_End)); |
| 487 | Hdr_Entries : constant Natural := |
| 488 | Header_Entries (State.Block_Cache (Cache_Start .. Cache_End)); |
| 489 | Hdr_Depth : constant Natural := |
| 490 | Header_Depth (State.Block_Cache (Cache_Start .. Cache_End)); |
| 491 | First_Logical : constant FSBlock_Logical := |
| 492 | Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), 1); |
| 493 | begin |
| 494 | Success := Success and then |
| 495 | Hdr_Magic = Extent_Header_Magic and then |
| 496 | Hdr_Depth = Depth and then |
| 497 | Hdr_Entries <= Dynamic_Max_Index and then |
| 498 | First_Logical = Logical_Off; |
| 499 | if not Success then |
| 500 | Next := 1; |
| 501 | else |
| 502 | Next := Bin_Search (State.Block_Cache (Cache_Start .. Cache_End), Hdr_Entries); |
| 503 | end if; |
| 504 | end; |
| 505 | end Next_Ref; |
| 506 | |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 507 | Inline_Extents : Ext2.Inode_Extents renames State.Inode.Inline; |
| 508 | Inode_Magic : constant Unsigned_16 := Header_Magic (Inline_Extents); |
| 509 | Inode_Entries : constant Natural := Header_Entries (Inline_Extents); |
| 510 | First_Logical : constant FSBlock_Logical := Extent_Logical (Inline_Extents, 1); |
| 511 | Depth : Natural := Header_Depth (Inline_Extents); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 512 | |
| 513 | Cache_Start, Cache_End : Max_Block_Index; |
| 514 | Logical_Off, Length : FSBlock_Logical; |
| 515 | Idx : Extent_Idx; |
| 516 | begin |
| 517 | Success := |
| 518 | Inode_Magic = Extent_Header_Magic and then |
| 519 | Inode_Entries > 0 and then |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 520 | Inode_Entries < Inline_Extents'Length / Extent_Header_Size and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 521 | First_Logical <= Logical; |
| 522 | if not Success then |
| 523 | Physical := 0; |
| 524 | return; |
| 525 | end if; |
| 526 | |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 527 | Idx := Bin_Search (Inline_Extents, Inode_Entries); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 528 | if Depth = 0 then |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 529 | Physical := Extent_Physical (Inline_Extents, Idx); |
| 530 | Logical_Off := Extent_Logical (Inline_Extents, Idx); |
| 531 | Length := Extent_Length (Inline_Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 532 | else |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 533 | Physical := Index_Physical (Inline_Extents, Idx); |
| 534 | Logical_Off := Index_Logical (Inline_Extents, Idx); |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 535 | loop |
| 536 | Depth := Depth - 1; |
| 537 | Next_Ref |
| 538 | (Current => Physical, |
| 539 | Logical_Off => Logical_Off, |
| 540 | Depth => Depth, |
| 541 | Next => Idx, |
| 542 | Cache_Start => Cache_Start, |
| 543 | Cache_End => Cache_End, |
| 544 | Success => Success); |
| 545 | if not Success then |
| 546 | return; |
| 547 | end if; |
| 548 | |
| 549 | exit when Depth = 0; |
| 550 | Physical := Index_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 551 | Logical_Off := Index_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 552 | end loop; |
| 553 | |
| 554 | Physical := Extent_Physical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 555 | Logical_Off := Extent_Logical (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 556 | Length := Extent_Length (State.Block_Cache (Cache_Start .. Cache_End), Idx); |
| 557 | end if; |
| 558 | |
| 559 | Success := |
| 560 | Length > 0 and then |
Nico Huber | c4c7a5e | 2023-12-14 00:08:56 +0100 | [diff] [blame] | 561 | Logical_Off <= FSBlock_Logical'Last - Length and then |
Nico Huber | fffc8c1 | 2023-12-13 12:44:12 +0100 | [diff] [blame] | 562 | Logical < Logical_Off + Length; |
| 563 | if Success then |
| 564 | Physical := Physical + FSBlock_Offset (Logical - Logical_Off); |
| 565 | end if; |
| 566 | end Extent_Block_Map; |
| 567 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 568 | procedure Open |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 569 | (State : in out T; |
| 570 | Inode : in Inode_Index; |
| 571 | Success : out Boolean) |
| 572 | with |
| 573 | Pre => Is_Mounted (State) and not Is_Open (State), |
| 574 | Post => Success = Is_Open (State) |
| 575 | is |
| 576 | type Group_Index is new Natural; |
| 577 | subtype Desc_In_Block_Index is Group_Index |
| 578 | range 0 .. Group_Index (2 ** Log_Block_Size'Last / Desc_Size'First - 1); |
| 579 | |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 580 | Inodes_Per_Block : constant Inode_Index := Inode_Index (State.Block_Size / State.Inode_Size); |
| 581 | Desc_Per_Block : constant Group_Index := Group_Index (State.Block_Size / State.Desc_Size); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 582 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 583 | ------------------------ |
| 584 | -- Group deserialization |
| 585 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 586 | subtype Group_Off is Natural range 0 .. Desc_Size'Last; |
| 587 | function Group_Byte_Offset (Idx : Group_Index; Off : Group_Off) return Natural |
| 588 | is |
| 589 | (Natural (Idx) * State.Desc_Size + Off); |
| 590 | |
| 591 | function Group_Inode_Table (Buf : Buffer_Type; Idx : Group_Index) return FSBlock_Offset |
| 592 | is |
| 593 | (FSBlock_Offset ( |
| 594 | (if State.Feature_64Bit |
| 595 | then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 40))), 32) |
| 596 | else 0) |
| 597 | or |
| 598 | Unsigned_64 (Read_LE32 (Buf, Group_Byte_Offset (Idx, 8))))) |
| 599 | with |
| 600 | Pre => Buf'Length >= Group_Byte_Offset (Idx, 44); |
| 601 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 602 | ------------------------ |
| 603 | -- Inode deserialization |
| 604 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 605 | subtype Inode_Off is Natural range 0 .. Inode_Size'Last; |
| 606 | function Inode_Byte_Offset (Idx : Inode_Index; Off : Inode_Off) return Natural |
| 607 | is |
| 608 | (Natural (Idx) * State.Inode_Size + Off); |
| 609 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 610 | function Inode_Mode (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_16 |
| 611 | is |
| 612 | (Read_LE16 (Buf, Inode_Byte_Offset (Idx, 0))) |
| 613 | with |
| 614 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 2); |
| 615 | |
| 616 | function Inode_Size (Buf : Buffer_Type; Idx : Inode_Index; Mode : Inode_Type) return Unsigned_64 |
| 617 | is |
| 618 | ((if Mode = Regular |
| 619 | then Shift_Left (Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 108))), 32) |
| 620 | else 0) or |
| 621 | Unsigned_64 (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 4)))) |
| 622 | with |
| 623 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 112); |
| 624 | |
| 625 | function Inode_Blocks (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 626 | is |
| 627 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 28))) |
| 628 | with |
| 629 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 32); |
| 630 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 631 | function Inode_Flags (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 632 | is |
| 633 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 32))) |
| 634 | with |
| 635 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 36); |
| 636 | |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 637 | function Inode_File_ACL (Buf : Buffer_Type; Idx : Inode_Index) return Unsigned_32 |
| 638 | is |
| 639 | (Read_LE32 (Buf, Inode_Byte_Offset (Idx, 104))) |
| 640 | with |
| 641 | Pre => Buf'Length >= Inode_Byte_Offset (Idx, 108); |
| 642 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 643 | Group : constant Group_Index := Group_Index ((Inode - 1) / State.Inodes_Per_Group); |
| 644 | Desc_Block : constant FSBlock_Offset := |
| 645 | 1 + State.First_Data_Block + FSBlock_Offset (Group / Desc_Per_Block); |
| 646 | Cache_Start, Cache_End : Max_Block_Index; |
| 647 | begin |
| 648 | Cache_FSBlock |
| 649 | (State => State, |
| 650 | Phys => Desc_Block, |
| 651 | Level => Natural'Min (Natural (Group / Desc_Per_Block), Block_Cache_Index'Last), |
| 652 | Cache_Start => Cache_Start, |
| 653 | Cache_End => Cache_End, |
| 654 | Success => Success); |
| 655 | if not Success then |
| 656 | return; |
| 657 | end if; |
| 658 | |
| 659 | declare |
| 660 | Desc : constant Desc_In_Block_Index := Group mod Desc_Per_Block; |
| 661 | Inode_In_Group : constant Inode_Index := (Inode - 1) mod State.Inodes_Per_Group; |
| 662 | Inode_Block : constant FSBlock_Offset := |
| 663 | Group_Inode_Table (State.Block_Cache (Cache_Start .. Cache_End), Group) + |
| 664 | FSBlock_Offset (Inode_In_Group / Inodes_Per_Block); |
| 665 | begin |
| 666 | Cache_FSBlock |
| 667 | (State => State, |
| 668 | Phys => Inode_Block, |
| 669 | Level => Block_Cache_Index'Last, |
| 670 | Cache_Start => Cache_Start, |
| 671 | Cache_End => Cache_End, |
| 672 | Success => Success); |
| 673 | if not Success then |
| 674 | return; |
| 675 | end if; |
| 676 | |
| 677 | declare |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 678 | S_IFMT : constant := 8#170000#; |
| 679 | S_IFDIR : constant := 8#040000#; |
| 680 | S_IFREG : constant := 8#100000#; |
| 681 | S_IFLNK : constant := 8#120000#; |
| 682 | |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 683 | Inode_In_Block : constant Inode_Index := Inode_In_Group mod Inodes_Per_Block; |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 684 | I_Mode : constant Unsigned_16 := |
| 685 | Inode_Mode (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block); |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 686 | I_Flags : constant Unsigned_32 := |
| 687 | Inode_Flags (State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block); |
| 688 | begin |
Nico Huber | 022e226 | 2023-12-15 23:15:17 +0100 | [diff] [blame] | 689 | case I_Mode and S_IFMT is |
| 690 | when S_IFDIR => State.Inode.Mode := Dir; |
| 691 | when S_IFREG => State.Inode.Mode := Regular; |
| 692 | when S_IFLNK => State.Inode.Mode := Link; |
| 693 | when others => Success := False; return; |
| 694 | end case; |
| 695 | if State.Inode.Mode = Link then |
| 696 | declare |
| 697 | I_File_ACL : constant Unsigned_32 := Inode_File_ACL ( |
| 698 | State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block); |
| 699 | I_Blocks : constant Unsigned_32 := Inode_Blocks ( |
| 700 | State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block); |
| 701 | begin |
| 702 | if (I_File_ACL = 0 and I_Blocks = 0) or |
| 703 | (I_File_ACL /= 0 and I_Blocks = 2 ** (State.Block_Size_Bits - 9)) |
| 704 | then |
| 705 | State.Inode.Mode := Fast_Link; |
| 706 | end if; |
| 707 | end; |
| 708 | end if; |
| 709 | declare |
| 710 | I_Size : constant Unsigned_64 := Inode_Size ( |
| 711 | State.Block_Cache (Cache_Start .. Cache_End), Inode_In_Block, State.Inode.Mode); |
| 712 | begin |
| 713 | if I_Size <= Unsigned_64 (Inode_Length'Last) then |
| 714 | State.Inode.Size := Inode_Length (I_Size); |
| 715 | else |
| 716 | Success := False; |
| 717 | end if; |
| 718 | end; |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 719 | State.Inode.Use_Extents := State.Feature_Extents and (I_Flags and EXT4_EXTENTS_FL) /= 0; |
Nico Huber | 7403a54 | 2023-12-15 23:13:47 +0100 | [diff] [blame] | 720 | State.Inode.Inline := State.Block_Cache (Cache_Start + 40 .. Cache_Start + 100 - 1); |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 721 | State.Inode.I := Inode; |
Nico Huber | cdc0351 | 2023-12-13 23:32:54 +0100 | [diff] [blame] | 722 | Reset_Cache_Logical (State); |
| 723 | State.S := File_Opened; |
| 724 | end; |
| 725 | end; |
| 726 | end Open; |
| 727 | |
| 728 | procedure Open |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 729 | (State : in out T; |
| 730 | File_Len : out File_Length; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 731 | File_Type : out FS.File_Type; |
| 732 | File_Name : in String; |
| 733 | In_Root : in Boolean; |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 734 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 735 | is |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 736 | File_Name_Max : constant := 255; |
| 737 | Root_Inode : constant := 2; |
| 738 | |
| 739 | function Str_Buf_Equal (Str : String; Buf : Buffer_Type) return Boolean is |
| 740 | begin |
| 741 | for I in Str'Range loop |
| 742 | if Character'Pos (Str (I)) /= Buf (Buf'First + (I - Str'First)) then |
| 743 | return False; |
| 744 | end if; |
| 745 | end loop; |
| 746 | return True; |
| 747 | end Str_Buf_Equal; |
| 748 | |
| 749 | File_Inode : Inode_Index; |
| 750 | File_Pos : File_Length; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 751 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 752 | File_Len := 0; |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 753 | File_Type := FS.File_Type'First; |
| 754 | |
| 755 | if File_Name'Length > File_Name_Max then |
| 756 | Success := False; |
| 757 | return; |
| 758 | end if; |
| 759 | |
| 760 | -- Ensure dir is opened: |
| 761 | -- |
| 762 | if State.S = File_Opened then |
| 763 | if State.Cur_Dir /= State.Inode.I then |
| 764 | Success := False; |
| 765 | return; |
| 766 | end if; |
| 767 | else |
| 768 | if In_Root then |
| 769 | State.Cur_Dir := Root_Inode; |
| 770 | end if; |
| 771 | Open (State, State.Cur_Dir, Success); |
| 772 | if not Success then |
| 773 | return; |
| 774 | end if; |
| 775 | end if; |
| 776 | |
| 777 | -- Lookup file in opened dir: |
| 778 | -- |
| 779 | File_Pos := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 780 | Success := False; |
Nico Huber | 21d4202 | 2023-12-16 02:50:22 +0100 | [diff] [blame] | 781 | while Unsigned_64 (File_Pos) < Unsigned_64 (State.Inode.Size) loop |
| 782 | declare |
| 783 | Dir_Entry_Header_Length : constant := 4 + 2 + 1 + 1; |
| 784 | subtype Dir_Entry_Index is Natural |
| 785 | range 0 .. Dir_Entry_Header_Length + File_Name_Max - 1; |
| 786 | Dir_Entry : Buffer_Type (Dir_Entry_Index); |
| 787 | Entry_File_Pos : File_Offset := File_Pos; |
| 788 | Len : Natural; |
| 789 | begin |
| 790 | Read |
| 791 | (State => State, |
| 792 | File_Pos => Entry_File_Pos, |
| 793 | Buf => Dir_Entry (0 .. 7), |
| 794 | Len => Len); |
| 795 | if Len < Dir_Entry_Header_Length then |
| 796 | return; |
| 797 | end if; |
| 798 | |
| 799 | -- Only check filenames of exact same length |
| 800 | if Read_LE32 (Dir_Entry, 0) > Root_Inode and then |
| 801 | File_Name'Length = Natural (Dir_Entry (6)) |
| 802 | then |
| 803 | Read |
| 804 | (State => State, |
| 805 | File_Pos => Entry_File_Pos, |
| 806 | Buf => Dir_Entry (8 .. 8 + File_Name'Length - 1), |
| 807 | Len => Len); |
| 808 | if Len < File_Name'Length then |
| 809 | return; |
| 810 | end if; |
| 811 | |
| 812 | File_Inode := Inode_Index (Read_LE32 (Dir_Entry, 0)); |
| 813 | Success := Str_Buf_Equal (File_Name, Dir_Entry (8 .. 8 + File_Name'Length - 1)); |
| 814 | exit when Success; |
| 815 | end if; |
| 816 | |
| 817 | File_Pos := File_Pos + File_Length (Read_LE16 (Dir_Entry, 4)); |
| 818 | end; |
| 819 | end loop; |
| 820 | |
| 821 | if Success then |
| 822 | Open (State, File_Inode, Success); |
| 823 | if not Success then |
| 824 | return; |
| 825 | end if; |
| 826 | |
| 827 | if State.Inode.Mode = Dir then |
| 828 | State.Cur_Dir := File_Inode; |
| 829 | end if; |
| 830 | end if; |
| 831 | |
| 832 | Success := Unsigned_64 (State.Inode.Size) <= Unsigned_64 (File_Length'Last); |
| 833 | if Success then |
| 834 | File_Len := File_Length (State.Inode.Size); |
| 835 | File_Type := (case State.Inode.Mode is |
| 836 | when Dir => FS.Dir, |
| 837 | when Regular => FS.Regular, |
| 838 | when Link .. Fast_Link => FS.Link); |
| 839 | else |
| 840 | Close (State); |
| 841 | end if; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 842 | end Open; |
| 843 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 844 | procedure Close (State : in out T) is |
| 845 | begin |
| 846 | State.S := Mounted; |
| 847 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 848 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 849 | procedure Read |
| 850 | (State : in out T; |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 851 | File_Pos : in out File_Offset; |
| 852 | Buf : out Buffer_Type; |
| 853 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 854 | is |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 855 | Pos : Natural; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 856 | begin |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 857 | if State.Inode.Mode = Fast_Link then |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 858 | if State.Inode.Size > Inode_Length (State.Inode.Inline'Length) or |
| 859 | Inode_Length (File_Pos) >= State.Inode.Size then |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 860 | Len := 0; |
| 861 | else |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 862 | Len := Natural'Min (Buf'Length, Natural (State.Inode.Size) - Natural (File_Pos)); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 863 | end if; |
| 864 | Buf (Buf'First .. Buf'First + Len - 1) := |
| 865 | State.Inode.Inline (Natural (File_Pos) .. Natural (File_Pos) + Len - 1); |
| 866 | Buf (Buf'First + Len .. Buf'Last) := (others => 16#00#); |
| 867 | File_Pos := File_Pos + File_Length (Len); |
| 868 | return; |
| 869 | end if; |
| 870 | |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 871 | Len := 0; |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 872 | Pos := Buf'First; |
| 873 | while Pos <= Buf'Last and Inode_Length (File_Pos) < State.Inode.Size loop |
| 874 | declare |
Nico Huber | 700a411 | 2024-01-08 15:50:09 +0100 | [diff] [blame^] | 875 | In_Block : constant Max_Block_Index := Natural (File_Pos) mod State.Block_Size; |
| 876 | Logical : constant FSBlock_Logical := FSBlock_Logical (File_Pos / File_Offset (State.Block_Size)); |
| 877 | In_Block_Space : constant Natural := Natural (State.Block_Size) - In_Block; |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 878 | In_File_Space : constant Inode_Length := State.Inode.Size - Inode_Length (File_Pos); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 879 | In_Buf_Space : constant Natural := Buf'Last - Pos + 1; |
| 880 | Len_Here : Natural; |
| 881 | begin |
| 882 | Len_Here := In_Block_Space; |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 883 | if In_File_Space < Inode_Length (Len_Here) then |
| 884 | Len_Here := Natural (In_File_Space); |
Nico Huber | d3644be | 2023-12-16 01:43:00 +0100 | [diff] [blame] | 885 | end if; |
| 886 | if In_Buf_Space < Len_Here then |
| 887 | Len_Here := In_Buf_Space; |
| 888 | end if; |
| 889 | |
| 890 | declare |
| 891 | Last : constant Index_Type := Pos + Len_Here - 1; |
| 892 | Cache_Start, Cache_End : Max_Block_Index; |
| 893 | Physical : FSBlock_Offset; |
| 894 | Success : Boolean; |
| 895 | begin |
| 896 | if State.Inode.Use_Extents then |
| 897 | Extent_Block_Map (State, Logical, Physical, Success); |
| 898 | else |
| 899 | Ext2_Block_Map (State, Logical, Physical, Success); |
| 900 | end if; |
| 901 | if Success then |
| 902 | Cache_FSBlock |
| 903 | (State => State, |
| 904 | Phys => Physical, |
| 905 | Level => Block_Cache_Index'Last, |
| 906 | Cache_Start => Cache_Start, |
| 907 | Cache_End => Cache_End, |
| 908 | Success => Success); |
| 909 | end if; |
| 910 | exit when not Success; |
| 911 | |
| 912 | Buf (Pos .. Last) := State.Block_Cache ( |
| 913 | Cache_Start + In_Block .. Cache_Start + In_Block + Len_Here - 1); |
| 914 | File_Pos := File_Pos + File_Length (Len_Here); |
| 915 | Pos := Pos + Len_Here; |
| 916 | Len := Len + Len_Here; |
| 917 | end; |
| 918 | end; |
| 919 | end loop; |
| 920 | Buf (Pos .. Buf'Last) := (others => 16#00#); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 921 | end Read; |
| 922 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 923 | -------------------------------------------------------------------------- |
| 924 | |
| 925 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 926 | |
| 927 | function C_Mount return int |
| 928 | with |
| 929 | Export, |
| 930 | Convention => C, |
| 931 | External_Name => "ext2fs_mount"; |
| 932 | function C_Mount return int |
| 933 | with |
| 934 | SPARK_Mode => Off |
| 935 | is |
| 936 | begin |
| 937 | return C.C_Mount; |
| 938 | end C_Mount; |
| 939 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 940 | function C_Open (File_Path : System.Address) return int |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 941 | with |
| 942 | Export, |
| 943 | Convention => C, |
| 944 | External_Name => "ext2fs_dir"; |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 945 | function C_Open (File_Path : System.Address) return int |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 946 | with |
| 947 | SPARK_Mode => Off |
| 948 | is |
| 949 | begin |
| 950 | return C.C_Open (File_Path); |
| 951 | end C_Open; |
| 952 | |
| 953 | function C_Read (Buf : System.Address; Len : int) return int |
| 954 | with |
| 955 | Export, |
| 956 | Convention => C, |
| 957 | External_Name => "ext2fs_read"; |
| 958 | function C_Read (Buf : System.Address; Len : int) return int |
| 959 | with |
| 960 | SPARK_Mode => Off |
| 961 | is |
| 962 | begin |
| 963 | return C.C_Read (Buf, Len); |
| 964 | end C_Read; |
| 965 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 966 | end FILO.FS.Ext2; |