xref: /haiku/src/system/kernel/debug/safemode_settings.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 /*
2  * Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <KernelExport.h>
8 
9 #include <safemode.h>
10 #include <kernel.h>
11 #include <syscalls.h>
12 
13 #include <string.h>
14 
15 
16 extern "C" status_t
17 get_safemode_option(const char* parameter, char* buffer, size_t* _bufferSize)
18 {
19 	status_t status = B_ENTRY_NOT_FOUND;
20 
21 	void* handle = load_driver_settings(B_SAFEMODE_DRIVER_SETTINGS);
22 	if (handle != NULL) {
23 		status = B_NAME_NOT_FOUND;
24 
25 		const char* value = get_driver_parameter(handle, parameter, NULL, NULL);
26 		if (value != NULL) {
27 			*_bufferSize = strlcpy(buffer, value, *_bufferSize);
28 			status = B_OK;
29 		}
30 
31 		unload_driver_settings(handle);
32 	}
33 
34 	if (status != B_OK) {
35 		// Try kernel settings file as a fall back
36 		handle = load_driver_settings("kernel");
37 		if (handle != NULL) {
38 			const char* value = get_driver_parameter(handle, parameter, NULL,
39 				NULL);
40 			if (value != NULL) {
41 				*_bufferSize = strlcpy(buffer, value, *_bufferSize);
42 				status = B_OK;
43 			}
44 
45 			unload_driver_settings(handle);
46 		}
47 	}
48 
49 	return status;
50 }
51 
52 
53 extern "C" bool
54 get_safemode_boolean(const char* parameter, bool defaultValue)
55 {
56 	char value[16];
57 	size_t length = sizeof(value);
58 
59 	if (get_safemode_option(parameter, value, &length) != B_OK)
60 		return defaultValue;
61 
62 	return !strcasecmp(value, "on") || !strcasecmp(value, "true")
63 		|| !strcmp(value, "1") || !strcasecmp(value, "yes")
64 		|| !strcasecmp(value, "enabled");
65 }
66 
67 
68 //	#pragma mark - syscalls
69 
70 
71 #ifndef _BOOT_MODE
72 
73 
74 extern "C" status_t
75 _user_get_safemode_option(const char* userParameter, char* userBuffer,
76 	size_t* _userBufferSize)
77 {
78 	char parameter[B_FILE_NAME_LENGTH];
79 	char buffer[B_PATH_NAME_LENGTH];
80 	size_t bufferSize, originalBufferSize;
81 
82 	if (!IS_USER_ADDRESS(userParameter) || !IS_USER_ADDRESS(userBuffer)
83 		|| !IS_USER_ADDRESS(_userBufferSize)
84 		|| user_memcpy(&bufferSize, _userBufferSize, sizeof(size_t)) != B_OK
85 		|| user_strlcpy(parameter, userParameter, B_FILE_NAME_LENGTH) < B_OK)
86 		return B_BAD_ADDRESS;
87 
88 	if (bufferSize > B_PATH_NAME_LENGTH)
89 		bufferSize = B_PATH_NAME_LENGTH;
90 
91 	originalBufferSize = bufferSize;
92 	status_t status = get_safemode_option(parameter, buffer, &bufferSize);
93 
94 	if (status == B_OK
95 		&& (user_strlcpy(userBuffer, buffer, originalBufferSize) < B_OK
96 			|| user_memcpy(_userBufferSize, &bufferSize, sizeof(size_t))
97 				!= B_OK))
98 		return B_BAD_ADDRESS;
99 
100 	return status;
101 }
102 
103 
104 #endif	// !_BOOT_MODE
105