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 <SupportDefs.h>
10
11#include <util/DoublyLinkedList.h>
12#include <boot/stage2_args.h>
13
14
15#ifdef __cplusplus
16
17struct file_map_run;
18
19/** This is the base class for all VFS nodes */
20
21class Node : public DoublyLinkedListLinkImpl<Node> {
22	public:
23		Node();
24		virtual ~Node();
25
26		virtual status_t Open(void **_cookie, int mode);
27		virtual status_t Close(void *cookie);
28
29		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) = 0;
30		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize) = 0;
31
32		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
33		virtual status_t GetFileMap(struct file_map_run *runs, int32 *count);
34		virtual int32 Type() const;
35		virtual off_t Size() const;
36		virtual ino_t Inode() const;
37
38		status_t Acquire();
39		status_t Release();
40
41	protected:
42		int32		fRefCount;
43};
44
45typedef DoublyLinkedList<Node> NodeList;
46typedef NodeList::Iterator NodeIterator;
47
48
49class Directory : public Node {
50	public:
51		Directory();
52
53		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize);
54		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize);
55
56		virtual int32 Type() const;
57
58		virtual Node *Lookup(const char *name, bool traverseLinks) = 0;
59
60		virtual status_t GetNextEntry(void *cookie, char *nameBuffer, size_t bufferSize) = 0;
61		virtual status_t GetNextNode(void *cookie, Node **_node) = 0;
62		virtual status_t Rewind(void *cookie) = 0;
63		virtual bool IsEmpty() = 0;
64
65		virtual status_t CreateFile(const char *name, mode_t permissions,
66			Node **_node);
67};
68
69/** The console based nodes don't need cookies for I/O, they
70 *	also don't support to change the stream position.
71 *	Live is simple in the boot loader :-)
72 */
73
74class ConsoleNode : public Node {
75	public:
76		ConsoleNode();
77
78		virtual ssize_t Read(void *buffer, size_t bufferSize);
79		virtual ssize_t Write(const void *buffer, size_t bufferSize);
80};
81
82
83class MemoryDisk : public Node {
84	public:
85		MemoryDisk(const uint8* data, size_t size, const char* name);
86
87		virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer,
88			size_t bufferSize);
89		virtual ssize_t WriteAt(void* cookie, off_t pos, const void* buffer,
90			size_t bufferSize);
91
92		virtual off_t Size() const;
93		virtual status_t GetName(char *nameBuffer, size_t bufferSize) const;
94
95	private:
96		const uint8*	fData;
97		size_t			fSize;
98		char			fName[64];
99};
100
101
102/* function prototypes */
103
104extern status_t vfs_init(stage2_args *args);
105extern status_t register_boot_file_system(Directory *directory);
106extern Directory *get_boot_file_system(stage2_args *args);
107extern status_t mount_file_systems(stage2_args *args);
108extern int open_node(Node *node, int mode);
109extern int open_from(Directory *directory, const char *path, int mode,
110			mode_t permissions = 0);
111
112extern Node *get_node_from(int fd);
113
114extern status_t add_partitions_for(int fd, bool mountFileSystems, bool isBootDevice = false);
115extern status_t add_partitions_for(Node *device, bool mountFileSystems, bool isBootDevice = false);
116
117#endif	/* __cplusplus */
118
119#endif	/* KERNEL_BOOT_VFS_H */
120