Initial import of Scalos. To decrease size I have
[AROS-Contrib.git] / scalos / Modules / FormatDisk.Gadtools / Device_Handler.c
blob711f3829bc8b7fb20c6c6e199b997da8fc546084
1 // Device_Handler.c
2 // $Date$
3 // $Revision$
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <stdarg.h>
12 #include <exec/types.h>
13 #include <exec/exec.h>
14 #include <intuition/intuition.h>
15 #include <dos/dos.h>
16 #include <dos/filehandler.h>
17 #include <workbench/startup.h>
18 #include <libraries/gadtools.h>
19 #include <workbench/icon.h>
20 #include <devices/trackdisk.h>
21 #include <dos/rdargs.h>
23 #include <proto/exec.h>
24 #include <proto/intuition.h>
25 #include <proto/dos.h>
26 #include <proto/gadtools.h>
27 #include <proto/icon.h>
28 #include <proto/graphics.h>
29 #include <proto/utility.h>
31 #include <defs.h>
32 #include "Format.h"
34 //-----------------------------------------------------------------------------
36 STRPTR NameOfDevice = ""; // +jmc+ : driver name copied here, problems found after twice analyzes for a same drive.
38 LONG TextHighCyl=0;
39 LONG TextLayoutBPT=0;
41 //-----------------------------------------------------------------------------
43 // Convert a volume name to a device name, and get information
44 // on the layout of the disk
46 BOOL volumeToDevName(BPTR volumeLock, char *dev, DriveLayout *layout)
48 struct DosList *dosList;
49 char name[36];
50 struct Process *process;
51 APTR oldWdw;
52 BOOL Found = FALSE;
55 d1(KPrintF("%s/%s/%ld: START dev=<%s>\n", __FILE__, __FUNC__, __LINE__, dev));
56 debugLock_d1(volumeLock);
58 // Disable requestors during the execution of this function
59 process = (struct Process *) FindTask(NULL);
60 oldWdw = process->pr_WindowPtr;
61 process->pr_WindowPtr = (APTR)(-1);
63 // Get the DOS device list
64 dosList = LockDosList(LDF_DEVICES|LDF_READ);
66 // Go through each entry
67 while (!Found && dosList != NULL)
69 dosList = NextDosEntry(dosList, LDF_DEVICES|LDF_READ);
70 if (dosList == NULL)
71 break;
73 d1(KPrintF("%s/%s/%ld: dol_Startup=%08lx dol_Handler=%08lx Type=%ld Task=%08lx Lock=%08lx\n", \
74 __FILE__, __FUNC__, __LINE__, dosList->dol_misc.dol_handler.dol_Startup,
75 dosList->dol_misc.dol_handler.dol_Handler, dosList->dol_Type, dosList->dol_Task, dosList->dol_Lock));
77 // If the node in the list is a volume
78 if (dosList->dol_misc.dol_handler.dol_Startup > (BPTR) 1000 &&
79 (dosList->dol_Task /* || dosList->dol_misc.dol_handler.dol_Handler */ || dosList->dol_misc.dol_handler.dol_SegList))
81 BOOL stat = FALSE;
83 // Get the name of the device
84 BtoCString(dosList->dol_Name, name, sizeof(name) - 1);
85 strcat(name, ":");
87 /*Get the information on the device*/
88 /* driveEnv = (struct DosEnvec *)
89 BADDR(((struct FileSysStartupMsg *)
90 BADDR(dosList->dol_misc.dol_handler.dol_Startup))->fssm_Environ);*/
92 d1(KPrintF("%s/%s/%ld: dol_Name=<%s>\n", __FILE__, __FUNC__, __LINE__, name));
94 // If the volume lock is NULL, the 'dev' is assumed to be the
95 // name of a device holding an unformatted disk, so compare the
96 // name to the name of the current node
98 if (volumeLock == (BPTR)NULL)
100 stat = (Stricmp(dev, name) == 0);
101 d1(KPrintF("%s/%s/%ld: stat=%ld dev=%s name=%s\n", __FILE__, __FUNC__, __LINE__, stat, dev,name));
103 else
105 // Otherwise, since 'dev' could hold a volume rather than
106 // device name, get a lock on it and compare it to the lock
107 // on the device that was given; if they're the same, we've
108 // found the drive
109 BPTR tempLock;
111 tempLock = Lock(name, ACCESS_READ);
112 debugLock_d1(tempLock);
113 if (tempLock)
115 ULONG same;
117 same = SameLock(tempLock,volumeLock);
118 d1(KPrintF("%s/%s/%ld: same=%ld\n", __FILE__, __FUNC__, __LINE__, same));
119 stat = LOCK_SAME == same || LOCK_SAME_VOLUME == same;
120 UnLock(tempLock);
124 /*If weve found the drive, get the information on it*/
125 if (stat)
127 // Get a pointer to the structure that holds the needed info
128 struct FileSysStartupMsg *startup;
129 struct DosEnvec *driveEnv;
131 startup = (struct FileSysStartupMsg *) BADDR(dosList->dol_misc.dol_handler.dol_Startup);
132 driveEnv = (struct DosEnvec *) BADDR(startup->fssm_Environ);
134 // Get the information
135 layout->unit = startup->fssm_Unit;
137 // Copy the device name to layout->devName
138 BtoCString(startup->fssm_Device, layout->devName, sizeof(layout->devName) - 1);
139 d1(KPrintF("%s/%s/%ld: layout->devName=%s\n", __FILE__, __FUNC__, __LINE__, layout->devName));
141 if (strlen(NameOfDevice) > 0)
143 d1(KPrintF("%s/%s/%ld: ** OK ** NameOfDevice lenght > 0 Len=[%ld]\n", __FILE__, __FUNC__, __LINE__, strlen(NameOfDevice)));
145 strcpy(layout->devName, NameOfDevice);
147 d1(KPrintF("%s/%s/%ld: ** SO ** layout.devName = NameofDevice=<%s>\n", __FILE__, __FUNC__, __LINE__, layout->devName));
149 else
150 NameOfDevice = layout->devName;
152 d1(KPrintF("%s/%s/%ld: NameOfDevice=<%s> layout->devName=<%s>\n", __FILE__, __FUNC__, __LINE__, NameOfDevice, layout->devName));
155 layout->flags = startup->fssm_Flags;
156 layout->memType = driveEnv->de_BufMemType;
157 layout->lowCyl = driveEnv->de_LowCyl;
158 layout->highCyl = driveEnv->de_HighCyl;
159 layout->surfaces = driveEnv->de_Surfaces;
160 layout->BPT = driveEnv->de_BlocksPerTrack;
161 layout->blockSize = driveEnv->de_SizeBlock;
163 //---------------------------------------------------------------------------------------------------------------------------------
164 TextHighCyl = layout->highCyl;
165 TextLayoutBPT = layout->BPT;
168 // Copy the device name back to 'dev'
169 strcpy(dev,name);
171 d1(KPrintF("%s/%s/%ld: FOUBND dev=<%s> name=<%s> flags=%lx memType=%lx lowCyl=%ld highCyl=%ld surfaces=%ld BlocksPerTrack=%ld blockSize=%ld TAILLE: [ %ld ]\n",
172 __FILE__, __FUNC__, __LINE__, dev, name, layout->flags, layout->memType, layout->lowCyl, layout->highCyl,
173 layout->surfaces, layout->BPT, layout->blockSize, ((layout->highCyl + 1) * layout->BPT) ));
175 //--------------------------------------------------------------------------------------------------------------------------------------
177 Found = TRUE;
182 d1(KPrintF("%s/%s/%ld: Found=%ld\n", __FILE__, __FUNC__, __LINE__, Found));
184 // We didnt find the drive in the list, so unlock the list
185 UnLockDosList(LDF_DEVICES|LDF_READ);
187 // Restore the requester pointer
188 process->pr_WindowPtr = oldWdw;
190 // And return
191 return Found;
194 //-----------------------------------------------------------------------------
196 // Create a communications port linking this process to the drive
197 struct IOExtTD *OpenDrive(char *driveDevName,ULONG unit,ULONG flags)
199 struct MsgPort *diskPort;
200 struct IOExtTD *diskRequest;
202 // Create the message port
203 if ((diskPort = CreateMsgPort()) != NULL)
205 // Create the IORequest
206 diskRequest = (struct IOExtTD *)
207 CreateIORequest(diskPort,sizeof(struct IOExtTD));
208 if(diskRequest != NULL)
210 // Open the device, and return the IORequest if the device
211 // opened successfully
213 d1(KPrintF("%s/%s/%ld: driveDevName=<%s> unit=[%ld]\n", __FILE__, __FUNC__, __LINE__, driveDevName, unit));
215 if(!OpenDevice(driveDevName,unit,(struct IORequest *)diskRequest,flags))
216 return(diskRequest);
218 // The device didnt open, so clean up
219 DeleteIORequest((struct IORequest *)diskRequest);
222 DeleteMsgPort(diskPort);
225 // Return NULL to indicate that an error occurred
226 return(NULL);
229 //-----------------------------------------------------------------------------
231 // Close an open device and delete the accompanying port, etc.
232 void CloseDrive(struct IOExtTD *diskRequest)
234 CloseDevice((struct IORequest *)diskRequest);
235 DeleteMsgPort(diskRequest->iotd_Req.io_Message.mn_ReplyPort);
236 DeleteIORequest((struct IORequest *)diskRequest);
239 //-----------------------------------------------------------------------------
241 // Convert a CSTR to a BSTR
242 BSTR makeBSTR(CONST_STRPTR in, char *out)
244 int c;
246 out[0] = strlen(in);
247 for(c = 0; c < out[0]; c++)
248 out[c+1] = in[c];
250 return MKBADDR(out);
254 //-----------------------------------------------------------------------------
256 // Convert a BSTR to a CSTR
258 STRPTR BtoCString(BPTR bString, STRPTR Buffer, size_t MaxLen)
260 #ifdef __AROS__
261 // AROS needs special handling because it uses NULL-terminated
262 // strings on some platforms.
263 size_t Len = AROS_BSTR_strlen(bString);
264 if (Len >= MaxLen)
265 Len = MaxLen - 1;
266 strncpy(Buffer, AROS_BSTR_ADDR(bString), Len);
267 Buffer[Len] = '\0';
269 return Buffer;
270 #else
271 UBYTE *bStringAddr = BADDR(bString);
272 size_t Length = bStringAddr[0];
274 if (Length >= MaxLen)
275 Length = MaxLen - 1;
277 memcpy(Buffer, bStringAddr + 1, Length);
278 Buffer[Length] = '\0';
280 return Buffer;
281 #endif
284 //-----------------------------------------------------------------------------