xref: /haiku/src/system/boot/loader/main.cpp (revision 23338ed551920aae841646afa77530c41efb42c8)
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 	add_stage2_driver_settings(args);
42 
43 	platform_init_video();
44 
45 	// the main platform dependent initialisation
46 	// has already taken place at this point.
47 
48 	if (vfs_init(args) < B_OK)
49 		panic("Could not initialize VFS!\n");
50 
51 	dprintf("Welcome to the Haiku boot loader!\n");
52 
53 	bool mountedAllVolumes = false;
54 
55 	Directory *volume = get_boot_file_system(args);
56 
57 	if (volume == NULL || (platform_boot_options() & BOOT_OPTION_MENU) != 0) {
58 		if (volume == NULL)
59 			puts("\tno boot path found, scan for all partitions...\n");
60 
61 		if (mount_file_systems(args) < B_OK) {
62 			// That's unfortunate, but we still give the user the possibility
63 			// to insert a CD-ROM or just rescan the available devices
64 			puts("Could not locate any supported boot devices!\n");
65 		}
66 
67 		// ToDo: check if there is only one bootable volume!
68 
69 		mountedAllVolumes = true;
70 
71 		if (user_menu(&volume) < B_OK) {
72 			// user requested to quit the loader
73 			goto out;
74 		}
75 	}
76 
77 	if (volume != NULL) {
78 		// we got a volume to boot from!
79 		status_t status;
80 		while ((status = load_kernel(args, volume)) < B_OK) {
81 			// loading the kernel failed, so let the user choose another
82 			// volume to boot from until it works
83 			volume = NULL;
84 
85 			if (!mountedAllVolumes) {
86 				// mount all other file systems, if not already happened
87 				if (mount_file_systems(args) < B_OK)
88 					panic("Could not locate any supported boot devices!\n");
89 
90 				mountedAllVolumes = true;
91 			}
92 
93 			if (user_menu(&volume) < B_OK || volume == NULL) {
94 				// user requested to quit the loader
95 				goto out;
96 			}
97 		}
98 
99 		// if everything is okay, continue booting; the kernel
100 		// is already loaded at this point and we definitely
101 		// know our boot volume, too
102 		if (status == B_OK) {
103 			register_boot_file_system(volume);
104 
105 			if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) == 0)
106 				platform_switch_to_logo();
107 
108 			load_modules(args, volume);
109 			load_driver_settings(args, volume);
110 
111 			// set up kernel args version info
112 			gKernelArgs.kernel_args_size = sizeof(kernel_args);
113 			gKernelArgs.version = CURRENT_KERNEL_ARGS_VERSION;
114 
115 			// clone the boot_volume KMessage into kernel accessible memory
116 			// note, that we need to 4 byte align the buffer and thus allocate
117 			// 3 more bytes
118 			KMessage& bootVolume = gKernelArgs.boot_volume;
119 			void* buffer = kernel_args_malloc(bootVolume.ContentSize() + 3);
120 			if (!buffer) {
121 				panic("Could not allocate memory for the boot volume kernel "
122 					"arguments");
123 			}
124 
125 			buffer = (void*)(((addr_t)buffer + 3) & ~(addr_t)0x3);
126 			memcpy(buffer, bootVolume.Buffer(), bootVolume.ContentSize());
127 			bootVolume.SetTo(buffer, bootVolume.ContentSize());
128 
129 			// ToDo: cleanup, heap_release() etc.
130 			platform_start_kernel();
131 		}
132 	}
133 
134 out:
135 	heap_release(args);
136 	return 0;
137 }
138