6 * - Storage Driver Helper Functions
10 #include <api_drv_disk.h>
12 // --- Disk Driver Helpers ---
13 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
14 tDrvUtil_Read_Callback ReadBlocks, Uint64 BlockSize, void *Argument)
16 Uint8 tmp[BlockSize]; // C99
17 Uint64 block = Start / BlockSize;
18 int offset = Start - block * BlockSize;
19 int leading = BlockSize - offset;
24 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize pArgument",
25 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
27 // Non aligned start, let's fix that!
30 if(leading > Length) leading = Length;
31 LOG("Reading %i bytes from Block1+%i", leading, offset);
32 ret = ReadBlocks(block, 1, tmp, Argument);
37 memcpy( Buffer, &tmp[offset], leading );
39 if(leading == Length) {
44 Buffer = (Uint8*)Buffer + leading;
46 num = ( Length - leading ) / BlockSize;
47 tailings = Length - num * BlockSize - leading;
50 num = Length / BlockSize;
51 tailings = Length % BlockSize;
54 // Read central blocks
57 LOG("Reading %i blocks", num);
58 ret = ReadBlocks(block, num, Buffer, Argument);
60 LEAVE('X', leading + ret * BlockSize);
61 return leading + ret * BlockSize;
65 // Read last tailing block
68 LOG("Reading %i bytes from last block", tailings);
70 Buffer = (Uint8*)Buffer + num * BlockSize;
71 ret = ReadBlocks(block, 1, tmp, Argument);
73 LEAVE('X', leading + num * BlockSize);
74 return leading + num * BlockSize;
76 memcpy( Buffer, tmp, tailings );
83 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, const void *Buffer,
84 tDrvUtil_Read_Callback ReadBlocks, tDrvUtil_Write_Callback WriteBlocks,
85 Uint64 BlockSize, void *Argument)
87 Uint8 tmp[BlockSize]; // C99
88 Uint64 block = Start / BlockSize;
89 int offset = Start - block * BlockSize;
90 int leading = BlockSize - offset;
95 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize pArgument",
96 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
98 // Non aligned start, let's fix that!
101 if(leading > Length) leading = Length;
102 LOG("Writing %i bytes to Block1+%i", leading, offset);
103 // Read a copy of the block
104 ret = ReadBlocks(block, 1, tmp, Argument);
110 memcpy( &tmp[offset], Buffer, leading );
112 ret = WriteBlocks(block, 1, tmp, Argument);
118 if(leading == Length) {
123 Buffer = (Uint8*)Buffer + leading;
125 num = ( Length - leading ) / BlockSize;
126 tailings = Length - num * BlockSize - leading;
129 num = Length / BlockSize;
130 tailings = Length % BlockSize;
133 // Read central blocks
136 LOG("Writing %i blocks", num);
137 ret = WriteBlocks(block, num, Buffer, Argument);
139 LEAVE('X', leading + ret * BlockSize);
140 return leading + ret * BlockSize;
144 // Read last tailing block
147 LOG("Writing %i bytes to last block", tailings);
149 Buffer = (Uint8*)Buffer + num * BlockSize;
151 ret = ReadBlocks(block, 1, tmp, Argument);
153 LEAVE('X', leading + num * BlockSize);
154 return leading + num * BlockSize;
157 memcpy( tmp, Buffer, tailings );
159 ret = WriteBlocks(block, 1, tmp, Argument);
161 LEAVE('X', leading + num * BlockSize);
162 return leading + num * BlockSize;