1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/**
* @file initrd.c
* Filesystem module for handling the initrd
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <kernel/heap.h>
#include <kernel/vfs.h>
typedef struct {
char *address;
uint32_t size;
} initrd_info;
extern uint8_t _binary_initrd_img_start[];
extern uint8_t _binary_initrd_img_size[];
static const uint8_t *initrd_start = (uint8_t *)_binary_initrd_img_start;
static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size;
void *initrd_open(const char *file);
uint32_t initrd_read(vfs_file_info *info, uint32_t count, uint8_t *buffer);
int initrd_close(vfs_file_info *info);
int initrd_seek(vfs_file_info *info, uint32_t offset, int whence);
char *initrd_getfile(uint32_t offset);
static const vfs_volume_funcs initrd_funcs = {
initrd_open,
initrd_close,
initrd_read,
0, // write
0, // readdir
initrd_seek
};
int initrd_strncmp(const char *a, const char *b, unsigned int n)
{
for (unsigned int i = 0; i < n; i++) {
if (a[i] != b[i])
return 1;
}
return 0;
}
void initrd_init(void)
{
vfs_mount(&initrd_funcs, VFS_READONLY);
}
void *initrd_open(const char *file)
{
char *ptr;
for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr);
if (!initrd_strncmp(file, ptr + 4, len)) {
initrd_info *file = (initrd_info *)malloc(
sizeof(initrd_info));
file->address = ptr + len + 8;
file->size = *(uint32_t *)(ptr + len + 4);
return file;
}
}
return 0;
}
int initrd_close(vfs_file_info *info)
{
// Nothing to do
free(info->fsinfo);
return 0;
}
uint32_t initrd_read(vfs_file_info *info, uint32_t count, uint8_t *buffer)
{
initrd_info *iinfo = (initrd_info *)info->fsinfo;
if (iinfo == 0 || iinfo->address == 0)
return 0;
uint32_t i;
for (i = 0; i < count; i++) {
if (info->pos >= iinfo->size)
break;
buffer[info->pos] = iinfo->address[info->pos];
info->pos++;
}
return i;
}
int initrd_seek(vfs_file_info *info, uint32_t offset, int whence)
{
initrd_info *iinfo = (initrd_info *)info->fsinfo;
if (iinfo == 0 || iinfo->address == 0)
return 0;
if (whence == SEEK_SET)
info->pos = offset;
else if (whence == SEEK_CUR)
info->pos += offset;
else if (whence == SEEK_END)
info->pos = iinfo->size + offset;
else
return -1;
return 0;
}
char *initrd_getfile(uint32_t offset)
{
char *ptr = (char *)initrd_start;
for (uint32_t i = 0; i < offset; i++) {
uint32_t len = *((uint32_t *)ptr);
uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
ptr += len + datalen + 8;
if (ptr >= (char *)(initrd_start + initrd_size))
return 0;
}
return ptr;
}
|