From 8d2cb24237fc5c200136c564e86c9cb7b0395ebd Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 7 Jul 2026 00:02:02 +0200 Subject: [PATCH] cpio: add initcpio loading Signed-off-by: Michael Shaw --- fs/vfs/Makefile | 1 + fs/vfs/initcpio.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++ include/fs/cpio.h | 7 +- 3 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 fs/vfs/initcpio.c diff --git a/fs/vfs/Makefile b/fs/vfs/Makefile index f399368..7da549d 100644 --- a/fs/vfs/Makefile +++ b/fs/vfs/Makefile @@ -2,3 +2,4 @@ obj-y += vfs.o obj-y += inode.o obj-y += ramfs.o +obj-y += initcpio.o diff --git a/fs/vfs/initcpio.c b/fs/vfs/initcpio.c new file mode 100644 index 0000000..ff84b3d --- /dev/null +++ b/fs/vfs/initcpio.c @@ -0,0 +1,183 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +#include +#include +#include + +#include +#include +#include + +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; + + if (strncmp(file + offset_footer, CPIO_TRAILER, footer_len)) + 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); + + if (!strncmp(cursor, "./", 2)) { + cursor += 2; + name_len -= 2; + } else if (!strcmp(cursor, ".")) { + /* is root */ + cursor += name_len; + cursor = file + ALIGN_UP(cursor - file, 4); + return cursor - file; + } + + memcpy(name, cursor, name_len); + cursor += name_len; + + cursor = file + ALIGN_UP(cursor - file, 4); + + 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 + ALIGN_UP(cursor - file, 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; +} diff --git a/include/fs/cpio.h b/include/fs/cpio.h index 9940205..ff329b6 100644 --- a/include/fs/cpio.h +++ b/include/fs/cpio.h @@ -2,11 +2,12 @@ #ifndef _CPIO_H_ #define _CPIO_H_ +#include +#include + #define CPIO_NEWC_MAGIC "070701" #define CPIO_TRAILER "TRAILER!!!" -#include - struct cpio_newc_header { char c_magic[6]; char c_ino[8]; @@ -24,4 +25,6 @@ struct cpio_newc_header { char c_check[8]; } PACKED; +int cpio_read_and_init(const char *file, size_t len, fs_t **out); + #endif /* _CPIO_H_ */