Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/boot2/mboot.c

1/*
2 File added by David F. Elliott <dfe@cox.net> on 2007/06/26
3*/
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.h"
8
9#include "mboot.h"
10
11#define OFFSET_1MEG 0x100000
12#define BAD_BOOT_DEVICE 0xffffffff
13
14struct multiboot_info *gMI;
15
16#if UNUSED
17int multiboot_timeout=0;
18int multiboot_timeout_set=0;
19int multiboot_partition=0;
20int multiboot_partition_set=0;
21#endif
22// Global multiboot info, if using multiboot.
23
24extern void continue_at_low_address(void);
25
26// prototype hi_multiboot and keep its implementation below multiboot_to_boot
27// to ensure that it doesn't get inlined by the compiler
28// We don't want it inlined because we specifically want the stack frame
29// pointer to be as high as possible and the hi_multiboot function
30// copies multiboot_info onto its stack.
31uint32_t hi_multiboot(int multiboot_magic, struct multiboot_info *mi_orig);
32// prototype dochainload for the same reason.
33void dochainload();
34
35
36// This assumes that the address of the first argument to the function will
37// be exactly 4 bytes above the address of the return address.
38// It is intended to be used as an lvalue with a statement like this -= OFFSET_1MEG;
39#define RETURN_ADDRESS_USING_FIRST_ARG(arg) \
40 (*(uint32_t*)((char*)&(arg) - 4))
41
42#define FIX_RETURN_ADDRESS_USING_FIRST_ARG(arg) \
43 RETURN_ADDRESS_USING_FIRST_ARG(arg) -= OFFSET_1MEG
44
45extern void jump_to_chainbooter();
46extern unsigned char chainbootdev;
47extern unsigned char chainbootflag;
48
49void chainLoad();
50void waitThenReload();
51/*
52int multibootRamdiskReadBytes( int biosdev, unsigned int blkno,
53 unsigned int byteoff,
54 unsigned int byteCount, void * buffer );
55int multiboot_get_ramdisk_info(int biosdev, struct driveInfo *dip);
56static long multiboot_LoadExtraDrivers(FileLoadDrivers_t FileLoadDrivers_p);
57*/
58// Starts off in the multiboot context 1 MB high but eventually gets into low memory
59// and winds up with a bootdevice in eax which is all that boot() wants
60// This lets the stack pointer remain very high.
61// If we were to call boot directly from multiboot then the whole multiboot_info
62// would be on the stack which would possibly be using way too much stack.
63void multiboot_to_boot(int multiboot_magic, struct multiboot_info *mi_orig)
64{
65 uint32_t bootdevice = hi_multiboot(multiboot_magic, mi_orig);
66 if(bootdevice != BAD_BOOT_DEVICE)
67 {
68 // boot only returns to do a chain load.
69 for(;;)
70 { // NOTE: boot only uses the last byte (the drive number)
71 common_boot(bootdevice);
72 if(chainbootflag)
73 chainLoad();
74 else
75 waitThenReload();
76 }
77 }
78 // Avoid returning to high-memory address which isn't valid in the segment
79 // we are now in.
80 // Calling sleep() ensures the user ought to be able to use Ctrl+Alt+Del
81 // because the BIOS will have interrupts on.
82 for(;;)
83 sleep(10);
84 // NOTE: *IF* we needed to return we'd have to fix up our return address to
85 // be in low memory using the same trick as below.
86 // However, there doesn't seem to be any point in returning to assembly
87 // particularly when the remaining code merely halts the processor.
88}
89
90void chainLoad()
91{
92 /* TODO: We ought to load the appropriate partition table, for example
93 the MBR if booting a primary partition or the particular extended
94 partition table if booting a logical drive. For example, the
95 regular MS MBR booter will relocate itself (e.g. the MBR) from
96 0:7C00 to 0:0600 and will use SI as the offset when reading
97 the partition data from itself. Thus when it jumps to the partition
98 boot sector, SI will be 0x600 + 446 + i<<4 where i is the partition
99 table index.
100
101 On the other hand, our code for the non-Multiboot case doesn't do
102 this either, although GRUB does.
103 */
104
105 const unsigned char *bootcode = (const unsigned char*)0x7c00;
106 if(bootcode[0x1fe] == 0x55 && bootcode[0x1ff] == 0xaa)
107 {
108 printf("Calling chainbooter\n");
109 jump_to_chainbooter();
110 /* NORETURN */
111 }
112 else
113 {
114 printf("Bad chain boot sector magic: %02x%02x\n", bootcode[0x1fe], bootcode[0x1ff]);
115 }
116}
117
118void waitThenReload()
119{
120 /* FIXME: Ctrl+Alt+Del does not work under Boot Camp */
121uint8_t i = 5;
122 printf("Darwin booter exited for some reason.\n");
123 printf("Please reboot (Ctrl+Alt+Del) your machine.\n");
124 printf("Restarting Darwin booter in %d seconds...",i);
125 sleep(1);
126while (1 < i--) {
127printf("%d...",i);
128sleep(1);
129}
130printf("0\n");
131}
132
133// Declare boot2_sym as an opaque struct so it can't be converted to a pointer
134// i.e. ensure the idiot programmer (me) makes sure to use address-of
135// Technically it's a function but it's real mode code and we sure don't
136// want to call it under any circumstances.
137extern struct {} boot2_sym asm("boot2");
138
139//char *patch_code_start;
140
141// prototype multiboot and keep its implementation below hi_multiboot to
142// ensure that it doesn't get inlined by the compiler
143static inline uint32_t multiboot(int multiboot_magic, struct multiboot_info *mi);
144
145
146/*!
147 Returns a pointer to the first safe address we can use for stowing the multiboot info.
148 This might actually be a bit pedantic because mboot.c32 and GRUB both stow the multiboot
149 info in low memory meaning that the >= 128 MB location we choose is plenty high enough.
150 */
151void *determine_safe_hi_addr(int multiboot_magic, struct multiboot_info *mi_orig)
152{
153 // hi_addr must be at least up in 128MB+ space so it doesn't get clobbered
154 void *hi_addr = (void*)PREBOOT_DATA;
155
156 // Fail if the magic isn't correct. We'll complain later.
157 if(multiboot_magic != MULTIBOOT_INFO_MAGIC)
158 return NULL;
159 // Make sure the command-line isn't in high memory.
160 if(mi_orig->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE)
161 {
162 char *end = mi_orig->mi_cmdline;
163 if(end != NULL)
164 {
165 for(; *end != '\0'; ++end)
166 ;
167 ++end;
168 if( (void*)end > hi_addr)
169 hi_addr = end;
170 }
171 }
172 // Make sure the module information isn't in high memory
173 if(mi_orig->mi_flags & MULTIBOOT_INFO_HAS_MODS)
174 {
175 struct multiboot_module *modules = (void*)mi_orig->mi_mods_addr;
176 int i;
177 for(i=0; i < mi_orig->mi_mods_count; ++i)
178 {
179 // make sure the multiboot_module struct itself won't get clobbered
180 void *modinfo_end = modules+i+1;
181 if(modinfo_end > hi_addr)
182 hi_addr = modinfo_end;
183 // make sure the module itself won't get clobbered
184 modinfo_end = (void*)modules[i].mm_mod_end;
185 if(modinfo_end > hi_addr)
186 hi_addr = modinfo_end;
187 // make sure the module string doesn't get clobbered
188 char *end = modules[i].mm_string;
189 for(; *end != '\0'; ++end)
190 ;
191 ++end;
192 modinfo_end = end;
193 if(modinfo_end > hi_addr)
194 hi_addr = modinfo_end;
195 }
196 }
197 // TODO: Copy syms (never needed), mmap, drives, config table, loader name, apm table, VBE info
198
199 // Round up to page size
200 hi_addr = (void*)(((uint32_t)hi_addr + 0xfff) & ~(uint32_t)0xfff);
201 return hi_addr;
202}
203
204/*!
205 Like malloc but with a preceding input/output parameter which points to the next available
206 location for data. The original value of *hi_addr is returned and *hi_addr is incremented
207 by size bytes.
208 */
209void * _hi_malloc(void **hi_addr, size_t size)
210{
211 void *ret = *hi_addr;
212 *hi_addr += size;
213 return ret;
214}
215
216/*!
217 Like strdup but with a preceding input/output parameter. The original value of *hi_addr is
218 returned and *hi_addr is incremented by the number of bytes necessary to complete the string
219 copy including its NUL terminator.
220 */
221char * _hi_strdup(void **hi_addr, char *src)
222{
223 char *dstStart;
224 char *dst = dstStart = *hi_addr;
225 for(; *src != '\0'; ++src, ++dst, ++(*hi_addr))
226 *dst = *src;
227 *dst = '\0';
228 ++(*hi_addr);
229 return dstStart;
230}
231
232// Convenience macros
233#define hi_malloc(size) _hi_malloc(&hi_addr, (size))
234#define hi_strdup(src) _hi_strdup(&hi_addr, (src))
235
236/*!
237 Copies the Multiboot info and any associated data (e.g. various strings and any multiboot modules)
238 up to very high RAM (above 128 MB) to ensure it doesn't get clobbered by the booter.
239 */
240struct multiboot_info * copyMultibootInfo(int multiboot_magic, struct multiboot_info *mi_orig)
241{
242 void *hi_addr = determine_safe_hi_addr(multiboot_magic, mi_orig);
243 if(hi_addr == NULL)
244 return NULL;
245
246 struct multiboot_info *mi_copy = hi_malloc(sizeof(*mi_copy));
247 memcpy(mi_copy, mi_orig, sizeof(*mi_copy));
248
249 // Copy the command line
250 if(mi_orig->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE)
251 {
252 mi_copy->mi_cmdline = hi_strdup(mi_orig->mi_cmdline);
253 }
254 // Copy the loader name
255 if(mi_orig->mi_flags & MULTIBOOT_INFO_HAS_LOADER_NAME)
256 {
257 mi_copy->mi_loader_name = hi_strdup(mi_orig->mi_loader_name);
258 }
259 // Copy the module info
260 if(mi_orig->mi_flags & MULTIBOOT_INFO_HAS_MODS)
261 {
262 struct multiboot_module *dst_modules = hi_malloc(sizeof(*dst_modules)*mi_orig->mi_mods_count);
263 struct multiboot_module *src_modules = (void*)mi_orig->mi_mods_addr;
264 mi_copy->mi_mods_addr = (uint32_t)dst_modules;
265
266 // Copy all of the module info plus the actual module into high memory
267 int i;
268 for(i=0; i < mi_orig->mi_mods_count; ++i)
269 {
270 // Assume mod_end is 1 past the actual end (i.e. it is start + size, not really end (i.e. start + size - 1))
271 // This is what GRUB and mboot.c32 do although the spec is unclear on this.
272 uint32_t mod_length = src_modules[i].mm_mod_end - src_modules[i].mm_mod_start;
273
274 dst_modules[i].mm_mod_start = (uint32_t)hi_malloc(mod_length);
275 dst_modules[i].mm_mod_end = (uint32_t)dst_modules[i].mm_mod_start + mod_length;
276 memcpy((char*)dst_modules[i].mm_mod_start, (char*)src_modules[i].mm_mod_start, mod_length);
277
278 dst_modules[i].mm_string = hi_strdup(src_modules[i].mm_string);
279 dst_modules[i].mm_reserved = src_modules[i].mm_reserved;
280 }
281 }
282 // Make sure that only stuff that didn't need to be copied or that we did deep copy is indicated in the copied struct.
283 mi_copy->mi_flags &= MULTIBOOT_INFO_HAS_MEMORY | MULTIBOOT_INFO_HAS_BOOT_DEVICE | MULTIBOOT_INFO_HAS_CMDLINE | MULTIBOOT_INFO_HAS_LOADER_NAME | MULTIBOOT_INFO_HAS_MODS;
284
285 return mi_copy;
286}
287
288// When we enter, we're actually 1 MB high.
289// Fortunately, memcpy is position independent, and it's all we need
290uint32_t hi_multiboot(int multiboot_magic, struct multiboot_info *mi_orig)
291{
292 // Copy the multiboot info out of the way.
293 // We can't bitch about the magic yet because printf won't work
294 // because it contains an absolute location of putchar which
295 // contains absolute locations to other things which eventually
296 // makes a BIOS call from real mode which of course won't work
297 // because we're stuck in extended memory at this point.
298 struct multiboot_info *mi_p = copyMultibootInfo(multiboot_magic, mi_orig);
299
300
301//memcpy(patch_code_start, (char*)&boot2_sym + OFFSET_1MEG, 0x5fe00 /* 383.5k */);
302
303 // Get us in to low memory so we can run everything
304
305 // We cannot possibly be more than 383.5k and copying extra won't really hurt anything
306 // We use the address of the assembly entrypoint to get our starting location.
307 memcpy(&boot2_sym, (char*)&boot2_sym + OFFSET_1MEG, 0x5fe00 /* 383.5k */);
308
309 // This is a little assembler routine that returns to us in the correct selector
310 // instead of the kernel selector we're running in now and at the correct
311 // instruction pointer ( current minus 1 MB ). It does not fix our return
312 // address nor does it fix the return address of our caller.
313 continue_at_low_address();
314
315 // Now fix our return address.
316 FIX_RETURN_ADDRESS_USING_FIRST_ARG(multiboot_magic);
317
318 // We can now do just about anything, including return to our caller correctly.
319 // However, our caller must fix his return address if he wishes to return to
320 // his caller and so on and so forth.
321
322 /* Zero the BSS and initialize malloc */
323 initialize_runtime();
324
325 gMI = mi_p;
326
327 /* Set up a temporary bootArgs so we can call console output routines
328 like printf that check the v_display. Note that we purposefully
329 do not initialize anything else at this early stage.
330
331 We are reasonably sure we're already in text mode if GRUB booted us.
332 This is the same assumption that initKernBootStruct makes.
333 We could check the multiboot info I guess, but why bother?
334 */
335 boot_args temporaryBootArgsData;
336 bzero(&temporaryBootArgsData, sizeof(boot_args));
337 bootArgs = &temporaryBootArgsData;
338 bootArgs->Video.v_display = VGA_TEXT_MODE;
339
340
341 // Since we call multiboot ourselves, its return address will be correct.
342 // That is unless it's inlined in which case it does not matter.
343 uint32_t bootdevice = multiboot(multiboot_magic, mi_p);
344 // We're about to exit and temporaryBootArgs will no longer be valid
345 bootArgs = NULL;
346 return bootdevice;
347}
348
349// This is the meat of our implementation. It grabs the boot device from
350// the multiboot_info and returns it as is. If it fails it returns
351// BAD_BOOT_DEVICE. We can call an awful lot of libsa and libsaio but
352// we need to take care not to call anything that requires malloc because
353// it won't be initialized until boot() does it.
354static inline uint32_t multiboot(int multiboot_magic, struct multiboot_info *mi)
355{
356 if(multiboot_magic != MULTIBOOT_INFO_MAGIC)
357 {
358 printf("Wrong Multiboot magic\n");
359 sleep(2);
360 return BAD_BOOT_DEVICE;
361 }
362 printf("Multiboot info @0x%x\n", (uint32_t)mi);
363 if(mi->mi_flags & MULTIBOOT_INFO_HAS_LOADER_NAME)
364 printf("Loaded by %s\n", mi->mi_loader_name);
365
366 // Multiboot puts boot device in high byte
367 // Normal booter wants it in low byte
368 int bootdevice = mi->mi_boot_device_drive;
369
370 bool doSelectDevice = false;
371 if(mi->mi_flags & MULTIBOOT_INFO_HAS_BOOT_DEVICE)
372 {
373 printf("Boot device 0x%x\n", bootdevice);
374 }
375 else
376 {
377 printf("Multiboot info does not include chosen boot device\n");
378 doSelectDevice = true;
379 bootdevice = BAD_BOOT_DEVICE;
380 }
381 if(mi->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE)
382 {
383 const char *val;
384 int size;
385
386 if(getValueForBootKey(mi->mi_cmdline, "biosdev", &val, &size))
387 {
388 char *endptr;
389 int intVal = strtol(val, &endptr, 16 /* always hex */);
390 if(*val != '\0' && (*endptr == '\0' || *endptr == ' ' || *endptr == '\t'))
391 {
392 printf("Boot device overridden to %02x with biosdev=%s\n", intVal, val);
393 bootdevice = intVal;
394 doSelectDevice = false;
395 }
396 else
397 doSelectDevice = true;
398 }
399#if UNUSED
400 if(getValueForBootKey(mi->mi_cmdline, "timeout", &val, &size))
401 {
402 char *endptr;
403 int intVal = strtol(val, &endptr, 0);
404 if(*val != '\0' && (*endptr == '\0' || *endptr == ' ' || *endptr == '\t'))
405 {
406 printf("Timeout overridden to %d with timeout=%s\n", intVal, val);
407 multiboot_timeout = intVal;
408 multiboot_timeout_set = 1;
409 }
410 }
411
412 if(getValueForBootKey(mi->mi_cmdline, "partno", &val, &size))
413 {
414 char *endptr;
415 int intVal = strtol(val, &endptr, 0);
416 if(*val != '\0' && (*endptr == '\0' || *endptr == ' ' || *endptr == '\t'))
417 {
418 printf("Default partition overridden to %d with timeout=%s\n", intVal, val);
419 multiboot_partition = intVal;
420 multiboot_partition_set = 1;
421 }
422 }
423#endif
424 }
425 if(bootdevice == BAD_BOOT_DEVICE)
426 sleep(2); // pause for a second before halting
427 return bootdevice;
428}
429

Archive Download this file

Revision: 789