xref: /haiku/src/system/boot/loader/main.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2  * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "menu.h"
8 #include "loader.h"
9 #include "load_driver_settings.h"
10 
11 #include <boot/stage2.h>
12 #include <boot/vfs.h>
13 #include <boot/platform.h>
14 #include <boot/heap.h>
15 #include <boot/stdio.h>
16 
17 #include <util/kernel_cpp.h>
18 
19 
20 //#define TRACE_MAIN
21 #ifdef TRACE_MAIN
22 #	define TRACE(x) dprintf x
23 #else
24 #	define TRACE(x) ;
25 #endif
26 
27 
28 extern "C" int
29 main(stage2_args *args)
30 {
31 	TRACE(("boot(): enter\n"));
32 
33 	if (heap_init(args) < B_OK)
34 		panic("Could not initialize heap!\n");
35 
36 	TRACE(("boot(): heap initialized...\n"));
37 
38 	// construct boot_volume KMessage explicitely
39 	new(&gKernelArgs.boot_volume) KMessage;
40 
41 	platform_init_video();
42 
43 	// the main platform dependent initialisation
44 	// has already taken place at this point.
45 
46 	if (vfs_init(args) < B_OK)
47 		panic("Could not initialize VFS!\n");
48 
49 	dprintf("Welcome to the Haiku boot loader!\n");
50 
51 	bool mountedAllVolumes = false;
52 
53 	Directory *volume = get_boot_file_system(args);
54 
55 	if (volume == NULL || (platform_boot_options() & BOOT_OPTION_MENU) != 0) {
56 		if (volume == NULL)
57 			puts("\tno boot path found, scan for all partitions...\n");
58 
59 		if (mount_file_systems(args) < B_OK) {
60 			// That's unfortunate, but we still give the user the possibility
61 			// to insert a CD-ROM or just rescan the available devices
62 			puts("Could not locate any supported boot devices!\n");
63 		}
64 
65 		// ToDo: check if there is only one bootable volume!
66 
67 		mountedAllVolumes = true;
68 
69 		if (user_menu(&volume) < B_OK) {
70 			// user requested to quit the loader
71 			goto out;
72 		}
73 	}
74 
75 	if (volume != NULL) {
76 		// we got a volume to boot from!
77 		status_t status;
78 		while ((status = load_kernel(args, volume)) < B_OK) {
79 			// loading the kernel failed, so let the user choose another
80 			// volume to boot from until it works
81 			volume = NULL;
82 
83 			if (!mountedAllVolumes) {
84 				// mount all other file systems, if not already happened
85 				if (mount_file_systems(args) < B_OK)
86 					panic("Could not locate any supported boot devices!\n");
87 
88 				mountedAllVolumes = true;
89 			}
90 
91 			if (user_menu(&volume) < B_OK || volume == NULL) {
92 				// user requested to quit the loader
93 				goto out;
94 			}
95 		}
96 
97 		// if everything is okay, continue booting; the kernel
98 		// is already loaded at this point and we definitely
99 		// know our boot volume, too
100 		if (status == B_OK) {
101 			register_boot_file_system(volume);
102 
103 			if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) == 0)
104 				platform_switch_to_logo();
105 
106 			load_modules(args, volume);
107 			load_driver_settings(args, volume);
108 
109 			// set up kernel args version info
110 			gKernelArgs.kernel_args_size = sizeof(kernel_args);
111 			gKernelArgs.version = CURRENT_KERNEL_ARGS_VERSION;
112 
113 			// clone the boot_volume KMessage into kernel accessible memory
114 			// note, that we need to 4 byte align the buffer and thus allocate
115 			// 3 more bytes
116 			KMessage& bootVolume = gKernelArgs.boot_volume;
117 			void* buffer = kernel_args_malloc(bootVolume.ContentSize() + 3);
118 			if (!buffer) {
119 				panic("Could not allocate memory for the boot volume kernel "
120 					"arguments");
121 			}
122 
123 			buffer = (void*)(((addr_t)buffer + 3) & ~(addr_t)0x3);
124 			memcpy(buffer, bootVolume.Buffer(), bootVolume.ContentSize());
125 			bootVolume.SetTo(buffer, bootVolume.ContentSize());
126 
127 			// ToDo: cleanup, heap_release() etc.
128 			platform_start_kernel();
129 		}
130 	}
131 
132 out:
133 	heap_release(args);
134 	return 0;
135 }
136