xref: /haiku/src/system/boot/loader/FileMapDisk.cpp (revision 239222b2369c39dc52df52b0a7cdd6cc0a91bc92)
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 #include <boot_item.h>
8 
9 #include <new>
10 
11 #include <endian.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <OS.h>
16 #include <SupportDefs.h>
17 
18 
19 //#define TRACE_FILEMAPDISK
20 #ifdef TRACE_FILEMAPDISK
21 #	define TRACE(x) dprintf x
22 #else
23 #	define TRACE(x) ;
24 #endif
25 
26 // constructor
27 FileMapDisk::FileMapDisk()
28 {
29 }
30 
31 // destructor
32 FileMapDisk::~FileMapDisk()
33 {
34 }
35 
36 // Init
37 status_t
38 FileMapDisk::Init(Node *node/*, Partition *partition, FileMap *map, off_t imageSize*/)
39 {
40 	TRACE(("FileMapDisk::FileMapDisk(%p)\n", node));
41 	fNode = node;
42 	/*
43 	fPartition = partition;
44 	fMap = map;
45 	fImageSize = imageSize;
46 
47 	// create and bind socket
48 	fSocket = new(nothrow) UDPSocket;
49 	if (!fSocket)
50 		return B_NO_MEMORY;
51 
52 	status_t error = fSocket->Bind(INADDR_ANY, 6666);
53 	if (error != B_OK)
54 		return error;
55 	*/
56 
57 	return B_OK;
58 }
59 
60 
61 status_t
62 FileMapDisk::Open(void **_cookie, int mode)
63 {
64 	TRACE(("FileMapDisk::Open(, 0x%08x)\n", mode));
65 	if (!fNode)
66 		return B_NO_INIT;
67 
68 	return fNode->Open(_cookie, mode);
69 }
70 
71 
72 status_t
73 FileMapDisk::Close(void *cookie)
74 {
75 	TRACE(("FileMapDisk::Close(%p)\n", cookie));
76 	if (!fNode)
77 		return B_NO_INIT;
78 
79 	return fNode->Close(cookie);
80 }
81 
82 
83 // ReadAt
84 ssize_t
85 FileMapDisk::ReadAt(void *cookie, off_t pos, void *_buffer,
86 	size_t bufferSize)
87 {
88 	TRACE(("FileMapDisk::ReadAt(%p, %lld, , %ld)\n", cookie, pos, bufferSize));
89 	if (!fNode)
90 		return B_NO_INIT;
91 
92 	return fNode->ReadAt(cookie, pos, _buffer, bufferSize);
93 }
94 
95 
96 // WriteAt
97 ssize_t
98 FileMapDisk::WriteAt(void */*cookie*/, off_t pos, const void *buffer,
99 	size_t bufferSize)
100 {
101 	// Not needed in the boot loader.
102 	return B_PERMISSION_DENIED;
103 }
104 
105 // GetName
106 status_t
107 FileMapDisk::GetName(char *nameBuffer, size_t bufferSize) const
108 {
109 	const char *prefix = "FileMapDisk:";
110 	if (!nameBuffer)
111 		return B_BAD_VALUE;
112 
113 	snprintf(nameBuffer, bufferSize, prefix);
114 	if (bufferSize > strlen(prefix) && fNode)
115 		return fNode->GetName(nameBuffer + strlen(prefix),
116 			bufferSize - strlen(prefix));
117 
118 	return B_OK;
119 }
120 
121 
122 status_t
123 FileMapDisk::GetFileMap(struct file_map_run *runs, int32 *count)
124 {
125 	return fNode->GetFileMap(runs, count);
126 }
127 
128 
129 off_t
130 FileMapDisk::Size() const
131 {
132 	if (!fNode)
133 		return B_NO_INIT;
134 	return fNode->Size();
135 }
136 
137 
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 
172 
173 status_t
174 FileMapDisk::RegisterFileMapBootItem()
175 {
176 	return B_ERROR;
177 	struct file_map_boot_item *item;
178 	item = (struct file_map_boot_item *)malloc(sizeof(struct file_map_boot_item));
179 	item->num_runs = FMAP_MAX_RUNS;
180 	status_t err;
181 	err = GetFileMap(item->runs, &item->num_runs);
182 	if (err < B_OK)
183 		return err;
184 //	err = add_boot_item("file_map_disk", item, sizeof(struct file_map_boot_item));
185 	err = B_ERROR;
186 	return err;
187 }
188 
189 
190