preference.c revision 1.3 1 /* $NetBSD: preference.c,v 1.3 2000/03/19 11:10:59 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 1999 Shin Takemura.
5 * All rights reserved.
6 *
7 * This software is part of the PocketBSD.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the PocketBSD project
20 * and its contributors.
21 * 4. Neither the name of the project nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38 #include <pbsdboot.h>
39 #include <commctrl.h>
40 #include <res/resource.h>
41
42 struct preference_s pref;
43 TCHAR* where_pref_load_from = NULL;
44 static TCHAR filenamebuf[1024];
45
46 void
47 pref_init(struct preference_s* pref)
48 {
49 memset(pref, 0, sizeof(*pref));
50 }
51
52
53 void
54 pref_dump(struct preference_s* pref)
55 {
56 debug_printf(TEXT(" kernel_name: %s\n"), pref->kernel_name);
57 debug_printf(TEXT(" options: %s\n"), pref->options);
58 debug_printf(TEXT(" user def name: %s\n"), pref->setting_name);
59 debug_printf(TEXT(" setting index: %d\n"), pref->setting_idx);
60 debug_printf(TEXT(" type: %d\n"), pref->fb_type);
61 debug_printf(TEXT(" width: %d\n"), pref->fb_width);
62 debug_printf(TEXT(" height: %d\n"), pref->fb_height);
63 debug_printf(TEXT(" bytes/line: %d\n"), pref->fb_linebytes);
64 debug_printf(TEXT(" addr: %d\n"), pref->fb_addr);
65 debug_printf(TEXT(" cpu: %08lx\n"), pref->platid_cpu);
66 debug_printf(TEXT(" machine: %08lx\n"), pref->platid_machine);
67 debug_printf(TEXT(" last chance: %S\n"), pref->check_last_chance ?
68 "TRUE" : "FALSE");
69 debug_printf(TEXT("load debug info: %S\n"), pref->load_debug_info ?
70 "TRUE" : "FALSE");
71 debug_printf(TEXT(" serial port: %S\n"), pref->serial_port ?
72 "ON" : "OFF");
73 }
74
75
76 int
77 pref_read(TCHAR* filename, struct preference_s* pref)
78 {
79 HANDLE file;
80 DWORD n;
81 struct preference_s buf;
82
83 file = CreateFile(
84 filename, /* file name */
85 GENERIC_READ, /* access (read-write) mode */
86 FILE_SHARE_READ,/* share mode */
87 NULL, /* pointer to security attributes */
88 OPEN_EXISTING, /* how to create */
89 FILE_ATTRIBUTE_NORMAL, /* file attributes*/
90 NULL /* handle to file with attributes to */
91 );
92
93 if (file == INVALID_HANDLE_VALUE) {
94 return (-1);
95 }
96
97 if (!ReadFile(file, &buf, sizeof(buf), &n, NULL)) {
98 msg_printf(MSG_ERROR, TEXT("pref_load()"),
99 TEXT("ReadFile(): error=%d"), GetLastError());
100 debug_printf(TEXT("ReadFile(): error=%d\n"), GetLastError());
101 CloseHandle(file);
102 return (-1);
103 }
104
105 if (n != sizeof(buf)) {
106 msg_printf(MSG_ERROR, TEXT("pref_load()"),
107 TEXT("ReadFile(): read %d bytes"), n);
108 debug_printf(TEXT("ReadFile(): read %d bytes\n"), n);
109 CloseHandle(file);
110 return (-1);
111 }
112
113 CloseHandle(file);
114
115 *pref = buf;
116
117 return (0);
118 }
119
120
121 int
122 pref_load(struct path_s load_path[], int pathlen)
123 {
124 int i;
125
126 where_pref_load_from = NULL;
127 for (i = 0; i < pathlen; i++) {
128 wsprintf(filenamebuf, TEXT("%s%s"),
129 load_path[i].name, PREFNAME);
130 debug_printf(TEXT("pref_load: try to '%s'\n"), filenamebuf);
131 if (pref_read(filenamebuf, &pref) == 0) {
132 debug_printf(TEXT("pref_load: succeded, '%s'.\n"),
133 filenamebuf);
134 pref_dump(&pref);
135 where_pref_load_from = filenamebuf;
136 return (0);
137 }
138 }
139
140 return (-1);
141 }
142
143
144 int
145 pref_save(struct path_s load_path[], int pathlen)
146 {
147 int i;
148
149 if (where_pref_load_from) {
150 if (pref_write(where_pref_load_from, &pref) != 0) {
151 msg_printf(MSG_ERROR, TEXT("Error()"),
152 TEXT("Can't write %s"), where_pref_load_from);
153 return -1;
154 }
155 return 0;
156 }
157 for (i = 0; i < pathlen; i++) {
158 if (!(load_path[i].flags & PATH_SAVE)) {
159 continue;
160 }
161 wsprintf(filenamebuf, TEXT("%s%s"),
162 load_path[i].name, PREFNAME);
163 debug_printf(TEXT("pref_save: try to '%s'\n"), filenamebuf);
164 if (pref_write(filenamebuf, &pref) == 0) {
165 debug_printf(TEXT("pref_write: succeded, '%s'.\n"),
166 filenamebuf);
167 return (0);
168 }
169 }
170
171 msg_printf(MSG_ERROR, TEXT("Error()"),
172 TEXT("Can't write %s"), PREFNAME);
173
174 return (-1);
175 }
176
177
178 int
179 pref_write(TCHAR* filename, struct preference_s* buf)
180 {
181 HANDLE file;
182 DWORD n;
183
184 debug_printf(TEXT("pref_write('%s').\n"), filename);
185 pref_dump(&pref);
186
187 file = CreateFile(
188 filename, /* file name */
189 GENERIC_WRITE, /* access (read-write) mode */
190 FILE_SHARE_WRITE,/* share mode */
191 NULL, /* pointer to security attributes */
192 CREATE_ALWAYS, /* how to create */
193 FILE_ATTRIBUTE_NORMAL, /* file attributes*/
194 NULL /* handle to file with attributes to */
195 );
196
197 if (file == INVALID_HANDLE_VALUE) {
198 debug_printf(TEXT("CreateFile(): error=%d\n"), GetLastError());
199 return (-1);
200 }
201
202 if (!WriteFile(file, buf, sizeof(*buf), &n, NULL)) {
203 msg_printf(MSG_ERROR, TEXT("pref_write()"),
204 TEXT("WriteFile(): error=%d"), GetLastError());
205 debug_printf(TEXT("WriteFile(): error=%d\n"), GetLastError());
206 CloseHandle(file);
207 return (-1);
208 }
209
210 if (n != sizeof(*buf)) {
211 msg_printf(MSG_ERROR, TEXT("pref_write()"),
212 TEXT("WriteFile(): write %d bytes"), n);
213 debug_printf(TEXT("WriteFile(): write %d bytes\n"), n);
214 CloseHandle(file);
215 return (-1);
216 }
217
218 CloseHandle(file);
219 return (0);
220 }
221
222