Skip to content

Commit 5d494fd

Browse files
Added proper proc folder with files. (currently only placeholder stat and a working heap)
1 parent e29e913 commit 5d494fd

File tree

13 files changed

+304
-45
lines changed

13 files changed

+304
-45
lines changed

disk.img

0 Bytes
Binary file not shown.

source/includes/ahci.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ typedef enum {
163163
FS_UDF,
164164

165165
// OS / Custom
166-
FS_FROSTFS,
167-
FS_RAMFS,
166+
FS_PROC
168167
} partition_fs_type_t;
169168

170169

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @file proc.h
3+
* @author Pradosh ([email protected])
4+
* @brief The proc folder to handle by the VFS.
5+
* @version 0.1
6+
* @date 2026-01-05
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
12+
#ifndef PROC_H
13+
#define PROC_H
14+
15+
#include <basics.h>
16+
#include <filesystems/vfs.h>
17+
18+
typedef int (*procfs_read_cb)(
19+
vfs_file_t* file,
20+
uint8_t* buf,
21+
uint32_t size,
22+
void* priv
23+
);
24+
25+
typedef int (*procfs_write_cb)(
26+
vfs_file_t* file,
27+
const uint8_t* buf,
28+
uint32_t size,
29+
void* priv
30+
);
31+
32+
/* One proc file entry */
33+
typedef struct procfs_entry {
34+
const char* name; // "stat", "uptime", etc
35+
procfs_read_cb read;
36+
procfs_write_cb write;
37+
void* priv;
38+
} procfs_entry_t;
39+
40+
/* API */
41+
void procfs_init(void);
42+
int procfs_register(procfs_entry_t* entry);
43+
44+
int procfs_open(vfs_file_t* file);
45+
int procfs_read(vfs_file_t* file, uint8_t* buf, uint32_t size);
46+
int procfs_write(vfs_file_t* file, const uint8_t* buf, uint32_t size);
47+
void procfs_close(vfs_file_t* file);
48+
49+
#endif

source/includes/filesystems/vfs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
typedef struct {
1919
mount_entry_t* mnt;
2020
fat16_file_t f;
21+
uint32_t pos; // for virtual files only, must not be used for real fs
2122
int flags;
23+
24+
char rel_path[64]; // for virtual files only, must not be used for real fs
2225
} vfs_file_t;
2326

2427
typedef struct {

source/includes/heap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include <stddef.h>
1414
#include <basics.h>
1515

16+
extern uint64_t heap_begin;
17+
extern uint64_t heap_end;
18+
extern uint64_t last_alloc;
19+
extern uint64_t alloc_count;
20+
extern uint64_t memory_used;
21+
1622
/**
1723
* @brief Function to initlialize heap.
1824
*
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @file proc.c
3+
* @author Pradosh ([email protected])
4+
* @brief The proc folder to handle by the VFS.
5+
* @version 0.1
6+
* @date 2026-01-05
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#include <filesystems/layers/proc.h>
12+
#include <basics.h>
13+
#include <strings.h>
14+
#include <heap.h>
15+
16+
#define PROCFS_MAX_FILES 32
17+
18+
static procfs_entry_t* proc_files[PROCFS_MAX_FILES];
19+
static int proc_file_count = 0;
20+
21+
/* CODE FOR PROC FILES */
22+
23+
static int proc_stat_read(
24+
vfs_file_t* file,
25+
uint8_t* buf,
26+
uint32_t size,
27+
void* priv
28+
) {
29+
(void)priv;
30+
31+
char tmp[128];
32+
int len = snprintf(tmp, sizeof(tmp),
33+
"cpu 0 0 0 0\n"
34+
);
35+
36+
if (file->pos >= (uint32_t)len)
37+
return 0;
38+
39+
uint32_t rem = len - file->pos;
40+
if (rem > size) rem = size;
41+
42+
memcpy(buf, tmp + file->pos, rem);
43+
file->pos += rem;
44+
return rem;
45+
}
46+
47+
static procfs_entry_t proc_stat = {
48+
.name = "stat",
49+
.read = proc_stat_read,
50+
.write = NULL,
51+
.priv = NULL
52+
};
53+
54+
static int proc_heap_read(
55+
vfs_file_t* file,
56+
uint8_t* buf,
57+
uint32_t size,
58+
void* priv
59+
) {
60+
(void)priv;
61+
62+
char tmp[128];
63+
int len = snprintf(tmp, sizeof(tmp),
64+
"HeapTotal: %u bytes\nHeapUsed: %u bytes\nHeapFree: %u bytes\nAllocCount: %u",
65+
(heap_end - heap_begin),
66+
(memory_used),
67+
(heap_end - last_alloc),
68+
alloc_count
69+
);
70+
71+
if (file->pos >= (uint32_t)len)
72+
return 0;
73+
74+
uint32_t rem = len - file->pos;
75+
if (rem > size) rem = size;
76+
77+
memcpy(buf, tmp + file->pos, rem);
78+
file->pos += rem;
79+
return rem;
80+
}
81+
82+
static procfs_entry_t proc_heap = {
83+
.name = "heap",
84+
.read = proc_heap_read,
85+
.write = NULL,
86+
.priv = NULL
87+
};
88+
89+
90+
/* END */
91+
92+
void procfs_init(void) {
93+
proc_file_count = 0;
94+
memset(proc_files, 0, sizeof(proc_files));
95+
procfs_register(&proc_stat);
96+
procfs_register(&proc_heap);
97+
}
98+
99+
/* Register a virtual proc file */
100+
int procfs_register(procfs_entry_t* entry) {
101+
if (proc_file_count >= PROCFS_MAX_FILES)
102+
return -1;
103+
104+
proc_files[proc_file_count++] = entry;
105+
return 0;
106+
}
107+
108+
/* Find proc entry by rel_path */
109+
static procfs_entry_t* procfs_find(const char* name) {
110+
for (int i = 0; i < proc_file_count; i++) {
111+
if (strcmp(proc_files[i]->name, name) == 0)
112+
return proc_files[i];
113+
}
114+
return NULL;
115+
}
116+
117+
int procfs_open(vfs_file_t* file) {
118+
procfs_entry_t* e = procfs_find(file->rel_path);
119+
if (!e)
120+
return -1;
121+
122+
file->pos = 0;
123+
return 0;
124+
}
125+
126+
int procfs_read(vfs_file_t* file, uint8_t* buf, uint32_t size) {
127+
procfs_entry_t* e = procfs_find(file->rel_path);
128+
if (!e || !e->read)
129+
return 0;
130+
131+
return e->read(file, buf, size, e->priv);
132+
}
133+
134+
int procfs_write(vfs_file_t* file, const uint8_t* buf, uint32_t size) {
135+
procfs_entry_t* e = procfs_find(file->rel_path);
136+
if (!e || !e->write)
137+
return -1;
138+
139+
return e->write(file, buf, size, e->priv);
140+
}
141+
142+
void procfs_close(vfs_file_t* file) {
143+
(void)file;
144+
}
145+
146+
int procfs_ls(void) {
147+
for (int i = 0; i < proc_file_count; i++) {
148+
printfnoln(blue_color "%s " reset_color, proc_files[i]->name);
149+
}
150+
return 0;
151+
}

0 commit comments

Comments
 (0)