e32boot.cpp revision 1.1 1 /* $NetBSD: e32boot.cpp,v 1.1 2013/04/28 12:11:26 kiyohara Exp $ */
2 /*
3 * Copyright (c) 2012, 2013 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <e32base.h>
29 #include <e32cons.h>
30 #include <e32def.h>
31 #include <e32hal.h>
32 #include <e32svr.h> /* XXXXX */
33 #include <w32std.h>
34
35 #include "e32boot.h"
36 #include "netbsd.h"
37 #include "../../../include/bootinfo.h"
38
39 CConsoleBase *console;
40 LOCAL_C NetBSD *LoadNetBSDL(void);
41 LOCAL_C struct btinfo_common *CreateBootInfo(TAny *);
42 LOCAL_C struct btinfo_common *FindBootInfoL(struct btinfo_common *, int);
43 TUint SummaryBootInfoMemory(struct btinfo_common *);
44 LOCAL_C void E32BootL(void);
45
46 struct memmap {
47 TUint address;
48 TUint size; /* KB */
49 };
50 struct memmap series5_4m[] = {{ 0xc0000000, 512 }, { 0xc0100000, 512 },
51 { 0xc0400000, 512 }, { 0xc0500000, 512 },
52 { 0xc1000000, 512 }, { 0xc1100000, 512 },
53 { 0xc1400000, 512 }, { 0xc1500000, 512 }};
54 struct memmap series5_8m[] = {{ 0xc0000000, 512 }, { 0xc0100000, 512 },
55 { 0xc0400000, 512 }, { 0xc0500000, 512 },
56 { 0xc1000000, 512 }, { 0xc1100000, 512 },
57 { 0xc1400000, 512 }, { 0xc1500000, 512 },
58 { 0xd0000000, 512 }, { 0xd0100000, 512 },
59 { 0xd0400000, 512 }, { 0xd0500000, 512 },
60 { 0xd1000000, 512 }, { 0xd1100000, 512 },
61 { 0xd1400000, 512 }, { 0xd1500000, 512 }};
62 struct memmap revo[] = {{ 0xc0000000, 4096 }, { 0xc0800000, 4096 }};
63 struct memmap revopuls[] = {{ 0xc0000000, 4096 }, { 0xc0800000, 4096 },
64 { 0xd0000000, 4096 }, { 0xd0800000, 4096 }};
65 struct memmap series5mx_16m[] = {{ 0xc0000000, 8192 }, { 0xc1000000, 8192 }};
66 struct memmap series5mxpro_24m[] = {{ 0xc0000000, 8192 }, { 0xc1000000, 8192 },
67 { 0xd0000000, 4096 }, { 0xd0800000, 4096 }};
68 struct memmap series5mxpro_32m[] = {{ 0xc0000000, 8192 }, { 0xc1000000, 8192 },
69 { 0xd0000000, 8192 }, { 0xd1000000, 8192 }};
70 struct memmap series7_16m[] = {{ 0xc0000000, 16384 }};
71 struct memmap series7_32m[] = {{ 0xc0000000, 16384 }, { 0xc8000000, 16384 }};
72
73 struct {
74 char *model;
75 TInt width;
76 TInt height;
77 TUint memsize;
78 struct memmap *memmaps;
79 } memmaps[] = {
80 { "SERIES5 R1", 640, 240, 4096, series5_4m },
81 { "SERIES5 R1", 640, 240, 8192, series5_8m },
82 { "SERIES5 R1", 640, 320, 4096, series5_4m }, /* Geofox One */
83 { "SERIES5 R1", 640, 320, 8192, series5_8m }, /* Geofox One */
84 // { "SERIES5 R1", 640, 320, 16384, one_16m },
85 { "SERIES5 R1", 320, 200, 4096, series5_4m }, /* Osaris */
86 // { "SERIES5 R1", 320, 200, 16384, osaris_16m },
87 { "SERIES5mx", 480, 160, 8192, revo },
88 { "SERIES5mx", 480, 160, 16384, revopuls },
89 { "SERIES5mx", 640, 240, 16384, series5mx_16m },
90 { "SERIES5mx", 640, 240, 24576, series5mxpro_24m },
91 { "SERIES5mx", 640, 240, 32768, series5mxpro_32m },
92 { "SERIES7", 800, 600, 16384, series7_16m },
93 { "SERIES7", 800, 600, 32768, series7_32m },
94 };
95
96 class E32BootLogicalChannel : public RLogicalChannel {
97 public:
98 TInt DoCreate(const TDesC *aChan, TInt aUnit, const TDesC *aDriver,
99 const TDesC8 *anInfo)
100 {
101
102 return RLogicalChannel::DoCreate(E32BootName, TVersion(0, 0, 0),
103 aChan, aUnit, aDriver, anInfo);
104 }
105
106 TInt DoControl(TInt aFunction, TAny *a1)
107 {
108
109 return RLogicalChannel::DoControl(aFunction, a1);
110 }
111
112 TInt DoControl(TInt aFunction, TAny *a1, TAny *a2)
113 {
114
115 return RLogicalChannel::DoControl(aFunction, a1, a2);
116 }
117 };
118
119
120 LOCAL_C void
121 E32BootL(void)
122 {
123 E32BootLogicalChannel *E32BootChannel = new E32BootLogicalChannel;
124 NetBSD *netbsd = NULL;
125 TScreenInfoV01 screenInfo;
126 TPckg<TScreenInfoV01> sI(screenInfo);
127 TBuf<32> ldd;
128 TInt err;
129 TUint membytes;
130 TAny *buf, *safeAddress;
131 struct btinfo_common *bootinfo;
132 struct btinfo_model *model;
133 struct btinfo_video *video;
134
135 console =
136 Console::NewL(E32BootName, TSize(KConsFullScreen, KConsFullScreen));
137
138 buf = User::AllocL(ALIGN_SAFE_PAGE_SIZE); /* bootinfo buffer */
139
140 /* Put banner */
141 console->Printf(_L("\n"));
142 console->Printf(_L(">> %s, Revision %s\n"),
143 bootprog_name, bootprog_rev);
144
145 UserSvr::ScreenInfo(sI);
146 if (!screenInfo.iScreenAddressValid)
147 User::Leave(KErrNotSupported);
148 safeAddress = screenInfo.iScreenAddress;
149
150 bootinfo = CreateBootInfo((TAny *)PAGE_ALIGN(buf));
151
152 model = (struct btinfo_model *)FindBootInfoL(bootinfo, BTINFO_MODEL);
153 console->Printf(_L(">> Model %s\n"), model->model);
154
155 membytes = SummaryBootInfoMemory(bootinfo);
156 console->Printf(_L(">> Memory %d k\n"), membytes / 1024);
157
158 video = (struct btinfo_video *)FindBootInfoL(bootinfo, BTINFO_VIDEO);
159 console->Printf(_L(">> Video %d x %d\n"), video->width, video->height);
160
161 console->Printf(_L("\n"));
162
163 TRAP(err, netbsd = LoadNetBSDL());
164 if (err != KErrNone)
165 User::Leave(err);
166 else if (netbsd == NULL)
167 return;
168 console->Printf(_L("\nLoaded\n"));
169
170 netbsd->ParseHeader();
171
172 /* Load logical device(kernel part of e32boot). */
173 if (_L(model->model).CompareF(_L("SERIES5 R1")) == 0)
174 ldd = _L("e32boot-s5.ldd");
175 else if (_L(model->model).CompareF(_L("SERIES5mx")) == 0)
176 ldd = _L("e32boot-s5mx.ldd");
177 // else if (_L(model->model).CompareF(_L("SERIES7")) == 0)
178 // ldd = _L("e32boot-s7.ldd"); // not yet.
179 else {
180 console->Printf(_L("Not Supported machine\n"));
181 console->Getch();
182 User::Leave(KErrNotSupported);
183 }
184 err = User::LoadLogicalDevice(ldd);
185 if (err != KErrNone && err != KErrAlreadyExists) {
186 console->Printf(_L("LoadLogicalDevice failed: %d\n"), err);
187 console->Getch();
188 User::Leave(err);
189 }
190 /* Create channel to kernel part. */
191 err = E32BootChannel->DoCreate(NULL, KNullUnit, NULL, NULL);
192 if (err == KErrNone) {
193 E32BootChannel->DoControl(KE32BootSetSafeAddress, safeAddress);
194 E32BootChannel->DoControl(KE32BootBootNetBSD, netbsd, bootinfo);
195 } else {
196 console->Printf(_L("DoCreate failed: %d\n"), err);
197 console->Getch();
198 }
199
200 User::FreeLogicalDevice(ldd);
201 if (err != KErrNone)
202 User::Leave(err);
203 }
204
205 GLDEF_C TInt E32Main(void) /* main function called by E32 */
206 {
207
208 __UHEAP_MARK;
209 CTrapCleanup *cleanup = CTrapCleanup::New();
210
211 TRAPD(error, E32BootL());
212 __ASSERT_ALWAYS(!error, User::Panic(E32BootName, error));
213
214 delete cleanup;
215 __UHEAP_MARKEND;
216 return 0;
217 }
218
219 LOCAL_C NetBSD *
220 LoadNetBSDL(void)
221 {
222 NetBSD *netbsd = NULL;
223 TBuf<KMaxCommandLine> input;
224 TPtrC Default = _L("C:\\netbsd");
225 TPtrC Prompt = _L("Boot: ");
226 TInt pos, err;
227 TBool retry;
228
229 input.Zero();
230 retry = false;
231 console->Printf(Prompt);
232 console->Printf(_L("["));
233 console->Printf(Default);
234 console->Printf(_L("]: "));
235 console->SetPos(Prompt.Length() +
236 _L("[").Length() +
237 Default.Length() +
238 _L("]: ").Length());
239 pos = 0;
240 while (1) {
241 TChar gChar = console->Getch();
242 switch (gChar) {
243 case EKeyEscape:
244 return NULL;
245
246 case EKeyEnter:
247 break;
248
249 case EKeyBackspace:
250 if (pos > 0) {
251 pos--;
252 input.Delete(pos, 1);
253 }
254 break;
255
256 default:
257 if (gChar.IsPrint()) {
258 if (input.Length() < KMaxCommandLine) {
259 TBuf<0x02> b;
260 b.Append(gChar);
261 input.Insert(pos++, b);
262 }
263 }
264 break;
265 }
266 if (gChar == EKeyEnter) {
267 if (input.Length() > 0) {
268 TBufC<KMaxCommandLine> kernel =
269 TBufC<KMaxCommandLine>(input);
270
271 TRAP(err, netbsd = NetBSD::New(kernel));
272 } else {
273 TRAP(err, netbsd = NetBSD::New(Default));
274 }
275 if (err == 0 && netbsd != NULL)
276 break;
277 console->Printf(_L("\nLoad failed: %d\n"), err);
278
279 input.Zero();
280 console->Printf(Prompt);
281 pos = 0;
282 retry = true;
283 }
284 TInt base = Prompt.Length();
285 if (!retry)
286 base += (_L("[").Length() + Default.Length() +
287 _L("]: ").Length());
288 console->SetPos(base + pos);
289 console->ClearToEndOfLine();
290 console->SetPos(base);
291 console->Write(input);
292 console->SetPos(base + pos);
293 }
294
295 return netbsd;
296 }
297
298 #define KB * 1024
299
300 LOCAL_C struct btinfo_common *
301 CreateBootInfo(TAny *buf)
302 {
303 TMachineInfoV1Buf MachInfo;
304 TMemoryInfoV1Buf MemInfo;
305 struct btinfo_common *bootinfo, *common;
306 struct btinfo_model *model;
307 struct btinfo_memory *memory;
308 struct btinfo_video *video;
309 struct memmap *memmap;
310 TUint memsize;
311 TUint i;
312
313 UserHal::MachineInfo(MachInfo);
314 UserHal::MemoryInfo(MemInfo);
315
316 common = bootinfo = (struct btinfo_common *)buf;
317
318 /* Set machine name to bootinfo. */
319 common->len = sizeof(struct btinfo_model);
320 common->type = BTINFO_MODEL;
321 model = (struct btinfo_model *)common;
322 Mem::Copy(model->model, &MachInfo().iMachineName[0],
323 sizeof(model->model));
324 common = &(model + 1)->common;
325
326 /* Set video width/height to bootinfo. */
327 common->len = sizeof(struct btinfo_video);
328 common->type = BTINFO_VIDEO;
329 video = (struct btinfo_video *)common;
330 video->width = MachInfo().iDisplaySizeInPixels.iWidth;
331 video->height = MachInfo().iDisplaySizeInPixels.iHeight;
332 common = &(video + 1)->common;
333
334 /* Set memory size to bootinfo. */
335 memsize = MemInfo().iTotalRamInBytes / 1024;
336 for (i = 0; i < sizeof(memmaps) / sizeof(memmaps[0]); i++) {
337 if (_L(memmaps[i].model).CompareF(_L(model->model)) == 0 &&
338 memmaps[i].width == video->width &&
339 memmaps[i].height == video->height &&
340 memmaps[i].memsize == memsize) {
341 memmap = memmaps[i].memmaps;
342 while (memsize > 0) {
343 common->len = sizeof(struct btinfo_memory);
344 common->type = BTINFO_MEMORY;
345 memory = (struct btinfo_memory *)common;
346 memory->address = memmap->address;
347 memory->size = memmap->size KB;
348 common = &(memory + 1)->common;
349 memsize -= memmap->size;
350 memmap++;
351 }
352 break;
353 }
354 }
355 if (i == sizeof(memmaps) / sizeof(memmaps[0])) {
356 common->len = sizeof(struct btinfo_memory);
357 common->type = BTINFO_MEMORY;
358 memory = (struct btinfo_memory *)common;
359 memory->address = 0xc0000000; /* default is here */
360 memory->size = 4096 KB; /* XXXXX */
361 common = &(memory + 1)->common;
362 }
363
364 common->len = 0;
365 common->type = BTINFO_NONE;
366
367 /* Terminate bootinfo. */
368 return bootinfo;
369 }
370
371 #undef KB
372
373 LOCAL_C struct btinfo_common *
374 FindBootInfoL(struct btinfo_common *bootinfo, int type)
375 {
376 struct btinfo_common *entry;
377
378 entry = bootinfo;
379 while (entry->type != BTINFO_NONE) {
380 if (entry->type == type)
381 return entry;
382 entry = (struct btinfo_common *)((int)entry + entry->len);
383 }
384 User::Leave(KErrNotFound);
385
386 /* NOTREACHED */
387
388 return NULL;
389 }
390
391 TUint
392 SummaryBootInfoMemory(struct btinfo_common *bootinfo)
393 {
394 struct btinfo_common *entry;
395 struct btinfo_memory *memory;
396 TUint memsize = 0;
397
398 entry = bootinfo;
399 while (entry->type != BTINFO_NONE) {
400 if (entry->type == BTINFO_MEMORY) {
401 memory = (struct btinfo_memory *)entry;
402 memsize += memory->size;
403 }
404 entry = (struct btinfo_common *)((int)entry + entry->len);
405 }
406 return memsize;
407 }
408