1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #define USED_PLUGIN_API_VERSION 8
28 #include <devices/diskimage.h>
29 #include <proto/exec.h>
30 #include <proto/dos.h>
34 #include "device_locale.h"
35 #include <SDI_compiler.h>
36 #include "rev/diskimage.device_rev.h"
40 extern struct DiskImagePlugin fdi_plugin
;
42 PLUGIN_TABLE(&fdi_plugin
)
44 BOOL
FDI_Init (struct DiskImagePlugin
*Self
, const struct PluginData
*data
);
45 void FDI_Exit (struct DiskImagePlugin
*Self
);
46 BOOL
FDI_CheckImage (struct DiskImagePlugin
*Self
, BPTR file
, CONST_STRPTR name
, QUAD file_size
,
47 const UBYTE
*test
, LONG testsize
);
48 APTR
FDI_OpenImage (struct DiskImagePlugin
*Self
, APTR unit
, BPTR file
, CONST_STRPTR name
);
49 void FDI_CloseImage (struct DiskImagePlugin
*Self
, APTR image_ptr
);
50 LONG
FDI_Geometry (struct DiskImagePlugin
*Self
, APTR image_ptr
, struct DriveGeometry
*dg
);
51 LONG
FDI_Read (struct DiskImagePlugin
*Self
, APTR image_ptr
, struct IOStdReq
*io
);
53 struct DiskImagePlugin fdi_plugin
= {
54 PLUGIN_NODE(0, "FDI"),
73 struct Library
*SysBase
;
74 struct Library
*DOSBase
;
75 struct DIPluginIFace
*IPlugin
;
77 struct Library
*MathIeeeDoubBasBase
;
80 BOOL
FDI_Init (struct DiskImagePlugin
*Self
, const struct PluginData
*data
) {
81 SysBase
= data
->SysBase
;
82 DOSBase
= data
->DOSBase
;
83 IPlugin
= data
->IPlugin
;
87 void FDI_Exit (struct DiskImagePlugin
*Self
) {
89 if (MathIeeeDoubBasBase
) CloseLibrary(MathIeeeDoubBasBase
);
93 BOOL
FDI_CheckImage (struct DiskImagePlugin
*Self
, BPTR file
, CONST_STRPTR name
, QUAD file_size
,
94 const UBYTE
*test
, LONG testsize
)
96 return testsize
>= 27 && !memcmp(test
, "Formatted Disk Image file\r\n", 27);
113 APTR
FDI_OpenImage (struct DiskImagePlugin
*Self
, APTR unit
, BPTR file
, CONST_STRPTR name
) {
115 LONG error
= NO_ERROR
;
116 LONG error_string
= NO_ERROR_STRING
;
117 IPTR error_args
[4] = {0};
118 struct FDIImage
*image
= NULL
;
121 if (!MathIeeeDoubBasBase
) {
122 MathIeeeDoubBasBase
= OpenLibrary("mathieeedoubbas.library", 0);
123 if (!MathIeeeDoubBasBase
) {
124 error
= ERROR_OBJECT_NOT_FOUND
;
125 error_string
= MSG_REQ
;
126 error_args
[0] = (IPTR
)"mathieeedoubbas.library";
132 image
= AllocVec(sizeof(*image
), MEMF_CLEAR
);
134 error
= ERROR_NO_FREE_STORE
;
139 image
->fdi
= fdi2raw_header(image
->file
);
145 image
->tracks
= fdi2raw_get_last_track(image
->fdi
);
146 image
->heads
= fdi2raw_get_last_head(image
->fdi
)+1;
147 image
->sectors
= fdi2raw_get_num_sector(image
->fdi
);
148 image
->track_size
= image
->sectors
<< 9;
150 image
->mfmbuf_size
= 0x8000;
151 if (image
->sectors
== 22) image
->mfmbuf_size
*= 2;
153 image
->mfmbuf
= AllocVec(image
->mfmbuf_size
, MEMF_ANY
);
154 image
->tracktiming
= AllocVec(image
->mfmbuf_size
, MEMF_CLEAR
);
155 if (!image
->mfmbuf
|| !image
->tracktiming
) {
156 error
= ERROR_NO_FREE_STORE
;
159 image
->track_in_buf
= ~0;
166 Plugin_CloseImage(Self
, image
);
171 if (error
== NO_ERROR
) {
172 error
= ERROR_OBJECT_WRONG_TYPE
;
173 error_string
= MSG_EOF
;
175 IPlugin_SetDiskImageErrorA(unit
, error
, error_string
, error_args
);
180 void FDI_CloseImage (struct DiskImagePlugin
*Self
, APTR image_ptr
) {
181 struct FDIImage
*image
= image_ptr
;
183 if (image
->fdi
) fdi2raw_header_free(image
->fdi
);
184 FreeVec(image
->mfmbuf
);
185 FreeVec(image
->tracktiming
);
191 LONG
FDI_Geometry (struct DiskImagePlugin
*Self
, APTR image_ptr
, struct DriveGeometry
*dg
) {
192 struct FDIImage
*image
= image_ptr
;
193 dg
->dg_SectorSize
= 512;
194 dg
->dg_Heads
= image
->heads
;
195 dg
->dg_Cylinders
= image
->tracks
/ image
->heads
;
196 dg
->dg_TrackSectors
= image
->sectors
;
197 dg
->dg_CylSectors
= image
->sectors
* image
->heads
;
198 dg
->dg_TotalSectors
= image
->sectors
* image
->tracks
;
199 return IOERR_SUCCESS
;
202 static LONG
read_sector (UBYTE
*buf
, int track
, int sector
, UWORD
*data
, LONG len
);
203 static UWORD
getmfmword (const UWORD
*mbuf
, ULONG shift
);
204 static ULONG
getmfmlong (const UWORD
*mbuf
, ULONG shift
);
206 LONG
FDI_Read (struct DiskImagePlugin
*Self
, APTR image_ptr
, struct IOStdReq
*io
) {
207 struct FDIImage
*image
= image_ptr
;
210 LONG track
, block
, sectors
;
211 unsigned int tracklen
;
212 FDI
*fdi
= image
->fdi
;
213 UWORD
*mfmbuf
= image
->mfmbuf
;
216 buffer
= io
->io_Data
;
217 offset
= io
->io_Offset
;
218 size
= io
->io_Length
;
221 if (offset
& 511) return IOERR_BADADDRESS
;
222 if (size
& 511) return IOERR_BADLENGTH
;
227 sectors
= image
->sectors
;
228 track
= offset
/ sectors
;
229 block
= offset
% sectors
;
232 if (track
!= image
->track_in_buf
) {
234 if (track
>= image
->tracks
) return TDERR_SeekError
;
237 if (!fdi2raw_loadtrack(fdi
, mfmbuf
, image
->tracktiming
,
238 track
, &tracklen
, NULL
, NULL
, 1) || !tracklen
)
240 image
->track_in_buf
= ~0;
241 return TDERR_NotSpecified
;
243 image
->tracklen
= tracklen
>>= 3;
244 image
->track_in_buf
= track
;
246 tracklen
= image
->tracklen
;
249 for (; size
&& block
< sectors
; block
++, size
--) {
250 error
= read_sector(buffer
, track
, block
, mfmbuf
, tracklen
);
252 /*DebugPrintF("error: %ld, track: %ld, block: %ld, tracklen: %ld\n",
253 error, track, block, tracklen);*/
257 io
->io_Actual
+= 512;
263 return IOERR_SUCCESS
;
266 static LONG
read_sector (UBYTE
*buf
, int track
, int sector
, UWORD
*data
, LONG len
) {
268 ULONG odd
, even
, chksum
, id
, dlong
;
272 end
= data
+ ((len
+1) >> 1);
275 while (data
<= end
) {
276 while (getmfmword(data
, shift
) != 0x4489) {
278 return TDERR_NoSecHdr
;
286 while (getmfmword(data
, shift
) == 0x4489) {
287 if (data
>= end
) return TDERR_BadSecPreamble
;
291 odd
= getmfmlong(data
, shift
);
292 even
= getmfmlong(data
+ 2, shift
);
294 id
= (odd
<< 1)|even
;
296 for (i
= 0; i
< 4; i
++) {
297 odd
= getmfmlong(data
, shift
);
298 even
= getmfmlong(data
+ 8, shift
);
301 dlong
= (odd
<< 1)|even
;
302 chksum
^= odd
^ even
;
305 odd
= getmfmlong(data
, shift
);
306 even
= getmfmlong(data
+ 2, shift
);
308 if (((odd
<< 1)|even
) != chksum
||
309 ((id
& 0xff000000) >> 24) != 0xff ||
310 ((id
& 0x00ff0000) >> 16) != track
)
312 return TDERR_BadSecHdr
;
315 odd
= getmfmlong(data
, shift
);
316 even
= getmfmlong(data
+ 2, shift
);
318 chksum
= (odd
<< 1)|even
;
319 if (((id
& 0x0000ff00) >> 8) == sector
) {
320 for (i
= 0; i
< 128; i
++) {
321 odd
= getmfmlong(data
, shift
);
322 even
= getmfmlong(data
+ 256, shift
);
324 dlong
= (odd
<< 1)|even
;
327 chksum
^= odd
^ even
;
330 return TDERR_BadSecSum
;
332 return IOERR_SUCCESS
;
337 return TDERR_NoSecHdr
;
340 #define MFMMASK 0x55555555
342 static UWORD
getmfmword (const UWORD
*mbuf
, ULONG shift
) {
343 return (rbe16(&mbuf
[0]) << shift
) | (rbe16(&mbuf
[1]) >> (16 - shift
));
346 static ULONG
getmfmlong (const UWORD
*mbuf
, ULONG shift
) {
347 return ((getmfmword (mbuf
, shift
) << 16) | getmfmword (mbuf
+ 1, shift
)) & MFMMASK
;