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 | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 130 | procedure Open |
| 131 | (State : in out T; |
| 132 | File_Len : out File_Length; |
| 133 | File_Path : in String; |
| 134 | Success : out Boolean) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 135 | is |
| 136 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 137 | File_Len := 0; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 138 | Success := False; |
| 139 | end Open; |
| 140 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 141 | procedure Close (State : in out T) is |
| 142 | begin |
| 143 | State.S := Mounted; |
| 144 | end Close; |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 145 | |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 146 | procedure Read |
| 147 | (State : in out T; |
| 148 | File_Len : in File_Length; |
| 149 | File_Pos : in out File_Offset; |
| 150 | Buf : out Buffer_Type; |
| 151 | Len : out Natural) |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 152 | is |
| 153 | begin |
Nico Huber | 57d3a85 | 2023-12-04 15:42:40 +0100 | [diff] [blame] | 154 | Buf := (others => 0); |
Nico Huber | 1d7727f | 2023-11-30 15:58:46 +0100 | [diff] [blame] | 155 | Len := 0; |
| 156 | end Read; |
| 157 | |
Nico Huber | 26f7183 | 2023-12-05 16:26:56 +0100 | [diff] [blame] | 158 | -------------------------------------------------------------------------- |
| 159 | |
| 160 | package C is new VFS (T => T, Initial => (S => Unmounted, others => <>)); |
Nico Huber | 8ec45a1 | 2023-12-04 17:11:08 +0100 | [diff] [blame] | 161 | |
| 162 | function C_Mount return int |
| 163 | with |
| 164 | Export, |
| 165 | Convention => C, |
| 166 | External_Name => "ext2fs_mount"; |
| 167 | function C_Mount return int |
| 168 | with |
| 169 | SPARK_Mode => Off |
| 170 | is |
| 171 | begin |
| 172 | return C.C_Mount; |
| 173 | end C_Mount; |
| 174 | |
| 175 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 176 | with |
| 177 | Export, |
| 178 | Convention => C, |
| 179 | External_Name => "ext2fs_dir"; |
| 180 | function C_Open (File_Path : Strings.chars_ptr) return int |
| 181 | with |
| 182 | SPARK_Mode => Off |
| 183 | is |
| 184 | begin |
| 185 | return C.C_Open (File_Path); |
| 186 | end C_Open; |
| 187 | |
| 188 | function C_Read (Buf : System.Address; Len : int) return int |
| 189 | with |
| 190 | Export, |
| 191 | Convention => C, |
| 192 | External_Name => "ext2fs_read"; |
| 193 | function C_Read (Buf : System.Address; Len : int) return int |
| 194 | with |
| 195 | SPARK_Mode => Off |
| 196 | is |
| 197 | begin |
| 198 | return C.C_Read (Buf, Len); |
| 199 | end C_Read; |
| 200 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 201 | end FILO.FS.Ext2; |