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