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