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