Home | History | Annotate | Line # | Download | only in pbsdboot
preference.c revision 1.3
      1  1.3  takemura /*	$NetBSD: preference.c,v 1.3 2000/03/19 11:10:59 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.3  takemura pref_load(struct path_s 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.3  takemura 			 load_path[i].name, 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.3  takemura pref_save(struct path_s load_path[], int pathlen)
    146  1.3  takemura {
    147  1.3  takemura 	int i;
    148  1.3  takemura 
    149  1.3  takemura 	if (where_pref_load_from) {
    150  1.3  takemura 		if (pref_write(where_pref_load_from, &pref) != 0) {
    151  1.3  takemura 			msg_printf(MSG_ERROR, TEXT("Error()"),
    152  1.3  takemura 			    TEXT("Can't write %s"), where_pref_load_from);
    153  1.3  takemura 			return -1;
    154  1.3  takemura 		}
    155  1.3  takemura 		return 0;
    156  1.3  takemura 	}
    157  1.3  takemura 	for (i = 0; i < pathlen; i++) {
    158  1.3  takemura 		if (!(load_path[i].flags & PATH_SAVE)) {
    159  1.3  takemura 			continue;
    160  1.3  takemura 		}
    161  1.3  takemura 		wsprintf(filenamebuf, TEXT("%s%s"),
    162  1.3  takemura 		    load_path[i].name, PREFNAME);
    163  1.3  takemura 		debug_printf(TEXT("pref_save: try to '%s'\n"), filenamebuf);
    164  1.3  takemura 		if (pref_write(filenamebuf, &pref) == 0) {
    165  1.3  takemura 			debug_printf(TEXT("pref_write: succeded, '%s'.\n"),
    166  1.3  takemura 			    filenamebuf);
    167  1.3  takemura 			return (0);
    168  1.3  takemura 		}
    169  1.3  takemura 	}
    170  1.3  takemura 
    171  1.3  takemura 	msg_printf(MSG_ERROR, TEXT("Error()"),
    172  1.3  takemura 	    TEXT("Can't write %s"), PREFNAME);
    173  1.3  takemura 
    174  1.3  takemura 	return (-1);
    175  1.3  takemura }
    176  1.3  takemura 
    177  1.3  takemura 
    178  1.3  takemura int
    179  1.2  takemura pref_write(TCHAR* filename, struct preference_s* buf)
    180  1.2  takemura {
    181  1.2  takemura 	HANDLE file;
    182  1.2  takemura 	DWORD n;
    183  1.2  takemura 
    184  1.2  takemura 	debug_printf(TEXT("pref_write('%s').\n"), filename);
    185  1.2  takemura 	pref_dump(&pref);
    186  1.2  takemura 
    187  1.2  takemura        	file = CreateFile(
    188  1.2  takemura 	       	filename,      	/* file name */
    189  1.2  takemura 	       	GENERIC_WRITE,	/* access (read-write) mode */
    190  1.2  takemura 	       	FILE_SHARE_WRITE,/* share mode */
    191  1.2  takemura 	       	NULL,		/* pointer to security attributes */
    192  1.2  takemura 	       	CREATE_ALWAYS,	/* how to create */
    193  1.2  takemura 	       	FILE_ATTRIBUTE_NORMAL,	/* file attributes*/
    194  1.2  takemura 	       	NULL		/* handle to file with attributes to */
    195  1.2  takemura 	       	);
    196  1.2  takemura 
    197  1.2  takemura 	if (file == INVALID_HANDLE_VALUE) {
    198  1.2  takemura 		debug_printf(TEXT("CreateFile(): error=%d\n"), GetLastError());
    199  1.2  takemura 		return (-1);
    200  1.2  takemura 	}
    201  1.2  takemura 
    202  1.2  takemura 	if (!WriteFile(file, buf, sizeof(*buf), &n, NULL)) {
    203  1.2  takemura 		msg_printf(MSG_ERROR, TEXT("pref_write()"),
    204  1.2  takemura 			   TEXT("WriteFile(): error=%d"), GetLastError());
    205  1.2  takemura 		debug_printf(TEXT("WriteFile(): error=%d\n"), GetLastError());
    206  1.2  takemura 		CloseHandle(file);
    207  1.2  takemura 		return (-1);
    208  1.2  takemura 	}
    209  1.2  takemura 
    210  1.2  takemura 	if (n != sizeof(*buf)) {
    211  1.2  takemura 		msg_printf(MSG_ERROR, TEXT("pref_write()"),
    212  1.2  takemura 			   TEXT("WriteFile(): write %d bytes"), n);
    213  1.2  takemura 		debug_printf(TEXT("WriteFile(): write %d bytes\n"), n);
    214  1.2  takemura 		CloseHandle(file);
    215  1.2  takemura 		return (-1);
    216  1.2  takemura 	}
    217  1.2  takemura 
    218  1.2  takemura 	CloseHandle(file);
    219  1.2  takemura 	return (0);
    220  1.2  takemura }
    221  1.2  takemura 
    222