Kernel: Implement basic random device to /dev/random
This commit is contained in:
parent
969563c06a
commit
23194d1f53
|
@ -20,6 +20,7 @@ set(KERNEL_SOURCES
|
|||
kernel/Device/Device.cpp
|
||||
kernel/Device/FramebufferDevice.cpp
|
||||
kernel/Device/NullDevice.cpp
|
||||
kernel/Device/RandomDevice.cpp
|
||||
kernel/Device/ZeroDevice.cpp
|
||||
kernel/Errors.cpp
|
||||
kernel/FS/DevFS/FileSystem.cpp
|
||||
|
|
|
@ -12,6 +12,7 @@ namespace Kernel
|
|||
PTSMaster,
|
||||
Null,
|
||||
Zero,
|
||||
Random,
|
||||
Debug,
|
||||
Keyboard,
|
||||
Mouse,
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
#include <kernel/Device/Device.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class RandomDevice : public CharacterDevice
|
||||
{
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::RefPtr<RandomDevice>> create(mode_t, uid_t, gid_t);
|
||||
|
||||
virtual dev_t rdev() const override { return m_rdev; }
|
||||
|
||||
virtual BAN::StringView name() const override { return "random"_sv; }
|
||||
|
||||
protected:
|
||||
RandomDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
|
||||
: CharacterDevice(mode, uid, gid)
|
||||
, m_rdev(rdev)
|
||||
{ }
|
||||
|
||||
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
|
||||
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override { return buffer.size(); };
|
||||
|
||||
virtual bool can_read_impl() const override { return true; }
|
||||
virtual bool can_write_impl() const override { return false; }
|
||||
virtual bool has_error_impl() const override { return false; }
|
||||
|
||||
private:
|
||||
const dev_t m_rdev;
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include <kernel/Device/DeviceNumbers.h>
|
||||
#include <kernel/Device/RandomDevice.h>
|
||||
#include <kernel/Random.h>
|
||||
|
||||
#include <sys/sysmacros.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
BAN::ErrorOr<BAN::RefPtr<RandomDevice>> RandomDevice::create(mode_t mode, uid_t uid, gid_t gid)
|
||||
{
|
||||
static uint32_t minor = 0;
|
||||
auto* result = new RandomDevice(mode, uid, gid, makedev(DeviceNumber::Random, minor++));
|
||||
if (result == nullptr)
|
||||
return BAN::Error::from_errno(ENOMEM);
|
||||
return BAN::RefPtr<RandomDevice>::adopt(result);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<size_t> RandomDevice::read_impl(off_t, BAN::ByteSpan buffer)
|
||||
{
|
||||
size_t nwritten = 0;
|
||||
while (nwritten < buffer.size())
|
||||
{
|
||||
const uint64_t random = Random::get_u64();
|
||||
const size_t to_copy = BAN::Math::min(buffer.size() - nwritten, sizeof(random));
|
||||
memcpy(buffer.data() + nwritten, &random, to_copy);
|
||||
nwritten += to_copy;
|
||||
}
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
#include <kernel/Device/DebugDevice.h>
|
||||
#include <kernel/Device/FramebufferDevice.h>
|
||||
#include <kernel/Device/NullDevice.h>
|
||||
#include <kernel/Device/RandomDevice.h>
|
||||
#include <kernel/Device/ZeroDevice.h>
|
||||
#include <kernel/FS/DevFS/FileSystem.h>
|
||||
#include <kernel/FS/TmpFS/Inode.h>
|
||||
|
@ -26,6 +27,7 @@ namespace Kernel
|
|||
MUST(s_instance->TmpFileSystem::initialize(0755, 0, 0));
|
||||
s_instance->add_device(MUST(DebugDevice::create(0666, 0, 0)));
|
||||
s_instance->add_device(MUST(NullDevice::create(0666, 0, 0)));
|
||||
s_instance->add_device(MUST(RandomDevice::create(0666, 0, 0)));
|
||||
s_instance->add_device(MUST(ZeroDevice::create(0666, 0, 0)));
|
||||
s_instance->add_device(MUST(KeyboardDevice::create(0440, 0, 901)));
|
||||
s_instance->add_device(MUST(MouseDevice::create(0440, 0, 901)));
|
||||
|
|
Loading…
Reference in New Issue