Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 1 | package body FILO.Blockdev is |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 2 | |
| 3 | procedure Read |
Thomas Heijligen | 68ed36e | 2024-01-22 11:43:54 +0000 | [diff] [blame] | 4 | (Buffer : out Buffer_Type; |
Nico Huber | 9c04187 | 2023-12-12 13:24:55 +0100 | [diff] [blame] | 5 | Offset : in Blockdev_Offset; |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 6 | Success : out Boolean) |
Nico Huber | 1f3825e | 2023-12-14 00:05:20 +0100 | [diff] [blame] | 7 | with |
| 8 | SPARK_Mode => Off |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 9 | is |
Thomas Heijligen | a968f6f | 2023-12-11 09:40:06 +0000 | [diff] [blame] | 10 | use Interfaces.C; |
| 11 | |
Nico Huber | 7f61349 | 2023-12-12 13:32:06 +0100 | [diff] [blame] | 12 | Sector : constant unsigned_long := unsigned_long (Offset / BLOCK_SIZE); |
| 13 | Byte_Offset : constant unsigned_long := unsigned_long (Offset rem BLOCK_SIZE); |
Thomas Heijligen | a968f6f | 2023-12-11 09:40:06 +0000 | [diff] [blame] | 14 | Byte_Len : constant unsigned_long := unsigned_long (Buffer'Length); |
| 15 | Buf : constant System.Address := Buffer'Address; |
| 16 | |
| 17 | -- NOTE: C_devread returns 1 on success |
| 18 | function To_Success (Item : Interfaces.C.int) return Boolean is |
| 19 | (if Item = 1 then True else False); |
| 20 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 21 | begin |
Nico Huber | a529660 | 2023-12-12 13:30:31 +0100 | [diff] [blame] | 22 | -- With 32-bit longs, the current C interface |
| 23 | -- can't access more than 512 * 2 ** 32: |
Nico Huber | 6fb3854 | 2024-01-26 23:53:17 +0100 | [diff] [blame] | 24 | pragma Warnings (GNAT, Off, "condition can only be True if"); |
| 25 | pragma Warnings (GNAT, Off, "condition is always False"); |
Nico Huber | 2217322 | 2023-12-14 00:02:03 +0100 | [diff] [blame] | 26 | if Unsigned_64 (Offset / BLOCK_SIZE) > Unsigned_64 (unsigned_long'last) then |
Nico Huber | a529660 | 2023-12-12 13:30:31 +0100 | [diff] [blame] | 27 | Success := False; |
| 28 | return; |
| 29 | end if; |
Thomas Heijligen | a968f6f | 2023-12-11 09:40:06 +0000 | [diff] [blame] | 30 | Success := To_Success (C_devread (Sector, Byte_Offset, Byte_Len, Buf)); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 31 | end Read; |
| 32 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 33 | end FILO.Blockdev; |