blob: af6db7077848409687a01ecb0a1c5915919669af [file] [log] [blame]
Nico Huber26f71832023-12-05 16:26:56 +01001-- 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 Huber8ec45a12023-12-04 17:11:08 +010010with System;
Nico Huber1d7727f2023-11-30 15:58:46 +010011with Interfaces;
Nico Huber8ec45a12023-12-04 17:11:08 +010012with Interfaces.C;
13with Interfaces.C.Strings;
Nico Huber1d7727f2023-11-30 15:58:46 +010014
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000015with FILO.Blockdev;
16with FILO.FS.VFS;
Nico Huber8ec45a12023-12-04 17:11:08 +010017
18use Interfaces.C;
Nico Huber1d7727f2023-11-30 15:58:46 +010019
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000020package body FILO.FS.Ext2 is
Nico Huber1d7727f2023-11-30 15:58:46 +010021
Nico Huber57d3a852023-12-04 15:42:40 +010022 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 Huber26f71832023-12-05 16:26:56 +010025 --------------------------------------------------------------------------
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 Huberf5d99d02023-12-12 13:42:55 +010036 -- Minimum ext2 block size is 1KiB (two 512B blocks)
37 type FSBlock_Offset is new Block_Offset range 0 .. Block_Offset'Last / 2;
38
Nico Huber57d3a852023-12-04 15:42:40 +010039 procedure Mount
40 (State : in out T;
41 Part_Len : in Partition_Length;
42 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +010043 is
Nico Huber26f71832023-12-05 16:26:56 +010044 Super_Block : Buffer_Type (0 .. SUPERBLOCK_SIZE - 1) := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +010045 begin
Nico Huber26f71832023-12-05 16:26:56 +010046 if Part_Len < 2 * SUPERBLOCK_SIZE then
47 Success := False;
48 return;
49 end if;
50
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000051 Blockdev.Read (Super_Block, 1 * SUPERBLOCK_SIZE, Success);
Nico Huber26f71832023-12-05 16:26:56 +010052 if not Success then
53 return;
54 end if;
55
56 if Read_LE16 (Super_Block, 14 * 4) /= 16#ef53# then
57 Success := False;
58 return;
59 end if;
60
61 State.Part_Len := Part_Len;
62 State.First_Data_Block := Block_Offset (Read_LE32 (Super_Block, 5 * 4));
63
64 declare
65 S_Log_Block_Size : constant Unsigned_32 := Read_LE32 (Super_Block, 6 * 4);
66 begin
67 if S_Log_Block_Size <= Unsigned_32 (Log_Block_Size'Last - 10) then
68 State.Block_Size_Bits := Log_Block_Size (S_Log_Block_Size + 10);
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
78 if S_Inodes_Per_Group in 1 .. Unsigned_32 (Positive'Last) then
79 State.Inodes_Per_Group := Positive (S_Inodes_Per_Group);
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
93 if S_Inode_Size in
94 Unsigned_16 (Inode_Size'First) .. Unsigned_16 (Inode_Size'Last)
95 then
96 State.Inode_Size := Inode_Size (S_Inode_Size);
97 else
98 Success := False;
99 return;
100 end if;
101 end;
102 else
103 State.Inode_Size := Inode_Size'First;
104 end if;
105 end;
106
107 declare
108 S_Feature_Incompat : constant Unsigned_32 := Read_LE32 (Super_Block, 24 * 4);
109 begin
110 State.Feature_Extents := (S_Feature_Incompat and FEATURE_INCOMPAT_EXTENTS) /= 0;
111 State.Feature_64Bit := (S_Feature_Incompat and FEATURE_INCOMPAT_64BIT) /= 0;
112 if State.Feature_64Bit then
113 declare
114 S_Desc_Size : constant Unsigned_16 := Read_LE16 (Super_Block, 63 * 4 + 2);
115 begin
116 if S_Desc_Size in
117 Unsigned_16 (Desc_Size'First) .. Unsigned_16 (Desc_Size'Last)
118 then
119 State.Desc_Size := Desc_Size (S_Desc_Size);
120 else
121 Success := False;
122 return;
123 end if;
124 end;
125 else
126 State.Desc_Size := Desc_Size'First;
127 end if;
128 end;
129
130 State.S := Mounted;
Nico Huber1d7727f2023-11-30 15:58:46 +0100131 end Mount;
132
Nico Huberf5d99d02023-12-12 13:42:55 +0100133 procedure Read_FSBlock
134 (State : in T;
135 Buf : out Buffer_Type;
136 FSBlock : in FSBlock_Offset;
137 Success : out Boolean)
138 with
139 Pre =>
140 Is_Mounted (State) and
141 Buf'Length = 2 ** State.Block_Size_Bits
142 is
143 Block_Size : constant Blockdev_Length := 2 ** State.Block_Size_Bits;
144 Max_Block_Offset : constant FSBlock_Offset :=
145 FSBlock_Offset (State.Part_Len / Block_Size - 1);
146 begin
147 if FSBlock > Max_Block_Offset then
148 Success := False;
149 return;
150 end if;
151 Blockdev.Read (Buf, Blockdev_Length (FSBlock) * Block_Size, Success);
152 end Read_FSBlock;
153
Nico Huber57d3a852023-12-04 15:42:40 +0100154 procedure Open
155 (State : in out T;
156 File_Len : out File_Length;
157 File_Path : in String;
158 Success : out Boolean)
Nico Huber1d7727f2023-11-30 15:58:46 +0100159 is
160 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100161 File_Len := 0;
Nico Huber1d7727f2023-11-30 15:58:46 +0100162 Success := False;
163 end Open;
164
Nico Huber57d3a852023-12-04 15:42:40 +0100165 procedure Close (State : in out T) is
166 begin
167 State.S := Mounted;
168 end Close;
Nico Huber1d7727f2023-11-30 15:58:46 +0100169
Nico Huber57d3a852023-12-04 15:42:40 +0100170 procedure Read
171 (State : in out T;
172 File_Len : in File_Length;
173 File_Pos : in out File_Offset;
174 Buf : out Buffer_Type;
175 Len : out Natural)
Nico Huber1d7727f2023-11-30 15:58:46 +0100176 is
177 begin
Nico Huber57d3a852023-12-04 15:42:40 +0100178 Buf := (others => 0);
Nico Huber1d7727f2023-11-30 15:58:46 +0100179 Len := 0;
180 end Read;
181
Nico Huber26f71832023-12-05 16:26:56 +0100182 --------------------------------------------------------------------------
183
184 package C is new VFS (T => T, Initial => (S => Unmounted, others => <>));
Nico Huber8ec45a12023-12-04 17:11:08 +0100185
186 function C_Mount return int
187 with
188 Export,
189 Convention => C,
190 External_Name => "ext2fs_mount";
191 function C_Mount return int
192 with
193 SPARK_Mode => Off
194 is
195 begin
196 return C.C_Mount;
197 end C_Mount;
198
199 function C_Open (File_Path : Strings.chars_ptr) return int
200 with
201 Export,
202 Convention => C,
203 External_Name => "ext2fs_dir";
204 function C_Open (File_Path : Strings.chars_ptr) return int
205 with
206 SPARK_Mode => Off
207 is
208 begin
209 return C.C_Open (File_Path);
210 end C_Open;
211
212 function C_Read (Buf : System.Address; Len : int) return int
213 with
214 Export,
215 Convention => C,
216 External_Name => "ext2fs_read";
217 function C_Read (Buf : System.Address; Len : int) return int
218 with
219 SPARK_Mode => Off
220 is
221 begin
222 return C.C_Read (Buf, Len);
223 end C_Read;
224
Thomas Heijligen5c43abc2023-12-11 15:24:36 +0000225end FILO.FS.Ext2;