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