MOS Source Code
Loading...
Searching...
No Matches
cpio.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
5#include "mos/mm/mm.hpp"
8
9#include <algorithm>
10#include <mos/allocator.hpp>
16#include <mos/misc/setup.hpp>
17#include <mos/mos_global.h>
18#include <mos/syslog/printk.hpp>
19#include <mos_stdlib.hpp>
20#include <mos_string.hpp>
21
22#define CPIO_MODE_FILE_TYPE 0170000 // This masks the file type bits.
23#define CPIO_MODE_SOCKET 0140000 // File type value for sockets.
24#define CPIO_MODE_SYMLINK 0120000 // File type value for symbolic links. For symbolic links, the link body is stored as file data.
25#define CPIO_MODE_FILE 0100000 // File type value for regular files.
26#define CPIO_MODE_BLOCKDEV 0060000 // File type value for block special devices.
27#define CPIO_MODE_DIR 0040000 // File type value for directories.
28#define CPIO_MODE_CHARDEV 0020000 // File type value for character special devices.
29#define CPIO_MODE_FIFO 0010000 // File type value for named pipes or FIFOs.
30#define CPIO_MODE_SUID 0004000 // SUID bit.
31#define CPIO_MODE_SGID 0002000 // SGID bit.
32#define CPIO_MODE_STICKY 0001000 // Sticky bit.
33
34typedef struct
35{
36 char magic[6];
37 char ino[8];
38 char mode[8];
39 char uid[8];
40 char gid[8];
41 char nlink[8];
42 char mtime[8];
43
44 char filesize[8];
45 char devmajor[8];
46 char devminor[8];
47 char rdevmajor[8];
48 char rdevminor[8];
49
50 char namesize[8];
51 char check[8];
53
54MOS_STATIC_ASSERT(sizeof(cpio_newc_header_t) == 110, "cpio_newc_header has wrong size");
55
64
67extern const file_ops_t cpio_file_ops;
69extern const superblock_ops_t cpio_sb_ops;
70extern const file_ops_t cpio_noop_file_ops = { 0 };
71
72static size_t initrd_read(void *buf, size_t size, size_t offset)
73{
74 if (unlikely(offset + size > platform_info->initrd_npages * MOS_PAGE_SIZE))
75 mos_panic("initrd_read: out of bounds");
76 memcpy(buf, (void *) (pfn_va(platform_info->initrd_pfn) + offset), size);
77 return size;
78}
79
81{
83 switch (modebits & CPIO_MODE_FILE_TYPE)
84 {
85 case CPIO_MODE_FILE: type = FILE_TYPE_REGULAR; break;
86 case CPIO_MODE_DIR: type = FILE_TYPE_DIRECTORY; break;
87 case CPIO_MODE_SYMLINK: type = FILE_TYPE_SYMLINK; break;
88 case CPIO_MODE_CHARDEV: type = FILE_TYPE_CHAR_DEVICE; break;
90 case CPIO_MODE_FIFO: type = FILE_TYPE_NAMED_PIPE; break;
91 case CPIO_MODE_SOCKET: type = FILE_TYPE_SOCKET; break;
92 default: mos_warn("invalid cpio file mode"); break;
93 }
94
95 return type;
96}
97
98static bool cpio_read_metadata(const char *target, cpio_newc_header_t *header, size_t *header_offset, size_t *name_offset, size_t *name_length, size_t *data_offset,
99 size_t *data_length)
100{
101 if (unlikely(strcmp(target, "TRAILER!!!") == 0))
102 mos_panic("what the heck are you doing?");
103
104 size_t offset = 0;
105
106 while (true)
107 {
108 initrd_read(header, sizeof(cpio_newc_header_t), offset);
109
110 if (strncmp(header->magic, "07070", 5) != 0 || (header->magic[5] != '1' && header->magic[5] != '2'))
111 {
112 mos_warn("invalid cpio header magic, possibly corrupt archive");
113 return false;
114 }
115
116 offset += sizeof(cpio_newc_header_t);
117
118 const size_t filename_len = strntoll(header->namesize, NULL, 16, sizeof(header->namesize) / sizeof(char));
119
120 char filename[filename_len + 1]; // +1 for null terminator
121 initrd_read(filename, filename_len, offset);
122 filename[filename_len] = '\0';
123
124 bool found = strncmp(filename, target, filename_len) == 0;
125
126 if (found)
127 {
128 *header_offset = offset - sizeof(cpio_newc_header_t);
129 *name_offset = offset;
130 *name_length = filename_len;
131 }
132
133 if (unlikely(strcmp(filename, "TRAILER!!!") == 0))
134 return false;
135
136 offset += filename_len;
137 offset = ((offset + 3) & ~0x03); // align to 4 bytes
138
139 size_t data_len = strntoll(header->filesize, NULL, 16, sizeof(header->filesize) / sizeof(char));
140 if (found)
141 {
142 *data_offset = offset;
143 *data_length = data_len;
144 }
145
146 offset += data_len;
147 offset = ((offset + 3) & ~0x03); // align to 4 bytes (again)
148
149 if (found)
150 return true;
151 }
152
154}
155
157{
158 return container_of(inode, cpio_inode_t, inode);
159}
160
161// ============================================================================================================
162
163static cpio_inode_t *cpio_inode_trycreate(const char *path, superblock_t *sb)
164{
165 cpio_newc_header_t header = { 0 };
166 size_t header_offset = 0;
167 size_t name_offset = 0, name_length = 0;
168 size_t data_offset = 0, data_length = 0;
169 const bool found = cpio_read_metadata(path, &header, &header_offset, &name_offset, &name_length, &data_offset, &data_length);
170 if (!found)
171 return NULL;
172
174 cpio_inode->header = header;
175 cpio_inode->header_offset = header_offset;
176 cpio_inode->name_offset = name_offset;
177 cpio_inode->name_length = name_length;
178 cpio_inode->data_offset = data_offset;
179
180 const u32 modebits = strntoll(cpio_inode->header.mode, NULL, 16, sizeof(cpio_inode->header.mode) / sizeof(char));
181 const u64 ino = strntoll(cpio_inode->header.ino, NULL, 16, sizeof(cpio_inode->header.ino) / sizeof(char));
182 const file_type_t file_type = cpio_modebits_to_filetype(modebits & CPIO_MODE_FILE_TYPE);
183
184 inode_t *const inode = &cpio_inode->inode;
185 inode_init(inode, sb, ino, file_type);
186
187 // 0000777 - The lower 9 bits specify read/write/execute permissions for world, group, and user following standard POSIX conventions.
188 inode->perm = modebits & PERM_MASK;
189 inode->size = data_length;
190 inode->uid = strntoll(cpio_inode->header.uid, NULL, 16, sizeof(cpio_inode->header.uid) / sizeof(char));
191 inode->gid = strntoll(cpio_inode->header.gid, NULL, 16, sizeof(cpio_inode->header.gid) / sizeof(char));
192 inode->sticky = modebits & CPIO_MODE_STICKY;
193 inode->suid = modebits & CPIO_MODE_SUID;
194 inode->sgid = modebits & CPIO_MODE_SGID;
195 inode->nlinks = strntoll(cpio_inode->header.nlink, NULL, 16, sizeof(cpio_inode->header.nlink) / sizeof(char));
198 inode->cache.ops = &cpio_icache_ops;
199
200 return cpio_inode;
201}
202
203// ============================================================================================================
204
205static PtrResult<dentry_t> cpio_mount(filesystem_t *fs, const char *dev_name, const char *mount_options)
206{
207 if (unlikely(mount_options) && strlen(mount_options) > 0)
208 mos_warn("cpio: mount options are not supported");
209
210 if (dev_name && strcmp(dev_name, "none") != 0)
211 pr_warn("cpio: mount: dev_name is not supported");
212
214 sb->ops = &cpio_sb_ops;
215
217 if (!i)
218 {
219 delete sb;
220 return -ENOENT; // not found
221 }
222
223 pr_dinfo2(cpio, "cpio header: %.6s", i->header.magic);
224 sb->fs = fs;
226 dentry_attach(sb->root, &i->inode);
227 sb->root->superblock = i->inode.superblock = sb;
228 return sb->root;
229}
230
231static bool cpio_i_lookup(inode_t *parent_dir, dentry_t *dentry)
232{
233 // keep prepending the path with the parent path, until we reach the root
234 char pathbuf[MOS_PATH_MAX_LENGTH] = { 0 };
235 dentry_path(dentry, parent_dir->superblock->root, pathbuf, sizeof(pathbuf));
236 const char *path = pathbuf + 1; // skip the first slash
237
238 cpio_inode_t *inode = cpio_inode_trycreate(path, parent_dir->superblock);
239 if (!inode)
240 return false; // not found
241
242 dentry_attach(dentry, &inode->inode);
243 return true;
244}
245
247{
248 dentry_t *d_parent = dentry_parent(*dentry);
249 if (d_parent == NULL)
250 d_parent = root_dentry;
251
252 MOS_ASSERT(d_parent->inode != NULL);
253 MOS_ASSERT(dentry->inode);
254
255 add_record(state, dentry->inode->ino, ".", FILE_TYPE_DIRECTORY);
256 add_record(state, d_parent->inode->ino, "..", FILE_TYPE_DIRECTORY);
257
258 cpio_inode_t *inode = CPIO_INODE(dentry->inode);
259
260 char path_prefix[inode->name_length + 1]; // +1 for null terminator
261 initrd_read(path_prefix, inode->name_length, inode->name_offset);
262 path_prefix[inode->name_length] = '\0';
263 size_t prefix_len = strlen(path_prefix); // +1 for the slash
264
265 if (strcmp(path_prefix, ".") == 0)
266 path_prefix[0] = '\0', prefix_len = 0; // root directory
267
268 // find all children of this directory, that starts with 'path' and doesn't have any more slashes
269 cpio_newc_header_t header;
270 size_t offset = 0;
271
272 while (true)
273 {
274 initrd_read(&header, sizeof(cpio_newc_header_t), offset);
275 offset += sizeof(cpio_newc_header_t);
276
277 if (strncmp(header.magic, "07070", 5) != 0 || (header.magic[5] != '1' && header.magic[5] != '2'))
278 mos_panic("invalid cpio header magic, possibly corrupt archive");
279
280 const size_t filename_len = strntoll(header.namesize, NULL, 16, sizeof(header.namesize) / sizeof(char));
281
282 char filename[filename_len + 1]; // +1 for null terminator
283 initrd_read(filename, filename_len, offset);
284 filename[filename_len] = '\0';
285
286 const bool found = strncmp(path_prefix, filename, prefix_len) == 0 // make sure the path starts with the parent path
287 && (prefix_len == 0 || filename[prefix_len] == '/') // make sure it's a child, not a sibling (/path/to vs /path/toooo)
288 && strchr(filename + prefix_len + 1, '/') == NULL; // make sure it's only one level deep
289
290 const bool is_TRAILER = strcmp(filename, "TRAILER!!!") == 0;
291 const bool is_root_dot = strcmp(filename, ".") == 0;
292
293 if (found && !is_TRAILER && !is_root_dot)
294 {
295 pr_dinfo2(cpio, "prefix '%s' filename '%s'", path_prefix, filename);
296
297 const u32 modebits = strntoll(header.mode, NULL, 16, sizeof(header.mode) / sizeof(char));
299 const s64 ino = strntoll(header.ino, NULL, 16, sizeof(header.ino) / sizeof(char));
300
301 const char *name = filename + prefix_len + (prefix_len == 0 ? 0 : 1); // +1 for the slash if it's not the root
302 const size_t name_len = filename_len - prefix_len - (prefix_len == 0 ? 0 : 1); // -1 for the slash if it's not the root
303
304 add_record(state, ino, mos::string(name, name_len), type);
305 }
306
307 if (unlikely(is_TRAILER))
308 break;
309
310 offset += filename_len;
311 offset = ALIGN_UP(offset, 4); // align to 4 bytes
312
313 const size_t data_len = strntoll(header.filesize, NULL, 16, sizeof(header.filesize) / sizeof(char));
314 offset += data_len;
315 offset = ALIGN_UP(offset, 4); // align to 4 bytes (again)
316 }
317}
318
319static size_t cpio_i_readlink(dentry_t *dentry, char *buffer, size_t buflen)
320{
321 cpio_inode_t *inode = CPIO_INODE(dentry->inode);
322 return initrd_read(buffer, std::min(buflen, inode->inode.size), inode->data_offset);
323}
324
325static bool cpio_sb_drop_inode(inode_t *inode)
326{
327 delete CPIO_INODE(inode);
328 return true;
329}
330
332 .drop_inode = cpio_sb_drop_inode,
333};
334
336 .iterate_dir = cpio_i_iterate_dir,
337 .lookup = cpio_i_lookup,
338};
339
341 .readlink = cpio_i_readlink,
342};
343
345 .read = vfs_generic_read,
346};
347
349{
350 inode_t *i = cache->owner;
351 cpio_inode_t *cpio_i = CPIO_INODE(i);
352
354 if (!page)
355 return -ENOMEM;
356
357 pmm_ref_one(page);
358
359 if ((size_t) pgoff * MOS_PAGE_SIZE >= i->size)
360 return page; // EOF, no need to read anything
361
362 const size_t bytes_to_read = std::min((size_t) MOS_PAGE_SIZE, i->size - pgoff * MOS_PAGE_SIZE);
363 const size_t read = initrd_read((char *) phyframe_va(page), bytes_to_read, cpio_i->data_offset + pgoff * MOS_PAGE_SIZE);
364 MOS_ASSERT(read == bytes_to_read);
365 return page;
366}
367
369 .fill_cache = cpio_fill_cache,
370};
371
372static FILESYSTEM_DEFINE(fs_cpiofs, "cpiofs", cpio_mount, NULL);
#define MOS_ASSERT(cond)
Definition assert.hpp:14
#define MOS_UNREACHABLE()
Definition assert.hpp:11
#define mos_warn(fmt,...)
Definition assert.hpp:23
#define MOS_PATH_MAX_LENGTH
Definition autoconf.h:27
#define MOS_PAGE_SIZE
Definition autoconf.h:6
#define CPIO_MODE_SOCKET
Definition cpio.cpp:23
static PtrResult< dentry_t > cpio_mount(filesystem_t *fs, const char *dev_name, const char *mount_options)
Definition cpio.cpp:205
#define CPIO_MODE_CHARDEV
Definition cpio.cpp:28
static file_type_t cpio_modebits_to_filetype(u32 modebits)
Definition cpio.cpp:80
static cpio_inode_t * cpio_inode_trycreate(const char *path, superblock_t *sb)
Definition cpio.cpp:163
#define CPIO_MODE_FILE_TYPE
Definition cpio.cpp:22
PtrResult< phyframe_t > cpio_fill_cache(inode_cache_t *cache, uint64_t pgoff)
Definition cpio.cpp:348
#define CPIO_MODE_FIFO
Definition cpio.cpp:29
#define CPIO_MODE_DIR
Definition cpio.cpp:27
static size_t cpio_i_readlink(dentry_t *dentry, char *buffer, size_t buflen)
Definition cpio.cpp:319
const file_ops_t cpio_noop_file_ops
#define CPIO_MODE_SYMLINK
Definition cpio.cpp:24
#define CPIO_MODE_SGID
Definition cpio.cpp:31
static bool cpio_sb_drop_inode(inode_t *inode)
Definition cpio.cpp:325
static void cpio_i_iterate_dir(dentry_t *dentry, vfs_listdir_state_t *state, dentry_iterator_op add_record)
Definition cpio.cpp:246
should_inline cpio_inode_t * CPIO_INODE(inode_t *inode)
Definition cpio.cpp:156
static bool cpio_read_metadata(const char *target, cpio_newc_header_t *header, size_t *header_offset, size_t *name_offset, size_t *name_length, size_t *data_offset, size_t *data_length)
Definition cpio.cpp:98
#define CPIO_MODE_STICKY
Definition cpio.cpp:32
static size_t initrd_read(void *buf, size_t size, size_t offset)
Definition cpio.cpp:72
const inode_ops_t cpio_file_inode_ops
Definition cpio.cpp:340
#define CPIO_MODE_SUID
Definition cpio.cpp:30
const file_ops_t cpio_file_ops
Definition cpio.cpp:344
const inode_cache_ops_t cpio_icache_ops
Definition cpio.cpp:368
const superblock_ops_t cpio_sb_ops
Definition cpio.cpp:331
static bool cpio_i_lookup(inode_t *parent_dir, dentry_t *dentry)
Definition cpio.cpp:231
const inode_ops_t cpio_dir_inode_ops
Definition cpio.cpp:335
#define CPIO_MODE_BLOCKDEV
Definition cpio.cpp:26
#define CPIO_MODE_FILE
Definition cpio.cpp:25
#define PERM_MASK
Definition fs_types.h:61
file_type_t
Definition fs_types.h:14
@ FILE_TYPE_UNKNOWN
Definition fs_types.h:22
@ FILE_TYPE_CHAR_DEVICE
Definition fs_types.h:18
@ FILE_TYPE_NAMED_PIPE
Definition fs_types.h:20
@ FILE_TYPE_REGULAR
Definition fs_types.h:15
@ FILE_TYPE_BLOCK_DEVICE
Definition fs_types.h:19
@ FILE_TYPE_SYMLINK
Definition fs_types.h:17
@ FILE_TYPE_DIRECTORY
Definition fs_types.h:16
@ FILE_TYPE_SOCKET
Definition fs_types.h:21
void dentry_attach(dentry_t *d, inode_t *inode)
Attach an inode to a dentry.
Definition dentry.cpp:306
ssize_t dentry_path(dentry_t *dentry, dentry_t *root, char *buf, size_t size)
Get the path of a dentry.
should_inline dentry_t * dentry_parent(const dentry_t &dentry)
Definition dentry.hpp:67
MOSAPI char * strchr(const char *s, int c)
MOSAPI s32 strncmp(const char *str1, const char *str2, size_t n)
MOSAPI s32 strcmp(const char *str1, const char *str2)
MOSAPI s64 strntoll(const char *str, char **endptr, int base, size_t n)
#define phyframe_va(frame)
Definition mm.hpp:80
phyframe_t * mm_get_free_page(void)
Definition mm.cpp:39
#define pfn_va(pfn)
Definition mm.hpp:77
#define pmm_ref_one(thing)
Definition pmm.hpp:159
dentry_t * root_dentry
Definition vfs.cpp:35
void inode_init(inode_t *inode, superblock_t *sb, u64 ino, file_type_t type)
Definition inode.cpp:45
#define should_inline
Definition mos_global.h:37
#define unlikely(x)
Definition mos_global.h:40
#define MOS_STATIC_ASSERT
Definition mos_global.h:14
#define ALIGN_UP(addr, size)
Definition mos_global.h:74
mos::basic_string< char, mos::default_allocator > string
Definition string.hpp:336
T * create(Args &&...args)
Definition allocator.hpp:10
#define uint64_t
#define mos_panic(fmt,...)
Definition panic.hpp:51
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_warn(fmt,...)
Definition printk.hpp:38
#define pr_dinfo2(feat, fmt,...)
Definition printk.hpp:27
mos_platform_info_t *const platform_info
size_t size
Definition slab.cpp:34
const char * name
Definition slab.cpp:35
size_t name_length
Definition cpio.cpp:60
cpio_newc_header_t header
Definition cpio.cpp:62
size_t data_offset
Definition cpio.cpp:61
size_t name_offset
Definition cpio.cpp:60
inode_t inode
Definition cpio.cpp:58
size_t header_offset
Definition cpio.cpp:59
char nlink[8]
Definition cpio.cpp:41
char rdevminor[8]
Definition cpio.cpp:48
char magic[6]
Definition cpio.cpp:36
char rdevmajor[8]
Definition cpio.cpp:47
char devminor[8]
Definition cpio.cpp:46
char namesize[8]
Definition cpio.cpp:50
char filesize[8]
Definition cpio.cpp:44
char devmajor[8]
Definition cpio.cpp:45
char mtime[8]
Definition cpio.cpp:42
char check[8]
Definition cpio.cpp:51
superblock_t * superblock
inode_t * inode
inode_t * owner
const inode_cache_ops_t * ops
superblock_t * superblock
uid_t uid
file_perm_t perm
const inode_ops_t * ops
bool sgid
size_t size
const file_ops_t * file_ops
bool suid
inode_cache_t cache
bool sticky
gid_t gid
ssize_t nlinks
dentry_t * root
const superblock_ops_t * ops
filesystem_t * fs
static char buffer[2048]
unsigned int u32
Definition types.h:17
signed long long int s64
Definition types.h:13
unsigned long long u64
Definition types.h:19
#define container_of(ptr, type, member)
Definition types.hpp:31
#define FILESYSTEM_DEFINE(var, fsname, mountfn, unmountfn)
Definition vfs_types.hpp:24
void dentry_iterator_op(vfs_listdir_state_t *state, u64 ino, mos::string_view name, file_type_t type)
Definition vfs_types.hpp:61
#define FILESYSTEM_AUTOREGISTER(fs)
Definition vfs_types.hpp:31
dentry_t * dentry_get_from_parent(superblock_t *sb, dentry_t *parent, mos::string_view name)
Create a new dentry with the given name and parent.
Definition vfs_utils.cpp:37
ssize_t vfs_generic_read(const file_t *file, void *buf, size_t size, off_t offset)
Definition vfs_utils.cpp:93