Skip to content

Commit ea4ad6e

Browse files
committed
main: cleanup kern_main demo routines
This commit will remove the file system currently done in kern_main. Instead, the contents of the INITRD:/SYSTEM/MOTD.TXT file will be placed in the screen if the ramdisk was successfully mounted into the INITRD: volume when the system starts.
1 parent 0352142 commit ea4ad6e

File tree

5 files changed

+43
-161
lines changed

5 files changed

+43
-161
lines changed

kernel/kern/kern_main.c

Lines changed: 25 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: GPL-3.0-only
55
*/
66

7-
#include <fs/tarfs/tar.h>
87
#include <machine/multiboot.h>
98
#include <sys/device.h>
109
#include <sys/stdkern.h>
@@ -21,13 +20,6 @@
2120
* do something useful).
2221
*/
2322

24-
// FIXME: These should not be declared here.
25-
extern void null_install(void);
26-
extern void vgatext_install(void);
27-
extern void keyboard_install(void);
28-
extern void pctimer_install(void);
29-
extern void uart8250_install(void);
30-
3123
static void ramdisk_init(void);
3224
static void kernel_welcome(void);
3325

@@ -50,12 +42,20 @@ kernel_main(void)
5042
static void
5143
ramdisk_init(void)
5244
{
45+
unsigned int i;
5346
multiboot_module_t *multiboot_mods;
54-
unsigned char *tar_buffer;
47+
unsigned char *tar;
5548

5649
multiboot_mods = (multiboot_module_t *) multiboot_info->mods_addr;
57-
tar_buffer = (unsigned char *) multiboot_mods[0].mod_start;
58-
vfs_mount("tarfs", "INITRD", tar_buffer);
50+
for (i = 0; i < multiboot_info->mods_count; i++) {
51+
/* We can afford to strcmp because "ramdisk" is static. */
52+
if (!strcmp("ramdisk", (char *) multiboot_mods[0].string)) {
53+
/* We found the ramdisk. */
54+
tar = (unsigned char *) multiboot_mods[i].mod_start;
55+
vfs_mount("tarfs", "INITRD", tar);
56+
return;
57+
}
58+
}
5959
}
6060

6161
static vfs_node_t *
@@ -76,160 +76,26 @@ fs_write_string(vfs_node_t *node, unsigned int offt, const char *str)
7676
return fs_write(node, offt, (unsigned char *) str, strlen(str));
7777
}
7878

79-
static void dump_path(vfs_node_t *stdout, char *path);
80-
static void dump_dir(vfs_node_t *stdout, char *path, vfs_node_t *dir);
81-
82-
static void
83-
dump_dir(vfs_node_t *stdout, char *path, vfs_node_t *dir)
84-
{
85-
unsigned int i = 0;
86-
vfs_node_t *node;
87-
size_t child_len;
88-
char *child_path;
89-
90-
while ((node = fs_readdir(dir, i++))) {
91-
child_len = strlen(path) + strlen(node->vn_name) + 2;
92-
child_path = (char *) malloc(child_len);
93-
if (child_path) {
94-
strcpy(child_path, path);
95-
/* If the folder I'm in is not root. */
96-
if (node->vn_parent->vn_parent) {
97-
strcat(child_path, "/");
98-
}
99-
strcat(child_path, node->vn_name);
100-
dump_path(stdout, child_path);
101-
free(child_path);
102-
}
103-
}
104-
}
105-
106-
static void
107-
dump_file(vfs_node_t *stdout, vfs_node_t *node)
108-
{
109-
unsigned int read = 0;
110-
char buf[128];
111-
unsigned int offset = 0;
112-
113-
fs_write_string(stdout, 0, "\n");
114-
while ((read = fs_read(node, offset, buf, 128)) > 0) {
115-
fs_write(stdout, 0, buf, read);
116-
offset += read;
117-
}
118-
fs_write_string(stdout, 0, "\n");
119-
}
120-
121-
static void
122-
dump_path(vfs_node_t *stdout, char *path)
123-
{
124-
vfs_node_t *node = fs_resolve(path);
125-
126-
if (node) {
127-
switch (node->vn_flags) {
128-
case VN_FREGFILE:
129-
fs_write_string(stdout, 0, "[FIL]");
130-
break;
131-
case VN_FDIR:
132-
fs_write_string(stdout, 0, "[DIR]");
133-
break;
134-
case VN_FCHARDEV:
135-
fs_write_string(stdout, 0, "[CHD]");
136-
break;
137-
case VN_FBLOCKDEV:
138-
fs_write_string(stdout, 0, "[BLD]");
139-
break;
140-
}
141-
fs_write_string(stdout, 0, " ");
142-
fs_write_string(stdout, 0, path);
143-
fs_write_string(stdout, 0, " (");
144-
fs_write_string(stdout, 0, node->vn_name);
145-
fs_write_string(stdout, 0, ")\n");
146-
if (node->vn_flags == VN_FDIR) {
147-
dump_dir(stdout, path, node);
148-
} else if (node->vn_flags == VN_FREGFILE) {
149-
dump_file(stdout, node);
150-
}
151-
}
152-
}
153-
15479
static void
155-
dump_mountpoint(vfs_node_t *stdout, char *name)
156-
{
157-
char *path = malloc(strlen(name) + 3);
158-
strcpy(path, name);
159-
strcat(path, ":/");
160-
dump_path(stdout, path);
161-
fs_write_string(stdout, 0, "\n");
162-
}
163-
164-
static void
165-
dump_mountpoints(vfs_node_t *stdout)
80+
kernel_welcome(void)
16681
{
167-
vfs_node_t *rootfs, *mountpoint;
168-
unsigned int i = 0;
169-
170-
rootfs = fs_resolve("ROOT:/");
171-
if (rootfs) {
172-
while ((mountpoint = fs_readdir(rootfs, i++))) {
173-
/* Make sure ROOT is not dumped, infinite loop! */
174-
if (strcmp(mountpoint->vn_name, "ROOT")) {
175-
dump_mountpoint(stdout, mountpoint->vn_name);
176-
}
177-
}
178-
}
179-
}
82+
vfs_node_t *vtcon, *motd;
83+
int read, offt;
84+
char buffer[64];
18085

