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