AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ramdisk.c
Go to the documentation of this file.
1
11
12#include <core/system.h>
13#include <core/panic.h>
14#include <core/arch.h>
15#include <core/module.h>
16#include <core/string.h>
17
18#include <boot/boot.h>
19#include <dev/dev.h>
20#include <mm/vm.h>
21
22static void *rd_addr = NULL;
23
24/* the RAM disk's size, which the initramfs code reads too */
25size_t rd_size = 0;
26
29static ssize_t rd_read(struct devid *dd __unused, off_t offset, size_t size, void *buf)
30{
31 if (offset < 0)
32 return -EINVAL;
33
34 if ((size_t) offset >= rd_size)
35 return 0;
36
37 /* not past the end */
38 size = MIN(size, rd_size - (size_t) offset);
39
40 memcpy(buf, (char *) rd_addr + offset, size);
41
42 return size;
43}
44
47static ssize_t rd_write(struct devid *dd __unused, off_t offset, size_t size, void *buf)
48{
49 if (offset < 0)
50 return -EINVAL;
51
52 if ((size_t) offset >= rd_size)
53 return 0;
54
55 /* not past the end */
56 size = MIN(size, rd_size - (size_t) offset);
57
58 memcpy((char *) rd_addr + offset, buf, size);
59
60 return size;
61}
62
65static int rd_probe()
66{
67 extern struct boot *__kboot;
68
69 /* no module: the root is on a disk (root=), there's no RAM disk */
71 return 0;
72
73 module_t *rd_module = &__kboot->modules[0];
74
75 /* the first module, where the boot loader put it */
76 rd_size = rd_module->size;
77 rd_addr = arch_phys_map(rd_module->paddr, rd_size);
78
79 if (!rd_addr)
80 panic("ramdisk: no room to map it");
81
83
84 return 0;
85}
86
88static size_t rd_getbs(struct devid *dd __unused)
89{
90 return 1;
91}
92
93struct dev rddev = {
94 .name = "ramdisk",
95 .probe = rd_probe,
96 .bread = rd_read,
97 .bwrite = rd_write,
98 .getbs = rd_getbs,
99};
100
struct boot * __kboot
Definition init.c:19
#define panic(s)
Definition panic.h:15
#define MIN(a, b)
Definition system.h:11
void kdev_blkdev_register(devid_t major, struct dev *dev)
Registers a driver as the block device of a major number.
Definition kdev.c:605
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition libc.c:11
#define MODULE_INIT(name, i, f)
Definition module.h:10
void * arch_phys_map(uintptr_t paddr, size_t size)
Returns a kernel address for a range of physical memory: its address in the direct map if it's there,...
Definition pmap.c:497
struct dev rddev
Definition ramdisk.c:93
static void * rd_addr
Definition ramdisk.c:22
static ssize_t rd_read(struct devid *dd, off_t offset, size_t size, void *buf)
Reads from the RAM disk: up to size bytes at offset, fewer at its end.
Definition ramdisk.c:29
static int rd_probe()
Maps the RAM disk the boot loader loaded, and registers it as block device 1.
Definition ramdisk.c:65
static ssize_t rd_write(struct devid *dd, off_t offset, size_t size, void *buf)
Writes to the RAM disk: up to size bytes at offset, fewer at its end.
Definition ramdisk.c:47
size_t rd_size
Definition ramdisk.c:25
static size_t rd_getbs(struct devid *dd)
Returns the RAM disk's block size: 1, it is read byte by byte.
Definition ramdisk.c:88
#define NULL
Definition stddef.h:27
Definition boot.h:35
module_t * modules
Definition boot.h:41
int modules_count
Definition boot.h:39
A device driver: its operations, any of which may be NULL.
Definition dev.h:28
Which device: its kind (S_IFCHR or S_IFBLK) and numbers.
Definition dev.h:21
size_t size
Definition boot.h:17
uintptr_t paddr
Definition boot.h:16
long int ssize_t
Definition types.h:12
long int off_t
Definition types.h:10