2 Copyright © 1995-2019, The AROS Development Team. All rights reserved.
5 Desc: Header for dos.library
9 #include <aros/debug.h>
10 #include <exec/execbase.h>
11 #include <exec/libraries.h>
12 #include <exec/memory.h>
13 #include <exec/resident.h>
14 #include <proto/exec.h>
15 #include <aros/symbolsets.h>
16 #include <dos/dosextens.h>
17 #include <dos/dostags.h>
18 #include <proto/dos.h>
19 #include <proto/alib.h>
20 #include <proto/utility.h>
21 #include <proto/partition.h>
22 #include <utility/tagitem.h>
23 #include <resources/filesysres.h>
25 #include LC_LIBDEFS_FILE
27 #include "dos_intern.h"
29 static const UBYTE version
[];
30 extern const char LIBEND
;
32 AROS_UFP3S(struct DosLibrary
*, DosInit
,
33 AROS_UFPA(ULONG
, dummy
, D0
),
34 AROS_UFPA(BPTR
, segList
, A0
),
35 AROS_UFPA(struct ExecBase
*, SysBase
, A6
));
37 const struct Resident Dos_resident
=
40 (struct Resident
*)&Dos_resident
,
42 0, /* We don't autoinit */
51 static const UBYTE version
[] = VERSION_STRING
;
53 extern const ULONG err_Numbers
[];
54 extern const char err_Strings
[];
56 static void DosExpunge(struct DosLibrary
*DOSBase
);
58 extern const APTR
GM_UNIQUENAME(FuncTable
)[];
60 THIS_PROGRAM_HANDLES_SYMBOLSET(INIT
)
61 THIS_PROGRAM_HANDLES_SYMBOLSET(EXIT
)
62 THIS_PROGRAM_HANDLES_SYMBOLSET(INITLIB
)
63 THIS_PROGRAM_HANDLES_SYMBOLSET(EXPUNGELIB
)
69 static void init_fs(struct DosLibrary
*DOSBase
)
71 struct FileSysResource
*fsr
;
72 struct Library
*PartitionBase
;
74 PartitionBase
= OpenLibrary("partition.library", 3);
76 LoadBootFileSystems();
77 CloseLibrary(PartitionBase
);
81 * Set dl_Root->rn_FileHandlerSegment to the AFS handler,
82 * if it's been loaded. Otherwise, use the first handler
83 * on the FileSystemResource list that has fse_PatchFlags
84 * set to mark it with a valid SegList
86 if ((fsr
= OpenResource("FileSystem.resource")))
88 struct FileSysEntry
*fse
;
91 ForeachNode(&fsr
->fsr_FileSysEntries
, fse
)
93 if ((fse
->fse_PatchFlags
& FSEF_SEGLIST
) && fse
->fse_SegList
)
95 /* We prefer DOS\001 */
96 if ((fse
->fse_DosType
== ID_FFS_DISK
) || (fse
->fse_DosType
== ID_FFS_muFS_DISK
))
98 defseg
= fse
->fse_SegList
;
101 /* This will remember the first defined seglist */
103 defseg
= fse
->fse_SegList
;
106 DOSBase
->dl_Root
->rn_FileHandlerSegment
= defseg
;
107 /* Add all that have both Handler and SegList defined to the Resident list */
108 ForeachNode(&fsr
->fsr_FileSysEntries
, fse
)
110 if ((fse
->fse_PatchFlags
& FSEF_HANDLER
) &&
111 (fse
->fse_PatchFlags
& FSEF_SEGLIST
) &&
112 (fse
->fse_Handler
!= BNULL
) &&
113 (fse
->fse_SegList
!= BNULL
))
115 D(bug("[DosInit] Adding \"%b\" (%p) at %p to the resident list\n",
116 fse
->fse_Handler
, BADDR(fse
->fse_Handler
), BADDR(fse
->fse_SegList
)));
117 AddSegment(AROS_BSTR_ADDR(fse
->fse_Handler
), fse
->fse_SegList
, CMD_SYSTEM
);
124 * Init routine is intentionally written by hands in order to be reentrant.
125 * Reentrancy is needed when there are already some devices mounted, but
126 * all of them are not bootable. And now we insert a floppy with OS3.1
127 * bootblock. It reenters this function...
130 AROS_UFH3S(struct DosLibrary
*, DosInit
,
131 AROS_UFHA(ULONG
, dummy
, D0
),
132 AROS_UFHA(BPTR
, segList
, A0
),
133 AROS_UFHA(struct ExecBase
*, sysBase
, A6
)
138 struct DosLibrary
*DOSBase
;
143 if (!set_call_funcs(SETNAME(INIT
), 1, 1))
146 DOSBase
= (struct DosLibrary
*)FindName(&SysBase
->LibList
, "dos.library");
148 D(bug("[DosInit] DOSBase 0x%p\n", DOSBase
));
153 struct DosInfo
*dosinfo
;
155 D(bug("[DosInit] Creating dos.library...\n"));
157 DOSBase
= (struct DosLibrary
*)MakeLibrary(GM_UNIQUENAME(FuncTable
), NULL
, NULL
, sizeof(struct IntDosBase
), BNULL
);
161 /* Initialize our header */
162 DOSBase
->dl_lib
.lib_Node
.ln_Name
= MOD_NAME_STRING
;
163 DOSBase
->dl_lib
.lib_Node
.ln_Type
= NT_LIBRARY
;
164 DOSBase
->dl_lib
.lib_Node
.ln_Pri
= RESIDENTPRI
;
165 DOSBase
->dl_lib
.lib_Version
= VERSION_NUMBER
;
166 DOSBase
->dl_lib
.lib_Revision
= REVISION_NUMBER
;
167 DOSBase
->dl_lib
.lib_IdString
= (char *)&version
[6];
168 DOSBase
->dl_lib
.lib_Flags
= LIBF_SUMUSED
|LIBF_CHANGED
;
171 * These two are allocated together with DOSBase, for reduced fragmentation.
172 * Structure pointed to by dl_Errors is intentionally read-write - who knows...
174 DOSBase
->dl_Root
= &((struct IntDosBase
*)DOSBase
)->rootNode
;
175 DOSBase
->dl_Errors
= &((struct IntDosBase
*)DOSBase
)->errors
;
177 DOSBase
->dl_Errors
->estr_Nums
= (LONG
*)err_Numbers
;
178 DOSBase
->dl_Errors
->estr_Strings
= (STRPTR
)err_Strings
;
180 /* Init the RootNode structure */
181 dosinfo
= AllocMem(sizeof(struct DosInfo
), MEMF_PUBLIC
|MEMF_CLEAR
);
188 DOSBase
->dl_Root
->rn_Info
= MKBADDR(dosinfo
);
190 taskarray
= AllocMem(sizeof(IPTR
) + sizeof(APTR
) * 20, MEMF_CLEAR
);
198 DOSBase
->dl_Root
->rn_TaskArray
= MKBADDR(taskarray
);
200 NEWLIST((struct List
*)&DOSBase
->dl_Root
->rn_CliList
);
201 InitSemaphore(&DOSBase
->dl_Root
->rn_RootLock
);
203 InitSemaphore(&dosinfo
->di_DevLock
);
204 InitSemaphore(&dosinfo
->di_EntryLock
);
205 InitSemaphore(&dosinfo
->di_DeleteLock
);
207 /* Initialize for Stricmp */
208 DOSBase
->dl_UtilityBase
= TaggedOpenLibrary(TAGGEDOPEN_UTILITY
);
209 if (!DOSBase
->dl_UtilityBase
)
215 /* Initialize for the fools that illegally used this field */
216 DOSBase
->dl_IntuitionBase
= TaggedOpenLibrary(TAGGEDOPEN_INTUITION
);
220 * I know this is bad, but I also know that the timer.device
221 * will never go away during the life of dos.library. I also
222 * don't intend to make any I/O calls using this.
224 * I also know that timer.device does exist in the device list
225 * at this point in time.
227 * I can't allocate a timerequest/MsgPort pair here anyway,
228 * because I need a separate one for each caller to Delay().
229 * However, CreateIORequest() will fail if MsgPort == NULL, so we
230 * supply some dummy value.
232 DOSBase
->dl_TimeReq
= CreateIORequest((APTR
)0xC0DEBAD0, sizeof(struct timerequest
));
233 if (!DOSBase
->dl_TimeReq
)
239 if (OpenDevice("timer.device", UNIT_VBLANK
, &DOSBase
->dl_TimeReq
->tr_node
, 0))
241 DeleteIORequest(DOSBase
->dl_TimeReq
);
242 DOSBase
->dl_TimeReq
= NULL
;
247 /* Call platform-specific init code (if any) */
248 if (!set_call_libfuncs(SETNAME(INITLIB
), 1, 1, DOSBase
))
254 /* debug.library is optional, so don't check result */
255 DebugBase
= OpenLibrary("debug.library", 0);
257 /* Initialization finished */
258 AddLibrary(&DOSBase
->dl_lib
);
264 if (CliInit(NULL
) == RETURN_OK
)
267 * We now restart the multitasking - this is done
268 * automatically by RemTask() when it switches.
272 /* We really really shouldn't ever get to this line. */
281 static void DosExpunge(struct DosLibrary
*DOSBase
)
283 struct DosInfo
*dinfo
= BADDR(DOSBase
->dl_Root
->rn_Info
);
284 struct Segment
*seg
, *stmp
;
286 D(bug("[DosInit] Expunge...\n"));
288 /* If we have anything in the Dos List,
291 if (dinfo
->di_DevInfo
!= BNULL
)
296 bug("[DosInit] Entries still in the Dos List, can't expunge.\n");
297 for (dol
= BADDR(dinfo
->di_DevInfo
); dol
!= NULL
; dol
= BADDR(dol
->dol_Next
))
299 bug("[DosInit] %d '%b'\n", dol
->dol_Type
, dol
->dol_Name
);
305 if (DOSBase
->dl_lib
.lib_OpenCnt
)
308 * Someone is holding us... Perhaps some handler started subprocess
309 * which didn't quit. Who knows...
311 D(bug("[DosInit] Open count is %d, can't expunge\n"));
315 /* Call platform-specific expunge code (if any) */
316 if (!set_call_libfuncs(SETNAME(EXPUNGELIB
), -1, 1, DOSBase
))
318 D(bug("[DosInit] Platform-dependent code failed to expunge\n"));
322 /* Close some libraries */
323 CloseLibrary(DebugBase
);
324 CloseLibrary((APTR
)DOSBase
->dl_IntuitionBase
);
325 CloseLibrary((APTR
)DOSBase
->dl_UtilityBase
);
327 /* Free the timer device */
328 if (DOSBase
->dl_TimeReq
)
330 CloseDevice(&DOSBase
->dl_TimeReq
->tr_node
);
331 DeleteIORequest(DOSBase
->dl_TimeReq
);
336 /* Remove all segments */
337 for (seg
= BADDR(dinfo
->di_ResList
); seg
!= NULL
; seg
= stmp
)
339 stmp
= BADDR(seg
->seg_Next
);
342 FreeMem(dinfo
, sizeof(*dinfo
));
346 FreeMem(BADDR(DOSBase
->dl_Root
->rn_TaskArray
), sizeof(IPTR
) + sizeof(APTR
) * 20);
348 if (DOSBase
->dl_lib
.lib_Node
.ln_Succ
)
351 * A fresh DOSBase after creation is filled with NULLs.
352 * ln_Succ will be set to something only after AddLibrary().
354 Remove(&DOSBase
->dl_lib
.lib_Node
);
357 FreeMem((char *)DOSBase
- DOSBase
->dl_lib
.lib_NegSize
, DOSBase
->dl_lib
.lib_NegSize
+ DOSBase
->dl_lib
.lib_PosSize
);
359 set_call_funcs(SETNAME(EXIT
), -1, 0);
361 D(bug("%s: Expunged.\n", __func__
));
365 * Simple open and close routines.
366 * We never auto-expunge, because if we ever do this,
367 * we won't be able to come up again. BTW, LDDemon constantly holds us open,
368 * so we always have at least one user.
370 AROS_LH1(struct DosLibrary
*, OpenLib
,
371 AROS_LHA(ULONG
, version
, D0
),
372 struct DosLibrary
*, DOSBase
, 1, Dos
)
376 /* I have one more opener. */
377 DOSBase
->dl_lib
.lib_OpenCnt
++;
383 AROS_LH0(BPTR
, CloseLib
,
384 struct DosLibrary
*, DOSBase
, 2, Dos
)
388 /* I have one fewer opener. */
389 DOSBase
->dl_lib
.lib_OpenCnt
--;