vbos/fs/vfs/initcpio.c
Michael Shaw acf469ff71 vfs: initcpio: read initramfs.cpio file into vfs
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-09 21:18:57 -04:00

177 lines
3.7 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/mem.h>
#include <kernel/slab.h>
#include <fs/vfs.h>
#include <fs/cpio.h>
#include <fs/ramfs.h>
#include <lib/string.h>
#include <lib/rbtree.h>
#include <lib/errno.h>
static bool cpio_verify_header_footer(const char *file, size_t len)
{
size_t magic_len = strlen(CPIO_NEWC_MAGIC);
size_t footer_len = strlen(CPIO_TRAILER);
if (len < sizeof(struct cpio_newc_header))
return false;
struct cpio_newc_header *header = (void *)file;
if (strncmp(header->c_magic, CPIO_NEWC_MAGIC, magic_len))
return false;
size_t offset_footer = len - footer_len;
/* empty archive */
if (offset_footer == sizeof(struct cpio_newc_header))
return false;
return true;
}
static ssize_t read_next(fs_t *fs, const char *file, rbtree_t *list, dirent_t **out)
{
size_t footer_len = strlen(CPIO_TRAILER);
if (!strncmp(file, CPIO_TRAILER, footer_len))
return 0;
char name[FILE_NAME_MAX_LEN + 1] = {0};
int res = 0;
struct cpio_newc_header *h = (void *)file;
size_t file_len = hex_to_int(h->c_filesize, 8);
size_t name_len = hex_to_int(h->c_namesize, 8);
size_t mode = hex_to_int(h->c_mode, 8);
u64 ino = hex_to_int(h->c_ino, 8);
u64 nlink = hex_to_int(h->c_nlink, 8);
if (name_len - 1 > FILE_NAME_MAX_LEN)
return -ENAMETOOLONG;
const char *cursor = file + sizeof(struct cpio_newc_header);
size_t data_offset = sizeof(struct cpio_newc_header) + ALIGN_UP(name_len, 4);
if (!strncmp(cursor, "./", 2)) {
cursor += 2;
name_len -= 2;
} else if (!strcmp(cursor, ".")) {
/* is root */
return data_offset;
}
memcpy(name, cursor, name_len);
cursor = file + data_offset;
char *parent_path = dirname(name);
char *leaf = basename(name);
dirent_t *parent;
bool parent_is_root = !strcmp(parent_path, ".") || !strcmp(parent_path, "");
if (parent_is_root)
parent = fs->root_dirent;
else
res = vfs_open(parent_path, &parent);
if (res < 0)
goto out_strings;
/* hardlink detection */
bool is_hardlink = false;
inode_t *existing_ino = NULL;
if (nlink > 1 && file_len == 0) {
rbnode_t *node = rbt_search(list, ino);
if (node) {
is_hardlink = true;
existing_ino = (inode_t *)node->ptr;
}
}
if (is_hardlink) {
inode_inc_ref(existing_ino);
dirent_t *new_dent;
write_lock(&parent->inode->rwlock);
res = parent->inode->i_ops.link(parent->inode, existing_ino, leaf, &new_dent);
write_unlock(&parent->inode->rwlock);
if (res == 0) {
new_dent->fs = parent->fs;
*out = new_dent;
} else {
inode_dec_ref(existing_ino);
}
} else {
res = vfs_creat(parent, leaf, mode, 0, out);
if (res == 0 && nlink > 1 && file_len == 0) {
rbnode_t *node = rbt_insert(list, ino);
if (node)
node->ptr = (*out)->inode;
}
}
if (res < 0)
goto out_tidy;
/* write file data for regular files */
if (!(mode & S_IFDIR) && file_len > 0 && !is_hardlink)
vfs_write((*out)->inode, (void *)cursor, file_len, 0);
/* advance past file data and align to next entry */
cursor += file_len;
cursor = file + data_offset + ALIGN_UP(file_len, 4);
res = cursor - file;
out_tidy:
if (!parent_is_root)
dirent_dec_ref(parent);
out_strings:
kfree(parent_path);
kfree(leaf);
return res;
}
int cpio_read_and_init(const char *file, size_t len, fs_t **out)
{
if (!cpio_verify_header_footer(file, len))
return -EINVAL;
int res;
fs_t *initramfs;
res = ramfs_create(0, &initramfs);
if (res < 0)
return res;
res = vfs_mount(NULL, initramfs);
if (res < 0)
return res;
rbtree_t inode_map = {0};
size_t offset = 0;
while (1) {
dirent_t *dent = NULL;
ssize_t n = read_next(initramfs, file + offset, &inode_map, &dent);
if (n < 0) {
res = n;
goto out;
}
if (n == 0)
break;
offset += n;
}
*out = initramfs;
out:
rbt_destroy(&inode_map);
return res;
}