xref: /haiku/src/system/boot/loader/main.cpp (revision a09c983cc63b6d50c46a0f2a872fb1adbffcc363)
1 /*
2  * Copyright 2003-2013, 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 
18 //#define TRACE_MAIN
19 #ifdef TRACE_MAIN
20 #	define TRACE(x) dprintf x
21 #else
22 #	define TRACE(x) ;
23 #endif
24 
25 
26 extern "C" int
27 main(stage2_args *args)
28 {
29 	TRACE(("boot(): enter\n"));
30 
31 	if (heap_init(args) < B_OK)
32 		panic("Could not initialize heap!\n");
33 
34 	TRACE(("boot(): heap initialized...\n"));
35 
36 	// set debug syslog default
37 #if KDEBUG_ENABLE_DEBUG_SYSLOG
38 	gKernelArgs.keep_debug_output_buffer = true;
39 #endif
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 			// apply boot settings
112 			apply_boot_settings();
113 
114 			// set up kernel args version info
115 			gKernelArgs.kernel_args_size = sizeof(kernel_args);
116 			gKernelArgs.version = CURRENT_KERNEL_ARGS_VERSION;
117 
118 			// clone the boot_volume KMessage into kernel accessible memory
119 			// note, that we need to 4 byte align the buffer and thus allocate
120 			// 3 more bytes
121 			void* buffer = kernel_args_malloc(gBootVolume.ContentSize() + 3);
122 			if (!buffer) {
123 				panic("Could not allocate memory for the boot volume kernel "
124 					"arguments");
125 			}
126 
127 			buffer = (void*)(((addr_t)buffer + 3) & ~(addr_t)0x3);
128 			memcpy(buffer, gBootVolume.Buffer(), gBootVolume.ContentSize());
129 			gKernelArgs.boot_volume = buffer;
130 			gKernelArgs.boot_volume_size = gBootVolume.ContentSize();
131 
132 			// ToDo: cleanup, heap_release() etc.
133 			platform_start_kernel();
134 		}
135 	}
136 
137 out:
138 	heap_release(args);
139 	return 0;
140 }
141