Skip to content

Commit

Permalink
add initramfs
Browse files Browse the repository at this point in the history
  • Loading branch information
olga24912 committed Apr 17, 2016
1 parent 82fb141 commit 53bcd5e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ASM := bootstrap.S videomem.S isr_wrapper.S threads_util.S
AOBJ:= $(ASM:%.S=.build/%.o)
ADEP:= $(ASM:%.S=.build/%.d)

SRC := main.c uart.c io.c interrupt.c timer.c memory_map.c buddy_allocator.c paging.c SLAB_allocator.c threads.c lock.c test_thread.c file_system.c
SRC := main.c uart.c io.c interrupt.c timer.c memory_map.c buddy_allocator.c paging.c SLAB_allocator.c threads.c lock.c test_thread.c file_system.c initramfs.c
COBJ := $(SRC:%.c=.build/%.o)
CDEP := $(SRC:%.c=.build/%.d)

Expand Down
11 changes: 11 additions & 0 deletions file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <sys/types.h>

// flags for function open folder
// For correct work need to ude exactly one of flags O_RDONLY O_WRONLY O_RDWR
#define O_RDONLY (1 << 0)
#define O_WRONLY (1 << 1)
#define O_RDWR (1 << 2)
Expand All @@ -10,24 +12,33 @@
#define O_EXCL (1 << 5)
#define O_TRUNC (1 << 6)

//used in readdir, opendir, closedir
typedef struct inode* DIR;

//open file
int open(const char* pathname, int flags);

//function for initialization file system
void init_file_system();

//print file system tree in some way
void print_file_system();

//read from file to buf n bytes, return count of read bytes
ssize_t read(int fildes, void* buf, size_t nbyte);

//write from buf to filr n bytes return count of write bytes
ssize_t write(int fildes, const void* buf, size_t nbyte);

//create dir
int mkdir(const char *path);

//return some info about subdirs, firstly need call opendir
struct inode* readdir(DIR* dirp);

DIR* opendir(const char * path);

//need call after work with dir
void closedir(DIR* dir);

int close(int fd);
86 changes: 86 additions & 0 deletions initramfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdbool.h>
#include <stdint.h>
#include "initramfs.h"
#include "SLAB_allocator.h"
#include "file_system.h"

void* current_addr;

uint32_t str_to_int(char* s, size_t len) {
uint32_t x = 0;
for (size_t i = 0; i < len; ++i) {
x = x * 16 ;
if (s[i] >= '0' && s[i] <= '9') {
x += s[i] - '0';
} else if (s[i] >= 'a' && s[i] <= 'z'){
x += s[i] - 'a' + 10;
} else {
x += s[i] - 'A' + 10;
}
}

return x;
}

int add_one_file() {
printf("start: %p; current: %p, end: %p\n", init_ref, current_addr, init_ref_end);
current_addr += 4 - ((uint64_t)current_addr)%4;
if ((uint64_t)current_addr % 4 == 0) current_addr -= 4;

if (current_addr >= init_ref_end) {
return 0;
}

printf("ok\n");
struct cpio_header* header = (struct cpio_header*)current_addr;
printf("ok\n");
current_addr += sizeof(struct cpio_header);

printf("current: %p\n", current_addr);

printf("%c\n", *((char*)header));
printf("%s\n", header->namesize);

uint32_t name_len = str_to_int(header->namesize, 8);

printf("name_len: %d\n", name_len);

char* name = malloc_small(name_len + 1);
for (uint32_t i = 0; i < name_len; ++i) {
name[i] = ((char*)current_addr)[i];
}

name[name_len] = 0;

printf("%s\n", name);

if (name_len == 10 && strncmp(name, END_OF_ARCHIVE, name_len)) {
return 0;
}

current_addr += name_len;

uint32_t mode = str_to_int(header->mode, 8);

if (S_ISDIR(mode)) {
mkdir(name);
} else if (S_ISREG(mode)) {
int file = open(name, O_WRONLY|O_CREAT|O_EXCL);

uint32_t filesize = str_to_int(header->filesize, 8);

current_addr += 4 - ((uint64_t)current_addr)%4;
if ((uint64_t)current_addr % 4 == 0) current_addr -= 4;

write(file, current_addr, filesize);

current_addr += filesize;
}
return 1;
}

