Minor fixes to comments.
[AROS.git] / rom / dos / dos_init.c
blob0185a6094448102676e11bf3b4458220550e53db
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_SYMBOLSET(INIT)
61 THIS_PROGRAM_HANDLES_SYMBOLSET(EXIT)
62 THIS_PROGRAM_HANDLES_SYMBOLSET(INITLIB)
63 THIS_PROGRAM_HANDLES_SYMBOLSET(EXPUNGELIB)
64 DEFINESET(INIT)
65 DEFINESET(EXIT)
66 DEFINESET(INITLIB)
67 DEFINESET(EXPUNGELIB)
69 static void init_fs(struct DosLibrary *DOSBase)
71 struct FileSysResource *fsr;
72 struct Library *PartitionBase;
74 PartitionBase = OpenLibrary("partition.library", 3);
75 if (PartitionBase) {
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;
89 BPTR defseg = BNULL;
90 const ULONG DosMagic = 0x444f5301; /* DOS\001 */
92 ForeachNode(&fsr->fsr_FileSysEntries, fse)
94 if ((fse->fse_PatchFlags & FSEF_SEGLIST) && fse->fse_SegList)
96 /* We prefer DOS\001 */
97 if (fse->fse_DosType == DosMagic)
99 defseg = fse->fse_SegList;
100 break;
102 /* This will remember the first defined seglist */
103 if (!defseg)
104 defseg = fse->fse_SegList;
107 DOSBase->dl_Root->rn_FileHandlerSegment = defseg;
108 /* Add all that have both Handler and SegList defined to the Resident list */
109 ForeachNode(&fsr->fsr_FileSysEntries, fse)
111 if ((fse->fse_PatchFlags & FSEF_HANDLER) &&
112 (fse->fse_PatchFlags & FSEF_SEGLIST) &&
113 (fse->fse_Handler != BNULL) &&
114 (fse->fse_SegList != BNULL))
116 D(bug("[DosInit] Adding \"%b\" (%p) at %p to the resident list\n",
117 fse->fse_Handler, BADDR(fse->fse_Handler), BADDR(fse->fse_SegList)));
118 AddSegment(AROS_BSTR_ADDR(fse->fse_Handler), fse->fse_SegList, CMD_SYSTEM);
125 * Init routine is intentionally written by hands in order to be reentrant.
126 * Reentrancy is needed when there are already some devices mounted, but
127 * all of them are not bootable. And now we insert a floppy with OS3.1
128 * bootblock. It reenters this function...
131 AROS_UFH3S(struct DosLibrary *, DosInit,
132 AROS_UFHA(ULONG, dummy, D0),
133 AROS_UFHA(BPTR, segList, A0),
134 AROS_UFHA(struct ExecBase *, sysBase, A6)
137 AROS_USERFUNC_INIT
139 struct DosLibrary *DOSBase;
141 if (!SysBase)
142 SysBase = sysBase;
144 if (!set_call_funcs(SETNAME(INIT), 1, 1))
145 return NULL;
147 DOSBase = (struct DosLibrary *)FindName(&SysBase->LibList, "dos.library");
149 D(bug("[DosInit] DOSBase 0x%p\n", DOSBase));
151 if (!DOSBase)
153 IPTR *taskarray;
154 struct DosInfo *dosinfo;
156 D(bug("[DosInit] Creating dos.library...\n"));
158 DOSBase = (struct DosLibrary *)MakeLibrary(GM_UNIQUENAME(FuncTable), NULL, NULL, sizeof(struct IntDosBase), BNULL);
159 if (!DOSBase)
160 return NULL;
162 /* Initialize our header */
163 DOSBase->dl_lib.lib_Node.ln_Name = MOD_NAME_STRING;
164 DOSBase->dl_lib.lib_Node.ln_Type = NT_LIBRARY;
165 DOSBase->dl_lib.lib_Node.ln_Pri = RESIDENTPRI;
166 DOSBase->dl_lib.lib_Version = VERSION_NUMBER;
167 DOSBase->dl_lib.lib_Revision = REVISION_NUMBER;
168 DOSBase->dl_lib.lib_IdString = (char *)&version[6];
169 DOSBase->dl_lib.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
172 * These two are allocated together with DOSBase, for reduced fragmentation.
173 * Structure pointed to by dl_Errors is intentionally read-write - who knows...
175 DOSBase->dl_Root = &((struct IntDosBase *)DOSBase)->rootNode;
176 DOSBase->dl_Errors = &((struct IntDosBase *)DOSBase)->errors;
178 DOSBase->dl_Errors->estr_Nums = (LONG *)err_Numbers;
179 DOSBase->dl_Errors->estr_Strings = (STRPTR)err_Strings;
181 /* Init the RootNode structure */
182 dosinfo = AllocMem(sizeof(struct DosInfo), MEMF_PUBLIC|MEMF_CLEAR);
183 if (!dosinfo)
185 DosExpunge(DOSBase);
186 return NULL;
189 DOSBase->dl_Root->rn_Info = MKBADDR(dosinfo);
191 taskarray = AllocMem(sizeof(IPTR) + sizeof(APTR) * 20, MEMF_CLEAR);
192 if (!taskarray)
194 DosExpunge(DOSBase);
195 return NULL;
198 taskarray[0] = 20;
199 DOSBase->dl_Root->rn_TaskArray = MKBADDR(taskarray);
201 NEWLIST((struct List *)&DOSBase->dl_Root->rn_CliList);
202 InitSemaphore(&DOSBase->dl_Root->rn_RootLock);
204 InitSemaphore(&dosinfo->di_DevLock);
205 InitSemaphore(&dosinfo->di_EntryLock);
206 InitSemaphore(&dosinfo->di_DeleteLock);
208 /* Initialize for Stricmp */
209 DOSBase->dl_UtilityBase = TaggedOpenLibrary(TAGGEDOPEN_UTILITY);
210 if (!DOSBase->dl_UtilityBase)
212 DosExpunge(DOSBase);
213 return NULL;
216 /* Initialize for the fools that illegally used this field */
217 DOSBase->dl_IntuitionBase = TaggedOpenLibrary(TAGGEDOPEN_INTUITION);
220 * iaint:
221 * I know this is bad, but I also know that the timer.device
222 * will never go away during the life of dos.library. I also
223 * don't intend to make any I/O calls using this.
225 * I also know that timer.device does exist in the device list
226 * at this point in time.
228 * I can't allocate a timerequest/MsgPort pair here anyway,
229 * because I need a separate one for each caller to Delay().
230 * However, CreateIORequest() will fail if MsgPort == NULL, so we
231 * supply some dummy value.
233 DOSBase->dl_TimeReq = CreateIORequest((APTR)0xC0DEBAD0, sizeof(struct timerequest));
234 if (!DOSBase->dl_TimeReq)
236 DosExpunge(DOSBase);
237 return NULL;
240 if (OpenDevice("timer.device", UNIT_VBLANK, &DOSBase->dl_TimeReq->tr_node, 0))
242 DeleteIORequest(DOSBase->dl_TimeReq);
243 DOSBase->dl_TimeReq = NULL;
244 DosExpunge(DOSBase);
245 return NULL;
248 /* Call platform-specific init code (if any) */
249 if (!set_call_libfuncs(SETNAME(INITLIB), 1, 1, DOSBase))
251 DosExpunge(DOSBase);
252 return NULL;
255 /* debug.library is optional, so don't check result */
256 DebugBase = OpenLibrary("debug.library", 0);
258 /* Initialization finished */
259 AddLibrary(&DOSBase->dl_lib);
261 init_fs(DOSBase);
264 /* Try to boot */
265 if (CliInit(NULL) == RETURN_OK)
268 * We now restart the multitasking - this is done
269 * automatically by RemTask() when it switches.
271 RemTask(NULL);
273 /* We really really shouldn't ever get to this line. */
276 DosExpunge(DOSBase);
277 return NULL;
279 AROS_USERFUNC_EXIT
282 static void DosExpunge(struct DosLibrary *DOSBase)
284 struct DosInfo *dinfo = BADDR(DOSBase->dl_Root->rn_Info);
285 struct Segment *seg, *stmp;
287 D(bug("[DosInit] Expunge...\n"));
289 /* If we have anything in the Dos List,
290 * we can't die.
292 if (dinfo->di_DevInfo != BNULL)
294 #if DEBUG
295 struct DosList *dol;
297 bug("[DosInit] Entries still in the Dos List, can't expunge.\n");
298 for (dol = BADDR(dinfo->di_DevInfo); dol != NULL; dol = BADDR(dol->dol_Next))
300 bug("[DosInit] %d '%b'\n", dol->dol_Type, dol->dol_Name);
302 #endif
303 return;
306 if (DOSBase->dl_lib.lib_OpenCnt)
309 * Someone is holding us... Perhaps some handler started subprocess
310 * which didn't quit. Who knows...
312 D(bug("[DosInit] Open count is %d, can't expunge\n"));
313 return;
316 /* Call platform-specific expunge code (if any) */
317 if (!set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 1, DOSBase))
319 D(bug("[DosInit] Platform-dependent code failed to expunge\n"));
320 return;
323 /* Close some libraries */
324 CloseLibrary(DebugBase);
325 CloseLibrary((APTR)DOSBase->dl_IntuitionBase);
326 CloseLibrary((APTR)DOSBase->dl_UtilityBase);
328 /* Free the timer device */
329 if (DOSBase->dl_TimeReq)
331 CloseDevice(&DOSBase->dl_TimeReq->tr_node);
332 DeleteIORequest(DOSBase->dl_TimeReq);
335 if (dinfo)
337 /* Remove all segments */
338 for (seg = BADDR(dinfo->di_ResList); seg != NULL; seg = stmp)
340 stmp = BADDR(seg->seg_Next);
341 FreeVec(seg);
343 FreeMem(dinfo, sizeof(*dinfo));
346 /* Free memory */
347 FreeMem(BADDR(DOSBase->dl_Root->rn_TaskArray), sizeof(IPTR) + sizeof(APTR) * 20);
349 if (DOSBase->dl_lib.lib_Node.ln_Succ)
352 * A fresh DOSBase after creation is filled with NULLs.
353 * ln_Succ will be set to something only after AddLibrary().
355 Remove(&DOSBase->dl_lib.lib_Node);
358 FreeMem((char *)DOSBase - DOSBase->dl_lib.lib_NegSize, DOSBase->dl_lib.lib_NegSize + DOSBase->dl_lib.lib_PosSize);
360 set_call_funcs(SETNAME(EXIT), -1, 0);
362 D(bug("%s: Expunged.\n", __func__));
366 * Simple open and close routines.
367 * We never auto-expunge, because if we ever do this,
368 * we won't be able to come up again. BTW, LDDemon constantly holds us open,
369 * so we always have at least one user.
371 AROS_LH1(struct DosLibrary *, OpenLib,
372 AROS_LHA(ULONG, version, D0),
373 struct DosLibrary *, DOSBase, 1, Dos)
375 AROS_LIBFUNC_INIT
377 /* I have one more opener. */
378 DOSBase->dl_lib.lib_OpenCnt++;
379 return DOSBase;
381 AROS_LIBFUNC_EXIT
384 AROS_LH0(BPTR, CloseLib,
385 struct DosLibrary *, DOSBase, 2, Dos)
387 AROS_LIBFUNC_INIT
389 /* I have one fewer opener. */
390 DOSBase->dl_lib.lib_OpenCnt--;
391 return BNULL;
393 AROS_LIBFUNC_EXIT