xref: /haiku/src/system/boot/loader/main.cpp (revision dc0bc42494264bb8d17b6b081647377b5601570a)
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/PathBlacklist.h>
16 #include <boot/stdio.h>
17 #include <boot/net/NetStack.h>
18 
19 #include "file_systems/packagefs/packagefs.h"
20 
21 
22 //#define TRACE_MAIN
23 #ifdef TRACE_MAIN
24 #	define TRACE(x) dprintf x
25 #else
26 #	define TRACE(x) ;
27 #endif
28 
29 
30 void *__dso_handle;
31 
32 
33 extern "C" int
34 main(stage2_args *args)
35 {
36 	TRACE(("boot(): enter\n"));
37 
38 	if (heap_init(args) < B_OK)
39 		panic("Could not initialize heap!\n");
40 
41 	TRACE(("boot(): heap initialized...\n"));
42 
43 	// set debug syslog default
44 #if KDEBUG_ENABLE_DEBUG_SYSLOG
45 	gKernelArgs.keep_debug_output_buffer = true;
46 	gKernelArgs.previous_debug_size = true;
47 		// used as a boolean indicator until initialized for the kernel
48 #endif
49 
50 	add_stage2_driver_settings(args);
51 
52 	platform_init_video();
53 
54 	// the main platform dependent initialisation
55 	// has already taken place at this point.
56 
57 	if (vfs_init(args) < B_OK)
58 		panic("Could not initialize VFS!\n");
59 
60 	dprintf("Welcome to the Haiku boot loader!\n");
61 
62 	bool mountedAllVolumes = false;
63 
64 	BootVolume bootVolume;
65 	PathBlacklist pathBlacklist;
66 
67 	if (get_boot_file_system(args, bootVolume) != B_OK
68 		|| (platform_boot_options() & BOOT_OPTION_MENU) != 0) {
69 		if (!bootVolume.IsValid())
70 			puts("\tno boot path found, scan for all partitions...\n");
71 
72 		if (mount_file_systems(args) < B_OK) {
73 			// That's unfortunate, but we still give the user the possibility
74 			// to insert a CD-ROM or just rescan the available devices
75 			puts("Could not locate any supported boot devices!\n");
76 		}
77 
78 		// ToDo: check if there is only one bootable volume!
79 
80 		mountedAllVolumes = true;
81 
82 		if (user_menu(bootVolume, pathBlacklist) < B_OK) {
83 			// user requested to quit the loader
84 			goto out;
85 		}
86 	}
87 
88 	if (bootVolume.IsValid()) {
89 		// we got a volume to boot from!
90 		load_driver_settings(args, bootVolume.RootDirectory());
91 
92 		status_t status;
93 		while ((status = load_kernel(args, bootVolume)) < B_OK) {
94 			// loading the kernel failed, so let the user choose another
95 			// volume to boot from until it works
96 			bootVolume.Unset();
97 
98 			if (!mountedAllVolumes) {
99 				// mount all other file systems, if not already happened
100 				if (mount_file_systems(args) < B_OK)
101 					panic("Could not locate any supported boot devices!\n");
102 
103 				mountedAllVolumes = true;
104 			}
105 
106 			if (user_menu(bootVolume, pathBlacklist) != B_OK
107 				|| !bootVolume.IsValid()) {
108 				// user requested to quit the loader
109 				goto out;
110 			}
111 		}
112 
113 		// if everything is okay, continue booting; the kernel
114 		// is already loaded at this point and we definitely
115 		// know our boot volume, too
116 		if (status == B_OK) {
117 			if (bootVolume.IsPackaged()) {
118 				packagefs_apply_path_blacklist(bootVolume.SystemDirectory(),
119 					pathBlacklist);
120 			}
121 
122 			register_boot_file_system(bootVolume);
123 
124 			if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) == 0)
125 				platform_switch_to_logo();
126 
127 			load_modules(args, bootVolume);
128 
129 			gKernelArgs.ucode_data = NULL;
130 			gKernelArgs.ucode_data_size = 0;
131 			platform_load_ucode(bootVolume);
132 
133 			// apply boot settings
134 			apply_boot_settings();
135 
136 			// set up kernel args version info
137 			gKernelArgs.kernel_args_size = sizeof(kernel_args);
138 			gKernelArgs.version = CURRENT_KERNEL_ARGS_VERSION;
139 			if (gKernelArgs.ucode_data == NULL)
140 				gKernelArgs.kernel_args_size = kernel_args_size_v1;
141 
142 			// clone the boot_volume KMessage into kernel accessible memory
143 			// note, that we need to 8-byte align the buffer and thus allocate
144 			// 7 more bytes
145 			void* buffer = kernel_args_malloc(gBootVolume.ContentSize() + 7);
146 			if (!buffer) {
147 				panic("Could not allocate memory for the boot volume kernel "
148 					"arguments");
149 			}
150 
151 			buffer = (void*)(((addr_t)buffer + 7) & ~(addr_t)0x7);
152 			memcpy(buffer, gBootVolume.Buffer(), gBootVolume.ContentSize());
153 			gKernelArgs.boot_volume = buffer;
154 			gKernelArgs.boot_volume_size = gBootVolume.ContentSize();
155 
156 			platform_cleanup_devices();
157 			// TODO: cleanup, heap_release() etc.
158 			heap_print_statistics();
159 			platform_start_kernel();
160 		}
161 	}
162 
163 out:
164 	heap_release(args);
165 	return 0;
166 }
167