From 47bdfecedb09a7f2384820e94d2b443a20c3696e Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 19 Oct 2022 21:11:12 +0200 Subject: [PATCH] Devices: Add /dev/uptime This file contains how many milliseconds have passed since boot at the time of reading it :) --- kernel/include/fs/devices/Uptime.h | 9 +++++++++ kernel/src/fs/devices/DeviceFS.cpp | 2 ++ kernel/src/fs/devices/Uptime.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 kernel/include/fs/devices/Uptime.h create mode 100644 kernel/src/fs/devices/Uptime.cpp diff --git a/kernel/include/fs/devices/Uptime.h b/kernel/include/fs/devices/Uptime.h new file mode 100644 index 00000000..98e7baa9 --- /dev/null +++ b/kernel/include/fs/devices/Uptime.h @@ -0,0 +1,9 @@ +#pragma once +#include "fs/VFS.h" + +namespace UptimeDevice +{ + VFS::Node* create_new(const char* devname); + + ssize_t read(VFS::Node* node, size_t offset, size_t size, char* buffer); +} \ No newline at end of file diff --git a/kernel/src/fs/devices/DeviceFS.cpp b/kernel/src/fs/devices/DeviceFS.cpp index edb9a72a..80eb45fa 100644 --- a/kernel/src/fs/devices/DeviceFS.cpp +++ b/kernel/src/fs/devices/DeviceFS.cpp @@ -3,6 +3,7 @@ #include "fs/devices/Keyboard.h" #include "fs/devices/Random.h" #include "fs/devices/Serial.h" +#include "fs/devices/Uptime.h" #include "fs/devices/Version.h" #include "std/stdlib.h" #include "std/string.h" @@ -29,6 +30,7 @@ VFS::Node* DeviceFS::get() devfs_files[devfs_file_count++] = SerialDevice::create_new("serial"); devfs_files[devfs_file_count++] = RandomDevice::create_new("random"); devfs_files[devfs_file_count++] = KeyboardDevice::create_new("kbd"); + devfs_files[devfs_file_count++] = UptimeDevice::create_new("uptime"); return devfs_root; } diff --git a/kernel/src/fs/devices/Uptime.cpp b/kernel/src/fs/devices/Uptime.cpp new file mode 100644 index 00000000..a80cd09d --- /dev/null +++ b/kernel/src/fs/devices/Uptime.cpp @@ -0,0 +1,24 @@ +#include "fs/devices/Uptime.h" +#include "std/stdio.h" +#include "std/stdlib.h" +#include "std/string.h" +#include "thread/PIT.h" + +VFS::Node* UptimeDevice::create_new(const char* devname) +{ + VFS::Node* dev = new VFS::Node; + dev->read_func = UptimeDevice::read; + dev->inode = 0; + dev->length = 0; + dev->type = VFS_DEVICE; + dev->flags = 0; + strncpy(dev->name, devname, sizeof(dev->name)); + return dev; +} + +ssize_t UptimeDevice::read(VFS::Node* node, size_t, size_t size, char* buffer) +{ + if (!node) return -1; + snprintf(buffer, size + 1, "%ld", PIT::ms_since_boot); // FIXME: Support offseting this read + return (ssize_t)size; +} \ No newline at end of file