Indentation fix, cleanup.
[AROS.git] / arch / all-hosted / filesys / emul_handler / boot.c
blob54ab76fec31485b83be2c38568496bd1911ed902
1 /*
2 Copyright 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Emergency console launcher for hosted AROS
6 Lang: english
7 */
9 #define DEBUG 0
11 #include <aros/asmcall.h>
12 #include <aros/debug.h>
13 #include <dos/dosextens.h>
14 #include <dos/stdio.h>
15 #include <exec/resident.h>
16 #include <graphics/modeid.h>
17 #include <utility/tagitem.h>
18 #include <proto/exec.h>
19 #include <proto/dos.h>
20 #include <proto/graphics.h>
22 #include "emul_intern.h"
24 #include "expansion_intern.h"
25 #include "bootflags.h"
27 #undef GfxBase
29 static const UBYTE version[];
30 extern const char emul_End;
32 AROS_UFP3S(APTR, EmulBoot,
33 AROS_UFPA(ULONG, dummy, D0),
34 AROS_UFPA(BPTR, segList, A0),
35 AROS_UFPA(struct ExecBase *, SysBase, A6));
37 const struct Resident EmulBoot_resident =
39 RTC_MATCHWORD,
40 (struct Resident *)&EmulBoot_resident,
41 (APTR)&emul_End,
42 RTF_AFTERDOS,
44 NT_PROCESS,
45 -128,
46 "Emergency console",
47 (STRPTR)&version[6],
48 EmulBoot
51 static const UBYTE version[] = "$VER: emul-handler emergency console v2.0";
53 AROS_UFH3(APTR, EmulBoot,
54 AROS_UFPA(ULONG, dummy, D0),
55 AROS_UFPA(BPTR, segList, A0),
56 AROS_UFPA(struct ExecBase *, SysBase, A6));
58 AROS_USERFUNC_INIT
60 struct emulbase *EmulBase;
61 struct GfxBase *GfxBase;
62 struct DosLibrary *DOSBase;
63 struct Library *ExpansionBase;
64 struct MsgPort *emulport;
65 struct Process *me;
66 struct FileHandle *fh_stdin, *fh_stdout;
67 LONG rc;
69 DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36);
71 ExpansionBase = OpenLibrary("expansion.library", 0);
72 if (ExpansionBase)
74 ULONG BootFlags = IntExpBase(ExpansionBase)->BootFlags;
75 if (!(BootFlags & BF_NO_DISPLAY_DRIVERS))
77 if (DOSBase)
79 BPTR seg = LoadSeg("C:AROSMonDrvs");
80 if (seg != BNULL)
82 STRPTR args = "NOCOMPOSITION";
83 BPTR oldin, oldout;
85 oldin = SelectInput(Open("NIL:", MODE_OLDFILE));
86 oldout= SelectOutput(Open("NIL:", MODE_NEWFILE));
87 RunCommand(seg, AROS_STACKSIZE, args, strlen(args));
88 SelectInput(oldin);
89 SelectOutput(oldout);
91 /* We don't care about the return code */
92 UnLoadSeg(seg);
94 /* make sure the boot process doesnt try to load the drivers again.. */
95 IntExpBase(ExpansionBase)->BootFlags = BootFlags | BF_NO_DISPLAY_DRIVERS;
98 CloseLibrary(ExpansionBase);
102 * This actually checks if we have at least one display mode in the database.
103 * This means that we actually can open a display. If not, we enter emergency
104 * shell on host's console.
106 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 36);
107 if (GfxBase)
109 ULONG displayid = NextDisplayInfo(INVALID_ID);
111 CloseLibrary((APTR)GfxBase);
112 if (displayid != INVALID_ID)
114 if (DOSBase)
115 CloseLibrary((APTR)DOSBase);
116 /* Everything is ok, continue booting */
117 return NULL;
122 * Set up our own console I/O and run a shell without Startup-Sequence and other stuff.
123 * A failure in the following code causes dos.library to continue booting.
124 * Without display drivers this will end up in AN_SysScrn alert.
126 if (!DOSBase)
127 return NULL;
129 EmulBase = OpenResource("emul-handler");
130 if (!EmulBase)
131 return NULL;
133 emulport = DeviceProc("EMU");
134 if (!emulport)
135 return NULL;
137 fh_stdin = AllocDosObject(DOS_FILEHANDLE, NULL);
138 if (!fh_stdin)
140 CloseLibrary(&DOSBase->dl_lib);
141 return NULL;
144 fh_stdout = AllocDosObject(DOS_FILEHANDLE, NULL);
145 if (!fh_stdout)
147 CloseLibrary(&DOSBase->dl_lib);
148 return NULL;
151 /* A little bit of magic */
152 fh_stdin->fh_Interactive = DOSTRUE;
153 fh_stdin->fh_Type = emulport;
154 fh_stdin->fh_Arg1 = (SIPTR)EmulBase->eb_stdin;
155 fh_stdout->fh_Interactive = DOSTRUE;
156 fh_stdout->fh_Type = emulport;
157 fh_stdout->fh_Arg1 = (SIPTR)EmulBase->eb_stdout;
159 SetVBuf(MKBADDR(fh_stdin) , NULL, BUF_LINE, -1);
160 SetVBuf(MKBADDR(fh_stdout), NULL, BUF_LINE, -1);
162 Close(Input());
163 Close(Output());
165 D(bug("[EmulBoot] Selecting input and output for DOS\n"));
167 SelectInput(MKBADDR(fh_stdin));
168 SelectOutput(MKBADDR(fh_stdout));
169 me = (struct Process *)FindTask(NULL);
170 me->pr_CES = MKBADDR(fh_stdout);
172 SetPrompt("%N> ");
174 PutStr("Display driver(s) failed to initialize. Entering emergency shell.\n"
175 "EndCLI will quit AROS.\n");
177 rc = SystemTags("", SYS_Asynch, FALSE, SYS_Background, FALSE, TAG_DONE);
178 if (rc == -1)
180 /* Everything is too bad */
181 PutStr("Cannot open boot console. System halted.\n");
182 RemTask(NULL);
185 ShutdownA(SD_ACTION_POWEROFF);
187 return NULL; /* Just shut up the compiler, we'll never get here */
189 AROS_USERFUNC_EXIT