xref: /netbsd/sys/arch/hpc/stand/hpcboot/file_fat.cpp (revision bf9ec67e)
1 /*	$NetBSD: file_fat.cpp,v 1.3 2002/02/04 17:31:05 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 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 
47 	_debug = TRUE;
48 	DPRINTF((TEXT("FileManager: FAT\n")));
49 }
50 
51 FatFile::~FatFile(void)
52 {
53 
54 }
55 
56 BOOL
57 FatFile::setRoot(TCHAR *drive)
58 {
59 
60 	wcsncpy(_drive, drive, MAX_PATH);
61 	wcsncpy(_filename, drive, MAX_PATH);
62 
63 	return TRUE;
64 }
65 
66 BOOL
67 FatFile::open(const TCHAR *name, u_int32_t flags)
68 {
69 	// drive + filename
70 	wsprintf(_filename, TEXT("%s%s"), _drive, name);
71 
72 	// open it.
73 	_handle = CreateFile(_filename, GENERIC_READ, 0, 0,
74 	    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
75 	if (_handle == INVALID_HANDLE_VALUE) {
76 		DPRINTF((TEXT("can't open \"%s\". cause = %d\n"),
77 		    _filename, GetLastError()));
78 		return FALSE;
79 	}
80 
81 	size_t sz = size();
82 	if (sz == ~0) {
83 		DPRINTF((TEXT("can't get file size.\n")));
84 		CloseHandle(_handle);
85 		return FALSE;
86 	}
87 	DPRINTF((TEXT("open file \"%s\"(%d byte).\n"), _filename, sz));
88 
89 	return TRUE;
90 }
91 
92 size_t
93 FatFile::read(void *buf, size_t bytes, off_t ofs)
94 {
95 	unsigned long readed;
96 
97 	if (ofs != -1)
98 		SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
99 	ReadFile(_handle, buf, bytes, &readed, 0);
100 
101 	return size_t(readed);
102 }
103 
104 size_t
105 FatFile::write(const void *buf, size_t bytes, off_t ofs)
106 {
107 	unsigned long wrote;
108 
109 	if (ofs != -1)
110 		SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
111 	WriteFile(_handle, buf, bytes, &wrote, 0);
112 
113 	return size_t(wrote);
114 }
115 
116 BOOL
117 FatFile::seek(off_t ofs)
118 {
119 
120 	SetFilePointer(_handle, ofs, 0, FILE_BEGIN);
121 
122 	return TRUE;
123 }
124