Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 569f24aea1 | |||
| 8d2cb24237 | |||
| 94b7a9e342 | |||
| bfdc13af24 | |||
| f9a94f76dc | |||
| 841c00fad1 | |||
| 9ad4e0a419 | |||
| f24cd3ef74 | |||
| 260ed8a013 | |||
| 0d03ff47d4 |
12 changed files with 236 additions and 43 deletions
|
|
@ -2,3 +2,4 @@
|
|||
obj-y += vfs.o
|
||||
obj-y += inode.o
|
||||
obj-y += ramfs.o
|
||||
obj-y += initcpio.o
|
||||
|
|
|
|||
183
fs/vfs/initcpio.c
Normal file
183
fs/vfs/initcpio.c
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
/* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -79,7 +79,7 @@ int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out)
|
|||
|
||||
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
||||
{
|
||||
if (inode->mode != INODE_DIRECTORY)
|
||||
if ((inode->mode & S_IFMT) != S_IFDIR)
|
||||
return -ENOTDIR;
|
||||
|
||||
read_lock(&inode->rwlock);
|
||||
|
|
|
|||
12
fs/vfs/vfs.c
12
fs/vfs/vfs.c
|
|
@ -45,11 +45,11 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
return -EINVAL;
|
||||
|
||||
int res;
|
||||
char *build = kzalloc(256, ALLOC_KERN);
|
||||
char *build = kzalloc(FILE_NAME_MAX_LEN + 1, ALLOC_KERN);
|
||||
if (!build)
|
||||
return -ENOMEM;
|
||||
|
||||
for (int i = 0; i < 255; i++) {
|
||||
for (int i = 0; i < FILE_NAME_MAX_LEN; i++) {
|
||||
char c = path[i];
|
||||
|
||||
if (c == '/' || c == '\0') {
|
||||
|
|
@ -174,15 +174,15 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
|
||||
inode_t *ino = parent->inode;
|
||||
size_t len = strlen(name);
|
||||
if (len > 255)
|
||||
if (len > FILE_NAME_MAX_LEN)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY)
|
||||
if ((ino->mode & S_IFMT) != S_IFDIR)
|
||||
return -ENOTDIR;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
if ((ino->mode &S_IFMT) != S_IFDIR) {
|
||||
write_unlock(&ino->rwlock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
|
@ -231,7 +231,7 @@ int vfs_unlink(dirent_t *parent, const char *name)
|
|||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
if ((ino->mode & S_IFMT)!= S_IFDIR) {
|
||||
write_unlock(&ino->rwlock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
#ifndef _CPIO_H_
|
||||
#define _CPIO_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#define CPIO_NEWC_MAGIC "070701"
|
||||
#define CPIO_TRAILER "TRAILER!!!"
|
||||
|
||||
|
|
@ -20,6 +23,8 @@ struct cpio_newc_header {
|
|||
char c_rdevminor[8];
|
||||
char c_namesize[8];
|
||||
char c_check[8];
|
||||
};
|
||||
} PACKED;
|
||||
|
||||
int cpio_read_and_init(const char *file, size_t len, fs_t **out);
|
||||
|
||||
#endif /* _CPIO_H_ */
|
||||
|
|
|
|||
|
|
@ -7,28 +7,6 @@
|
|||
#include <lib/list.h>
|
||||
#include <lib/rwlock.h>
|
||||
|
||||
#define DIRENT_UNKNOWN_TYPE 0x0
|
||||
#define DIRENT_REGULAR_TYPE 0x1
|
||||
#define DIRENT_DIRECTORY_TYPE 0x2
|
||||
#define DIRENT_CHRDEV_TYPE 0x3
|
||||
#define DIRENT_BLKDEV_TYPE 0x4
|
||||
#define DIRENT_FIFO_TYPE 0x5
|
||||
#define DIRENT_SOCKET_TYPE 0x6
|
||||
#define DIRENT_SYMLINK_TYPE 0x7
|
||||
|
||||
#define DIRENT_MOUNT_POINT 0x8
|
||||
|
||||
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
|
||||
|
||||
#define INODE_UNKNOWN 0x0
|
||||
#define INODE_REGULAR 0x1
|
||||
#define INODE_DIRECTORY 0x2
|
||||
#define INODE_CHRDEV 0x3
|
||||
#define INODE_BLKDEV 0x4
|
||||
#define INODE_FIFO 0x5
|
||||
#define INODE_SOCKET 0x6
|
||||
#define INODE_SYMLINK 0x7
|
||||
|
||||
typedef struct _inode inode_t;
|
||||
typedef struct _dirent dirent_t;
|
||||
typedef struct _fs fs_t;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
#include <lib/rwlock.h>
|
||||
#include <lib/list.h>
|
||||
|
||||
#define FILE_NAME_MAX_LEN 255
|
||||
|
||||
#define S_IFMT 0xF000
|
||||
#define S_IFREG 0x8000
|
||||
#define S_IFDIR 0x4000
|
||||
|
|
@ -17,6 +19,27 @@
|
|||
#define S_IFIFO 0x1000
|
||||
#define S_IFSOCK 0xC000
|
||||
|
||||
#define S_ISUID 0004000 /* setuid */
|
||||
#define S_ISGID 0002000 /* setgid */
|
||||
#define S_ISVTX 0001000 /* sticky bit */
|
||||
|
||||
#define S_IRWXU 0000700 /* owner mask */
|
||||
#define S_IRUSR 0000400
|
||||
#define S_IWUSR 0000200
|
||||
#define S_IXUSR 0000100
|
||||
|
||||
#define S_IRWXG 0000070 /* group mask */
|
||||
#define S_IRGRP 0000040
|
||||
#define S_IWGRP 0000020
|
||||
#define S_IXGRP 0000010
|
||||
|
||||
#define S_IRWXO 0000007 /* other mask */
|
||||
#define S_IROTH 0000004
|
||||
#define S_IWOTH 0000002
|
||||
#define S_IXOTH 0000001
|
||||
|
||||
#define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
|
||||
|
||||
#define FT_MASK 0x07
|
||||
#define FT_UNKNOWN 0x00
|
||||
#define FT_REG_FILE 0x01
|
||||
|
|
@ -30,7 +53,7 @@
|
|||
#define FS_RDONLY 0x00000001
|
||||
|
||||
struct fs_driver {
|
||||
char name[256];
|
||||
char name[FILE_NAME_MAX_LEN + 1];
|
||||
int (*init)(int flags, inode_t *inode, fs_t **out);
|
||||
int (*deinit)(fs_t *fs);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@
|
|||
|
||||
#define ALIGN(_a) __attribute__((aligned(_a)))
|
||||
#define PHYSICAL_PAGE_MASK (~(hhdm_start))
|
||||
#define TO_VIRT(addr) ((addr) + hhdm_start)
|
||||
#define TO_PHYS(addr) ((addr) - hhdm_start)
|
||||
#define TO_VIRT(addr) ((void *)((paddr_t)(addr) + hhdm_start))
|
||||
#define TO_PHYS(addr) ((paddr_t)(addr) - hhdm_start)
|
||||
|
||||
#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((__typeof__(x))(a) - 1))
|
||||
|
||||
#define genrw(reg) \
|
||||
uint64_t reg##_read(); \
|
||||
|
|
@ -34,5 +36,4 @@ void *align(void *p, size_t align);
|
|||
extern vaddr_t hhdm_start;
|
||||
extern vaddr_t hhdm_end;
|
||||
|
||||
|
||||
#endif /* _MEM_H_ */
|
||||
|
|
|
|||
|
|
@ -30,4 +30,6 @@ typedef int64_t i64;
|
|||
typedef u64 off_t;
|
||||
typedef u64 user_t;
|
||||
|
||||
typedef i64 time_t;
|
||||
|
||||
#endif /* _TYPES_H_ */
|
||||
|
|
|
|||
10
lib/math.c
10
lib/math.c
|
|
@ -40,9 +40,9 @@ static int hexc_to_int(char c)
|
|||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
return c - 'F';
|
||||
return c - 'A' + 10;
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
return c - 'a';
|
||||
return c - 'a' + 10;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -52,13 +52,13 @@ u64 hex_to_int(char *ascii_hex, size_t n)
|
|||
u64 ret = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char c = ascii_hex[i];
|
||||
int i = hexc_to_int(c);
|
||||
int val = hexc_to_int(c);
|
||||
|
||||
if (i < 0)
|
||||
if (val < 0)
|
||||
break;
|
||||
|
||||
ret <<= 4;
|
||||
ret |= i;
|
||||
ret |= val;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ static void mem_init_zones()
|
|||
|
||||
if (usable) {
|
||||
struct mem_zone *zone;
|
||||
vaddr_t zone_vbase = TO_VIRT(ent->base);
|
||||
vaddr_t zone_vbase = (vaddr_t)TO_VIRT(ent->base);
|
||||
if (last_usable && last->virt_base + last->len == zone_vbase) {
|
||||
zone = &zones[n_zones - 1];
|
||||
zone->len += ent->length;
|
||||
|
|
|
|||
|
|
@ -114,21 +114,21 @@ static void map_hhdm_zones(pgd_t *pgd_table)
|
|||
device_base = hhdm_end;
|
||||
|
||||
for (paddr_t p = 0; p < top_paddr; p += 0x200000) {
|
||||
vaddr_t v = TO_VIRT(p);
|
||||
vaddr_t v =(vaddr_t) TO_VIRT(p);
|
||||
|
||||
pgd_t *pgd_entry = &pgd_table[pgd_index(v)];
|
||||
if (!(pgd_entry->pgd & PAGE_PRESENT)) {
|
||||
pgd_entry->pgd = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
|
||||
}
|
||||
|
||||
pud_t *pud_table = (pud_t *)TO_VIRT(pgd_entry->pgd & PAGE_MASK);
|
||||
pud_t *pud_table = TO_VIRT(pgd_entry->pgd & PAGE_MASK);
|
||||
pud_t *pud_entry = &pud_table[pud_index(v)];
|
||||
if (!(pud_entry->pud & PAGE_PRESENT)) {
|
||||
pud_entry->pud = TO_PHYS((u64)pt_alloc()) | PAGE_PRESENT | PAGE_RW;
|
||||
}
|
||||
|
||||
/* use PAGE_PS for 2MB pages */
|
||||
pmd_t *pmd_table = (pmd_t *)TO_VIRT(pud_entry->pud & PAGE_MASK);
|
||||
pmd_t *pmd_table = TO_VIRT(pud_entry->pud & PAGE_MASK);
|
||||
pmd_t *pmd_entry = &pmd_table[pmd_index(v)];
|
||||
|
||||
pmd_entry->pmd = p | PAGE_PRESENT | PAGE_RW | PAGE_PS;
|
||||
|
|
|
|||
Loading…
Reference in a new issue