2 * ADF Library. (C) 1997-1999 Laurent Clevy
6 * logical disk/volume code
19 #include "adf_nativ.h"
22 #include "adf_cache.h"
24 extern struct Env adfEnv
;
26 unsigned long bitMask
[32] = {
28 0x10, 0x20, 0x40, 0x80,
29 0x100, 0x200, 0x400, 0x800,
30 0x1000, 0x2000, 0x4000, 0x8000,
31 0x10000, 0x20000, 0x40000, 0x80000,
32 0x100000, 0x200000, 0x400000, 0x800000,
33 0x1000000, 0x2000000, 0x4000000, 0x8000000,
34 0x10000000, 0x20000000, 0x40000000, 0x80000000 };
37 RETCODE
adfInstallBootBlock(struct Volume
*vol
, unsigned char* code
)
40 struct bBootBlock boot
;
42 if (vol
->dev
->devType
!=DEVTYPE_FLOPDD
&& vol
->dev
->devType
!=DEVTYPE_FLOPHD
)
45 if (adfReadBootBlock(vol
, &boot
)!=RC_OK
)
49 for(i
=0; i
<1024-12; i
++) /* bootcode */
50 boot
.data
[i
] = code
[i
+12];
52 if (adfWriteBootBlock(vol
, &boot
)!=RC_OK
)
65 BOOL
isSectNumValid(struct Volume
*vol
, SECTNUM nSect
)
67 return( 0<=nSect
&& nSect
<=(vol
->lastBlock
- vol
->firstBlock
) );
76 void adfVolumeInfo(struct Volume
*vol
)
78 struct bRootBlock root
;
82 if (adfReadRootBlock(vol
, vol
->rootBlock
, &root
)!=RC_OK
)
85 memset(diskName
, 0, 35);
86 memcpy(diskName
, root
.diskName
, root
.nameLen
);
88 printf ("Name : %-30s\n",vol
->volName
);
90 switch(vol
->dev
->devType
) {
92 printf ("Floppy Double Density : 880 KBytes\n");
95 printf ("Floppy High Density : 1760 KBytes\n");
97 case DEVTYPE_HARDDISK
:
98 printf ("Hard Disk partition : %3.1f KBytes\n",
99 (vol
->lastBlock
- vol
->firstBlock
+1) * 512.0/1024.0);
101 case DEVTYPE_HARDFILE
:
102 printf ("HardFile : %3.1f KBytes\n",
103 (vol
->lastBlock
- vol
->firstBlock
+1) * 512.0/1024.0);
106 printf ("Unknown devType!\n");
108 printf ("Filesystem : ");
109 printf("%s ",isFFS(vol
->dosType
) ? "FFS" : "OFS");
110 if (isINTL(vol
->dosType
))
112 if (isDIRCACHE(vol
->dosType
))
113 printf ("DIRCACHE ");
116 printf("Free blocks = %ld\n", adfCountFreeBlocks(vol
));
118 printf("Read only\n");
120 printf("Read/Write\n");
123 adfDays2Date(root
.coDays
, &year
, &month
, &days
);
124 printf ("created %d/%02d/%02d %ld:%02ld:%02ld\n",days
,month
,year
,
125 root
.coMins
/60,root
.coMins
%60,root
.coTicks
/50);
126 adfDays2Date(root
.days
, &year
, &month
, &days
);
127 printf ("last access %d/%02d/%02d %ld:%02ld:%02ld, ",days
,month
,year
,
128 root
.mins
/60,root
.mins
%60,root
.ticks
/50);
129 adfDays2Date(root
.cDays
, &year
, &month
, &days
);
130 printf ("%d/%02d/%02d %ld:%02ld:%02ld\n",days
,month
,year
,
131 root
.cMins
/60,root
.cMins
%60,root
.cTicks
/50);
141 struct Volume
* adfMount( struct Device
*dev
, int nPart
, BOOL readOnly
)
144 struct bRootBlock root
;
145 struct bBootBlock boot
;
148 if (dev
==NULL
|| nPart
<nPart
|| nPart
>= dev
->nVol
) {
149 (*adfEnv
.eFct
)("adfMount : invalid parameter(s)");
153 vol
= dev
->volList
[nPart
];
157 /*printf("first=%ld last=%ld root=%ld\n",vol->firstBlock,
158 vol->lastBlock, vol->rootBlock);
160 if (adfReadBootBlock(vol
, &boot
)!=RC_OK
) {
161 (*adfEnv
.wFct
)("adfMount : BootBlock invalid");
165 vol
->dosType
= boot
.dosType
[3];
166 if (isFFS(vol
->dosType
))
167 vol
->datablockSize
=512;
169 vol
->datablockSize
=488;
171 if (dev
->readOnly
/*|| isDIRCACHE(vol->dosType)*/)
172 vol
->readOnly
= TRUE
;
174 vol
->readOnly
= readOnly
;
176 if (adfReadRootBlock(vol
, vol
->rootBlock
, &root
)!=RC_OK
) {
177 (*adfEnv
.wFct
)("adfMount : RootBlock invalid");
181 nBlock
= vol
->lastBlock
- vol
->firstBlock
+1 - 2;
183 adfReadBitmap( vol
, nBlock
, &root
);
184 vol
->curDirPtr
= vol
->rootBlock
;
186 //printf("blockSize=%d\n",vol->blockSize);
196 * free bitmap structures
199 void adfUnMount(struct Volume
*vol
)
202 (*adfEnv
.eFct
)("adfUnMount : vol is null");
208 vol
->mounted
= FALSE
;
219 struct Volume
* adfCreateVol( struct Device
* dev
, long start
, long len
,
220 char* volName
, int volType
)
222 struct bBootBlock boot
;
223 struct bRootBlock root
;
224 // struct bDirCacheBlock dirc;
229 if (adfEnv
.useProgressBar
)
230 (*adfEnv
.progressBar
)(0);
232 vol
=(struct Volume
*)malloc(sizeof(struct Volume
));
234 (*adfEnv
.eFct
)("adfCreateVol : malloc vol");
239 vol
->firstBlock
= (dev
->heads
* dev
->sectors
)*start
;
240 vol
->lastBlock
= (vol
->firstBlock
+ (dev
->heads
* dev
->sectors
)*len
)-1;
241 vol
->rootBlock
= ((vol
->lastBlock
- vol
->firstBlock
+1)-1+2)/2;
242 /*printf("first=%ld last=%ld root=%ld\n",vol->firstBlock,
243 vol->lastBlock, vol->rootBlock);
245 vol
->curDirPtr
= vol
->rootBlock
;
247 vol
->readOnly
= dev
->readOnly
;
251 nlen
= min( MAXNAMELEN
, strlen(volName
) );
252 vol
->volName
= (char*)malloc(nlen
+1);
254 (*adfEnv
.eFct
)("adfCreateVol : malloc");
255 free(vol
); return NULL
;
257 memcpy(vol
->volName
, volName
, nlen
);
258 vol
->volName
[nlen
]='\0';
260 if (adfEnv
.useProgressBar
)
261 (*adfEnv
.progressBar
)(25);
263 memset(&boot
, 0, 1024);
264 boot
.dosType
[3] = volType
;
265 /*printf("first=%d last=%d\n", vol->firstBlock, vol->lastBlock);
266 printf("name=%s root=%d\n", vol->volName, vol->rootBlock);
268 if (adfWriteBootBlock(vol
, &boot
)!=RC_OK
) {
269 free(vol
->volName
); free(vol
);
273 if (adfEnv
.useProgressBar
)
274 (*adfEnv
.progressBar
)(20);
276 if (adfCreateBitmap( vol
)!=RC_OK
) {
277 free(vol
->volName
); free(vol
);
281 if (adfEnv
.useProgressBar
)
282 (*adfEnv
.progressBar
)(40);
285 /*for(i=0; i<127; i++)
286 printf("%3d %x, ",i,vol->bitmapTable[0]->map[i]);
288 if ( isDIRCACHE(volType
) )
289 adfGetFreeBlocks( vol
, 2, blkList
);
291 adfGetFreeBlocks( vol
, 1, blkList
);
294 /*printf("[0]=%d [1]=%d\n",blkList[0],blkList[1]);*/
296 memset(&root
, 0, LOGICAL_BLOCK_SIZE
);
298 if (strlen(volName
)>MAXNAMELEN
)
299 volName
[MAXNAMELEN
]='\0';
300 root
.nameLen
= strlen(volName
);
301 memcpy(root
.diskName
,volName
,root
.nameLen
);
302 adfTime2AmigaTime(adfGiveCurrentTime(),&(root
.coDays
),&(root
.coMins
),&(root
.coTicks
));
305 if ( isDIRCACHE(volType
) ) {
307 root
.secType
= ST_ROOT
; /* needed by adfCreateEmptyCache() */
308 adfCreateEmptyCache(vol
, (struct bEntryBlock
*)&root
, blkList
[1]);
311 if (adfEnv
.useProgressBar
)
312 (*adfEnv
.progressBar
)(60);
314 if (adfWriteRootBlock(vol
, blkList
[0], &root
)!=RC_OK
) {
315 free(vol
->volName
); free(vol
);
319 /* fills root->bmPages[] and writes filled bitmapExtBlocks */
320 if (adfWriteNewBitmap(vol
)!=RC_OK
)
323 if (adfEnv
.useProgressBar
)
324 (*adfEnv
.progressBar
)(80);
326 if (adfUpdateBitmap(vol
)!=RC_OK
)
329 if (adfEnv
.useProgressBar
)
330 (*adfEnv
.progressBar
)(100);
331 //printf("free blocks %ld\n",adfCountFreeBlocks(vol));
333 /* will be managed by adfMount() later */
336 vol
->mounted
= FALSE
;
350 adfReadBlock(struct Volume
* vol
, long nSect
, unsigned char* buf
)
352 /* char strBuf[80];*/
354 struct nativeFunctions
*nFct
;
358 (*adfEnv
.eFct
)("the volume isn't mounted, adfReadBlock not possible");
362 /* translate logical sect to physical sect */
363 pSect
= nSect
+vol
->firstBlock
;
365 if (adfEnv
.useRWAccess
)
366 (*adfEnv
.rwhAccess
)(pSect
,nSect
,FALSE
);
368 //printf("psect=%ld nsect=%ld\n",pSect,nSect);
369 /* sprintf(strBuf,"ReadBlock : accessing logical block #%ld", nSect);
370 (*adfEnv.vFct)(strBuf);
372 if (pSect
<vol
->firstBlock
|| pSect
>vol
->lastBlock
) {
373 (*adfEnv
.wFct
)("adfReadBlock : nSect out of range");
376 //printf("pSect R =%ld\n",pSect);
377 nFct
= adfEnv
.nativeFct
;
378 if (vol
->dev
->isNativeDev
)
379 rc
= (*nFct
->adfNativeReadSector
)(vol
->dev
, pSect
, 512, buf
);
381 rc
= adfReadDumpSector(vol
->dev
, pSect
, 512, buf
);
382 //printf("rc=%ld\n",rc);
394 RETCODE
adfWriteBlock(struct Volume
* vol
, long nSect
, unsigned char *buf
)
397 struct nativeFunctions
*nFct
;
401 (*adfEnv
.eFct
)("the volume isn't mounted, adfWriteBlock not possible");
406 (*adfEnv
.wFct
)("adfWriteBlock : can't write block, read only volume");
410 pSect
= nSect
+vol
->firstBlock
;
411 //printf("write nsect=%ld psect=%ld\n",nSect,pSect);
413 if (adfEnv
.useRWAccess
)
414 (*adfEnv
.rwhAccess
)(pSect
,nSect
,TRUE
);
416 if (pSect
<vol
->firstBlock
|| pSect
>vol
->lastBlock
) {
417 (*adfEnv
.wFct
)("adfWriteBlock : nSect out of range");
420 nFct
= adfEnv
.nativeFct
;
421 //printf("nativ=%d\n",vol->dev->isNativeDev);
422 if (vol
->dev
->isNativeDev
)
423 rc
= (*nFct
->adfNativeWriteSector
)(vol
->dev
, pSect
, 512, buf
);
425 rc
= adfWriteDumpSector(vol
->dev
, pSect
, 512, buf
);
435 /*#######################################################################################*/