Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

309511054 lab6 & lab7 #174

Open
wants to merge 10 commits into
base: 309511054
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cpio:

user:
rm initramfs.cpio
wget https://oscapstone.github.io/_downloads/58c515e3041658a045033c8e56ecff4c/initramfs.cpio
wget https://oscapstone.github.io/_downloads/3cb3bdb8f851d1cf29ac6f4f5d585981/vfs1.img

run: all
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -display none -serial null -serial stdio -initrd ./initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb
Expand Down
22 changes: 10 additions & 12 deletions command.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "command.h"

#include "gpio.h"
#include "uart.h"
#include "mbox.h"
#include "cpio.h"
Expand All @@ -11,10 +12,10 @@
#include "timer.h"

#define PM_PASSWORD 0x5a000000
#define PM_RSTC 0x3F10001c
#define PM_WDOG 0x3F100024
#define PM_RSTC ((volatile unsigned long)(MMIO_BASE+0x0010001c))
#define PM_WDOG ((volatile unsigned long)(MMIO_BASE+0x00100024))

void set(long addr, unsigned int value) {
void set(unsigned long addr, unsigned int value) {
volatile unsigned int* point = (unsigned int*)addr;
*point = value;
}
Expand Down Expand Up @@ -60,7 +61,7 @@ void exec_cat() {
}

void exec_load() {
load_file();
load_cpio("initramfs/vfs1.img");
}

void exec_lsfdt() {
Expand Down Expand Up @@ -92,7 +93,7 @@ void exec_timeout(char *command_string) {
}

void exec_testmem() {
load_cpio("app.img");
test_malloc();
}

void exec_check(char *command_string) {
Expand Down Expand Up @@ -138,8 +139,7 @@ void mbox_board_revision() {
// send the message to the GPU and receive answer
if (mbox_call(MBOX_CH_PROP)) {
uart_puts("My board revision is: 0x");
uart_hex(mbox[5]);
uart_puts("\n");
printf("%x\n", mbox[5]);
} else {
uart_puts("Unable to query serial!\n");
}
Expand All @@ -161,11 +161,9 @@ void mbox_arm_memory() {
// send the message to the GPU and receive answer
if (mbox_call(MBOX_CH_PROP)) {
uart_puts("My ARM memory base address is: 0x");
uart_hex(mbox[5]);
uart_puts("\n");
printf("%x\n", mbox[5]);
uart_puts("My ARM memory size is: 0x");
uart_hex(mbox[6]);
uart_puts("\n");
printf("%x\n", mbox[6]);
} else {
uart_puts("Unable to query serial!\n");
}
Expand All @@ -185,7 +183,7 @@ void parse_command(char* command_string) {
else if (!strcmp(command_string, "cat"))
exec_cat();
else if (!strcmp(command_string, "load"))
load_file();
exec_load();
else if (!strcmp(command_string, "test"))
exec_testmem();
else if (!strcmp(command_string, "check"))
Expand Down
3 changes: 0 additions & 3 deletions command.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// void input_buffer_overflow_message(char[]);

void set(long addr, unsigned int value);
void reset(int tick);
void cancel_reset();
void exec_reboot();

void exec_help();
Expand Down
117 changes: 117 additions & 0 deletions cpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "exception.h"
#include "mem.h"
#include "thread.h"
#include "tmpfs.h"
#include "vfs.h"

//Cpio New ASCII Format https://www.freebsd.org/cgi/man.cgi?query=cpio&sektion=5
typedef struct{
Expand Down Expand Up @@ -259,4 +261,119 @@ void load_file(){
// load_cpio(addr,target);
load_cpio(target);
}
}

void build_file_tree(vnode* root) {
List node, dirname;
node.entry = root;
char *none = "";
dirname.entry = none;
List *top = &node;
top->next = NULLPTR;
List *top_name = &dirname;
top_name->next = NULLPTR;

cpio_t* addr = (cpio_t*)cpio_start;
if (strcmp((char*)(addr+1), "."))
printf("error no cpio\n");
else
while(1){
unsigned long nsize, fsize;
nsize=strtoi(addr->c_namesize);
fsize=strtoi(addr->c_filesize);

// total size of the fixed header plus pathname is a multiple of four
if((sizeof(cpio_t) + nsize) & 3)
nsize += 4 - ((sizeof(cpio_t) + nsize) & 3);
if(fsize & 3)
fsize += 4 - (fsize & 3);

// check filename and data
char* filename = (char*)(addr+1);
char* data = filename + nsize;

while (top_name->next != NULLPTR) {
if (!strncmp(top_name->entry, filename))
break;
else {
List *free_node = top;
List *free_name = top_name;
top = top->next;
top_name = top_name->next;
kfree(free_node);
kfree(free_name);
}
}
if(strcmp(filename,"TRAILER!!!") == 0)
break;

char *childname;
if (strlen(top_name->entry))
childname = filename +1 + strlen(top_name->entry);
else
childname = filename;

vnode *parent = (vnode*)top->entry;
vnode *child = vnode_create(parent, childname);
File_Info *child_info = (File_Info*)child->internal;

int c_mode = strtoi(addr->c_mode) >> 12;
if (c_mode == MODE_DIR) {
strcpy(child_info->name, childname);
child_info->mode = MODE_DIR;
child_info->size = 0;
child_info->data = (vnode**)kmalloc(TMPFS_DIR_LEN * 8);
for (int i=0; i<TMPFS_DIR_LEN; i++)
((vnode**)child_info->data)[i] = NULLPTR;

if (!strcmp(child_info->name, ".")) {
addr=(cpio_t*)(data+fsize);
continue;
}
else if(!strcmp(child_info->name, "initramfs")) {
struct mount *new_mount = kmalloc(sizeof(struct mount));
register_filesystem("initramfs");
struct filesystem *mount_fs = find_fs("initramfs");
mount_fs->setup_mount(mount_fs, new_mount);
vnode *mount_vnode = new_mount->root;

File_Info *mount_info = (File_Info*)mount_vnode->internal;
strcpy(mount_info->name, "initramfs");
new_mount->root->parent = parent;

File_Info* parent_info = (File_Info*)parent->internal;
vnode **childs = (vnode**)parent_info->data;
for (int i=0; i<TMPFS_DIR_LEN; i++) {
if (childs[i] == child) {
childs[i] = mount_vnode;
break;
}
}

child = mount_vnode;
}

List *new_node = kmalloc(sizeof(List));
List *new_dir = kmalloc(sizeof(List));
new_node->entry = child;
new_dir->entry = filename;
new_node->next = top;
new_dir->next = top_name;
top = new_node;
top_name = new_dir;

}
else if (c_mode == MODE_FILE) {
strcpy(child_info->name, childname);
child_info->mode = MODE_FILE;
child_info->size = fsize;
child_info->data = data;
}
else {
printf("unknown c_mode\n");
while (1) {}
}

addr=(cpio_t*)(data+fsize);
}
}
5 changes: 4 additions & 1 deletion cpio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "tmpfs.h"

void cat_file();
void list_file();
void load_file();
void load_cpio(char* target);
void jump_cpio(char* target);
void jump_cpio(char* target);
void build_file_tree(vnode* root);
23 changes: 13 additions & 10 deletions devicetree.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#define FDT_NOP 0x00000004
#define FDT_END 0x00000009

extern void *_fdt_ptr;

int fdt_indent = 0;
unsigned long fdt_addr = 0x8200000;

typedef struct {
unsigned int magic;
Expand All @@ -25,14 +24,18 @@ typedef struct {
unsigned int size_dt_struct;
} fdt_header;

void fdt_address() {
asm volatile("mov %0, x9 \n":"=r"(fdt_addr):);
fdt_addr += 0xFFFF000000000000;
}


unsigned int endiantoi(void* _endian) {
char* endian = _endian;
unsigned int tmp = 0;
tmp |= endian[0] << 24;
tmp |= endian[1] << 16;
tmp |= endian[2] << 8;
tmp |= endian[3];
for (int i=0; i<4; i++) {
tmp |= endian[i] << (3-i)*8;
}
return tmp;
}

Expand Down Expand Up @@ -78,15 +81,15 @@ void initramfs_callback(int type, char *name, void *data, unsigned int size) {

void cpio_callback(int type, char *name, void *data, unsigned int size) {
if (type == FDT_PROP && !strcmp(name, "linux,initrd-start")) {
cpio_start = (void *)(unsigned long)endiantoi(data);
cpio_start = (void *)((unsigned long)endiantoi(data) + 0xFFFF000000000000);
}
if (type == FDT_PROP && !strcmp(name, "linux,initrd-end")) {
cpio_end = (void *)(unsigned long)endiantoi(data);
cpio_end = (void *)((unsigned long)endiantoi(data) + 0xFFFF000000000000);
}
}

int fdt_parser(unsigned long _dt_struct, unsigned long _dt_strings, unsigned int totalsize, void (*callback)(int type, char *name, void *data, unsigned int size)) {
unsigned int end = _dt_struct + totalsize;
unsigned long end = _dt_struct + totalsize;

while(_dt_struct < end) {
unsigned int type = endiantoi((char*)_dt_struct);
Expand Down Expand Up @@ -132,7 +135,7 @@ int fdt_parser(unsigned long _dt_struct, unsigned long _dt_strings, unsigned int


int fdt_traverse(void (*callback)(int type, char *name, void *data, unsigned int size)) {
unsigned long addr = (unsigned long)_fdt_ptr;
unsigned long addr = fdt_addr;
fdt_header* ftd = (fdt_header*)addr;

if (endiantoi(&ftd->magic) != 0xd00dfeed)
Expand Down
1 change: 1 addition & 0 deletions devicetree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ void *cpio_start;
void *cpio_end;
void *fdt_start;
void *fdt_end;
void fdt_address();
void initramfs_callback(int type, char *name, void *data, unsigned int size);
void cpio_callback(int type, char *name, void *data, unsigned int size);
int fdt_parser(unsigned long _dt_struct, unsigned long off_dt_strings, unsigned int totalsize, void (*callback)(int type, char *name, void *data, unsigned int size));
Expand Down
Loading