Skip to content

109550062 Lab6, 7, 8 #124

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 1 commit into
base: 109550062
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
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ INITRAMFS_SRC_DIR = $(SRC_DIR)/initramfs

MAKE = make

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only -ggdb
COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only -ggdb -std=gnu11
ASMOPS = -Iinclude -ggdb -mtune=cortex-a53


Expand All @@ -37,7 +37,7 @@ DEP_FILES += $(BOOTLOADER_OBJ_FILES:%.o=%.d)


.DEFAULT_GOAL=all
.PHONY: default all clean kernel bootloader debug send $(INITRAMFS_BUILD_DIR)
.PHONY: default all clean kernel bootloader debug send $(INITRAMFS_BUILD_DIR) lab5 lab6
default: all

all : kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)
Expand All @@ -56,7 +56,22 @@ run: kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)

boot: kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)
cd build/initramfs && find . | cpio -o -H newc > ../../initramfs.cpio
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -display none
# qemu-system-aarch64 -M raspi3b -kernel bootloader.img -initrd ./initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -serial null -serial pty -d int

lab5:
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display vnc=0.0.0.0:0 -initrd lab5.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -vga std -drive if=sd,file=sfn_nctuos.img,format=raw

lab6:
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display vnc=0.0.0.0:0 -initrd lab6.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -vga std -drive if=sd,file=sfn_nctuos.img,format=raw
# qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display gtk -initrd lab6.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -vga std

lab7:
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display vnc=0.0.0.0:0 -initrd lab7.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -vga std -drive if=sd,file=sfn_nctuos.img,format=raw


lab8:
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display vnc=0.0.0.0:0 -initrd lab8.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int -vga std -drive if=sd,file=sfn_nctuos.img,format=raw

copy:
make && sudo ./copy_kernel.sh ./bootloader.img && sudo ./copy_ramdisk.sh build/initramfs
Expand Down
84 changes: 84 additions & 0 deletions Makefile.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
ARMGNU ?= aarch64-linux-gnu

INITRAMFS_BUILD_DIR = $(BUILD_DIR)/initramfs
INITRAMFS_SRC_DIR = $(SRC_DIR)/initramfs

MAKE = make

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only -ggdb
ASMOPS = -Iinclude -ggdb -mtune=cortex-a53

SRC_DIR = src
BUILD_DIR = build
KERNEL_BUILD_DIR = $(BUILD_DIR)/kernel
KERNEL_SRC_DIR = $(SRC_DIR)/kernel

BOOTLOADER_BUILD_DIR = $(BUILD_DIR)/bootloader
BOOTLOADER_SRC_DIR = $(SRC_DIR)/bootloader


.PHONY: all clean kernel bootloader debug send $(INITRAMFS_BUILD_DIR)
all : kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)

kernel: kernel8.img

bootloader: bootloader.img

clean :
rm -rf $(KERNEL_BUILD_DIR) $(BOOTLOADER_BUILD_DIR) $(BUILD_DIR) $(INITRAMFS_BUILD_DIR) *.img

# run :
# make && qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -dtb bcm2710-rpi-3-b-plus.dtb -m 1G -smp 4 -serial null -serial stdio -kernel kernel8.img -display none
run: kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)
qemu-system-aarch64 -M raspi3b -serial null -serial stdio -kernel kernel8.img -display none

boot: kernel8.img bootloader.img $(INITRAMFS_BUILD_DIR)
cd build/initramfs && find . | cpio -o -H newc > ../../initramfs.cpio
qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -serial null -serial pty -kernel bootloader.img -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int

copy:
make && sudo ./copy_kernel.sh ./bootloader.img && sudo ./copy_ramdisk.sh build/initramfs

send:
make kernel && sudo python3 send_kernel.py $(SER_DEV)

