xref: /haiku/src/system/boot/loader/main.cpp (revision a5061ecec55353a5f394759473f1fd6df04890da)
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/PathBlocklist.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 	PathBlocklist pathBlocklist;
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, pathBlocklist) < 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 
91 		// TODO: fix for riscv64
92 #ifndef __riscv
93 		load_driver_settings(args, bootVolume.RootDirectory());
94 #endif
95 		status_t status;
96 		while ((status = load_kernel(args, bootVolume)) < B_OK) {
97 			// loading the kernel failed, so let the user choose another
98 			// volume to boot from until it works
99 			bootVolume.Unset();
100 
101 			if (!mountedAllVolumes) {
102 				// mount all other file systems, if not already happened
103 				if (mount_file_systems(args) < B_OK)
104 					panic("Could not locate any supported boot devices!\n");
105 
106 				mountedAllVolumes = true;
107 			}
108 
109 			if (user_menu(bootVolume, pathBlocklist) != B_OK
110 				|| !bootVolume.IsValid()) {
111 				// user requested to quit the loader
112 				goto out;
113 			}
114 		}
115 
116 		// if everything is okay, continue booting; the kernel
117 		// is already loaded at this point and we definitely
118 		// know our boot volume, too
119 		if (status == B_OK) {
120 			if (bootVolume.IsPackaged()) {
121 				packagefs_apply_path_blocklist(bootVolume.SystemDirectory(),
122 					pathBlocklist);
123 			}
124 
125 			register_boot_file_system(bootVolume);
126 
127 			if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) == 0)
128 				platform_switch_to_logo();
129 
130 			load_modules(args, bootVolume);
131 
132 			gKernelArgs.ucode_data = NULL;
133 			gKernelArgs.ucode_data_size = 0;
134 			platform_load_ucode(bootVolume);
135 
136 			// TODO: fix for riscv64
137 #ifndef __riscv
138 			// apply boot settings
139 			apply_boot_settings();
140 #endif
141 
142 			// set up kernel args version info
143 			gKernelArgs.kernel_args_size = sizeof(kernel_args);
144 			gKernelArgs.version = CURRENT_KERNEL_ARGS_VERSION;
145 			if (gKernelArgs.ucode_data == NULL)
146 				gKernelArgs.kernel_args_size = kernel_args_size_v1;
147 
148 			// clone the boot_volume KMessage into kernel accessible memory
149 			// note, that we need to 8-byte align the buffer and thus allocate
150 			// 7 more bytes
151 			void* buffer = kernel_args_malloc(gBootVolume.ContentSize() + 7);
152 			if (!buffer) {
153 				panic("Could not allocate memory for the boot volume kernel "
154 					"arguments");
155 			}
156 
157 			buffer = (void*)(((addr_t)buffer + 7) & ~(addr_t)0x7);
158 			memcpy(buffer, gBootVolume.Buffer(), gBootVolume.ContentSize());
159 			gKernelArgs.boot_volume = buffer;
160 			gKernelArgs.boot_volume_size = gBootVolume.ContentSize();
161 
162 			platform_cleanup_devices();
163 			// TODO: cleanup, heap_release() etc.
164 			heap_print_statistics();
165 			platform_start_kernel();
166 		}
167 	}
168 
169 out:
170 	heap_release(args);
171 	return 0;
172 }
173