WIP: add an initial skeleton for a real scsi.device based upon the ata device impleme...
[AROS.git] / rom / dos / dos_init.c
blobc74e825d2e743c01fe58fac3a16fc37627b9cc5c
1 /*
2 Copyright © 1995-2019, 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;
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;
99 break;
101 /* This will remember the first defined seglist */
102 if (!defseg)
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)
136 AROS_USERFUNC_INIT
138 struct DosLibrary *DOSBase;
140 if (!SysBase)
141 SysBase = sysBase;
143 if (!set_call_funcs(SETNAME(INIT), 1, 1))
144 return NULL;
146 DOSBase = (struct DosLibrary *)FindName(&SysBase->LibList, "dos.library");
148 D(bug("[DosInit] DOSBase 0x%p\n", DOSBase));
150 if (!DOSBase)
152 IPTR *taskarray;
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);
158 if (!DOSBase)
159 return NULL;
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);
182 if (!dosinfo)
184 DosExpunge(DOSBase);
185 return NULL;
188 DOSBase->dl_Root->rn_Info = MKBADDR(dosinfo);
190 taskarray = AllocMem(sizeof(IPTR) + sizeof(APTR) * 20, MEMF_CLEAR);
191 if (!taskarray)
193 DosExpunge(DOSBase);
194 return NULL;
197 taskarray[0] = 20;
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)
211 DosExpunge(DOSBase);
212 return NULL;
215 /* Initialize for the fools that illegally used this field */
216 DOSBase->dl_IntuitionBase = TaggedOpenLibrary(TAGGEDOPEN_INTUITION);
219 * iaint:
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)
235 DosExpunge(DOSBase);
236 return NULL;
239 if (OpenDevice("timer.device", UNIT_VBLANK, &DOSBase->dl_TimeReq->tr_node, 0))
241 DeleteIORequest(DOSBase->dl_TimeReq);
242 DOSBase->dl_TimeReq = NULL;
243 DosExpunge(DOSBase);
244 return NULL;
247 /* Call platform-specific init code (if any) */
248 if (!set_call_libfuncs(SETNAME(INITLIB), 1, 1, DOSBase))
250 DosExpunge(DOSBase);
251 return NULL;
254 /* debug.library is optional, so don't check result */
255 DebugBase = OpenLibrary("debug.library", 0);
257 /* Initialization finished */
258 AddLibrary(&DOSBase->dl_lib);
260 init_fs(DOSBase);
263 /* Try to boot */
264 if (CliInit(NULL) == RETURN_OK)
267 * We now restart the multitasking - this is done
268 * automatically by RemTask() when it switches.
270 RemTask(NULL);
272 /* We really really shouldn't ever get to this line. */
275 DosExpunge(DOSBase);
276 return NULL;
278 AROS_USERFUNC_EXIT
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,
289 * we can't die.
291 if (dinfo->di_DevInfo != BNULL)
293 #if DEBUG
294 struct DosList *dol;
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);
301 #endif
302 return;
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"));
312 return;
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"));
319 return;
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);
334 if (dinfo)
336 /* Remove all segments */
337 for (seg = BADDR(dinfo->di_ResList); seg != NULL; seg = stmp)
339 stmp = BADDR(seg->seg_Next);
340 FreeVec(seg);
342 FreeMem(dinfo, sizeof(*dinfo));
345 /* Free memory */
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)
374 AROS_LIBFUNC_INIT
376 /* I have one more opener. */
377 DOSBase->dl_lib.lib_OpenCnt++;
378 return DOSBase;
380 AROS_LIBFUNC_EXIT
383 AROS_LH0(BPTR, CloseLib,
384 struct DosLibrary *, DOSBase, 2, Dos)
386 AROS_LIBFUNC_INIT
388 /* I have one fewer opener. */
389 DOSBase->dl_lib.lib_OpenCnt--;
390 return BNULL;
392 AROS_LIBFUNC_EXIT