enable usb2otg device
[AROS.git] / tools / adflib / adf_dump.c
blob39396a82d1d50e8d0e63b1ddb67762ff57699cab
1 /*
2 * ADF Library
4 * adf_dump.c
6 * Amiga Dump File specific routines
7 */
9 #define _FILE_OFFSET_BITS 64
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
15 #include "adf_defs.h"
16 #include "adf_str.h"
17 #include "adf_disk.h"
18 #include "adf_nativ.h"
19 #include "adf_err.h"
21 extern struct Env adfEnv;
24 * adfInitDumpDevice
27 RETCODE adfInitDumpDevice(struct Device* dev, char* name, BOOL ro)
29 struct nativeDevice* nDev;
30 long size;
32 nDev = (struct nativeDevice*)malloc(sizeof(struct nativeDevice));
33 if (!nDev) {
34 (*adfEnv.eFct)("adfInitDumpDevice : malloc");
35 return RC_MALLOC;
37 dev->nativeDev = nDev;
39 dev->readOnly = ro;
40 errno = 0;
41 if (!ro) {
42 nDev->fd = fopen(name,"rb+");
43 /* force read only */
44 if (!nDev->fd && (errno==EACCES || errno==EROFS) ) {
45 nDev->fd = fopen(name,"rb");
46 dev->readOnly = TRUE;
47 if (nDev->fd)
48 (*adfEnv.wFct)("myInitDevice : fopen, read-only mode forced");
51 else
52 /* read only requested */
53 nDev->fd = fopen(name,"rb");
55 if (!nDev->fd) {
56 free(nDev);
57 (*adfEnv.eFct)("myInitDevice : fopen");
58 return RC_ERROR;
61 /* determines size */
62 fseek(nDev->fd, 0, SEEK_END);
63 size = ftell(nDev->fd);
64 fseek(nDev->fd, 0, SEEK_SET);
66 /* Let's make some guesses about geometry...
68 dev->size = size;
69 if (size <= (11 * 80 * 512 * 2)) {
70 dev->sectors = 11;
71 dev->heads = 2;
72 } else {
73 dev->sectors = 128;
74 dev->heads = 4;
76 dev->cylinders = dev->size / 512 / dev->sectors / dev->heads;
78 return RC_OK;
83 * adfReadDumpSector
86 RETCODE adfReadDumpSector(struct Device *dev, SECTNUM n, int size, unsigned char* buf)
88 struct nativeDevice* nDev;
89 int r;
90 long seek = 512 * (long)n;
92 // printf("R 0x%08lx\n", seek);
93 nDev = (struct nativeDevice*)dev->nativeDev;
94 r = fseek(nDev->fd, seek, SEEK_SET);
95 //printf("nnn=%ld size=%d\n",n,size);
96 if (r==-1)
97 return RC_ERROR;
98 //puts("123");
99 if ((r=fread(buf, 1, size, nDev->fd))!=size) {
100 //printf("rr=%d\n",r);
101 return RC_ERROR;
103 //puts("1234");
105 return RC_OK;
110 * adfWriteDumpSector
113 RETCODE adfWriteDumpSector(struct Device *dev, SECTNUM n, int size, unsigned char* buf)
115 struct nativeDevice* nDev;
116 int r;
117 long seek = 512 * (long)n;
119 nDev = (struct nativeDevice*)dev->nativeDev;
121 // printf("W 0x%08lx\n", seek);
122 r=fseek(nDev->fd, seek, SEEK_SET);
123 if (r==-1)
124 return RC_ERROR;
126 if ( fwrite(buf, 1, size, nDev->fd)!=(unsigned int)(size) )
127 return RC_ERROR;
129 return RC_OK;
134 * adfReleaseDumpDevice
137 RETCODE adfReleaseDumpDevice(struct Device *dev)
139 struct nativeDevice* nDev;
141 if (!dev->nativeDev)
142 return RC_ERROR;
144 nDev = (struct nativeDevice*)dev->nativeDev;
145 fclose(nDev->fd);
147 free(nDev);
149 return RC_OK;
154 * adfCreateHdFile
157 RETCODE adfCreateHdFile(struct Device* dev, char* volName, int volType)
160 if (dev==NULL) {
161 (*adfEnv.eFct)("adfCreateHdFile : dev==NULL");
162 return RC_ERROR;
164 dev->volList =(struct Volume**) malloc(sizeof(struct Volume*));
165 if (!dev->volList) {
166 (*adfEnv.eFct)("adfCreateHdFile : unknown device type");
167 return RC_ERROR;
170 dev->volList[0] = adfCreateVol( dev, 0L, (long)dev->cylinders, 2, volName, volType );
171 if (dev->volList[0]==NULL) {
172 free(dev->volList);
173 return RC_ERROR;
176 dev->nVol = 1;
177 dev->devType = DEVTYPE_HARDFILE;
179 return RC_OK;
184 * adfCreateDumpDevice
186 * returns NULL if failed
188 struct Device*
189 adfCreateDumpDevice(char* filename, long cylinders, long heads, long sectors)
191 struct Device* dev;
192 unsigned char buf[LOGICAL_BLOCK_SIZE];
193 struct nativeDevice* nDev;
194 // long i;
195 int r;
197 dev=(struct Device*)malloc(sizeof(struct Device));
198 if (!dev) {
199 (*adfEnv.eFct)("adfCreateDumpDevice : malloc dev");
200 return NULL;
202 nDev = (struct nativeDevice*)malloc(sizeof(struct nativeDevice));
203 if (!nDev) {
204 free(dev);
205 (*adfEnv.eFct)("adfCreateDumpDevice : malloc nDev");
206 return NULL;
208 dev->nativeDev = nDev;
210 nDev->fd = (FILE*)fopen(filename,"wb");
211 if (!nDev->fd) {
212 free(nDev); free(dev);
213 (*adfEnv.eFct)("adfCreateDumpDevice : fopen");
214 return NULL;
217 /* for(i=0; i<cylinders*heads*sectors; i++)
218 fwrite(buf, sizeof(unsigned char), 512 , nDev->fd);
220 r=fseek(nDev->fd, ((cylinders*heads*sectors)-1)*LOGICAL_BLOCK_SIZE, SEEK_SET);
221 if (r==-1) {
222 fclose(nDev->fd); free(nDev); free(dev);
223 (*adfEnv.eFct)("adfCreateDumpDevice : fseek");
224 return NULL;
227 fwrite(buf, LOGICAL_BLOCK_SIZE, 1, nDev->fd);
229 fclose(nDev->fd);
231 nDev->fd=(FILE*)fopen(filename,"rb+");
232 if (!nDev->fd) {
233 free(nDev); free(dev);
234 (*adfEnv.eFct)("adfCreateDumpDevice : fopen");
235 return NULL;
237 dev->cylinders = cylinders;
238 dev->heads = heads;
239 dev->sectors = sectors;
240 dev->size = cylinders*heads*sectors* LOGICAL_BLOCK_SIZE;
242 if (dev->size==80*11*2*LOGICAL_BLOCK_SIZE)
243 dev->devType = DEVTYPE_FLOPDD;
244 else if (dev->size==80*22*2*LOGICAL_BLOCK_SIZE)
245 dev->devType = DEVTYPE_FLOPHD;
246 else
247 dev->devType = DEVTYPE_HARDDISK;
249 dev->nVol = 0;
250 dev->isNativeDev = FALSE;
251 dev->readOnly = FALSE;
253 return(dev);
256 /*##################################################################################*/