3 * Common Driver/Filesystem Helper Functions
7 #include <tpl_drv_disk.h>
10 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
11 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
13 Uint8 tmp[BlockSize]; // C99
14 Uint64 block = Start / BlockSize;
15 int offset = Start - block * BlockSize;
16 int leading = BlockSize - offset;
21 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
22 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
24 // Non aligned start, let's fix that!
27 if(leading > Length) leading = Length;
28 LOG("Reading %i bytes from Block1+%i", leading, offset);
29 ret = ReadBlocks(block, 1, tmp, Argument);
34 memcpy( Buffer, &tmp[offset], leading );
36 if(leading == Length) {
43 num = ( Length - leading ) / BlockSize;
44 tailings = Length - num * BlockSize - leading;
47 num = Length / BlockSize;
48 tailings = Length % BlockSize;
51 // Read central blocks
54 LOG("Reading %i blocks", num);
55 ret = ReadBlocks(block, num, Buffer, Argument);
57 LEAVE('X', leading + ret * BlockSize);
58 return leading + ret * BlockSize;
62 // Read last tailing block
65 LOG("Reading %i bytes from last block", tailings);
67 Buffer += num * BlockSize;
68 ret = ReadBlocks(block, 1, tmp, Argument);
70 LEAVE('X', leading + num * BlockSize);
71 return leading + num * BlockSize;
73 memcpy( Buffer, tmp, tailings );
80 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
81 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
82 Uint64 BlockSize, Uint Argument)
84 Uint8 tmp[BlockSize]; // C99
85 Uint64 block = Start / BlockSize;
86 int offset = Start - block * BlockSize;
87 int leading = BlockSize - offset;
92 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
93 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
95 // Non aligned start, let's fix that!
98 if(leading > Length) leading = Length;
99 LOG("Writing %i bytes to Block1+%i", leading, offset);
100 // Read a copy of the block
101 ret = ReadBlocks(block, 1, tmp, Argument);
107 memcpy( &tmp[offset], Buffer, leading );
109 ret = WriteBlocks(block, 1, tmp, Argument);
115 if(leading == Length) {
122 num = ( Length - leading ) / BlockSize;
123 tailings = Length - num * BlockSize - leading;
126 num = Length / BlockSize;
127 tailings = Length % BlockSize;
130 // Read central blocks
133 LOG("Writing %i blocks", num);
134 ret = WriteBlocks(block, num, Buffer, Argument);
136 LEAVE('X', leading + ret * BlockSize);
137 return leading + ret * BlockSize;
141 // Read last tailing block
144 LOG("Writing %i bytes to last block", tailings);
146 Buffer += num * BlockSize;
148 ret = ReadBlocks(block, 1, tmp, Argument);
150 LEAVE('X', leading + num * BlockSize);
151 return leading + num * BlockSize;
154 memcpy( tmp, Buffer, tailings );
156 ret = WriteBlocks(block, 1, tmp, Argument);
158 LEAVE('X', leading + num * BlockSize);
159 return leading + num * BlockSize;