Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / hpcmips / stand / pbsdboot / preference.c
blob3b0a4dc81e0e7c018198f875431635f234be8c47
1 /* $NetBSD: preference.c,v 1.3 2000/03/19 11:10:59 takemura Exp $ */
3 /*-
4 * Copyright (c) 1999 Shin Takemura.
5 * All rights reserved.
7 * This software is part of the PocketBSD.
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.
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.
38 #include <pbsdboot.h>
39 #include <commctrl.h>
40 #include <res/resource.h>
43 struct preference_s pref;
44 TCHAR* where_pref_load_from = NULL;
45 static TCHAR filenamebuf[1024];
48 #define SETTING_IDX 1
49 #define FB_TYPE 2
50 #define FB_WIDTH 3
51 #define FB_HEIGHT 4
52 #define FB_LINEBYTES 5
53 #define BOOT_TIME 6
54 #define FB_ADDR 7
55 #define PLATID_CPU 8
56 #define PLATID_MACHINE 9
57 #define SETTING_NAME 10
58 #define KERNEL_NAME 11
59 #define OPTIONS 12
60 #define CHECK_LAST_CHANCE 13
61 #define LOAD_DEBUG_INFO 14
62 #define SERIAL_PORT 15
63 #define REVERSE_VIDEO 16
64 #define AUTOBOOT 17
66 #define NOFID 18
69 TCHAR *id_table[] = {
71 TEXT("setting_idx"),
72 TEXT("fb_type"),
73 TEXT("fb_width"),
74 TEXT("fb_height"),
75 TEXT("fb_linebytes"),
76 TEXT("boot_time"),
77 TEXT("fb_addr"),
78 TEXT("platid_cpu"),
79 TEXT("platid_machine"),
80 TEXT("setting_name"),
81 TEXT("kernel_name"),
82 TEXT("options"),
83 TEXT("check_last_chance"),
84 TEXT("load_debug_info"),
85 TEXT("serial_port"),
86 TEXT("reverse_video"),
87 TEXT("autoboot"),
92 void
93 pref_init(struct preference_s* pref)
95 memset(pref, 0, sizeof(*pref));
100 argument file is handle that was already opened .
101 if read is faile , this function will "not" close this handle .
102 return 0 if error . if end of file , return -1
104 int read1byte(HANDLE file,char *c){
105 DWORD n;
106 if(!ReadFile(file,c,sizeof(char),&n,NULL)){
107 msg_printf(MSG_ERROR, TEXT("pref_load()"),
108 TEXT("ReadFile(): error=%d"), GetLastError());
109 debug_printf(TEXT("ReadFile(): error=%d\r"), GetLastError());
111 return 0;
113 if (n != sizeof(char)) {
114 if( n == 0){
115 return (-1);
117 msg_printf(MSG_ERROR, TEXT("pref_load()"),
118 TEXT("ReadFile(): read %d bytes"), n);
119 debug_printf(TEXT("ReadFile(): read %d bytes\r"), n);
121 return 0;
123 return 1;
127 argument file is handle that was already opened .
128 if write is faile, this function will "not" close this handle .
129 return 0 if error . write one line of string .
131 int write1string(HANDLE file,char *string){
132 DWORD n;
133 if(!WriteFile(file,string,sizeof(char)*strlen(string),&n,NULL)){
134 msg_printf(MSG_ERROR, TEXT("pref_write()"),
135 TEXT("WriteFile(): error=%d"), GetLastError());
136 debug_printf(TEXT("WriteFile(): error=%d\n"), GetLastError());
138 return 0;
140 if (n != sizeof(char)*strlen(string)) {
141 msg_printf(MSG_ERROR, TEXT("pref_write()"),
142 TEXT("WriteFile(): write %d bytes"), n);
143 debug_printf(TEXT("WriteFile(): write %d bytes\n"), n);
145 return (0);
148 return 1;
151 void
152 pref_dump(struct preference_s* pref)
154 debug_printf(TEXT(" kernel_name: %s\n"), pref->kernel_name);
155 debug_printf(TEXT(" options: %s\n"), pref->options);
156 debug_printf(TEXT(" user def name: %s\n"), pref->setting_name);
157 debug_printf(TEXT(" setting index: %d\n"), pref->setting_idx);
158 debug_printf(TEXT(" type: %d\n"), pref->fb_type);
159 debug_printf(TEXT(" width: %d\n"), pref->fb_width);
160 debug_printf(TEXT(" height: %d\n"), pref->fb_height);
161 debug_printf(TEXT(" bytes/line: %d\n"), pref->fb_linebytes);
162 debug_printf(TEXT(" addr: %d\n"), pref->fb_addr);
163 debug_printf(TEXT(" cpu: %08lx\n"), pref->platid_cpu);
164 debug_printf(TEXT(" machine: %08lx\n"), pref->platid_machine);
165 debug_printf(TEXT(" last chance: %S\n"), pref->check_last_chance ?
166 "TRUE" : "FALSE");
167 debug_printf(TEXT("load debug info: %S\n"), pref->load_debug_info ?
168 "TRUE" : "FALSE");
169 debug_printf(TEXT(" serial port: %S\n"), pref->serial_port ?
170 "ON" : "OFF");
175 /* To Do . modify this function*/
177 pref_read(TCHAR* filename, struct preference_s* pref)
179 HANDLE file;
180 DWORD length;
181 static struct preference_s buf;
182 char tempbuf[1024];
183 TCHAR unidata[1024];
184 TCHAR identif[1024];
185 char c;
186 int i,flag,d;
187 int result;/* count of loading pref item */
190 file = CreateFile(
191 filename, /* file name */
192 GENERIC_READ, /* access (read-write) mode */
193 FILE_SHARE_READ,/* share mode */
194 NULL, /* pointer to security attributes */
195 OPEN_EXISTING, /* how to create */
196 FILE_ATTRIBUTE_NORMAL, /* file attributes*/
197 NULL /* handle to file with attributes to */
200 if (file == INVALID_HANDLE_VALUE) {
201 return (-1);
207 flag = 1;
208 result = 0;
209 while(flag){
210 i = 0;
211 d = read1byte(file,&c);
212 if( d <=0){
213 if(d == -1){
214 flag = 0;
215 break;
217 else
219 CloseHandle(file);
220 return (-1);
225 while(c != ':' && c != '\r' && c!= '\n'){
226 tempbuf[i] = c;
227 d = read1byte(file,&c);
228 if( d <=0){
229 if(d == -1){
230 flag = 0;
231 break;
233 else
235 CloseHandle(file);
236 return (-1);
239 i++;
241 if(c == ':'){
244 tempbuf[i] = '\0';
245 length = MultiByteToWideChar(CP_ACP,0,tempbuf,-1,identif,0);
246 MultiByteToWideChar(CP_ACP,0,tempbuf,-1,identif,length);
248 i = 0;
249 d = read1byte(file,&c);
250 flag = 1;
251 while(c != '\r' && c != '\n' && flag){/* get unidata */
252 if(d <= 0){
253 if(d == -1){
254 flag = 0;/* though needless ... */
255 break;
257 else{
258 CloseHandle(file);
259 return -1;
262 tempbuf[i] = c;
263 d = read1byte(file,&c);
264 i++;
267 if(c == '\r'){/* skip \n */
268 read1byte(file,&c);
270 tempbuf[i] = '\0';
271 length = MultiByteToWideChar(CP_ACP,0,tempbuf,-1,unidata,0);
272 MultiByteToWideChar(CP_ACP,0,tempbuf,-1,unidata,length);
274 for(i = 1; i < NOFID;i++){
275 if(wcscmp(identif,id_table[i])==0){
276 break;
279 switch(i){
280 case SETTING_IDX:
281 d = _wtoi(unidata);
282 buf.setting_idx = d;
283 result++;
284 break;
285 case FB_TYPE:
286 d = _wtoi(unidata);
287 buf.fb_type = d;
288 result++;
289 break;
290 case FB_WIDTH:
291 d = _wtoi(unidata);
292 buf.fb_width = d;
293 result++;
294 break;
295 case FB_HEIGHT:
296 d = _wtoi(unidata);
297 buf.fb_height = d;
298 result++;
299 break;
300 case FB_LINEBYTES:
301 d = _wtoi(unidata);
302 buf.fb_linebytes = d;
303 result++;
304 break;
305 case BOOT_TIME:
306 d = _wtoi(unidata);
307 buf.boot_time = d;
308 result++;
309 break;
310 case FB_ADDR:
311 d = _wtoi(unidata);
312 buf.fb_addr = d;
313 result++;
314 break;
315 case PLATID_CPU:
316 d = _wtoi(unidata);
317 buf.platid_cpu = d;
318 result++;
319 break;
320 case PLATID_MACHINE:
321 d = _wtoi(unidata);
322 buf.platid_machine = d;
323 result++;
324 break;
325 case SETTING_NAME:
326 wcscpy(buf.setting_name,unidata);
327 result++;
328 break;
329 case KERNEL_NAME:
330 wcscpy(buf.kernel_name,unidata);
331 result++;
332 break;
333 case OPTIONS:
334 wcscpy(buf.options,unidata);
335 result++;
336 break;
337 case CHECK_LAST_CHANCE:
338 if(wcscmp(unidata,TEXT("t")) == 0){
339 buf.check_last_chance = TRUE;
341 else{
342 buf.check_last_chance = FALSE;
344 result++;
345 break;
346 case LOAD_DEBUG_INFO:
347 if(wcscmp(unidata,TEXT("t")) == 0){
348 buf.load_debug_info = TRUE;
350 else{
351 buf.load_debug_info = FALSE;
353 result++;
354 break;
355 case SERIAL_PORT:
356 if(wcscmp(unidata,TEXT("t")) == 0){
357 buf.serial_port = TRUE;
359 else{
360 buf.serial_port = FALSE;
362 result++;
363 break;
364 case REVERSE_VIDEO:
365 if(wcscmp(unidata,TEXT("t")) == 0){
366 buf.reverse_video = TRUE;
368 else{
369 buf.reverse_video = FALSE;
371 result++;
372 break;
373 case AUTOBOOT:
374 if(wcscmp(unidata,TEXT("t")) == 0){
375 buf.autoboot = TRUE;
377 else{
378 buf.autoboot = FALSE;
380 result++;
381 break;
382 default:
383 break;
391 CloseHandle(file);
393 #if 0
394 /* shortage of item is not error */
395 if(result != NOFID -1){
396 return -1;/* data is shortage */
398 #endif
400 *pref = buf;
402 return (0);
408 pref_load(struct path_s load_path[], int pathlen)
410 int i;
412 where_pref_load_from = NULL;
413 for (i = 0; i < pathlen; i++) {
414 wsprintf(filenamebuf, TEXT("%s%s"),
415 load_path[i].name, PREFNAME);
416 debug_printf(TEXT("pref_load: try to '%s'\n"), filenamebuf);
418 if (pref_read(filenamebuf, &pref) == 0) {
419 debug_printf(TEXT("pref_load: succeded, '%s'.\n"),
420 filenamebuf);
421 pref_dump(&pref);
422 where_pref_load_from = filenamebuf;
423 return (0);
427 return (-1);
432 pref_save(struct path_s load_path[], int pathlen)
434 int i;
436 if (where_pref_load_from) {
437 if (pref_write(where_pref_load_from, &pref) != 0) {
438 msg_printf(MSG_ERROR, TEXT("Error()"),
439 TEXT("Can't write %s"), where_pref_load_from);
440 return -1;
442 return 0;
444 for (i = 0; i < pathlen; i++) {
445 if (!(load_path[i].flags & PATH_SAVE)) {
446 continue;
448 wsprintf(filenamebuf, TEXT("%s%s"),
449 load_path[i].name, PREFNAME);
450 debug_printf(TEXT("pref_save: try to '%s'\n"), filenamebuf);
451 if (pref_write(filenamebuf, &pref) == 0) {
452 debug_printf(TEXT("pref_write: succeded, '%s'.\n"),
453 filenamebuf);
454 return (0);
458 msg_printf(MSG_ERROR, TEXT("Error()"),
459 TEXT("Can't write %s"), PREFNAME);
461 return (-1);
466 pref_write(TCHAR* filename, struct preference_s* buf)
468 HANDLE file;
469 char tempbuf[1024];
470 TCHAR unibuf[1024];
471 DWORD length;
474 debug_printf(TEXT("pref_write('%s').\n"), filename);
475 pref_dump(&pref);
477 file = CreateFile(
478 filename, /* file name */
479 GENERIC_WRITE, /* access (read-write) mode */
480 FILE_SHARE_WRITE,/* share mode */
481 NULL, /* pointer to security attributes */
482 CREATE_ALWAYS, /* how to create */
483 FILE_ATTRIBUTE_NORMAL, /* file attributes*/
484 NULL /* handle to file with attributes to */
487 if (file == INVALID_HANDLE_VALUE) {
488 debug_printf(TEXT("CreateFile(): error=%d\n"), GetLastError());
489 return (-1);
493 wsprintf(unibuf,TEXT("setting_idx:%d\r\n"),buf->setting_idx);
494 debug_printf(TEXT("setting_idx,tempbuf=%s"),unibuf);
495 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
496 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
497 if(!write1string(file,tempbuf)){
498 CloseHandle(file);
499 return (-1);
503 wsprintf(unibuf,TEXT("fb_type:%d\r\n"),buf->fb_type);
504 debug_printf(TEXT("fb_type,tempbuf=%s,"),unibuf);
505 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
506 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
507 if(!write1string(file,tempbuf)){
508 CloseHandle(file);
509 return (-1);
513 wsprintf(unibuf,TEXT("fb_width:%d\r\n"),buf->fb_width);
514 debug_printf(TEXT("fb_width,tempbuf=%s"),unibuf);
515 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
516 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
517 if(!write1string(file,tempbuf)){
518 CloseHandle(file);
519 return (-1);
524 wsprintf(unibuf,TEXT("fb_height:%d\r\n"),buf->fb_height);
525 debug_printf(TEXT("fb_height,tempbuf=%s"),unibuf);
526 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
527 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
528 if(!write1string(file,tempbuf)){
529 CloseHandle(file);
530 return (-1);
535 wsprintf(unibuf,TEXT("fb_linebytes:%d\r\n"),buf->fb_linebytes);
536 debug_printf(TEXT("fb_linebytes,tempbuf=%s"),unibuf);
537 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
538 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
539 if(!write1string(file,tempbuf)){
540 CloseHandle(file);
541 return (-1);
545 wsprintf(unibuf,TEXT("boot_time:%d\r\n"),buf->boot_time);
546 debug_printf(TEXT("boot_time,tempbuf=%s\n"),unibuf);
547 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
548 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
549 if(!write1string(file,tempbuf)){
550 CloseHandle(file);
551 return (-1);
554 wsprintf(unibuf,TEXT("fb_addr:%d\r\n"),buf->fb_addr);
555 debug_printf(TEXT("fb_addr,tempbuf=%s"),unibuf);
556 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
557 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
558 if(!write1string(file,tempbuf)){
559 CloseHandle(file);
560 return (-1);
565 wsprintf(unibuf,TEXT("platid_cpu:%d\r\n"),buf->platid_cpu);
566 debug_printf(TEXT("platid_cpu,tempbuf=%s"),unibuf);
567 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
568 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
569 if(!write1string(file,tempbuf)){
570 CloseHandle(file);
571 return (-1);
575 wsprintf(unibuf,TEXT("platid_machine:%d\r\n"),buf->platid_machine);
576 debug_printf(TEXT("platid_machine,tempbuf=%s"),unibuf);
577 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
578 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
579 if(!write1string(file,tempbuf)){
580 CloseHandle(file);
581 return (-1);
586 wsprintf(unibuf,TEXT("setting_name:%s\r\n"),buf->setting_name);
587 debug_printf(TEXT("setting_name,unibuf=%s,wcslen=%d"),unibuf,
588 wcslen(unibuf));
589 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
590 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
592 if(!write1string(file,tempbuf)){
593 CloseHandle(file);
594 return (-1);
599 wsprintf(unibuf,TEXT("kernel_name:%s\r\n"),buf->kernel_name);
600 debug_printf(TEXT("kernel_name,unibuf=%s,wcslen=%d"),unibuf,
601 wcslen(unibuf));
602 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
603 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
604 if(!write1string(file,tempbuf)){
605 CloseHandle(file);
606 return (-1);
611 wsprintf(unibuf,TEXT("options:%s\r\n"),buf->options);
612 debug_printf(TEXT("options,unibuf=%s,wcslen=%d"),unibuf,
613 wcslen(unibuf));
614 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
615 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
616 if(!write1string(file,tempbuf)){
617 CloseHandle(file);
618 return (-1);
622 if(buf->check_last_chance){
623 wsprintf(unibuf,TEXT("check_last_chance:t\r\n"));
625 else{
626 wsprintf(unibuf,TEXT("check_last_chance:n\r\n"));
630 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
631 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
632 if(!write1string(file,tempbuf)){
633 CloseHandle(file);
634 return (-1);
640 if(buf->load_debug_info){
641 wsprintf(unibuf,TEXT("load_debug_info:t\r\n"));
643 else{
644 wsprintf(unibuf,TEXT("load_debug_info:n\r\n"));
648 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
649 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
652 if(!write1string(file,tempbuf)){
653 CloseHandle(file);
654 return (-1);
659 if(buf->serial_port){
660 wsprintf(unibuf,TEXT("serial_port:t\r\n"));
662 else{
663 wsprintf(unibuf,TEXT("serial_port:n\r\n"));
666 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
667 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
669 if(!write1string(file,tempbuf)){
670 CloseHandle(file);
671 return (-1);
676 if(buf->reverse_video){
677 wsprintf(unibuf,TEXT("reverse_video:t\r\n"));
679 else{
680 wsprintf(unibuf,TEXT("reverse_video:n\r\n"));
683 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
684 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
685 if(!write1string(file,tempbuf)){
686 CloseHandle(file);
687 return (-1);
690 if(buf->autoboot){
691 wsprintf(unibuf,TEXT("autoboot:t\r\n"));
693 else{
694 wsprintf(unibuf,TEXT("autoboot:n\r\n"));
697 length = WideCharToMultiByte(CP_ACP,0,unibuf,-1,NULL,0,NULL,NULL);
698 WideCharToMultiByte(CP_ACP,0,unibuf,-1,tempbuf,length,NULL,NULL);
699 if(!write1string(file,tempbuf)){
700 CloseHandle(file);
701 return (-1);
704 CloseHandle(file);
705 return (0);