AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
22
static
void
*
rd_addr
=
NULL
;
23
24
/* the RAM disk's size, which the initramfs code reads too */
25
size_t
rd_size
= 0;
26
29
static
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
47
static
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
65
static
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 */
70
if
(!
__kboot
->
modules_count
)
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
82
kdev_blkdev_register
(1, &
rddev
);
83
84
return
0;
85
}
86
88
static
size_t
rd_getbs
(
struct
devid
*dd __unused)
89
{
90
return
1;
91
}
92
93
struct
dev
rddev
= {
94
.name =
"ramdisk"
,
95
.probe =
rd_probe
,
96
.bread =
rd_read
,
97
.bwrite =
rd_write
,
98
.getbs =
rd_getbs
,
99
};
100
101
MODULE_INIT
(rd,
rd_probe
,
NULL
)
__kboot
struct boot * __kboot
Definition
init.c:19
panic
#define panic(s)
Definition
panic.h:15
dev.h
boot.h
arch.h
panic.h
system.h
MIN
#define MIN(a, b)
Definition
system.h:11
kdev_blkdev_register
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
memcpy
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition
libc.c:11
module.h
MODULE_INIT
#define MODULE_INIT(name, i, f)
Definition
module.h:10
arch_phys_map
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
rddev
struct dev rddev
Definition
ramdisk.c:93
rd_addr
static void * rd_addr
Definition
ramdisk.c:22
rd_read
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
rd_probe
static int rd_probe()
Maps the RAM disk the boot loader loaded, and registers it as block device 1.
Definition
ramdisk.c:65
rd_write
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
rd_size
size_t rd_size
Definition
ramdisk.c:25
rd_getbs
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
NULL
#define NULL
Definition
stddef.h:27
string.h
boot
Definition
boot.h:35
boot::modules
module_t * modules
Definition
boot.h:41
boot::modules_count
int modules_count
Definition
boot.h:39
dev
A device driver: its operations, any of which may be NULL.
Definition
dev.h:28
devid
Which device: its kind (S_IFCHR or S_IFBLK) and numbers.
Definition
dev.h:21
module_t
Definition
boot.h:15
module_t::size
size_t size
Definition
boot.h:17
module_t::paddr
uintptr_t paddr
Definition
boot.h:16
ssize_t
long int ssize_t
Definition
types.h:12
off_t
long int off_t
Definition
types.h:10
vm.h
kernel
dev
rd
ramdisk.c
Generated by
1.18.0