3 * Common Driver/Filesystem Helper Functions
7 #include <api_drv_disk.h>
8 #include <api_drv_video.h>
11 // --- Video Driver Helpers ---
12 Uint64 DrvUtil_Video_2DStream(void *Ent, void *Buffer, int Length,
13 tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers)
15 void *stream = Buffer;
22 stream = (void*)((tVAddr)stream + 1);
24 if(op > NUM_VIDEO_2DOPS) {
25 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Unknown"
29 if(op*sizeof(void*) > SizeofHandlers) {
30 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver does"
31 " not support op %i", op);
37 case VIDEO_2DOP_NOP: break;
40 if(rem < 12) return Length-rem;
43 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
44 " does not support VIDEO_2DOP_FILL");
50 ((Uint16*)stream)[0], ((Uint16*)stream)[1],
51 ((Uint16*)stream)[2], ((Uint16*)stream)[3],
56 stream = (void*)((tVAddr)stream + 12);
60 if(rem < 12) return Length-rem;
63 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
64 " does not support VIDEO_2DOP_BLIT");
70 ((Uint16*)stream)[0], ((Uint16*)stream)[1],
71 ((Uint16*)stream)[2], ((Uint16*)stream)[3],
72 ((Uint16*)stream)[4], ((Uint16*)stream)[5]
76 stream = (void*)((tVAddr)stream + 12);
84 // --- Disk Driver Helpers ---
85 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
86 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
88 Uint8 tmp[BlockSize]; // C99
89 Uint64 block = Start / BlockSize;
90 int offset = Start - block * BlockSize;
91 int leading = BlockSize - offset;
96 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
97 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
99 // Non aligned start, let's fix that!
102 if(leading > Length) leading = Length;
103 LOG("Reading %i bytes from Block1+%i", leading, offset);
104 ret = ReadBlocks(block, 1, tmp, Argument);
109 memcpy( Buffer, &tmp[offset], leading );
111 if(leading == Length) {
116 Buffer = (Uint8*)Buffer + leading;
118 num = ( Length - leading ) / BlockSize;
119 tailings = Length - num * BlockSize - leading;
122 num = Length / BlockSize;
123 tailings = Length % BlockSize;
126 // Read central blocks
129 LOG("Reading %i blocks", num);
130 ret = ReadBlocks(block, num, Buffer, Argument);
132 LEAVE('X', leading + ret * BlockSize);
133 return leading + ret * BlockSize;
137 // Read last tailing block
140 LOG("Reading %i bytes from last block", tailings);
142 Buffer = (Uint8*)Buffer + num * BlockSize;
143 ret = ReadBlocks(block, 1, tmp, Argument);
145 LEAVE('X', leading + num * BlockSize);
146 return leading + num * BlockSize;
148 memcpy( Buffer, tmp, tailings );
155 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
156 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
157 Uint64 BlockSize, Uint Argument)
159 Uint8 tmp[BlockSize]; // C99
160 Uint64 block = Start / BlockSize;
161 int offset = Start - block * BlockSize;
162 int leading = BlockSize - offset;
167 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
168 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
170 // Non aligned start, let's fix that!
173 if(leading > Length) leading = Length;
174 LOG("Writing %i bytes to Block1+%i", leading, offset);
175 // Read a copy of the block
176 ret = ReadBlocks(block, 1, tmp, Argument);
182 memcpy( &tmp[offset], Buffer, leading );
184 ret = WriteBlocks(block, 1, tmp, Argument);
190 if(leading == Length) {
195 Buffer = (Uint8*)Buffer + leading;
197 num = ( Length - leading ) / BlockSize;
198 tailings = Length - num * BlockSize - leading;
201 num = Length / BlockSize;
202 tailings = Length % BlockSize;
205 // Read central blocks
208 LOG("Writing %i blocks", num);
209 ret = WriteBlocks(block, num, Buffer, Argument);
211 LEAVE('X', leading + ret * BlockSize);
212 return leading + ret * BlockSize;
216 // Read last tailing block
219 LOG("Writing %i bytes to last block", tailings);
221 Buffer = (Uint8*)Buffer + num * BlockSize;
223 ret = ReadBlocks(block, 1, tmp, Argument);
225 LEAVE('X', leading + num * BlockSize);
226 return leading + num * BlockSize;
229 memcpy( tmp, Buffer, tailings );
231 ret = WriteBlocks(block, 1, tmp, Argument);
233 LEAVE('X', leading + num * BlockSize);
234 return leading + num * BlockSize;