Fixed some typos/errors in comments and docs.
[AROS.git] / rom / dos / dos_init.c
blobde9f5c1017429eb6d7072268920ad5af307066be
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Header for dos.library
6 Lang: english
7 */
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 =
39 RTC_MATCHWORD,
40 (struct Resident *)&Dos_resident,
41 (APTR)&LIBEND,
42 0, /* We don't autoinit */
43 VERSION_NUMBER,
44 NT_LIBRARY,
45 RESIDENTPRI,
46 MOD_NAME_STRING,
47 (STRPTR)&version[6],
48 DosInit
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_SYMBOLSETS
61 DEFINESET(INITLIB)
62 DEFINESET(EXPUNGELIB)
64 static void init_fs(struct DosLibrary *DOSBase)
66 struct FileSysResource *fsr;
67 struct Library *PartitionBase;
69 PartitionBase = OpenLibrary("partition.library", 3);
70 if (PartitionBase) {
71 LoadBootFileSystems();
72 CloseLibrary(PartitionBase);
76 * Set dl_Root->rn_FileHandlerSegment to the AFS handler,
77 * if it's been loaded. Otherwise, use the first handler
78 * on the FileSystemResource list that has fse_PatchFlags
79 * set to mark it with a valid SegList
81 if ((fsr = OpenResource("FileSystem.resource")))
83 struct FileSysEntry *fse;
84 BPTR defseg = BNULL;
85 const ULONG DosMagic = 0x444f5301; /* DOS\001 */
87 ForeachNode(&fsr->fsr_FileSysEntries, fse)
89 if ((fse->fse_PatchFlags & FSEF_SEGLIST) && fse->fse_SegList)
91 /* We prefer DOS\001 */
92 if (fse->fse_DosType == DosMagic)
94 defseg = fse->fse_SegList;
95 break;
97 /* This will remember the first defined seglist */
98 if (!defseg)
99 defseg = fse->fse_SegList;
102 DOSBase->dl_Root->rn_FileHandlerSegment = defseg;
103 /* Add all that have both Handler and SegList defined to the Resident list */
104 ForeachNode(&fsr->fsr_FileSysEntries, fse)
106 if ((fse->fse_PatchFlags & FSEF_HANDLER) &&
107 (fse->fse_PatchFlags & FSEF_SEGLIST) &&
108 (fse->fse_Handler != BNULL) &&
109 (fse->fse_SegList != BNULL))
111 D(bug("[DosInit] Adding \"%b\" (%p) at %p to the resident list\n",
112 fse->fse_Handler, BADDR(fse->fse_Handler), BADDR(fse->fse_SegList)));
113 AddSegment(AROS_BSTR_ADDR(fse->fse_Handler), fse->fse_SegList, CMD_SYSTEM);
120 * Init routine is intentionally written by hands in order to be reentrant.
121 * Reentrancy is needed when there are already some devices mounted, but
122 * all of them are not bootable. And now we insert a floppy with OS3.1
123 * bootblock. It reenters this function...
126 AROS_UFH3S(struct DosLibrary *, DosInit,
127 AROS_UFHA(ULONG, dummy, D0),
128 AROS_UFHA(BPTR, segList, A0),
129 AROS_UFHA(struct ExecBase *, SysBase, A6)
132 AROS_USERFUNC_INIT
134 struct DosLibrary *DOSBase = (struct DosLibrary *)FindName(&SysBase->LibList, "dos.library");
136 D(bug("[DosInit] DOSBase 0x%p\n", DOSBase));
138 if (!DOSBase)
140 IPTR *taskarray;
141 struct DosInfo *dosinfo;
143 D(bug("[DosInit] Creating dos.library...\n"));
145 DOSBase = (struct DosLibrary *)MakeLibrary(GM_UNIQUENAME(FuncTable), NULL, NULL, sizeof(struct IntDosBase), BNULL);
146 if (!DOSBase)
147 return NULL;
149 /* Initialize our header */
150 DOSBase->dl_lib.lib_Node.ln_Name = MOD_NAME_STRING;
151 DOSBase->dl_lib.lib_Node.ln_Type = NT_LIBRARY;
152 DOSBase->dl_lib.lib_Node.ln_Pri = RESIDENTPRI;
153 DOSBase->dl_lib.lib_Version = VERSION_NUMBER;
154 DOSBase->dl_lib.lib_Revision = REVISION_NUMBER;
155 DOSBase->dl_lib.lib_IdString = (char *)&version[6];
156 DOSBase->dl_lib.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
159 * These two are allocated together with DOSBase, for reduced fragmentation.
160 * Structure pointed to by dl_Errors is intentionally read-write - who knows...
162 DOSBase->dl_Root = &((struct IntDosBase *)DOSBase)->rootNode;
163 DOSBase->dl_Errors = &((struct IntDosBase *)DOSBase)->errors;
165 DOSBase->dl_Errors->estr_Nums = (LONG *)err_Numbers;
166 DOSBase->dl_Errors->estr_Strings = (STRPTR)err_Strings;
168 /* Init the RootNode structure */
169 dosinfo = AllocMem(sizeof(struct DosInfo), MEMF_PUBLIC|MEMF_CLEAR);
170 if (!dosinfo)
172 DosExpunge(DOSBase);
173 return NULL;
176 DOSBase->dl_Root->rn_Info = MKBADDR(dosinfo);
178 taskarray = AllocMem(sizeof(IPTR) + sizeof(APTR) * 20, MEMF_CLEAR);
179 if (!taskarray)
181 DosExpunge(DOSBase);
182 return NULL;
185 taskarray[0] = 20;
186 DOSBase->dl_Root->rn_TaskArray = MKBADDR(taskarray);
188 NEWLIST((struct List *)&DOSBase->dl_Root->rn_CliList);
189 InitSemaphore(&DOSBase->dl_Root->rn_RootLock);
191 InitSemaphore(&dosinfo->di_DevLock);
192 InitSemaphore(&dosinfo->di_EntryLock);
193 InitSemaphore(&dosinfo->di_DeleteLock);
195 /* Initialize for Stricmp */
196 DOSBase->dl_UtilityBase = TaggedOpenLibrary(TAGGEDOPEN_UTILITY);
197 if (!DOSBase->dl_UtilityBase)
199 DosExpunge(DOSBase);
200 return NULL;
203 /* Initialize for the fools that illegally used this field */
204 DOSBase->dl_IntuitionBase = TaggedOpenLibrary(TAGGEDOPEN_INTUITION);
207 * iaint:
208 * I know this is bad, but I also know that the timer.device
209 * will never go away during the life of dos.library. I also
210 * don't intend to make any I/O calls using this.
212 * I also know that timer.device does exist in the device list
213 * at this point in time.
215 * I can't allocate a timerequest/MsgPort pair here anyway,
216 * because I need a separate one for each caller to Delay().
217 * However, CreateIORequest() will fail if MsgPort == NULL, so we
218 * supply some dummy value.
220 DOSBase->dl_TimeReq = CreateIORequest((APTR)0xC0DEBAD0, sizeof(struct timerequest));
221 if (!DOSBase->dl_TimeReq)
223 DosExpunge(DOSBase);
224 return NULL;
227 if (OpenDevice("timer.device", UNIT_VBLANK, &DOSBase->dl_TimeReq->tr_node, 0))
229 DeleteIORequest(DOSBase->dl_TimeReq);
230 DOSBase->dl_TimeReq = NULL;
231 DosExpunge(DOSBase);
232 return NULL;
235 /* Call platform-specific init code (if any) */
236 if (!set_call_libfuncs(SETNAME(INITLIB), 1, 1, DOSBase))
238 DosExpunge(DOSBase);
239 return NULL;
242 /* debug.library is optional, so don't check result */
243 DebugBase = OpenLibrary("debug.library", 0);
245 /* Initialization finished */
246 AddLibrary(&DOSBase->dl_lib);
248 init_fs(DOSBase);
251 /* Try to boot */
252 if (CliInit(NULL) == RETURN_OK)
255 * We now restart the multitasking - this is done
256 * automatically by RemTask() when it switches.
258 RemTask(NULL);
260 /* We really really shouldn't ever get to this line. */
263 DosExpunge(DOSBase);
264 return NULL;
266 AROS_USERFUNC_EXIT
269 static void DosExpunge(struct DosLibrary *DOSBase)
271 struct DosInfo *dinfo = BADDR(DOSBase->dl_Root->rn_Info);
272 struct Segment *seg, *stmp;
274 D(bug("[DosInit] Expunge...\n"));
276 /* If we have anything in the Dos List,
277 * we can't die.
279 if (dinfo->di_DevInfo != BNULL)
281 #if DEBUG
282 struct DosList *dol;
284 bug("[DosInit] Entries still in the Dos List, can't expunge.\n");
285 for (dol = BADDR(dinfo->di_DevInfo); dol != NULL; dol = BADDR(dol->dol_Next))
287 bug("[DosInit] %d '%b'\n", dol->dol_Type, dol->dol_Name);
289 #endif
290 return;
293 if (DOSBase->dl_lib.lib_OpenCnt)
296 * Someone is holding us... Perhaps some handler started subprocess
297 * which didn't quit. Who knows...
299 D(bug("[DosInit] Open count is %d, can't expunge\n"));
300 return;
303 /* Call platform-specific expunge code (if any) */
304 if (!set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 1, DOSBase))
306 D(bug("[DosInit] Platform-dependent code failed to expunge\n"));
307 return;
310 /* Close some libraries */
311 CloseLibrary(DebugBase);
312 CloseLibrary((APTR)DOSBase->dl_IntuitionBase);
313 CloseLibrary((APTR)DOSBase->dl_UtilityBase);
315 /* Free the timer device */
316 if (DOSBase->dl_TimeReq)
318 CloseDevice(&DOSBase->dl_TimeReq->tr_node);
319 DeleteIORequest(DOSBase->dl_TimeReq);
322 if (dinfo)
324 /* Remove all segments */
325 for (seg = BADDR(dinfo->di_ResList); seg != NULL; seg = stmp)
327 stmp = BADDR(seg->seg_Next);
328 FreeVec(seg);
330 FreeMem(dinfo, sizeof(*dinfo));
333 /* Free memory */
334 FreeMem(BADDR(DOSBase->dl_Root->rn_TaskArray), sizeof(IPTR) + sizeof(APTR) * 20);
336 if (DOSBase->dl_lib.lib_Node.ln_Succ)
339 * A fresh DOSBase after creation is filled with NULLs.
340 * ln_Succ will be set to something only after AddLibrary().
342 Remove(&DOSBase->dl_lib.lib_Node);
345 FreeMem((char *)DOSBase - DOSBase->dl_lib.lib_NegSize, DOSBase->dl_lib.lib_NegSize + DOSBase->dl_lib.lib_PosSize);
346 D(bug("%s: Expunged.\n", __func__));
350 * Simple open and close routines.
351 * We never auto-expunge, because if we ever do this,
352 * we won't be able to come up again. BTW, LDDemon constantly holds us open,
353 * so we always have at least one user.
355 AROS_LH1(struct DosLibrary *, OpenLib,
356 AROS_LHA(ULONG, version, D0),
357 struct DosLibrary *, DOSBase, 1, Dos)
359 AROS_LIBFUNC_INIT
361 /* I have one more opener. */
362 DOSBase->dl_lib.lib_OpenCnt++;
363 return DOSBase;
365 AROS_LIBFUNC_EXIT
368 AROS_LH0(BPTR, CloseLib,
369 struct DosLibrary *, DOSBase, 2, Dos)
371 AROS_LIBFUNC_INIT
373 /* I have one fewer opener. */
374 DOSBase->dl_lib.lib_OpenCnt--;
375 return BNULL;
377 AROS_LIBFUNC_EXIT