Files
serenity/Kernel/Devices/Generic/RandomDevice.cpp
Liav A. 16244c490a Kernel: Allocate all device major numbers within one known header file
We used to allocate major numbers quite randomly, with no common place
to look them up if needed.
This commit is changing that by placing all major number allocations
under a new C++ namespace, in the API/MajorNumberAllocation.h file.

We also add the foundations of what is needed before we can publish this
information (allocated numbers for block and char devices) to userspace.
2024-07-06 21:42:32 +02:00

50 lines
1.3 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/MajorNumberAllocation.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/Generic/RandomDevice.h>
#include <Kernel/Sections.h>
#include <Kernel/Security/Random.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullLockRefPtr<RandomDevice> RandomDevice::must_create()
{
auto random_device_or_error = DeviceManagement::try_create_device<RandomDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!random_device_or_error.is_error());
return random_device_or_error.release_value();
}
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
: CharacterDevice(MajorAllocation::CharacterDeviceFamily::Generic, 8)
{
}
UNMAP_AFTER_INIT RandomDevice::~RandomDevice() = default;
bool RandomDevice::can_read(OpenFileDescription const&, u64) const
{
return true;
}
ErrorOr<size_t> RandomDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
return buffer.write_buffered<256>(size, [&](Bytes bytes) {
get_good_random_bytes(bytes);
return bytes.size();
});
}
ErrorOr<size_t> RandomDevice::write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t size)
{
// FIXME: Use input for entropy? I guess that could be a neat feature?
return size;
}
}