xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/file_fat.cpp (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: file_fat.cpp,v 1.2 2001/05/08 18:51:22 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <file.h>
40 #include <file_fat.h>
41 #include <console.h>
42 
43 FatFile::FatFile(Console *&cons)
44 	: File(cons)
45 {
46 	_debug = TRUE;
47 	DPRINTF((TEXT("File: FAT\n")));
48 }
49 
50 FatFile::~FatFile(void)
51 {
52 }
53 
54 BOOL
55 FatFile::setRoot(TCHAR *drive)
56 {
57 	wcsncpy(_drive, drive, MAX_PATH);
58 	wcsncpy(_filename, drive, MAX_PATH);
59 
60 	return TRUE;
61 }
62 
63 BOOL
64 FatFile::open(const TCHAR *name, u_int32_t flags)
65 {
66 	// drive + filename
67 	wcsncpy(_filename, _drive, MAX_PATH);
68 	wcscat(_filename, name);
69 	// open it.
70 	_handle = CreateFile(_filename, GENERIC_READ | GENERIC_WRITE,
71 	    0, 0, flags, 0, 0);
72 	if (_handle == INVALID_HANDLE_VALUE) {
73 		DPRINTF((TEXT("can't open \"%s\". cause = %d\n"),
74 		    name, GetLastError()));
75 		return FALSE;
76 	}
77 
78 	size_t sz = size();
79 	if (sz == ~0) {
80 		DPRINTF((TEXT("can't get file size.\n")));
81 		CloseHandle(_handle);
82 		return FALSE;
83 	}
84 	DPRINTF((TEXT("open file \"%s\"(%d byte).\n"), _filename, sz));
85 
86 	return TRUE;
87 }
88 
89 size_t
90 FatFile::read(void *buf, size_t bytes, off_t ofs)
91 {
92 	unsigned long readed;
93 
94 	if (ofs != -1)
95 		SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
96 	ReadFile(_handle, buf, bytes, &readed, 0);
97 
98 	return size_t(readed);
99 }
100 
101 size_t
102 FatFile::write(const void *buf, size_t bytes, off_t ofs)
103 {
104 	unsigned long wrote;
105 
106 	if (ofs != -1)
107 		SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
108 	WriteFile(_handle, buf, bytes, &wrote, 0);
109 
110 	return size_t(wrote);
111 }
112 
113 BOOL
114 FatFile::seek(off_t ofs)
115 {
116 	SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
117 
118 	return TRUE;
119 }
120