-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
138 lines (114 loc) · 3.01 KB
/
settings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Wii U Linux Launcher -- Settings
*
* Copyright (C) 2017 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program, in the file LICENSE.GPLv2.
*/
#include <stddef.h>
#include <string.h>
#include <os_functions.h>
#include "main.h"
#include "fs.h"
char kernel_path[256];
char dtb_path[256];
char initrd_path[256];
char cmdline[256];
static void load_default_settings(void)
{
snprintf(kernel_path, sizeof kernel_path,
"%s/wiiu/linux/zImage", sdcard_path);
snprintf(dtb_path, sizeof dtb_path,
"%s/wiiu/linux/wiiu.dtb", sdcard_path);
snprintf(initrd_path, sizeof initrd_path, "", 0);
snprintf(cmdline, sizeof cmdline, "", 0);
}
static void get_config_path(char *buf, size_t size)
{
snprintf(buf, size, "%s/wiiu/apps/linux/config.txt", sdcard_path);
}
/* Find the first newline or return NULL */
static char *find_newline(char *str)
{
char *p;
for (p = str; *p; p++)
if (*p == '\n' || *p == '\r')
return p;
return NULL;
}
/* Does a start with b? */
static int starts_with(const char *string, const char *pattern)
{
size_t i;
for (i = 0;; i++) {
if (pattern[i] == '\0')
return 1;
if (string[i] != pattern[i])
return 0;
}
}
static void parse_settings(char *string)
{
char *line, *next = string;
char *newline;
while (next) {
line = next;
newline = find_newline(line);
if (newline) {
*newline = '\0';
next = newline + 1;
} else {
next = NULL;
}
if (starts_with(line, "kernel="))
snprintf(kernel_path, sizeof kernel_path, "%s", line + 7);
else if (starts_with(line, "dtb="))
snprintf(dtb_path, sizeof dtb_path, "%s", line + 4);
else if (starts_with(line, "initrd="))
snprintf(initrd_path, sizeof initrd_path, "%s", line + 7);
else if (starts_with(line, "cmdline="))
snprintf(cmdline, sizeof cmdline, "%s", line + 8);
/* ignore everything else */
}
}
static int try_load_settings(void)
{
int res;
char path[256], buf[1024];
get_config_path(path, sizeof path);
res = get_file_size(path, NULL);
if (res == 0)
return -1;
res = read_file_into_buffer(path, (u8 *)buf, sizeof(buf) - 1, NULL);
if (res < 0)
return res;
buf[res] = '\0';
parse_settings(buf);
return 0;
}
void load_settings(void)
{
int res = try_load_settings();
if (res < 0)
load_default_settings();
}
void save_settings(void)
{
char buf[1024], path[256];
int res;
get_config_path(path, sizeof buf);
res = snprintf(buf, sizeof buf,
"kernel=%s\ndtb=%s\ninitrd=%s\ncmdline=%s\n",
kernel_path, dtb_path, initrd_path, cmdline);
write_buffer_into_file(path, (u8 *)buf, res);
}