1 /* $NetBSD: preference.c,v 1.3 2000/03/19 11:10:59 takemura Exp $ */
4 * Copyright (c) 1999 Shin Takemura.
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
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
40 #include <res/resource.h>
43 struct preference_s pref
;
44 TCHAR
* where_pref_load_from
= NULL
;
45 static TCHAR filenamebuf
[1024];
52 #define FB_LINEBYTES 5
56 #define PLATID_MACHINE 9
57 #define SETTING_NAME 10
58 #define KERNEL_NAME 11
60 #define CHECK_LAST_CHANCE 13
61 #define LOAD_DEBUG_INFO 14
62 #define SERIAL_PORT 15
63 #define REVERSE_VIDEO 16
79 TEXT("platid_machine"),
83 TEXT("check_last_chance"),
84 TEXT("load_debug_info"),
86 TEXT("reverse_video"),
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
){
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());
113 if (n
!= sizeof(char)) {
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
);
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
){
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());
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
);
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
?
167 debug_printf(TEXT("load debug info: %S\n"), pref
->load_debug_info
?
169 debug_printf(TEXT(" serial port: %S\n"), pref
->serial_port
?
175 /* To Do . modify this function*/
177 pref_read(TCHAR
* filename
, struct preference_s
* pref
)
181 static struct preference_s buf
;
187 int result
;/* count of loading pref item */
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
) {
211 d
= read1byte(file
,&c
);
225 while(c
!= ':' && c
!= '\r' && c
!= '\n'){
227 d
= read1byte(file
,&c
);
245 length
= MultiByteToWideChar(CP_ACP
,0,tempbuf
,-1,identif
,0);
246 MultiByteToWideChar(CP_ACP
,0,tempbuf
,-1,identif
,length
);
249 d
= read1byte(file
,&c
);
251 while(c
!= '\r' && c
!= '\n' && flag
){/* get unidata */
254 flag
= 0;/* though needless ... */
263 d
= read1byte(file
,&c
);
267 if(c
== '\r'){/* skip \n */
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){
302 buf
.fb_linebytes
= d
;
322 buf
.platid_machine
= d
;
326 wcscpy(buf
.setting_name
,unidata
);
330 wcscpy(buf
.kernel_name
,unidata
);
334 wcscpy(buf
.options
,unidata
);
337 case CHECK_LAST_CHANCE
:
338 if(wcscmp(unidata
,TEXT("t")) == 0){
339 buf
.check_last_chance
= TRUE
;
342 buf
.check_last_chance
= FALSE
;
346 case LOAD_DEBUG_INFO
:
347 if(wcscmp(unidata
,TEXT("t")) == 0){
348 buf
.load_debug_info
= TRUE
;
351 buf
.load_debug_info
= FALSE
;
356 if(wcscmp(unidata
,TEXT("t")) == 0){
357 buf
.serial_port
= TRUE
;
360 buf
.serial_port
= FALSE
;
365 if(wcscmp(unidata
,TEXT("t")) == 0){
366 buf
.reverse_video
= TRUE
;
369 buf
.reverse_video
= FALSE
;
374 if(wcscmp(unidata
,TEXT("t")) == 0){
378 buf
.autoboot
= FALSE
;
394 /* shortage of item is not error */
395 if(result
!= NOFID
-1){
396 return -1;/* data is shortage */
408 pref_load(struct path_s load_path
[], int pathlen
)
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"),
422 where_pref_load_from
= filenamebuf
;
432 pref_save(struct path_s load_path
[], int pathlen
)
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
);
444 for (i
= 0; i
< pathlen
; i
++) {
445 if (!(load_path
[i
].flags
& PATH_SAVE
)) {
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"),
458 msg_printf(MSG_ERROR
, TEXT("Error()"),
459 TEXT("Can't write %s"), PREFNAME
);
466 pref_write(TCHAR
* filename
, struct preference_s
* buf
)
474 debug_printf(TEXT("pref_write('%s').\n"), filename
);
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());
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
)){
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
)){
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
)){
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
)){
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
)){
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
)){
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
)){
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
)){
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
)){
586 wsprintf(unibuf
,TEXT("setting_name:%s\r\n"),buf
->setting_name
);
587 debug_printf(TEXT("setting_name,unibuf=%s,wcslen=%d"),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
)){
599 wsprintf(unibuf
,TEXT("kernel_name:%s\r\n"),buf
->kernel_name
);
600 debug_printf(TEXT("kernel_name,unibuf=%s,wcslen=%d"),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
)){
611 wsprintf(unibuf
,TEXT("options:%s\r\n"),buf
->options
);
612 debug_printf(TEXT("options,unibuf=%s,wcslen=%d"),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
)){
622 if(buf
->check_last_chance
){
623 wsprintf(unibuf
,TEXT("check_last_chance:t\r\n"));
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
)){
640 if(buf
->load_debug_info
){
641 wsprintf(unibuf
,TEXT("load_debug_info:t\r\n"));
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
)){
659 if(buf
->serial_port
){
660 wsprintf(unibuf
,TEXT("serial_port:t\r\n"));
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
)){
676 if(buf
->reverse_video
){
677 wsprintf(unibuf
,TEXT("reverse_video:t\r\n"));
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
)){
691 wsprintf(unibuf
,TEXT("autoboot:t\r\n"));
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
)){