$(KERNEL_BUILD_DIR)/%_s.o: $(KERNEL_SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -c -o $@ $<

$(BOOTLOADER_BUILD_DIR)/%_s.o: $(BOOTLOADER_SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -c -o $@ $<

$(KERNEL_BUILD_DIR)/%_c.o: $(KERNEL_SRC_DIR)/%.c
$(ARMGNU)-gcc $(COPS) -MMD -c -o $@ $<

$(BOOTLOADER_BUILD_DIR)/%_c.o: $(BOOTLOADER_SRC_DIR)/%.c
$(ARMGNU)-gcc $(COPS) -MMD -c -o $@ $<

KERNEL_C_FILES = $(wildcard $(KERNEL_SRC_DIR)/*.c)
KERNEL_ASM_FILES = $(wildcard $(KERNEL_SRC_DIR)/*.S)
KERNEL_OBJ_FILES = $(KERNEL_C_FILES:$(KERNEL_SRC_DIR)/%.c=$(KERNEL_BUILD_DIR)/%_c.o)
KERNEL_OBJ_FILES += $(KERNEL_ASM_FILES:$(KERNEL_SRC_DIR)/%.S=$(KERNEL_BUILD_DIR)/%_s.o)
BOOTLOADER_C_FILES += $(wildcard $(BOOTLOADER_SRC_DIR)/*.c)
BOOTLOADER_ASM_FILES += $(wildcard $(BOOTLOADER_SRC_DIR)/*.S)
BOOTLOADER_OBJ_FILES += $(BOOTLOADER_C_FILES:$(BOOTLOADER_SRC_DIR)/%.c=$(BOOTLOADER_BUILD_DIR)/%_c.o)
BOOTLOADER_OBJ_FILES += $(BOOTLOADER_ASM_FILES:$(BOOTLOADER_SRC_DIR)/%.S=$(BOOTLOADER_BUILD_DIR)/%_s.o)

DEP_FILES = $(KERNEL_OBJ_FILES:%.o=%.d)
DEP_FILES += $(BOOTLOADER_OBJ_FILES:%.o=%.d)
-include $(DEP_FILES)

kernel8.img: $(KERNEL_SRC_DIR)/linker.ld $(KERNEL_OBJ_FILES)
mkdir -p $(KERNEL_BUILD_DIR)
$(ARMGNU)-ld -T $(KERNEL_SRC_DIR)/linker.ld -o $(KERNEL_BUILD_DIR)/kernel8.elf $(KERNEL_OBJ_FILES)
$(ARMGNU)-objcopy $(KERNEL_BUILD_DIR)/kernel8.elf -O binary kernel8.img

bootloader.img: $(BOOTLOADER_SRC_DIR)/linker.ld $(BOOTLOADER_OBJ_FILES)
$(ARMGNU)-ld -T $(BOOTLOADER_SRC_DIR)/linker.ld -o $(BOOTLOADER_BUILD_DIR)/bootloader.elf $(BOOTLOADER_OBJ_FILES)
$(ARMGNU)-objcopy $(BOOTLOADER_BUILD_DIR)/bootloader.elf -O binary bootloader.img

$(INITRAMFS_BUILD_DIR):
$(MAKE) -C $(INITRAMFS_SRC_DIR)

$(shell mkdir -p $(KERNEL_BUILD_DIR))
$(shell mkdir -p $(BOOTLOADER_BUILD_DIR))
$(shell mkdir -p $(INITRAMFS_BUILD_DIR))
3 changes: 0 additions & 3 deletions config.txt

This file was deleted.

17 changes: 14 additions & 3 deletions include/cpio/cpio.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#include "type.h"

#ifndef __CPIO_H

#define __CPIO_H
#include "type.h"
#include "fs/vfs.h"


#define C_FMASK 0170000
#define C_SOCK 0140000 // File type value for sockets.
#define C_SYMBOL 0120000 // File type value for symbolic links.
#define C_REG 0100000 // File type value for regular files.
#define C_BLKDEV 0060000 // File type value for block special devices.
#define C_DIR 0040000 // File type value for directories.
#define C_CHDEV 0020000 // File type value for character special devices.
#define C_FIFO 0010000 // File type value for named pipes or FIFOs.

// char *_cpio_buf = (char *)INITRAMFS_ADDR;

Expand All @@ -29,5 +37,8 @@ void list_files();
unsigned int cat_file(const char *file);
void set_initramfs_addr(uint32_t addr);
unsigned int load_program(const char *file);
uint8_t get_file(const char *filename, char **content, unsigned int *c_filesize);
void mount_initramfs();
int init_cpio_fs(struct filesystem *fs);

#endif
2 changes: 1 addition & 1 deletion include/dtb/dtb.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "type.h"
#ifndef __DTB_H
#define __DTB_H

#include "type.h"
struct fdt_header {
uint32_t magic;
uint32_t totalsize;
Expand Down
12 changes: 12 additions & 0 deletions include/framebuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __FRAMEBUFFER_H
#define __FRAMEBUFFER_H

int init_framebuffer(struct filesystem* fs);
int framebuffer_setup_mount(struct filesystem* fs, struct mount *mnt);
int framebuffer_close(struct file *f);
int framebuffer_lseek64(struct file *f, int offset, int whence);
int framebuffer_open(struct vnode *file_node, struct file **target);
int framebuffer_write(struct file *f, void *buf, long len);
int framebuffer_ioctl(struct file *f, unsigned long request, void *buf);
void setup_dev_framebuffer();
#endif
103 changes: 103 additions & 0 deletions include/fs/fat32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef __FAT32_H
#define __FAT32_H
#include "sd.h"
#include "type.h"
#include "fs/vfs.h"
#define ATTR_READ_ONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME_ID 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20
#define ATTR_LONG_NAME 0xf /* should be examined first */

#define END_OF_CLUSTER 0x0ffffff8

#define FAT_DIR_CACHE_NUM 16

struct fat32_file_cache {
char name[16];
uint32_t filesize;
uint32_t *FAT;
uint32_t FAT_cnt;
// unsigned int root_index;
uint32_t dir_ent_clus_idx;
uint32_t dir_idx;
void *tmpdata;
uint32_t data_cluster_index;
uint32_t maxsize;
uint8_t dirty;
uint8_t cached;
uint8_t opening;
};

struct fat32_dir_cache {
char dir_name[16];
uint32_t *FAT;
uint32_t dir_clus_idx;
uint32_t FAT_cnt;
};

struct partition_entry {
uint8_t state;
uint8_t beg_head;
uint16_t beg_cly_sec;
uint8_t type;
uint8_t end_head;
uint16_t end_cly_sec;
uint32_t num_between;
uint32_t num_sec;
}__attribute__((__packed__));

struct fat_boot_sector {
uint8_t jmpcode[3];
uint8_t oemname[8];
uint16_t n_bytes_per_sec;
uint8_t n_sec_per_clus;
uint16_t n_reserve_sec;
uint8_t n_copy;
uint16_t n_root_dir_ent;
uint16_t tot_sec_not_used;
uint8_t media_desc;
uint16_t n_sec_per_fat_not_used;
uint16_t n_sec_per_track;
uint16_t n_head;
uint32_t n_hidden_sec;
uint32_t tot_sec;
uint32_t n_sec_per_fat;
uint16_t mirror_flags;
uint16_t fs_version;
uint32_t first_cluster_of_root;
uint16_t fs_info_reserve;
uint16_t backup_sec;
uint8_t reserve[12];
uint8_t driver_num;
uint8_t cur_head;
uint8_t extend_sig;
uint32_t serial_num;
uint8_t label[11];
uint8_t fs_type[8];
}__attribute__((__packed__));

struct fat32_dir_entry {
uint8_t filename[8];
uint8_t ext[3];
uint8_t attr;
uint8_t reserve[8];
uint16_t beg_clus_h;
uint16_t mod_time;
uint16_t mod_date;
uint16_t beg_clus_l;
uint32_t file_size;
}__attribute__((__packed__));
int fat32_init(struct filesystem *fs);
int fat32_setup_mount(struct filesystem *fs, struct mount *mnt);
int fat32_lookup(struct vnode* dir_node, struct vnode **target, const char *component);
int fat32_open(struct vnode* node, struct file **target);
int fat32_read(struct file *f, void *buf, long len);
int fat32_write(struct file *f, void *buf, long len);
int fat32_create(struct vnode *dir_node, struct vnode **target, const char *comp);
void fat32_sync();
int fat32_close(struct file *f);
void setup_boot();
#endif
33 changes: 33 additions & 0 deletions include/fs/tmpfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __TMPFS_H
#define __TMPFS_H

#include "vfs.h"

#define TMPFS_MAX_CONTENT_SIZE 4096

struct tmpfs_vinfo {
char name[16];
int namesize;
int filesize;
// make this pointer
// we may store the absolute address from initramfs
char *content;
};
int mount_tmpfs(struct filesystem *fs, struct mount *mnt);

int tmpfs_lookup(struct vnode* dir_node, struct vnode** target, const char * componenet_name);

int tmpfs_create(struct vnode* dir_node, struct vnode** target, const char *component_name);

int tmpfs_mkdir(struct vnode* dir_node, struct vnode** target, const char *componenet_name);
int tmpfs_write(struct file* file, const void *buf, size_t len);
int tmpfs_read(struct file* file, const void *buf, size_t len);
int tmpfs_open(struct vnode* file_node, struct file** target);
int tmpfs_close(struct file* file);
long tmpfs_lseek64(struct file* file, long offset, int whence);
void tmpfs_print_name(struct vnode* node);
void init_fs();
struct vnode* new_tmpfs_node(const char *name, struct vnode *dir_node);
int init_tmpfs(struct filesystem *fs);
int tmpfs_stat(struct file *f, struct fstat *stat);
#endif
Loading