Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 1 | -- Derived from GRUB -- GRand Unified Bootloader |
| 2 | -- Copyright (C) 1999, 2001, 2003 Free Software Foundation, Inc. |
| 3 | -- Copyright (C) 2023 secunet Security Networks AG |
| 4 | -- |
| 5 | -- This program is free software; you can redistribute it and/or modify |
| 6 | -- it under the terms of the GNU General Public License as published by |
| 7 | -- the Free Software Foundation; either version 2 of the License, or |
| 8 | -- (at your option) any later version. |
| 9 | |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 10 | with System; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 11 | with Interfaces; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 12 | with Interfaces.C; |
| 13 | with Interfaces.C.Strings; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 14 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 15 | with FILO.Blockdev; |
| 16 | with FILO.FS.VFS; |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 17 | |
| 18 | use Interfaces.C; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 19 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 20 | package body FILO.FS.Ext2 is |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 21 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 22 | function Is_Mounted (State : T) return Boolean is (State.S >= Mounted); |
| 23 | function Is_Open (State : T) return Boolean is (State.S = File_Opened); |
| 24 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 25 | -------------------------------------------------------------------------- |
| 26 | |
| 27 | SUPERBLOCK_SIZE : constant := 1024; |
| 28 | SUPERBLOCK_BLOCKS : constant := SUPERBLOCK_SIZE / BLOCK_SIZE; |
| 29 | |
| 30 | SUPERBLOCK_MAGIC : constant := 16#ef53#; |
| 31 | OLD_REV : constant := 0; |
| 32 | DYNAMIC_REV : constant := 1; |
| 33 | FEATURE_INCOMPAT_EXTENTS : constant := 16#0040#; |
| 34 | FEATURE_INCOMPAT_64BIT : constant := 16#0080#; |
| 35 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 36 | procedure Mount |
| 37 | (State : in out T; |
| 38 | Part_Len : in Partition_Length; |
| 39 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 40 | is |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 41 | Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 42 | begin |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 43 | if Part_Len < 2 * SUPERBLOCK_SIZE then |
| 44 | Success := False; |
| 45 | return; |
| 46 | end if; |
| 47 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 48 | Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success); |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 49 | if not Success then |
| 50 | return; |
| 51 | end if; |
| 52 | |
| 53 | if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then |
| 54 | Success := False; |
| 55 | return; |
| 56 | end if; |
| 57 | |
| 58 | State.Part_Len := Part_Len; |
| 59 | State.First_Data_Block := Block_Offset (Read_LE32 (Super_Block, 5 * 4)); |
| 60 | |
| 61 | declare |
| 62 | S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4); |
| 63 | begin |
| 64 | if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then |
| 65 | State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10); |
| 66 | else |
| 67 | Success := False; |
| 68 | return; |
| 69 | end if; |
| 70 | end; |
| 71 | |
| 72 | declare |
| 73 | S_Inodes_Per_Group : constant Unsigned_32 := Read_LE32 (Super_Block, 10 * 4); |
| 74 | begin |
| 75 | if S_Inodes_Per_Group in 1 .. Unsigned_32 (Positive'Last) then |
| 76 | State.Inodes_Per_Group := Positive (S_Inodes_Per_Group); |
| 77 | else |
| 78 | Success := False; |
| 79 | return; |
| 80 | end if; |
| 81 | end; |
| 82 | |
| 83 | declare |
| 84 | S_Rev_Level : constant Unsigned_32 := Read_LE32 (Super_Block, 19 * 4); |
| 85 | begin |
| 86 | if S_Rev_Level >= DYNAMIC_REV then |
| 87 | declare |
| 88 | S_Inode_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 22 * 4); |
| 89 | begin |
| 90 | if S_Inode_Size in |
| 91 | Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last) |
| 92 | then |
| 93 | State.Inode_Size := Inode_Size (S_Inode_Size); |
| 94 | else |
| 95 | Success := False; |
| 96 | return; |
| 97 | end if; |
| 98 | end; |
| 99 | else |
| 100 | State.Inode_Size := Inode_Size'First; |
| 101 | end if; |
| 102 | end; |
| 103 | |
| 104 | declare |
| 105 | S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4); |
| 106 | begin |
| 107 | State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0; |
| 108 | State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0; |
| 109 | if State.Feature_64Bit then |
| 110 | declare |
| 111 | S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2); |
| 112 | begin |
| 113 | if S_Desc_Size in |
| 114 | Unsigned_16 (Desc_Size'First) .. Unsigned_16 (Desc_Size'Last) |
| 115 | then |
| 116 | State.Desc_Size := Desc_Size (S_Desc_Size); |
| 117 | else |
| 118 | Success := False; |
| 119 | return; |
| 120 | end if; |
| 121 | end; |
| 122 | else |
| 123 | State.Desc_Size := Desc_Size'First; |
| 124 | end if; |
| 125 | end; |
| 126 | |
| 127 | State.S := Mounted; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 128 | end Mount; |
| 129 | |
Nico Huber | f5d99d0 | 2023-12-12 13:42:55 +0100 | [diff] [blame] | 130 | procedure Read_FSBlock |
| 131 | (State : in T; |
| 132 | Buf : out Buffer_Type; |
| 133 | FSBlock : in FSBlock_Offset; |
| 134 | Success : out Boolean) |
| 135 | with |
| 136 | Pre => |
| 137 | Is_Mounted (State) and |
| 138 | Buf'Length = 2 ** State.Block_Size_Bits |
| 139 | is |
| 140 | Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits; |
| 141 | Max_Block_Offset : constant FSBlock_Offset := |
| 142 | FSBlock_Offset (State.Part_Len / Block_Size - 1); |
| 143 | begin |
| 144 | if FSBlock > Max_Block_Offset then |
| 145 | Success := False; |
| 146 | return; |
| 147 | end if; |
| 148 | Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success); |
| 149 | end Read_FSBlock; |
| 150 | |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 151 | procedure Ext2_Block_Map |
| 152 | (State : in out T; |
| 153 | Logical : in FSBlock_Logical; |
| 154 | Physical : out FSBlock_Offset; |
| 155 | Success : out Boolean) |
| 156 | is |
| 157 | Block_Size : constant Natural := 2 ** State.Block_Size_Bits; |
| 158 | Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (Block_Size / 4); |
| 159 | Max_Addr_Per_Block : constant FSBlock_Logical := FSBlock_Logical (2 ** Log_Block_Size'Last / 4); |
| 160 | type Addr_In_Block_Range is range 0 .. Max_Addr_Per_Block - 1; |
| 161 | |
| 162 | procedure Indirect_Block_Lookup |
| 163 | (Indirect_Block_Phys : in FSBlock_Offset; |
| 164 | Addr_In_Block : in Addr_In_Block_Range; |
| 165 | Level : in Block_Cache_Index; |
| 166 | Logical_Off : in FSBlock_Logical; |
| 167 | Next_Physical : out FSBlock_Offset; |
| 168 | Success : out Boolean) |
| 169 | with |
| 170 | Pre => FSBlock_Logical (Addr_In_Block) < Addr_Per_Block |
| 171 | is |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 172 | -- Limit cache usage depending on block size: |
| 173 | Max_Level : constant Block_Cache_Index := |
| 174 | 2 ** (Log_Block_Size'Last - State.Block_Size_Bits) - 1; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 175 | Cache_Level : constant Block_Cache_Index := |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 176 | Block_Cache_Index'Min (Level, Max_Level); |
| 177 | Cache_Start : constant Index_Type := Cache_Level * Block_Size; |
| 178 | Cache_End : constant Index_Type := Cache_Start + Block_Size - 1; |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 179 | begin |
| 180 | if State.Block_Cache_Index (Cache_Level) /= Logical_Off then |
| 181 | Read_FSBlock |
| 182 | (State => State, |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 183 | Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 184 | FSBlock => Indirect_Block_Phys, |
| 185 | Success => Success); |
| 186 | State.Block_Cache_Index (Cache_Level) := Logical_Off; |
| 187 | if not Success then |
| 188 | return; |
| 189 | end if; |
| 190 | end if; |
| 191 | |
| 192 | Next_Physical := FSBlock_Offset (Read_LE32 |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 193 | (Buf => State.Block_Cache (Cache_Start .. Cache_End), |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 194 | Off => Natural (Addr_In_Block) * 4)); |
| 195 | Success := True; |
| 196 | end Indirect_Block_Lookup; |
| 197 | |
| 198 | Logical_Rest : FSBlock_Logical := Logical; |
| 199 | begin |
| 200 | if Logical_Rest < Direct_Blocks then |
| 201 | Physical := FSBlock_Offset (State.Direct_Blocks (Natural (Logical))); |
| 202 | Success := True; |
| 203 | return; |
| 204 | end if; |
| 205 | |
| 206 | Logical_Rest := Logical_Rest - Direct_Blocks; |
| 207 | if Logical_Rest < Addr_Per_Block then |
| 208 | Indirect_Block_Lookup |
| 209 | (Indirect_Block_Phys => FSBlock_Offset (State.Indirect_Block), |
| 210 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 211 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 212 | Logical_Off => Logical - Logical_Rest, |
| 213 | Next_Physical => Physical, |
| 214 | Success => Success); |
| 215 | return; |
| 216 | end if; |
| 217 | |
| 218 | Logical_Rest := Logical_Rest - Addr_Per_Block; |
| 219 | if Logical_Rest < Addr_Per_Block ** 2 then |
| 220 | Indirect_Block_Lookup |
| 221 | (Indirect_Block_Phys => FSBlock_Offset (State.Double_Indirect), |
| 222 | Addr_In_Block => Addr_In_Block_Range (Logical_Rest / Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 223 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 224 | Logical_Off => Logical - Logical_Rest, |
| 225 | Next_Physical => Physical, |
| 226 | Success => Success); |
| 227 | if not Success then |
| 228 | return; |
| 229 | end if; |
| 230 | |
| 231 | Indirect_Block_Lookup |
| 232 | (Indirect_Block_Phys => Physical, |
| 233 | 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^] | 234 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 235 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 236 | Next_Physical => Physical, |
| 237 | Success => Success); |
| 238 | return; |
| 239 | end if; |
| 240 | |
| 241 | Logical_Rest := Logical_Rest - Addr_Per_Block ** 2; |
| 242 | if Logical_Rest < Addr_Per_Block ** 3 then |
| 243 | Indirect_Block_Lookup |
| 244 | (Indirect_Block_Phys => FSBlock_Offset (State.Triple_Indirect), |
| 245 | 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^] | 246 | Level => 2, |
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 / Addr_Per_Block mod Addr_Per_Block), |
Nico Huber | fe89712 | 2023-12-12 21:33:36 +0100 | [diff] [blame^] | 257 | Level => 1, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 258 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block ** 2), |
| 259 | Next_Physical => Physical, |
| 260 | Success => Success); |
| 261 | if not Success then |
| 262 | return; |
| 263 | end if; |
| 264 | |
| 265 | Indirect_Block_Lookup |
| 266 | (Indirect_Block_Phys => Physical, |
| 267 | 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^] | 268 | Level => 0, |
Nico Huber | 6623c98 | 2023-12-12 16:35:46 +0100 | [diff] [blame] | 269 | Logical_Off => Logical - (Logical_Rest mod Addr_Per_Block), |
| 270 | Next_Physical => Physical, |
| 271 | Success => Success); |
| 272 | return; |
| 273 | end if; |
| 274 | |
| 275 | -- Logical address was just too high. |
| 276 | Success := False; |
| 277 | end Ext2_Block_Map; |
| 278 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 279 | procedure Open |
| 280 | (State : in out T; |
| 281 | File_Len : out File_Length; |
| 282 | File_Path : in String; |
| 283 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 284 | is |
| 285 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 286 | File_Len := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 287 | Success := False; |
| 288 | end Open; |
| 289 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 290 | procedure Close (State : in out T) is |
| 291 | begin |
| 292 | State.S := Mounted; |
| 293 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 294 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 295 | procedure Read |
| 296 | (State : in out T; |
| 297 | File_Len : in File_Length; |
| 298 | File_Pos : in out File_Offset; |
| 299 | Buf : out Buffer_Type; |
| 300 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 301 | is |
| 302 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 303 | Buf := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 304 | Len := 0; |
| 305 | end Read; |
| 306 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 307 | -------------------------------------------------------------------------- |
| 308 | |
| 309 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 310 | |
| 311 | function C_Mount return int |
| 312 | with |
| 313 | Export, |
| 314 | Convention => C, |
| 315 | External_Name => "ext2fs_mount"; |
| 316 | function C_Mount return int |
| 317 | with |
| 318 | SPARK_Mode => Off |
| 319 | is |
| 320 | begin |
| 321 | return C.C_Mount; |
| 322 | end C_Mount; |
| 323 | |
| 324 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 325 | with |
| 326 | Export, |
| 327 | Convention => C, |
| 328 | External_Name => "ext2fs_dir"; |
| 329 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 330 | with |
| 331 | SPARK_Mode => Off |
| 332 | is |
| 333 | begin |
| 334 | return C.C_Open (File_Path); |
| 335 | end C_Open; |
| 336 | |
| 337 | function C_Read (Buf : System.Address; Len : int) return int |
| 338 | with |
| 339 | Export, |
| 340 | Convention => C, |
| 341 | External_Name => "ext2fs_read"; |
| 342 | function C_Read (Buf : System.Address; Len : int) return int |
| 343 | with |
| 344 | SPARK_Mode => Off |
| 345 | is |
| 346 | begin |
| 347 | return C.C_Read (Buf, Len); |
| 348 | end C_Read; |
| 349 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 350 | end FILO.FS.Ext2; |