Home | History | Annotate | Line # | Download | only in pbsdboot
preference.c revision 1.2
      1 /*	$NetBSD: preference.c,v 1.2 2000/01/16 03:07:33 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(TCHAR* 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], 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_write(TCHAR* filename, struct preference_s* buf)
    146 {
    147 	HANDLE file;
    148 	DWORD n;
    149 
    150 	debug_printf(TEXT("pref_write('%s').\n"), filename);
    151 	pref_dump(&pref);
    152 
    153        	file = CreateFile(
    154 	       	filename,      	/* file name */
    155 	       	GENERIC_WRITE,	/* access (read-write) mode */
    156 	       	FILE_SHARE_WRITE,/* share mode */
    157 	       	NULL,		/* pointer to security attributes */
    158 	       	CREATE_ALWAYS,	/* how to create */
    159 	       	FILE_ATTRIBUTE_NORMAL,	/* file attributes*/
    160 	       	NULL		/* handle to file with attributes to */
    161 	       	);
    162 
    163 	if (file == INVALID_HANDLE_VALUE) {
    164 		msg_printf(MSG_ERROR, TEXT("pref_write()"),
    165 			   TEXT("CreateFile(): error=%d"), GetLastError());
    166 		debug_printf(TEXT("CreateFile(): error=%d\n"), GetLastError());
    167 		return (-1);
    168 	}
    169 
    170 	if (!WriteFile(file, buf, sizeof(*buf), &n, NULL)) {
    171 		msg_printf(MSG_ERROR, TEXT("pref_write()"),
    172 			   TEXT("WriteFile(): error=%d"), GetLastError());
    173 		debug_printf(TEXT("WriteFile(): error=%d\n"), GetLastError());
    174 		CloseHandle(file);
    175 		return (-1);
    176 	}
    177 
    178 	if (n != sizeof(*buf)) {
    179 		msg_printf(MSG_ERROR, TEXT("pref_write()"),
    180 			   TEXT("WriteFile(): write %d bytes"), n);
    181 		debug_printf(TEXT("WriteFile(): write %d bytes\n"), n);
    182 		CloseHandle(file);
    183 		return (-1);
    184 	}
    185 
    186 	CloseHandle(file);
    187 	return (0);
    188 }
    189 
    190