3 * - Kernel Status Driver
12 #define VERSION ((0 << 8) | (1)) // 0.01
15 typedef struct sSysFS_Ent
17 struct sSysFS_Ent *Next;
18 struct sSysFS_Ent *ListNext;
19 struct sSysFS_Ent *Parent;
25 int SysFS_Install(char **Arguments);
26 int SysFS_IOCtl(tVFS_Node *Node, int Id, void *Data);
29 int SysFS_RegisterFile(const char *Path, const char *Data, int Length);
30 int SysFS_UpdateFile(int ID, const char *Data, int Length);
31 int SysFS_RemoveFile(int ID);
34 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Id);
35 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename);
36 size_t SysFS_Comm_ReadFile(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
37 void SysFS_Comm_CloseFile(tVFS_Node *Node);
40 extern tSysFS_Ent gSysFS_Version; // Defined Later
41 extern tSysFS_Ent gSysFS_Root; // Defined Later
42 MODULE_DEFINE(0, VERSION, SysFS, SysFS_Install, NULL, NULL);
43 tVFS_NodeType gSysFS_FileNodeType = {
44 .TypeName = "SysFS File",
45 .Read = SysFS_Comm_ReadFile
47 tVFS_NodeType gSysFS_DirNodeType = {
48 .TypeName = "SysFS Dir",
49 .ReadDir = SysFS_Comm_ReadDir,
50 .FindDir = SysFS_Comm_FindDir
52 tSysFS_Ent gSysFS_Version_Kernel = {
54 &gSysFS_Version, // Parent
56 .Inode = 1, // File #1
58 .ImplInt = (Uint)&gSysFS_Version_Kernel, // Self-Link
61 .ACLs = &gVFS_ACL_EveryoneRO,
62 .Type = &gSysFS_FileNodeType
66 tSysFS_Ent gSysFS_Version = {
71 .ImplPtr = &gSysFS_Version_Kernel,
72 .ImplInt = (Uint)&gSysFS_Version, // Self-Link
74 .ACLs = &gVFS_ACL_EveryoneRX,
75 .Flags = VFS_FFLAG_DIRECTORY,
76 .Type = &gSysFS_DirNodeType
80 // Root of the SysFS tree (just used to keep the code clean)
81 tSysFS_Ent gSysFS_Root = {
86 .ImplPtr = &gSysFS_Version,
87 .ImplInt = (Uint)&gSysFS_Root // Self-Link
91 tDevFS_Driver gSysFS_DriverInfo = {
95 .ACLs = &gVFS_ACL_EveryoneRX,
96 .Flags = VFS_FFLAG_DIRECTORY,
97 .ImplPtr = &gSysFS_Version,
98 .Size = sizeof(gSysFS_Root)/sizeof(tSysFS_Ent),
99 .Type = &gSysFS_DirNodeType
102 int giSysFS_NextFileID = 2;
103 tSysFS_Ent *gSysFS_FileList;
107 * \fn int SysFS_Install(char **Options)
108 * \brief Installs the SysFS Driver
110 int SysFS_Install(char **Options)
113 const char *fmt = "Acess2 "EXPAND_STR(KERNEL_VERSION)" "EXPAND_STR(ARCHDIR)" build %i, hash %s";
114 gSysFS_Version_Kernel.Node.Size = sprintf(NULL, fmt, BUILD_NUM, gsGitHash);
115 gSysFS_Version_Kernel.Node.ImplPtr = malloc( gSysFS_Version_Kernel.Node.Size + 1 );
116 sprintf(gSysFS_Version_Kernel.Node.ImplPtr, fmt, BUILD_NUM, gsGitHash);
119 DevFS_AddDevice( &gSysFS_DriverInfo );
120 return MODULE_ERR_OK;
124 * \fn int SysFS_RegisterFile(char *Path, char *Data, int Length)
125 * \brief Registers a file (buffer) for the user to be able to read from
126 * \param Path Path for the file to be accessable from (relative to SysFS root)
127 * \param Data Pointer to the data buffer (must be non-volatile)
128 * \param Length Length of the data buffer
129 * \return The file's identifier
131 int SysFS_RegisterFile(const char *Path, const char *Data, int Length)
135 tSysFS_Ent *ent = NULL;
136 tSysFS_Ent *child, *prev;
138 // Find parent directory
139 while( (tmp = strpos(&Path[start], '/')) != -1 )
144 child = ent->Node.ImplPtr;
146 child = gSysFS_DriverInfo.RootNode.ImplPtr;
147 for( ; child; prev = child, child = child->Next )
149 if( strncmp( &Path[start], child->Name, tmp+1 ) == '/' )
153 // Need a new directory?
156 child = calloc( 1, sizeof(tSysFS_Ent)+tmp+1 );
158 memcpy(child->Name, &Path[start], tmp);
159 child->Name[tmp] = '\0';
161 child->Node.Inode = 0;
162 child->Node.ImplPtr = NULL;
163 child->Node.ImplInt = (Uint)child; // Uplink
164 child->Node.NumACLs = 1;
165 child->Node.ACLs = &gVFS_ACL_EveryoneRX;
166 child->Node.Flags = VFS_FFLAG_DIRECTORY;
167 child->Node.Type = &gSysFS_DirNodeType;
170 ent->Node.ImplPtr = child;
172 gSysFS_DriverInfo.RootNode.ImplPtr = child;
173 // ^^^ Impossible (There is already /Version)
180 gSysFS_DriverInfo.RootNode.Size ++;
181 Log_Log("SysFS", "Added directory '%s'", child->Name);
189 // ent: Parent tSysFS_Ent or NULL
190 // start: beginning of last path element
192 // Check if the name is taken
195 child = ent->Node.ImplPtr;
197 child = gSysFS_DriverInfo.RootNode.ImplPtr;
198 for( ; child; child = child->Next )
200 if( strcmp( &Path[start], child->Name ) == 0 )
204 Log_Warning("SysFS", "'%s' is taken (in '%s')\n", &Path[start], Path);
209 child = calloc( 1, sizeof(tSysFS_Ent)+strlen(&Path[start])+1 );
211 strcpy(child->Name, &Path[start]);
214 child->Node.Inode = giSysFS_NextFileID++;
215 child->Node.ImplPtr = (void*)Data;
216 child->Node.ImplInt = (Uint)child; // Uplink
217 child->Node.Size = Length;
218 child->Node.NumACLs = 1;
219 child->Node.ACLs = &gVFS_ACL_EveryoneRO;
220 child->Node.Type = &gSysFS_FileNodeType;
222 // Add to parent's child list
225 child->Next = ent->Node.ImplPtr;
226 ent->Node.ImplPtr = child;
229 gSysFS_DriverInfo.RootNode.Size ++;
230 child->Next = gSysFS_DriverInfo.RootNode.ImplPtr;
231 gSysFS_DriverInfo.RootNode.ImplPtr = child;
233 // Add to global file list
234 child->ListNext = gSysFS_FileList;
235 gSysFS_FileList = child;
237 Log_Log("SysFS", "Added '%s' (%p)", Path, Data);
239 return child->Node.Inode;
243 * \fn int SysFS_UpdateFile(int ID, char *Data, int Length)
244 * \brief Updates a file
245 * \param ID Identifier returned by ::SysFS_RegisterFile
246 * \param Data Pointer to the data buffer
247 * \param Length Length of the data buffer
248 * \return Boolean Success
250 int SysFS_UpdateFile(int ID, const char *Data, int Length)
254 for( ent = gSysFS_FileList; ent; ent = ent->Next )
256 // It's a reverse sorted list
257 if(ent->Node.Inode < ID) return 0;
258 if(ent->Node.Inode == ID)
260 ent->Node.ImplPtr = (void*)Data;
261 ent->Node.Size = Length;
270 * \fn int SysFS_RemoveFile(int ID)
271 * \brief Removes a file from user access
272 * \param ID Identifier returned by ::SysFS_RegisterFile
273 * \return Boolean Success
274 * \note If a handle is still open to the file, it will be invalidated
276 int SysFS_RemoveFile(int ID)
279 tSysFS_Ent *ent, *parent, *prev;
282 for( ent = gSysFS_FileList; ent; prev = ent, ent = ent->Next )
284 // It's a reverse sorted list
285 if(ent->Node.Inode < ID) return 0;
286 if(ent->Node.Inode == ID) break;
291 // Set up for next part
293 parent = file->Parent;
295 // Remove from file list
297 prev->ListNext = file->ListNext;
299 gSysFS_FileList = file->ListNext;
301 file->Node.ImplPtr = NULL;
303 // Search parent directory
304 for( ent = parent->Node.ImplPtr; ent; prev = ent, ent = ent->Next )
306 if( ent == file ) break;
309 Log_Warning("SysFS", "Bookkeeping Error: File in list, but not in directory");
313 // Remove from parent directory
315 prev->Next = ent->Next;
317 parent->Node.ImplPtr = ent->Next;
319 // Free if not in use
320 if(file->Node.ReferenceCount == 0)
327 * \fn char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
328 * \brief Reads from a SysFS directory
330 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
332 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
333 if(Pos < 0 || Pos >= Node->Size) return NULL;
335 for( ; child; child = child->Next, Pos-- )
337 if( Pos == 0 ) return strdup(child->Name);
343 * \fn tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename)
344 * \brief Find a file in a SysFS directory
346 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename)
348 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
350 for( ; child; child = child->Next )
352 if( strcmp(child->Name, Filename) == 0 )
360 * \brief Read from an exposed buffer
362 size_t SysFS_Comm_ReadFile(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
364 if( Offset > Node->Size ) return -1;
365 if( Length > Node->Size ) Length = Node->Size;
366 if( Offset + Length > Node->Size) Length = Node->Size - Offset;
369 memcpy(Buffer, (void*)((Uint)Node->ImplPtr+(Uint)Offset), Length);
377 * \fn void SysFS_Comm_CloseFile(tVFS_Node *Node)
378 * \brief Closes an open file
379 * \param Node Node to close
381 void SysFS_Comm_CloseFile(tVFS_Node *Node)
384 Node->ReferenceCount --;
385 if( Node->ReferenceCount > 0 ) return;
387 // Check if it is still valid
388 if( Node->ImplPtr ) return;
391 free( (void*)Node->ImplInt );