xref: /haiku/src/system/boot/loader/FileMapDisk.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <boot/FileMapDisk.h>
7 
8 #include <new>
9 
10 #include <endian.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <OS.h>
15 #include <SupportDefs.h>
16 
17 
18 //#define TRACE_FILEMAPDISK
19 #ifdef TRACE_FILEMAPDISK
20 #	define TRACE(x) dprintf x
21 #else
22 #	define TRACE(x) ;
23 #endif
24 
25 // constructor
26 FileMapDisk::FileMapDisk()
27 {
28 }
29 
30 // destructor
31 FileMapDisk::~FileMapDisk()
32 {
33 }
34 
35 // Init
36 status_t
37 FileMapDisk::Init(Node *node/*, Partition *partition, FileMap *map, off_t imageSize*/)
38 {
39 	TRACE(("FileMapDisk::FileMapDisk(%p)\n", node));
40 	fNode = node;
41 	/*
42 	fPartition = partition;
43 	fMap = map;
44 	fImageSize = imageSize;
45 
46 	// create and bind socket
47 	fSocket = new(nothrow) UDPSocket;
48 	if (!fSocket)
49 		return B_NO_MEMORY;
50 
51 	status_t error = fSocket->Bind(INADDR_ANY, 6666);
52 	if (error != B_OK)
53 		return error;
54 	*/
55 
56 	return B_OK;
57 }
58 
59 
60 status_t
61 FileMapDisk::Open(void **_cookie, int mode)
62 {
63 	TRACE(("FileMapDisk::Open(, 0x%08x)\n", mode));
64 	if (!fNode)
65 		return B_NO_INIT;
66 
67 	return fNode->Open(_cookie, mode);
68 }
69 
70 
71 status_t
72 FileMapDisk::Close(void *cookie)
73 {
74 	TRACE(("FileMapDisk::Close(%p)\n", cookie));
75 	if (!fNode)
76 		return B_NO_INIT;
77 
78 	return fNode->Close(cookie);
79 }
80 
81 
82 // ReadAt
83 ssize_t
84 FileMapDisk::ReadAt(void *cookie, off_t pos, void *_buffer,
85 	size_t bufferSize)
86 {
87 	TRACE(("FileMapDisk::ReadAt(%p, %lld, , %ld)\n", cookie, pos, bufferSize));
88 	if (!fNode)
89 		return B_NO_INIT;
90 
91 	return fNode->ReadAt(cookie, pos, _buffer, bufferSize);
92 }
93 
94 
95 // WriteAt
96 ssize_t
97 FileMapDisk::WriteAt(void */*cookie*/, off_t pos, const void *buffer,
98 	size_t bufferSize)
99 {
100 	// Not needed in the boot loader.
101 	return B_PERMISSION_DENIED;
102 }
103 
104 // GetName
105 status_t
106 FileMapDisk::GetName(char *nameBuffer, size_t bufferSize) const
107 {
108 	const char *prefix = "FileMapDisk:";
109 	if (!nameBuffer)
110 		return B_BAD_VALUE;
111 
112 	snprintf(nameBuffer, bufferSize, prefix);
113 	if (bufferSize > strlen(prefix) && fNode)
114 		return fNode->GetName(nameBuffer + strlen(prefix),
115 			bufferSize - strlen(prefix));
116 
117 	return B_OK;
118 }
119 
120 // Size
121 off_t
122 FileMapDisk::Size() const
123 {
124 	if (!fNode)
125 		return B_NO_INIT;
126 	return fNode->Size();
127 }
128 
129 
130 status_t
131 FileMapDisk::GetFileMap(FileMap **map)
132 {
133 	return ENOSYS;
134 }
135 
136 
137 // FindAnyFileMapDisk
138 FileMapDisk *
139 FileMapDisk::FindAnyFileMapDisk(Directory *volume)
140 {
141 	TRACE(("FileMapDisk::FindAnyFileMapDisk(%p)\n", volume));
142 	Node *node;
143 	status_t error;
144 
145 	if (!volume)
146 		return NULL;
147 
148 	//XXX: check lower/mixed case as well
149 	Node *dirnode;
150 	Directory *dir;
151 	dirnode = volume->Lookup(FMAP_FOLDER_NAME, true);
152 	if (!dirnode || !S_ISDIR(dirnode->Type()))
153 		return NULL;
154 	dir = (Directory *)dirnode;
155 	node = dir->Lookup(FMAP_IMAGE_NAME, true);
156 	if (!node)
157 		return NULL;
158 
159 	// create a FileMapDisk object
160 	FileMapDisk *disk = new(nothrow) FileMapDisk;
161 	if (disk) {
162 		error = disk->Init(node);
163 		if (error != B_OK) {
164 			delete disk;
165 			disk = NULL;
166 		}
167 	}
168 
169 	return disk;
170 }
171