Skip to content

310555008 Lab6 & Lab7 #125

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

Open
wants to merge 10 commits into
base: 310555008
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.img
obj/
img/
core

.vscode
initramfs.cpio
22 changes: 9 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ KERNEL_IMG := $(IMG_DIR)/kernel8.img
KERNEL_ELF := $(OBJ_DIR)/$(KERNEL_DIR)/kernel8.elf
BOOTLOADER_IMG := $(IMG_DIR)/bootloader.img
BOOTLOADER_ELF := $(OBJ_DIR)/$(BOOT_DIR)/bootloader.elf
IMG_NAME := sdcard.img

RPI3_DTB := $(IMG_DIR)/bcm2710-rpi-3-b-plus.dtb
INITRAMFS_CPIO := $(IMG_DIR)/initramfs.cpio
Expand Down Expand Up @@ -47,7 +48,6 @@ endif
# @$(ECHO) "lib object file is $(LIB_OBJ_FILE)"

all:$(KERNEL_IMG) $(BOOTLOADER_IMG)
cd rootfs && make

$(KERNEL_IMG): $(KERNEL_ELF)
$(CROSS_COMPILER)objcopy -O binary $^ $(KERNEL_IMG)
Expand Down Expand Up @@ -88,34 +88,30 @@ $(OBJ_DIR)/$(LIB_DIR)/%_c.o: $(SRC_DIR)/$(LIB_DIR)/%.c
$(INITRAMFS_CPIO): $(INITRAMFS_FILE)
cd rootfs; find . | cpio -o -H newc > ../$(INITRAMFS_CPIO)

$(IMG_NAME):
./createimg.sh $(IMG_NAME)

qemub: all $(INITRAMFS_CPIO) $(RPI3_DTB)
qemu-system-aarch64 -M raspi3 -kernel $(BOOTLOADER_IMG) -display none \
-dtb $(RPI3_DTB) \
-initrd $(INITRAMFS_CPIO) \
-serial null -serial pty
qemuk: all $(INITRAMFS_CPIO) $(RPI3_DTB)
qemu-system-aarch64 -M raspi3 -kernel $(KERNEL_IMG) -display none \
-dtb $(RPI3_DTB) \
-initrd $(INITRAMFS_CPIO) \
-serial null -serial stdio

qemutest: all $(INITRAMFS_CPIO) $(RPI3_DTB)
qemuk: all $(INITRAMFS_CPIO) $(RPI3_DTB) $(IMG_NAME)
qemu-system-aarch64 -M raspi3 -kernel $(KERNEL_IMG) -display none \
-dtb $(RPI3_DTB) \
-initrd test/initramfs.cpio \
-initrd $(INITRAMFS_CPIO) \
-drive if=sd,file=$(IMG_NAME),format=raw \
-serial null -serial stdio


qemutty: $(KERNEL_IMG)
qemu-system-aarch64 -M raspi3 -kernel $(KERNEL_IMG) -display none \
-serial null -serial pty

.PHONY: clean
clean:
cd rootfs && make clean
rm -f $(KERNEL_OBJ_FILE) $(KERNEL_ELF) $(BOOTLOADER_OBJ_FILE) $(BOOTLOADER_ELF) $(LIB_OBJ_FILE) $(INITRAMFS_CPIO)

.PHONY: clean-all
clean-all: clean
cd rootfs && make clean-all
rm -f $(KERNEL_IMG) $(BOOTLOADER_IMG)
rm -f $(KERNEL_IMG) $(BOOTLOADER_IMG) $(IMG_NAME)
40 changes: 40 additions & 0 deletions createimg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

IMG_NAME=sdcard.img

truncate -s 64M $IMG_NAME

(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo t # Change partition type
echo c # Master Boot Record primary partitions type:LBA
echo w # Write changes
) | sudo fdisk $IMG_NAME

LOOPBACK=`sudo losetup --partscan --show --find $IMG_NAME`

echo ${LOOPBACK} | grep --quiet "/dev/loop"

if [ $? = 1 ]
then
echo "[!] losetup failed!"
exit 1
fi

sudo mkfs.vfat -F 32 ${LOOPBACK}p1

mkdir -p mnt

sudo mount -t vfat ${LOOPBACK}p1 mnt

sudo cp -r img/* mnt

sudo umount mnt

sudo losetup -d ${LOOPBACK}
# dd if=./sdcard.img of=/dev/<your SD card device>
32 changes: 32 additions & 0 deletions inc/arm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef _ARM_H
#define _ARM_H

/* ==== PAR_EL1 related ==== */
#define PAR_FAILED(par) (par & 1)
#define PAR_PA(par) (par & 0x0000fffffffff000)

