mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 06:58:22 +00:00
kernel: urandom device
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#define DEV_MAJOR_KEYBOARD 6
|
#define DEV_MAJOR_KEYBOARD 6
|
||||||
#define DEV_MAJOR_BLOCK 7
|
#define DEV_MAJOR_BLOCK 7
|
||||||
#define DEV_MAJOR_E9 8
|
#define DEV_MAJOR_E9 8
|
||||||
|
#define DEV_MAJOR_URANDOM 9
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int (*open)(int minor, int flags);
|
int (*open)(int minor, int flags);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
|
#include <kernel/timekeeper.h>
|
||||||
|
|
||||||
static int null_write(int minor, void *buffer, size_t count, uintmax_t offset, int flags, size_t *wcount) {
|
static int null_write(int minor, void *buffer, size_t count, uintmax_t offset, int flags, size_t *wcount) {
|
||||||
*wcount = count;
|
*wcount = count;
|
||||||
@@ -23,6 +24,22 @@ static int null_read(int minor, void *buffer, size_t count, uintmax_t offset, in
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long current = 0xdeadbeefbadc0ffe;
|
||||||
|
|
||||||
|
static uint8_t getrand8() {
|
||||||
|
current += timekeeper_timefromboot().ns * timekeeper_timefromboot().s - timekeeper_time().ns * timekeeper_time().s + timekeeper_time().s;
|
||||||
|
return current & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int urandom_read(int minor, void *_buffer, size_t count, uintmax_t offset, int flags, size_t *rcount) {
|
||||||
|
uint8_t *buffer = _buffer;
|
||||||
|
for (int i = 0; i < count; ++i)
|
||||||
|
buffer[i] = getrand8();
|
||||||
|
|
||||||
|
*rcount = count;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static devops_t nullops = {
|
static devops_t nullops = {
|
||||||
.read = null_read,
|
.read = null_read,
|
||||||
.write = null_write
|
.write = null_write
|
||||||
@@ -38,8 +55,14 @@ static devops_t zeroops = {
|
|||||||
.write = null_write
|
.write = null_write
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static devops_t urandomops = {
|
||||||
|
.read = urandom_read,
|
||||||
|
.write = null_write
|
||||||
|
};
|
||||||
|
|
||||||
void pseudodevices_init() {
|
void pseudodevices_init() {
|
||||||
__assert(devfs_register(&nullops, "null", V_TYPE_CHDEV, DEV_MAJOR_NULL, 0, 0644) == 0);
|
__assert(devfs_register(&nullops, "null", V_TYPE_CHDEV, DEV_MAJOR_NULL, 0, 0644) == 0);
|
||||||
__assert(devfs_register(&fullops, "full", V_TYPE_CHDEV, DEV_MAJOR_FULL, 0, 0644) == 0);
|
__assert(devfs_register(&fullops, "full", V_TYPE_CHDEV, DEV_MAJOR_FULL, 0, 0644) == 0);
|
||||||
__assert(devfs_register(&zeroops, "zero", V_TYPE_CHDEV, DEV_MAJOR_ZERO, 0, 0644) == 0);
|
__assert(devfs_register(&zeroops, "zero", V_TYPE_CHDEV, DEV_MAJOR_ZERO, 0, 0644) == 0);
|
||||||
|
__assert(devfs_register(&urandomops, "urandom", V_TYPE_CHDEV, DEV_MAJOR_URANDOM, 0, 0644) == 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user