181-
static void
182-
print_mountpoints(vfs_node_t *stdout)
183-
{
184-
vfs_node_t *rootfs, *mountpoint;
185-
unsigned int i = 0;
86+
vtcon = fs_resolve_and_open("DEV:/vtcon", VO_FWRITE);
18687

187-
rootfs = fs_resolve("ROOT:/");
188-
if (rootfs) {
189-
fs_write_string(stdout, 0, "Mounted file systems:\n");
190-
while ((mountpoint = fs_readdir(rootfs, i++))) {
191-
fs_write_string(stdout, 0, mountpoint->vn_name);
192-
fs_write_string(stdout, 0, "\n");
88+
motd = fs_resolve_and_open("INITRD:/SYSTEM/MOTD.TXT", VO_FREAD);
89+
if (motd) {
90+
offt = 0;
91+
while ((read = fs_read(motd, offt, buffer, 64)) > 0) {
92+
fs_write(vtcon, 0, buffer, read);
93+
offt += read;
19394
}
194-
fs_write_string(stdout, 0, "\n");
195-
} else {
196-
fs_write_string(stdout, 0, "Error! No rootfs is present\n\n");
95+
fs_write_string(vtcon, 0, "\n");
96+
fs_close(motd);
19797
}
198-
}
199-
200-
static unsigned int
201-
octal2int(char *octstring)
202-
{
203-
unsigned int acc = 0;
204-
while (*octstring) {
205-
acc = acc * 8 + (*octstring - '0');
206-
octstring++;
207-
}
208-
return acc;
209-
}
210-
211-
static void
212-
kernel_welcome(void)
213-
{
214-
vfs_node_t *vtcon, *kbd, *uart;
215-
vtcon = fs_resolve_and_open("DEV:/vtcon", VO_FWRITE);
216-
uart = fs_resolve_and_open("DEV:/uart", VO_FWRITE | VO_FREAD);
217-
218-
print_mountpoints(vtcon);
219-
dump_mountpoints(vtcon);
220-
221-
fs_write_string(vtcon, 0, "This is the NativeOS VTCON\n");
222-
fs_write_string(vtcon, 0, "Press any key to test the keyboard :)\n\n");
223-
224-
fs_write_string(uart, 0, "This is the NativeOS UART\n");
225-
fs_write_string(uart, 0, "Type some characters to get echo:\n\n");
226-
227-
unsigned char buf[16];
228-
unsigned int read;
22998
for (;;) {
230-
fs_read(vtcon, 0, buf, 16);
231-
232-
read = fs_read(uart, 0, buf, 16);
233-
fs_write(uart, 0, buf, read);
99+
fs_read(vtcon, 0, buffer, 64);
234100
}
235101
}
File renamed without changes.

ramdisk/SYSTEM/MOTD.TXT

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Welcome to NativeOS Preview.
2+
3+
Thank you for your interest in testing NativeOS. NativeOS is a toy
4+
OS for the i386 system architecture to help me learn more about how
5+
computers work.
6+
7+
This is a work in progress. Expect bugs. Please, keep an eye on the
8+
repository and the issue tracker for future work and bug fixes.
9+
10+
* Repository: https://git.danirod.es/nativeos.git
11+
* Issue tracker: https://redmine.danirod.es/projects/nativeos
12+
* GitHub Mirror: https://github.com/danirod/nativeos
13+
14+
15+
This is the virtual console test.
16+
Press keys to type them into the screen.
17+
Press keys F1 to F8 to switch context.

ramdisk/hello.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tools/cdrom/boot/grub/grub.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ set timeout=0
22

33
menuentry 'NativeOS' {
44
multiboot /boot/nativeos.exe
5-
module /ramdisk.tar
5+
module /ramdisk.tar ramdisk
66
boot
77
}

0 commit comments

Comments
 (0)