Re-enabled use of AROS.Boot file due to lack of general enthusiasm for
[tangerine.git] / arch / all-mingw32 / bootstrap / bootstrap.c
bloba7adc1ff224eae96294419c5dea1d85c77659e88
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <signal.h>
5 #include <windows.h>
6 #include <sys/stat.h>
7 #include <aros/system.h>
9 #define __typedef_LONG /* LONG, ULONG, WORD, BYTE and BOOL are declared in Windows headers. Looks like everything */
10 #define __typedef_WORD /* is the same except BOOL. It's defined to short on AROS and to int on Windows. This means */
11 #define __typedef_BYTE /* that you can't use it in OS-native part of the code and can't use any AROS structure */
12 #define __typedef_BOOL /* definition that contains BOOL. */
13 typedef unsigned AROS_16BIT_TYPE UWORD;
14 typedef unsigned char UBYTE;
16 #include <aros/kernel.h>
17 #include <utility/tagitem.h>
19 #include "debug.h"
20 #include "elfloader32.h"
21 #include "hostlib.h"
22 #include "shutdown.h"
23 #include "../kernel/hostinterface.h"
25 #define D(x)
27 static unsigned char __bss_track[32768];
28 struct TagItem km[64];
29 char bootstrapdir[MAX_PATH];
30 char *bootstrapname;
31 char *cmdline;
33 typedef int (*kernel_entry_fun_t)(struct TagItem *);
35 #define BASE_ALIGNMENT 16
38 * Some helpful functions that link us to the underlying host OS.
39 * Without them we would not be able to estabilish any interaction with it.
41 struct HostInterface HostIFace = {
42 Host_HostLib_Open,
43 Host_HostLib_Close,
44 Host_HostLib_GetPointer,
45 Host_HostLib_FreeErrorStr,
46 Host_HostLib_GetInterface,
47 Host_VKPrintF,
48 Host_PutChar,
49 Host_Shutdown
52 void *SysBase;
54 int main(int argc, char ** argv)
56 char *error;
57 unsigned long BadSyms;
58 struct TagItem *t;
59 int x;
60 struct stat st;
61 char _use_hostmem = 0;
62 int i = 1;
63 unsigned int memSize = 64;
64 char *kernel = "boot\\aros-mingw32";
65 char *KernelArgs = NULL;
67 GetCurrentDirectory(MAX_PATH, bootstrapdir);
68 bootstrapname = argv[0];
69 cmdline = GetCommandLine();
71 while (i < argc)
73 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
75 printf
77 "AROS for Windows\n"
78 "usage: %s [options] [kernel arguments]\n"
79 "Availible options:\n"
80 " -h show this page\n"
81 " -m <size> allocate <size> Megabytes of memory for AROS\n"
82 " (default is 64M)\n"
83 " -k <file> use <file> as a kernel\n"
84 " (default is boot\\aros-mingw32)\n"
85 " --help same as '-h'\n"
86 " --memsize <size> same as '-m <size>'\n"
87 " --kernel <file> same as '-k'\n"
88 " --hostmem Let AROS use the host operating system's facilities to\n"
89 " manage memory.\n"
90 "\n"
91 "Please report bugs to the AROS development team. http://www.aros.org/\n",
92 argv[0]
94 return 0;
96 else if (!strcmp(argv[i], "--memsize") || !strcmp(argv[i], "-m"))
98 i++;
99 x = 0;
100 memSize = 0;
101 while ((argv[i])[x] >= '0' && (argv[i])[x] <= '9')
103 memSize = memSize * 10 + (argv[i])[x] - '0';
104 x++;
106 i++;
108 else if (!strcmp(argv[i], "--kernel") || !strcmp(argv[i], "-k"))
110 kernel = argv[++i];
111 i++;
113 else if (!strcmp(argv[i], "--hostmem"))
115 _use_hostmem = 1;
116 i++;
118 else
119 break;
122 D(printf("[Bootstrap] %ld arguments processed\n", i));
123 D(printf("[Bootstrap] Raw command line: %s\n", cmdline));
124 if (i < argc) {
125 KernelArgs = cmdline;
126 while(isspace(*KernelArgs++));
127 for (x = 0; x < i; x++) {
128 while (!isspace(*KernelArgs++));
129 while (isspace(*KernelArgs))
130 KernelArgs++;
133 D(printf("[Bootstrap] Kernel arguments: %s\n", KernelArgs));
135 if (!stat("..\\AROS.boot", &st)) {
136 chdir("..");
139 D(printf("[Bootstrap] allocating working mem: %iMb\n",memSize));
141 void * memory = malloc((memSize << 20));
143 //fill in kernel message related to allocated ram regions
144 struct TagItem *tag = km;
146 tag->ti_Tag = KRN_KernelBss;
147 tag->ti_Data = (unsigned long)__bss_track;
148 tag++;
150 /* FIXME: These tags should point to a memory map in PC BIOS format, not to memory itself.
151 This is a temporary solution because hosted kernel should translate all AllocMem() requests
152 to host's allocation requests. In future a full-featured memory map will be implemented in order
153 to support loadable modules. */
154 tag->ti_Tag = KRN_MMAPAddress;
155 tag->ti_Data = (unsigned long)memory;
156 tag++;
158 tag->ti_Tag = KRN_MMAPLength;
159 tag->ti_Data = (unsigned long)(memSize << 20);
160 tag++;
162 //load elf-kernel and fill in the bootinfo
163 void * file = fopen(kernel, "rb");
164 size_t ksize=0;
165 if (file)
167 fseek(file,0,SEEK_END);
168 ksize = ftell(file);
169 ksize += BASE_ALIGNMENT - ksize % BASE_ALIGNMENT;
170 printf("[Bootstrap] opened \"%s\"(%p) size:%p\n", kernel, file, ksize);
171 fseek(file,0,SEEK_SET);
172 } else {
173 printf("[Bootstrap] unable to open kernel \"%s\"\n", kernel);
174 return -1;
176 unsigned int bufsize = 10*ksize;
177 void * buf = malloc(bufsize);
178 void * base = buf + ksize;
179 printf("[Bootstrap] memory allocated: %p-%p kernelBase: %p\n",buf,buf+bufsize,base);
180 set_base_address(base, __bss_track, &SysBase);
181 load_elf_file(file,0);
182 kernel_entry_fun_t kernel_entry_fun = (kernel_entry_fun_t)base;
184 tag->ti_Tag = KRN_KernelLowest;
185 tag->ti_Data = (unsigned long)kernel_lowest();
186 tag++;
188 tag->ti_Tag = KRN_KernelHighest;
189 tag->ti_Data = (unsigned long)kernel_highest();
190 tag++;
192 tag->ti_Tag = KRN_CmdLine;
193 tag->ti_Data = KernelArgs;
194 tag++;
196 tag->ti_Tag = KRN_HostInterface;
197 tag->ti_Data = &HostIFace;
198 tag++;
200 tag->ti_Tag = TAG_DONE;
202 printf("[Bootstrap] entering kernel@%p...\n",kernel_entry_fun);
203 int retval = kernel_entry_fun(km);
205 printf("kernel returned %i\n",retval);