6 * - Storage Driver Helper Functions
10 #include <api_drv_disk.h>
12 // --- Disk Driver Helpers ---
13 size_t DrvUtil_ReadBlock(Uint64 Start, size_t Length, void *Buffer,
14 tDrvUtil_Read_Callback ReadBlocks, size_t BlockSize, void *Argument)
16 Uint8 tmp[BlockSize]; // C99
17 Uint64 block = Start / BlockSize;
18 int offset = Start - block * BlockSize;
19 size_t 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;
55 // Read central blocks
58 LOG("Reading %i blocks", num);
59 ret = ReadBlocks(block, num, Buffer, Argument);
61 LOG("Incomplete read (%i != %i)", ret, num);
62 LEAVE('X', leading + ret * BlockSize);
63 return leading + ret * BlockSize;
67 // Read last tailing block
70 LOG("Reading %i bytes from last block", tailings);
72 Buffer = (Uint8*)Buffer + num * BlockSize;
73 ret = ReadBlocks(block, 1, tmp, Argument);
75 LEAVE('X', leading + num * BlockSize);
76 return leading + num * BlockSize;
78 memcpy( Buffer, tmp, tailings );
85 size_t DrvUtil_WriteBlock(Uint64 Start, size_t Length, const void *Buffer,
86 tDrvUtil_Read_Callback ReadBlocks, tDrvUtil_Write_Callback WriteBlocks,
87 size_t BlockSize, void *Argument)
89 Uint8 tmp[BlockSize]; // C99
90 Uint64 block = Start / BlockSize;
91 size_t offset = Start - block * BlockSize;
92 size_t leading = BlockSize - offset;
97 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize pArgument",
98 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
100 // Non aligned start, let's fix that!
103 if(leading > Length) leading = Length;
104 LOG("Writing %i bytes to Block1+%i", leading, offset);
105 // Read a copy of the block
106 ret = ReadBlocks(block, 1, tmp, Argument);
112 memcpy( &tmp[offset], Buffer, leading );
114 ret = WriteBlocks(block, 1, tmp, Argument);
120 if(leading == Length) {
125 Buffer = (Uint8*)Buffer + leading;
127 num = ( Length - leading ) / BlockSize;
128 tailings = Length - num * BlockSize - leading;
131 num = Length / BlockSize;
132 tailings = Length % BlockSize;
135 // Read central blocks
138 LOG("Writing %i blocks", num);
139 ret = WriteBlocks(block, num, Buffer, Argument);
141 LEAVE('X', leading + ret * BlockSize);
142 return leading + ret * BlockSize;
146 // Read last tailing block
149 LOG("Writing %i bytes to last block", tailings);
151 Buffer = (Uint8*)Buffer + num * BlockSize;
153 ret = ReadBlocks(block, 1, tmp, Argument);
155 LEAVE('X', leading + num * BlockSize);
156 return leading + num * BlockSize;
159 memcpy( tmp, Buffer, tailings );
161 ret = WriteBlocks(block, 1, tmp, Argument);
163 LEAVE('X', leading + num * BlockSize);
164 return leading + num * BlockSize;