void initramfs_to_fs() {
printf("START add to initranfs\n");
current_addr = init_ref;
while (add_one_file());
}
3 changes: 3 additions & 0 deletions initramfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ struct cpio_header {
} __attribute__((packed));

extern void* init_ref;
extern void* init_ref_end;

void initramfs_to_fs();

#endif /*__INITRAMFS_H__*/
10 changes: 5 additions & 5 deletions inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <stdint.h>

struct inode {
char* name;
int capacity_level;
uint64_t size;
char* name; //name of file
int capacity_level; // log(size of path)
uint64_t size; // size of file
struct inode* neighbor;
struct inode* child;
void* file_start;
uint8_t is_dir;
void* file_start; // start of file
uint8_t is_dir; // true if it is directory and false if folder
};
8 changes: 5 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "lock.h"
#include "test_thread.h"
#include "file_system.h"

#include "initramfs.h"

void main(void) {
start_critical_section();
Expand All @@ -25,7 +25,9 @@ void main(void) {

init_file_system();

int id = open("/file", O_CREAT|O_WRONLY|O_TRUNC);
initramfs_to_fs();

/*int id = open("/file", O_CREAT|O_WRONLY|O_TRUNC);
open("/file2", O_CREAT|O_WRONLY|O_TRUNC);
open("/file3", O_WRONLY|O_TRUNC);
Expand All @@ -51,7 +53,7 @@ void main(void) {
printf("%c", s[i]);
}
}
printf("\n");
printf("\n");*/

print_file_system();

Expand Down
7 changes: 3 additions & 4 deletions memory_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern char text_phys_begin[];
extern char bss_phys_end[];

void* init_ref;
void* init_ref_end;

size_t memory_map_size = 0;
struct memory_map_entry memory_map[MMAP_SIZE];
Expand Down Expand Up @@ -76,6 +77,7 @@ void find_initranfs_mode(uint32_t count, uint32_t* addr, uint32_t* start, uint32
*start = (uint32_t)(uint64_t) mod_start;
*end = (uint32_t)(uint64_t) mod_end;
init_ref = mod_start;
init_ref_end = mod_end;
return;
}
}
Expand Down Expand Up @@ -152,7 +154,4 @@ void print_mempry_map() {
(unsigned long long int) (memory_map[i].base_addr + memory_map[i].length - 1),
memory_map[i].type == 1 ? "Available" : memory_map[i].type == 0 ? "Kernel" : "Reserved");
}
}



}
18 changes: 18 additions & 0 deletions readme
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,21 @@ test_timer_interrupt "--- а это первый тест, который не

test_slab "--- ему тоже нужно прерывание таймера, проверяет, что я ничего не
сломала в выделение памяти, когда делала выделение потокобезоопасным.

-------------------------ЧЕТВЕРТОЕ ЗАДАНИЕ-----------------------------
К первой части относятся файлы file_system.c, file_system.h, inode.h

В inode.h описывается один узел файловой системы.

file_system.c/h реализована работа непосредственно с файловой системой. Подробнее
в комментариях в коде в h файлах.

Для второй части задания произошли изменения в memory_map.c в функции get_memory_map().
Теперь там же резервируется место для initramfs, если надо. И так же, там происходит
поиск расположения initramfs в функции find_initranfs_mode

Далее парсинг файла и добаление в файловую систему происходт в initramfs.c
Функция initramfs_to_fs().

void* init_ref; --- начало initramfs
void* init_ref_end; --- конец initramfs

0 comments on commit 53bcd5e

Please sign in to comment.