1/*
2 * Copyright 2003-2005, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef KERNEL_BOOT_VFS_H
6#define KERNEL_BOOT_VFS_H
7
8
9#include <dirent.h>
10
11#include <SupportDefs.h>
12
13#include <util/DoublyLinkedList.h>
14#include <boot/stage2_args.h>
15
16
17#ifdef __cplusplus
18
19struct file_map_run;
20struct stat;
21class PackageVolumeInfo;
22class PackageVolumeState;
23
24/** This is the base class for all VFS nodes */
25
26class Node : public DoublyLinkedListLinkImpl<Node> {
27	public:
28		Node();
29		virtual ~Node();
30
31		virtual status_t Open(void **_cookie, int mode);
32		virtual status_t Close(void *cookie);
33
34		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer,
35			size_t bufferSize) = 0;
36		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer,
37			size_t bufferSize) = 0;
38
39		virtual status_t ReadLink(char* buffer, size_t bufferSize);
40
41		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
42		virtual status_t GetFileMap(struct file_map_run *runs, int32 *count);
43		virtual int32 Type() const;
44		virtual off_t Size() const;
45		virtual ino_t Inode() const;
46
47		void Stat(struct stat& stat);
48
49		status_t Acquire();
50		status_t Release();
51
52	protected:
53		int32		fRefCount;
54};
55
56typedef DoublyLinkedList<Node> NodeList;
57typedef NodeList::Iterator NodeIterator;
58
59
60class Directory : public Node {
61	public:
62		Directory();
63
64		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize);
65		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize);
66
67		virtual int32 Type() const;
68
69		virtual Node* Lookup(const char* name, bool traverseLinks);
70		virtual Node* LookupDontTraverse(const char* name) = 0;
71
72		virtual status_t GetNextEntry(void *cookie, char *nameBuffer, size_t bufferSize) = 0;
73		virtual status_t GetNextNode(void *cookie, Node **_node) = 0;
74		virtual status_t Rewind(void *cookie) = 0;
75		virtual bool IsEmpty() = 0;
76
77		virtual status_t CreateFile(const char *name, mode_t permissions,
78			Node **_node);
79};
80
81/** The console based nodes don't need cookies for I/O, they
82 *	also don't support to change the stream position.
83 *	Live is simple in the boot loader :-)
84 */
85
86class ConsoleNode : public Node {
87	public:
88		ConsoleNode();
89
90		virtual ssize_t Read(void *buffer, size_t bufferSize);
91		virtual ssize_t Write(const void *buffer, size_t bufferSize);
92
93		virtual void	ClearScreen() = 0;
94		virtual int32	Width() = 0;
95		virtual int32	Height() = 0;
96		virtual void	SetCursor(int32 x, int32 y) = 0;
97		virtual void	SetCursorVisible(bool visible) = 0;
98		virtual void	SetColors(int32 foreground, int32 background) = 0;
99};
100
101
102class MemoryDisk : public Node {
103	public:
104		MemoryDisk(const uint8* data, size_t size, const char* name);
105
106		virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer,
107			size_t bufferSize);
108		virtual ssize_t WriteAt(void* cookie, off_t pos, const void* buffer,
109			size_t bufferSize);
110
111		virtual off_t Size() const;
112		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
113
114	private:
115		const uint8*	fData;
116		size_t			fSize;
117		char			fName[64];
118};
119
120
121class BootVolume {
122public:
123								BootVolume();
124								~BootVolume();
125
126			status_t			SetTo(Directory* rootDirectory,
127									PackageVolumeInfo* packageVolumeInfo
128										= NULL,
129									PackageVolumeState* packageVolumeState
130										= NULL);
131			void				Unset();
132
133			bool				IsValid() const
134									{ return fRootDirectory != NULL; }
135
136			Directory*			RootDirectory() const
137									{ return fRootDirectory; }
138			Directory*			SystemDirectory() const
139									{ return fSystemDirectory; }
140			bool				IsPackaged() const
141									{ return fPackageVolumeInfo != NULL; }
142			PackageVolumeInfo*	GetPackageVolumeInfo() const
143									{ return fPackageVolumeInfo; }
144			PackageVolumeState*	GetPackageVolumeState() const
145									{ return fPackageVolumeState; }
146
147private:
148			status_t			_SetTo(Directory* rootDirectory,
149									PackageVolumeInfo* packageVolumeInfo,
150									PackageVolumeState* packageVolumeState);
151			int					_OpenSystemPackage();
152
153private:
154			Directory*			fRootDirectory;
155				// root directory of the volume
156			Directory*			fSystemDirectory;
157				// "system" directory of the volume; if packaged the root
158				// directory of the mounted packagefs
159			PackageVolumeInfo*	fPackageVolumeInfo;
160			PackageVolumeState*	fPackageVolumeState;
161};
162
163
164/* function prototypes */
165
166extern status_t vfs_init(stage2_args *args);
167extern status_t register_boot_file_system(BootVolume& bootVolume);
168extern status_t get_boot_file_system(stage2_args* args,
169			BootVolume& _bootVolume);
170extern status_t mount_file_systems(stage2_args *args);
171extern int open_node(Node *node, int mode);
172extern int open_from(Directory *directory, const char *path, int mode,
173			mode_t permissions = 0);
174extern DIR* open_directory(Directory* baseDirectory, const char* path);
175extern status_t get_stat(Directory* directory, const char* path,
176			struct stat& st);
177
178extern Node* get_node_from(int fd);
179	// returns a reference
180extern Directory* directory_from(DIR* dir);
181	// does not return a reference
182
183extern status_t add_partitions_for(int fd, bool mountFileSystems, bool isBootDevice = false);
184extern status_t add_partitions_for(Node *device, bool mountFileSystems, bool isBootDevice = false);
185
186#endif	/* __cplusplus */
187
188#endif	/* KERNEL_BOOT_VFS_H */
189