/* ==== ESR_EL1 related ==== */
#define EC_SVC_64 0x15
// instruction abort
#define EC_IA_LE 0x20
// data abort
#define EC_DA_LE 0x24

#define ISS_FSC(esr) (esr->iss & 0x3f)

// Translation faults
#define FSC_TF_L0 0b000100
#define FSC_TF_L1 0b000101
#define FSC_TF_L2 0b000110
#define FSC_TF_L3 0b000111

// write abort
#define ISS_WnR(esr) (esr->iss & 0x40)

typedef struct{
unsigned int iss:25,
il:1,
ec:6;
} esr_el1_t;

#endif
6 changes: 3 additions & 3 deletions inc/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ void _hello(void);
void _hwinfo(void);
void _reboot(void);
void _echo(char *shell_buf);
void _ls(uint64 _initramfs_addr);
void _cat(uint64 _initramfs_addr, char *filename);
void _ls(void);
void _cat(char *filename);
void _parsedtb(char *fdt_base);
void *_malloc(char *size);
void _exec(uint64 _initramfs_addr, char *filename);
void _exec(char *filename);
void _chmod_uart();
int _setTimeout(char *shell_buf);
void _thread_test();
Expand Down
1 change: 1 addition & 0 deletions inc/cpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ void cpio_cat(char *cpio, char *filename);
* Return the size of the file, return 0 if no such file.
*/
uint32 cpio_load_prog(char *cpio, const char *filename, char **output_data);
uint32 cpio_read_hex(char *p);

#endif
56 changes: 56 additions & 0 deletions inc/cpiofs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef _CPIOFS_H
#define _CPIOFS_H

#include <list.h>
#include <vfs.h>

#define CPIO_TYPE_MASK 0060000
#define CPIO_TYPE_DIR 0040000
#define CPIO_TYPE_FILE 0000000

#define CPIOFS_TYPE_UNDEFINE 0x0
#define CPIOFS_TYPE_FILE 0x1
#define CPIOFS_TYPE_DIR 0x2

struct cpiofs_file_t {
const char *data;
int size;
};

struct cpiofs_dir_t {
/* Link cpiofs_internal */
struct list_head list;
};

struct cpiofs_internal {
const char *name;
int type;
union {
struct cpiofs_file_t file;
struct cpiofs_dir_t dir;
};
struct vnode *node;
struct list_head list;
};

int cpiofs_mount(struct filesystem *fs, struct mount *mount);
int cpiofs_sync(struct filesystem *fs);
int cpiofs_lookup(struct vnode *dir_node, struct vnode **target,
const char *component_name);
int cpiofs_create(struct vnode *dir_node, struct vnode **target,
const char *component_name);
int cpiofs_mkdir(struct vnode *dir_node, struct vnode **target,
const char *component_name);
int cpiofs_isdir(struct vnode *dir_node);
int cpiofs_getname(struct vnode *dir_node, const char **name);
int cpiofs_getsize(struct vnode *dir_node);
int cpiofs_write(struct file *file, const void *buf, size_t len);
int cpiofs_read(struct file *file, void *buf, size_t len);
int cpiofs_open(struct vnode *file_node, struct file *target);
int cpiofs_close(struct file *file);
long cpiofs_lseek64(struct file *file, long offset, int whence);
int cpiofs_ioctl(struct file *file, uint64 request, va_list args);

struct filesystem *cpiofs_init(void);

#endif
4 changes: 2 additions & 2 deletions inc/dt17.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int initramfs_parse_fdt(int level, char *cur, char *dt_strings);
void initramfs_init(char *fdt_base);
void dtb_traverse(char *fdt_base);

uint64 _initramfs_addr;
uint64 _initramfs_end;
void *_initramfs_addr;
void *_initramfs_end;

#define FDT_MAGIC_NUM 0xd00dfeed
#define FDT_BEGIN_NODE 0x00000001
Expand Down
9 changes: 9 additions & 0 deletions inc/entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _ENTRY_H
#define _ENTRY_H

#include <type.h>
#include <trapframe.h>

void el0_sync_handler(trapframe *regs, uint32 syn);

#endif
2 changes: 1 addition & 1 deletion inc/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
void user_prog_start(void);

// pass the user stack pointer and process memory location (program loaded)
void sched_new_user_prog(char *cpio, char *file_name);
void sched_new_user_prog(char *file_name);

void exit_user_prog(void);

Expand Down
Loading