xref: /haiku/src/system/boot/loader/load_driver_settings.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "load_driver_settings.h"
8 
9 #include <OS.h>
10 #include <drivers/driver_settings.h>
11 
12 #include <boot/driver_settings.h>
13 #include <boot/kernel_args.h>
14 #include <boot/stage2.h>
15 #include <boot/platform.h>
16 
17 #include <string.h>
18 #include <unistd.h>
19 
20 
21 static status_t
22 load_driver_settings_file(Directory *directory, const char *name)
23 {
24 	int fd = open_from(directory, name, O_RDONLY);
25 	if (fd < 0)
26 		return fd;
27 
28 	struct stat stat;
29 	fstat(fd, &stat);
30 
31 	char *buffer = (char *)kernel_args_malloc(stat.st_size + 1);
32 	if (buffer == NULL)
33 		return B_NO_MEMORY;
34 
35 	if (read(fd, buffer, stat.st_size) != stat.st_size)
36 		return B_IO_ERROR;
37 
38 	driver_settings_file *file = (driver_settings_file *)kernel_args_malloc(sizeof(driver_settings_file));
39 	if (file == NULL) {
40 		kernel_args_free(buffer);
41 		return B_NO_MEMORY;
42 	}
43 
44 	buffer[stat.st_size] = '\0';
45 		// null terminate the buffer
46 
47 	strlcpy(file->name, name, sizeof(file->name));
48 	file->buffer = buffer;
49 	file->size = stat.st_size;
50 
51 	// add it to the list
52 	file->next = gKernelArgs.driver_settings;
53 	gKernelArgs.driver_settings = file;
54 
55 	return B_OK;
56 }
57 
58 
59 status_t
60 load_driver_settings(stage2_args */*args*/, Directory *volume)
61 {
62 	int fd = open_from(volume, "home/config/settings/kernel/drivers", O_RDONLY);
63 	if (fd < B_OK)
64 		return fd;
65 
66 	Directory *settings = (Directory *)get_node_from(fd);
67 	if (settings == NULL)
68 		return B_ENTRY_NOT_FOUND;
69 
70 	void *cookie;
71 	if (settings->Open(&cookie, O_RDONLY) == B_OK) {
72 		char name[B_FILE_NAME_LENGTH];
73 		while (settings->GetNextEntry(cookie, name, sizeof(name)) == B_OK) {
74 			if (!strcmp(name, ".") || !strcmp(name, ".."))
75 				continue;
76 
77 			status_t status = load_driver_settings_file(settings, name);
78 			if (status != B_OK)
79 				dprintf("Could not load \"%s\" error %ld\n", name, status);
80 		}
81 
82 		settings->Close(cookie);
83 	}
84 
85 	return B_OK;
86 }
87 
88 
89 status_t
90 add_safe_mode_settings(char *settings)
91 {
92 	if (settings == NULL || settings[0] == '\0')
93 		return B_OK;
94 
95 	size_t length = strlen(settings);
96 	char *buffer = (char *)kernel_args_malloc(length + 1);
97 	if (buffer == NULL)
98 		return B_NO_MEMORY;
99 
100 	driver_settings_file *file = (driver_settings_file *)kernel_args_malloc(sizeof(driver_settings_file));
101 	if (file == NULL) {
102 		kernel_args_free(buffer);
103 		return B_NO_MEMORY;
104 	}
105 
106 	strlcpy(file->name, B_SAFEMODE_DRIVER_SETTINGS, sizeof(file->name));
107 	memcpy(buffer, settings, length + 1);
108 	file->buffer = buffer;
109 	file->size = length;
110 
111 	// add it to the list
112 	file->next = gKernelArgs.driver_settings;
113 	gKernelArgs.driver_settings = file;
114 
115 	return B_OK;
116 }
117 
118