3 * - Kernel Status Driver
12 #define VERSION ((0 << 8) | (1)) // 0.01
13 #define KERNEL_VERSION_STRING ("Acess2 " EXPAND_STR(KERNEL_VERSION) " build " EXPAND_STR(BUILD_NUM))
16 typedef struct sSysFS_Ent
18 struct sSysFS_Ent *Next;
19 struct sSysFS_Ent *ListNext;
20 struct sSysFS_Ent *Parent;
26 int SysFS_Install(char **Arguments);
27 int SysFS_IOCtl(tVFS_Node *Node, int Id, void *Data);
29 int SysFS_RegisterFile(char *Path, char *Data, int Length);
30 int SysFS_UpdateFile(int ID, char *Data, int Length);
31 int SysFS_RemoveFile(int ID);
33 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Id);
34 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, char *Filename);
35 Uint64 SysFS_Comm_ReadFile(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
36 void SysFS_Comm_CloseFile(tVFS_Node *Node);
39 extern tSysFS_Ent gSysFS_Version; // Defined Later
40 extern tSysFS_Ent gSysFS_Root; // Defined Later
41 MODULE_DEFINE(0, VERSION, SysFS, SysFS_Install, NULL, NULL);
42 tSysFS_Ent gSysFS_Version_Kernel = {
44 &gSysFS_Version, // Parent
47 .Inode = 1, // File #1
48 .ImplPtr = KERNEL_VERSION_STRING,
49 .ImplInt = (Uint)&gSysFS_Version_Kernel, // Self-Link
50 .Size = sizeof(KERNEL_VERSION_STRING)-1,
52 .ACLs = &gVFS_ACL_EveryoneRO,
53 .Read = SysFS_Comm_ReadFile
56 tSysFS_Ent gSysFS_Version = {
62 .ImplPtr = &gSysFS_Version_Kernel,
63 .ImplInt = (Uint)&gSysFS_Version, // Self-Link
65 .ACLs = &gVFS_ACL_EveryoneRX,
66 .Flags = VFS_FFLAG_DIRECTORY,
67 .ReadDir = SysFS_Comm_ReadDir,
68 .FindDir = SysFS_Comm_FindDir
71 // Root of the SysFS tree (just used to keep the code clean)
72 tSysFS_Ent gSysFS_Root = {
78 .ImplPtr = &gSysFS_Version,
79 .ImplInt = (Uint)&gSysFS_Root // Self-Link
82 tDevFS_Driver gSysFS_DriverInfo = {
86 .ACLs = &gVFS_ACL_EveryoneRX,
87 .Flags = VFS_FFLAG_DIRECTORY,
88 .ImplPtr = &gSysFS_Version,
89 .Size = sizeof(gSysFS_Root)/sizeof(tSysFS_Ent),
90 .ReadDir = SysFS_Comm_ReadDir,
91 .FindDir = SysFS_Comm_FindDir,
95 int giSysFS_NextFileID = 2;
96 tSysFS_Ent *gSysFS_FileList;
100 * \fn int SysFS_Install(char **Options)
101 * \brief Installs the SysFS Driver
103 int SysFS_Install(char **Options)
105 DevFS_AddDevice( &gSysFS_DriverInfo );
106 return MODULE_ERR_OK;
110 * \fn int SysFS_RegisterFile(char *Path, char *Data, int Length)
111 * \brief Registers a file (buffer) for the user to be able to read from
112 * \param Path Path for the file to be accessable from (relative to SysFS root)
113 * \param Data Pointer to the data buffer (must be non-volatile)
114 * \param Length Length of the data buffer
115 * \return The file's identifier
117 int SysFS_RegisterFile(char *Path, char *Data, int Length)
121 tSysFS_Ent *ent = NULL;
122 tSysFS_Ent *child, *prev;
124 // Find parent directory
125 while( (tmp = strpos(&Path[start], '/')) != -1 )
130 child = ent->Node.ImplPtr;
132 child = gSysFS_DriverInfo.RootNode.ImplPtr;
133 for( ; child; prev = child, child = child->Next )
135 if( strncmp( &Path[start], child->Name, tmp+1 ) == '/' )
139 // Need a new directory?
142 child = calloc( 1, sizeof(tSysFS_Ent) );
144 child->Name = malloc(tmp+1);
145 memcpy(child->Name, &Path[start], tmp);
146 child->Name[tmp] = '\0';
148 child->Node.Inode = 0;
149 child->Node.ImplPtr = NULL;
150 child->Node.ImplInt = (Uint)child; // Uplink
151 child->Node.NumACLs = 1;
152 child->Node.ACLs = &gVFS_ACL_EveryoneRX;
153 child->Node.Flags = VFS_FFLAG_DIRECTORY;
154 child->Node.ReadDir = SysFS_Comm_ReadDir;
155 child->Node.FindDir = SysFS_Comm_FindDir;
158 ent->Node.ImplPtr = child;
160 // gSysFS_DriverInfo.RootNode.ImplPtr = child;
161 // ^^^ Impossible (There is already /Version)
168 gSysFS_DriverInfo.RootNode.Size ++;
169 Log_Log("SysFS", "Added directory '%s'", child->Name);
177 // ent: Parent tSysFS_Ent or NULL
178 // start: beginning of last path element
180 // Check if the name is taken
183 child = ent->Node.ImplPtr;
185 child = gSysFS_DriverInfo.RootNode.ImplPtr;
186 for( child = ent->Node.ImplPtr; child; prev = child, child = child->Next )
188 if( strcmp( &Path[start], child->Name ) == 0 )
192 Log_Warning("SysFS", "'%s' is taken (in '%s')\n", &Path[start], Path);
197 child = calloc( 1, sizeof(tSysFS_Ent) );
199 child->Name = strdup(&Path[start]);
202 child->Node.Inode = giSysFS_NextFileID++;
203 child->Node.ImplPtr = Data;
204 child->Node.ImplInt = (Uint)child; // Uplink
205 child->Node.Size = Length;
206 child->Node.NumACLs = 1;
207 child->Node.ACLs = &gVFS_ACL_EveryoneRO;
208 child->Node.Read = SysFS_Comm_ReadFile;
209 child->Node.Close = SysFS_Comm_CloseFile;
211 // Add to parent's child list
214 child->Next = ent->Node.ImplPtr;
215 ent->Node.ImplPtr = child;
218 gSysFS_DriverInfo.RootNode.Size ++;
219 child->Next = gSysFS_DriverInfo.RootNode.ImplPtr;
220 gSysFS_DriverInfo.RootNode.ImplPtr = child;
222 // Add to global file list
223 child->ListNext = gSysFS_FileList;
224 gSysFS_FileList = child;
226 Log_Log("SysFS", "Added '%s' (%p)", Path, Data);
228 return child->Node.Inode;
232 * \fn int SysFS_UpdateFile(int ID, char *Data, int Length)
233 * \brief Updates a file
234 * \param ID Identifier returned by ::SysFS_RegisterFile
235 * \param Data Pointer to the data buffer
236 * \param Length Length of the data buffer
237 * \return Boolean Success
239 int SysFS_UpdateFile(int ID, char *Data, int Length)
243 for( ent = gSysFS_FileList; ent; ent = ent->Next )
245 // It's a reverse sorted list
246 if(ent->Node.Inode < ID) return 0;
247 if(ent->Node.Inode == ID)
249 ent->Node.ImplPtr = Data;
250 ent->Node.Size = Length;
259 * \fn int SysFS_RemoveFile(int ID)
260 * \brief Removes a file from user access
261 * \param ID Identifier returned by ::SysFS_RegisterFile
262 * \return Boolean Success
263 * \note If a handle is still open to the file, it will be invalidated
265 int SysFS_RemoveFile(int ID)
268 tSysFS_Ent *ent, *parent, *prev;
271 for( ent = gSysFS_FileList; ent; prev = ent, ent = ent->Next )
273 // It's a reverse sorted list
274 if(ent->Node.Inode < ID) return 0;
275 if(ent->Node.Inode == ID) break;
280 // Set up for next part
282 parent = file->Parent;
284 // Remove from file list
285 prev->ListNext = file->ListNext;
287 file->Node.ImplPtr = NULL;
289 // Search parent directory
290 for( ent = parent->Node.ImplPtr; ent; prev = ent, ent = ent->Next )
292 if( ent == file ) break;
295 Log_Warning("SysFS", "Bookkeeping Error: File in list, but not in directory");
299 // Remove from parent directory
300 prev->Next = ent->Next;
302 // Free if not in use
303 if(file->Node.ReferenceCount == 0)
310 * \fn char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
311 * \brief Reads from a SysFS directory
313 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
315 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
316 if(Pos < 0 || Pos >= Node->Size) return NULL;
318 for( ; child; child = child->Next, Pos-- )
320 if( Pos == 0 ) return strdup(child->Name);
326 * \fn tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, char *Filename)
327 * \brief Find a file in a SysFS directory
329 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, char *Filename)
331 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
333 for( ; child; child = child->Next )
335 if( strcmp(child->Name, Filename) == 0 )
343 * \fn Uint64 SysFS_Comm_ReadFile(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
344 * \brief Read from an exposed buffer
346 Uint64 SysFS_Comm_ReadFile(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
348 if( Offset > Node->Size ) return -1;
349 if( Length > Node->Size ) Length = Node->Size;
350 if( Offset + Length > Node->Size) Length = Node->Size - Offset;
353 memcpy(Buffer, (void*)((Uint)Node->ImplPtr+(Uint)Offset), Length);
361 * \fn void SysFS_Comm_CloseFile(tVFS_Node *Node)
362 * \brief Closes an open file
363 * \param Node Node to close
365 void SysFS_Comm_CloseFile(tVFS_Node *Node)
368 Node->ReferenceCount --;
369 if( Node->ReferenceCount > 0 ) return;
371 // Check if it is still valid
372 if( Node->ImplPtr ) return;
375 free( (void*)Node->ImplInt );