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