Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / diskimage / plugins / d64.c
blobb47d8b4a6de9fd2fdbe3d1bfc18b14ff210dc44b
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
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 #ifdef __AROS__
28 #define __EXEC_NOLIBBASE__
29 #define __DOS_NOLIBBASE__
30 #endif
32 #define USED_PLUGIN_API_VERSION 8
33 #include <devices/diskimage.h>
34 #include <proto/exec.h>
35 #include <proto/dos.h>
36 #include <string.h>
37 #include "device_locale.h"
38 #include <SDI_compiler.h>
39 #include "rev/diskimage.device_rev.h"
41 PLUGIN_VERSTAG("D64")
43 extern struct DiskImagePlugin d64_plugin;
45 PLUGIN_TABLE(&d64_plugin)
47 struct D64Image {
48 BPTR file;
49 ULONG block_size;
50 ULONG total_blocks;
51 UQUAD total_bytes;
54 BOOL D64_Init (struct DiskImagePlugin *Self, const struct PluginData *data);
55 BOOL D64_CheckImage (struct DiskImagePlugin *Self, BPTR file, CONST_STRPTR name, QUAD file_size,
56 const UBYTE *test, LONG testsize);
57 APTR D64_OpenImage (struct DiskImagePlugin *Self, APTR unit, BPTR file, CONST_STRPTR name);
58 void Generic_CloseImage (struct DiskImagePlugin *Self, APTR image_ptr);
59 LONG Generic_Geometry (struct DiskImagePlugin *Self, APTR image_ptr, struct DriveGeometry *dg);
60 LONG Generic_Read (struct DiskImagePlugin *Self, APTR image_ptr, struct IOStdReq *io);
61 LONG Generic_Write (struct DiskImagePlugin *Self, APTR image_ptr, struct IOStdReq *io);
63 struct DiskImagePlugin d64_plugin = {
64 PLUGIN_NODE(-126, "D64"),
65 PLUGIN_FLAG_M68K,
67 ZERO,
68 NULL,
69 D64_Init,
70 NULL,
71 D64_CheckImage,
72 D64_OpenImage,
73 Generic_CloseImage,
74 Generic_Geometry,
75 Generic_Read,
76 Generic_Write,
77 NULL,
78 NULL,
79 NULL,
80 NULL
83 static struct Library *SysBase;
84 static struct Library *DOSBase;
85 static struct DIPluginIFace *IPlugin;
87 BOOL D64_Init (struct DiskImagePlugin *Self, const struct PluginData *data) {
88 SysBase = data->SysBase;
89 DOSBase = data->DOSBase;
90 IPlugin = data->IPlugin;
91 return TRUE;
94 BOOL D64_CheckImage (struct DiskImagePlugin *Self, BPTR file, CONST_STRPTR name, QUAD file_size,
95 const UBYTE *test, LONG testsize)
97 switch (file_size) {
98 case 174848:
99 case 175531:
100 case 196608:
101 case 197376:
102 return TRUE;
103 default:
104 return FALSE;
108 APTR D64_OpenImage (struct DiskImagePlugin *Self, APTR unit, BPTR file, CONST_STRPTR name) {
109 LONG done = FALSE;
110 LONG error = NO_ERROR;
111 QUAD file_size;
112 struct D64Image *image = NULL;
114 file_size = GetFileSize(file);
115 if (file_size == -1) {
116 error = IoErr();
117 goto error;
120 /* some .d64 disk images have extra data at the end of the file */
121 switch (file_size) {
122 case 174848:
123 case 175531:
124 file_size = 174848;
125 break;
126 case 196608:
127 case 197376:
128 file_size = 196608;
129 break;
130 default:
131 error = ERROR_OBJECT_WRONG_TYPE;
132 goto error;
135 image = AllocVec(sizeof(*image), MEMF_CLEAR);
136 if (!image) {
137 error = ERROR_NO_FREE_STORE;
138 goto error;
140 image->file = file;
141 image->total_bytes = file_size;
143 image->block_size = 256;
144 image->total_blocks = image->total_bytes >> 8;
146 done = TRUE;
148 error:
149 if (!done) {
150 if (image) {
151 Plugin_CloseImage(Self, image);
152 image = NULL;
153 } else {
154 Close(file);
156 IPlugin_SetDiskImageError(unit, error, 0);
158 return image;