-
Notifications
You must be signed in to change notification settings - Fork 0
/
initrd.c
202 lines (179 loc) · 4.72 KB
/
initrd.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Initrd format:
* - uint32: version
* - uint32: checksum (xors with version to give initrd magic num)
* - Array of initrd_inode's, terminated by NULL entry
* - uint32_t: amount of initrd_disk_dir's to follow
* - Array of initrd_disk_dir's, that is, with num uint32_t's for each inode
* then num strings.
* - Raw data, sorted by inode
*/
#include "vfs.h"
#include "screen.h"
#include "string.h"
#include "system.h"
#include "malloc.h"
#include "initrd.h"
static const struct filesystem_ops initrd_fs_ops;
static const struct filesystem initrd_fs;
static unsigned initrd_read(struct vfs_inode *f, unsigned offset, unsigned size, char *buf)
{
struct initrd_superblock *sb = f->super->priv;
struct initrd_inode *i = &sb->inode_list[f->ino];
memcpy(buf, sb->contents+(i->offset+offset), size);
return size;
}
static unsigned initrd_open(struct superblock *s, uint32_t inode, struct vfs_inode *f)
{
struct initrd_superblock *sb = s->priv;
struct initrd_inode *di;
if (inode > sb->inode_num)
return 1;
di = &sb->inode_list[inode];
f->mask = di->mask;
assert(f->mask);
f->uid = di->uid;
f->gid = di->gid;
f->ino = di->ino;
f->length = di->length;
f->super = s;
return 0;
}
static struct initrd_online_dir *process_disk_dirs(struct initrd_disk_dir *disk_dirs, unsigned num, char **contents)
{
unsigned i;
struct initrd_online_dir *online_dirs = malloc(num*sizeof(struct initrd_online_dir));
if (!online_dirs)
return NULL;
for (i = 0; i < num; i++) {
char *s;
unsigned j;
struct initrd_subdir *sd = malloc(disk_dirs->num*sizeof(struct initrd_subdir));
if (!sd)
goto err;
online_dirs[i].ino = disk_dirs->ino;
online_dirs[i].num = disk_dirs->num;
online_dirs[i].subdirs = sd;
s = ((char *)disk_dirs->subdirs)+disk_dirs->num*sizeof(uint32_t);
for (j = 0; j < disk_dirs->num; j++) {
sd[j].ino = disk_dirs->subdirs[j];
sd[j].name = s;
s += strlen(s);
s = ALIGN(s, 4);
}
disk_dirs = (struct initrd_disk_dir *)s;
}
*contents = (char *)disk_dirs;
return online_dirs;
err:
for ( ; i; i--)
free(online_dirs[i].subdirs);
free(online_dirs);
return ERROR_PTR;
}
static struct initrd_online_dir *initrd_find_dir_entry(struct initrd_superblock *s, uint32_t ino)
{
unsigned mid, beg, end, i = s->dir_num;
beg = 0;
end = s->dir_num-1;
while (i) {
mid = beg+(end - beg)/2;
if (ino < s->dir_list[mid].ino)
end = mid;
else if (ino > s->dir_list[mid].ino)
beg = mid;
else if (ino == s->dir_list[mid].ino)
return &s->dir_list[mid];
i /= 2;
}
assert(0);
return NULL;
}
static int initrd_readdir(struct vfs_inode *f, unsigned index, struct vfs_dirent *d)
{
struct initrd_subdir *sdir;
struct initrd_online_dir *od;
struct initrd_superblock *sb = (struct initrd_superblock *)f->super->priv;
if (index == 0)
return vfs_filldir(".", f->ino, d);
od = initrd_find_dir_entry(sb, f->ino);
sdir = &od->subdirs[index-1];
if (index > od->num)
return -1;
return vfs_filldir(sdir->name, sdir->ino, d);
}
static struct vfs_inode *initrd_finddir(struct vfs_inode *f, char *name)
{
return vfs_finddir_wrapper(f, name);
}
static unsigned init_initrd_fs(struct superblock *s, char *disk, size_t length)
{
struct initrd_superblock *sb;
struct initrd_disk_dir *d;
struct initrd_inode *i;
uint32_t *header;
uint32_t version, checksum;
char *contents = NULL;
sb = malloc(sizeof(struct initrd_superblock));
if (!sb)
return 1;
if (length < 2*4) {
free(sb);
return 2;
}
length -= (2*4);
header = (uint32_t *)disk;
version = *header++;
checksum = *header++;
if ((version^checksum) != INITRD_MAGIC) {
free(sb);
puts("initrd: could not load disk: invalid format\n");
return 2;
}
if (version != SUPPORTED_VERSION) {
free(sb);
puts("initrd: could not load disk: wrong version\n");
return 2;
}
i = sb->inode_list = (struct initrd_inode *)header;
do {
if (length < sizeof(struct initrd_inode)) {
free(sb);
puts("initrd: could not load disk: wrong length (inode)\n");
return 2;
}
length -= sizeof(struct initrd_inode);
} while (i->mask && i++);
sb->inode_num = i-sb->inode_list;
header = (uint32_t *)++i;
sb->dir_num = *header++;
assert(sb->dir_num);
d = (struct initrd_disk_dir *)header;
sb->dir_list = process_disk_dirs(d, sb->dir_num, &contents);
if (!sb->dir_list) {
free(sb);
return 1;
}
sb->contents = contents;
s->fs = &initrd_fs;
s->priv = sb;
s->root = vfs_open_ino(s, 0, RDONLY);
return 0;
}
static const struct filesystem_ops initrd_fs_ops = {
.read = initrd_read,
.write = NULL,
.open = initrd_open,
.delinode = NULL,
.readdir = initrd_readdir,
.finddir = initrd_finddir,
};
static const struct filesystem initrd_fs = {
.name = "initrd",
.ops = &initrd_fs_ops,
.initfs = init_initrd_fs,
};
unsigned init_initrd(void)
{
return vfs_register_fs(&initrd_fs);
}