Skip to content

a #180

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: 311554005
Choose a base branch
from
Open

a #180

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
5 changes: 5 additions & 0 deletions LAB3/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"base.h": "c"
}
}
84 changes: 84 additions & 0 deletions LAB3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
CFLAG = -Iinclude -Wall -ffreestanding -fno-stack-protector -nostdlib -nostartfiles -mgeneral-regs-only
K8=kernel8
BL=bootloader
LINKER=linker.ld
START=start
CPIO:=archive/initramfs.cpio
DTB:=archive/bcm2710-rpi-3-b-plus.dtb
USER_DIR:=users/user
USER1:=user1
BUILD:=build/

BUILD_ELF:=$(shell find build/ -name '*.elf')
BUILD_IMG:=$(shell find build/ -name '*.img')
ARC_IMG:=$(shell find archive/ -name '*.img')

SRC_C:=$(shell find src/ -name '*.c')
SRC_S:=$(shell find src/ -name '*.s')

KER_C:=$(shell find kernel/ -name '*.c')
KER_S:=$(shell find kernel/ -name '*.s')

BL_C:=$(shell find bootloader/ -name '*.c')
BL_S:=$(shell find bootloader/ -name '*.s')

USER1_S:=$(shell find users/user1/ -name '*.s')
USER1_OBJS:= $(USER1_S:%.s=%.o)

OBJS:= $(SRC_C:%.c=%.o) \
$(SRC_S:%.s=%.o) \
rd.o


KERNEL_OBJS:=$(KER_C:%.c=%.o) \
$(KER_S:%.s=%.o)



BL_OBJS:=$(BL_C:%.c=%.o) \
$(BL_S:%.s=%.o) \
src/mini_uart.o src/utils_c.o src/utils_s.o src/memzero.o src/sprintf.o




all: $(BUILD)$(USER1).img $(BUILD)$(K8).img $(BUILD)$(BL).img



rd.o:$(CPIO)
aarch64-linux-gnu-ld -r -b binary -o rd.o $(CPIO)


%.o: %.s
aarch64-linux-gnu-gcc -g -o $@ -c $<

%.o: %.c
aarch64-linux-gnu-gcc -g -o $@ $(CFLAG) -c $<


$(BUILD)$(K8).img: $(OBJS) $(KERNEL_OBJS)
aarch64-linux-gnu-ld -T kernel/$(LINKER) -o $(BUILD)$(K8).elf $^
aarch64-linux-gnu-objcopy -O binary $(BUILD)$(K8).elf $@

$(BUILD)$(BL).img: $(BL_OBJS)
aarch64-linux-gnu-ld -T bootloader/$(LINKER) -o $(BUILD)$(BL).elf $^
aarch64-linux-gnu-objcopy -O binary $(BUILD)$(BL).elf $@

$(BUILD)$(USER1).img:$(USER1_OBJS)
aarch64-linux-gnu-ld -T $(USER_DIR)1/$(LINKER) -o $(BUILD)$(USER1).elf $<
aarch64-linux-gnu-objcopy -O binary $(BUILD)$(USER1).elf $@
cp $@ archive/rootfs/

$(CPIO):
cd archive/rootfs&&find . | cpio -o -H newc > ../initramfs.cpio

on:
screen /dev/cu.usbserial-0001 115200

clean:
$(RM) $(BUILD_ELF) $(CPIO) \
$(OBJS) $(KERNEL_OBJS) $(BL_OBJS) $(USER1_OBJS) \
$(BUILD_IMG) \
# $(BUILD_IMG) $(ARC_IMG)

1 change: 1 addition & 0 deletions LAB3/archive/rootfs/file1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing for File1
1 change: 1 addition & 0 deletions LAB3/archive/rootfs/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing for file2
1 change: 1 addition & 0 deletions LAB3/archive/rootfs/testfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaasq
Binary file added LAB3/archive/rootfs/user1.img
Binary file not shown.
Binary file added LAB3/bcm2710-rpi-3-b-plus.dtb
Binary file not shown.
28 changes: 28 additions & 0 deletions LAB3/bootloader/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ENTRY(_start)
SECTIONS
{
. = 0x60000;
_stext = .;
.text : {
*(.text.relo)
_bl_entry = .;
*(.text.boot)
*(.text)
*(.rodata)
}
. = ALIGN(0x1000);
_etext = .;

_sdata = .;
.data : { *(.data) }
. = ALIGN(0x1000);
_edata = .;


_sbss = .;
.bss : { *(.bss*) }
. = ALIGN(0x1000);
_ebss = .;

_blsize = _ebss - _stext;
}
48 changes: 48 additions & 0 deletions LAB3/bootloader/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "mini_uart.h"
#include "utils_c.h"
#include "utils_s.h"
#include <stddef.h>
#define BUFFER_MAX_SIZE 256u

extern char *_dtb;
void load_img()
{
char * kernel_addr = (char *)0x80000;
uart_send_string("Please sent the kernel image size:");
char buffer[BUFFER_MAX_SIZE];
// read_command(buffer);
size_t index = 0;
while (1)
{
buffer[index] = uart_recv();
uart_send(buffer[index]);
if (buffer[index] == '\n')
{
break;
}
index++;
}
buffer[index + 1] = '\0';
utils_newline2end(buffer);
uart_send('\r');
unsigned int img_size = utils_str2uint_dec(buffer);
uart_send_string("Start to load the kernel image... \n");

unsigned char *current = (unsigned char *)kernel_addr;
while (img_size--)
{
*current = uart_recv_raw();
current++;
uart_send('.');
}
uart_send_string("loading...\n");
// branchAddr(kernel_addr);
((void (*)(char *))kernel_addr)(_dtb);
}

void bootloader_main(void)
{
uart_init();
uart_send_string("In bootloader_main!\n");
load_img();
}
46 changes: 46 additions & 0 deletions LAB3/bootloader/start.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.section ".text.relo"
.globl _start

# need to relocate the bootloader from 0x80000 to 0x60000
_start:
adr x10, . //x10=0x80000
ldr x11, =_blsize
add x11, x11, x10
ldr x12, =_stext // x12=0x60000

moving_relo:
cmp x10, x11 //without bootloader
b.eq end_relo
ldr x13, [x10]
str x13, [x12] //move 0x80000 data to 0x60000
add x12, x12, #8
add x10, x10, #8
b moving_relo
end_relo:
ldr x14, =_bl_entry //jump to boot part
br x14


.section ".text.boot"
.globl _start_bl
ldr x20, =_dtb
str x0, [x20]
mrs x20, mpidr_el1
and x20, x20,#0xFF // Check processor id
cbz x20, master // Hang for all non-primary CPU

hang:
b hang

master:
adr x20, _sbss
adr x21, _ebss
sub x21, x21, x20
bl memzero

mov sp, #0x400000 // 4MB
bl bootloader_main

.global _dtb
.section .data
_dtb: .dc.a 0x0
3 changes: 3 additions & 0 deletions LAB3/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kernel=bootloader.img
arm_64bit=1
initramfs initramfs.cpio 0x2000000
34 changes: 34 additions & 0 deletions LAB3/include/_cpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __CPIO_H
#define __CPIO_H
// #define CPIO_ADDR (char *)0x8000000; // qemu
// #define CPIO_ADDR (char *)0x20000000; // raspi3

/*
cpio archive comprises a header record with basic numeric metadata followed by
the full pathname of the entry and the file data.
*/
typedef struct cpio_header
{
// uses 8-byte hexadecimal fields for all numbers
char c_magic[6]; //determine whether this archive is written with little-endian or big-endian integers.
char c_ino[8]; //determine when two entries refer to the same file.
char c_mode[8]; //specifies both the regular permissions and the file type.
char c_uid[8]; // numeric user id
char c_gid[8]; // numeric group id
char c_nlink[8]; // number of links to this file.
char c_mtime[8]; // Modification time of the file
char c_filesize[8]; // size of the file
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8]; // number of bytes in the pathname
char c_check[8]; // always set to zero by writers and ignored by readers.
}cpio_header;

extern char * cpio_addr;
void cpio_ls();
void cpio_cat(char *filename);
char * findFile(char *name);
void cpio_load_program(char *filename);
#endif
6 changes: 6 additions & 0 deletions LAB3/include/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _ALLOCATOR_H
#define _ALLOCATOR_H
#include <stddef.h>

void* malloc(size_t size);
#endif
35 changes: 35 additions & 0 deletions LAB3/include/dtb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _DTB_H
#define _DTB_H
#include <stdint.h>
#include <stddef.h>
/*
structure block: located at a 4-byte aligned offset from the beginning of the devicetree blob
token is a big-endian 32-bit integer, alligned on 32bit(padding 0)

*/

#define FDT_BEGIN_NODE 0x00000001
#define FDT_END_NODE 0x00000002
#define FDT_PROP 0x00000003
#define FDT_NOP 0x00000004
#define FDT_END 0x00000009

typedef struct fdt_header
{
uint32_t magic; // contain the value 0xd00dfeed (big-endian).
uint32_t totalsize; // in byte
uint32_t off_dt_struct; // the offset in bytes of the structure block from the beginning of the header
uint32_t off_dt_strings;
uint32_t off_mem_rsvmap;
uint32_t version;
uint32_t last_comp_version;
uint32_t boot_cpuid_phys;
uint32_t size_dt_strings; // the length in bytes of the strings block section
uint32_t size_dt_struct;
} fdt_header;

typedef void (*fdt_callback)(int type, const char *name, const void *data, uint32_t size);
void print_dtb(int type, const char *name, const void *data, uint32_t size) ;
void get_initramfs_addr(int type, const char *name, const void *data, uint32_t size) ;
int fdt_traverse(fdt_callback cb, void* dtb_ptr);
#endif
27 changes: 27 additions & 0 deletions LAB3/include/exception_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _EXCEPTION_C_H
#define _EXCEPTION_C_H

typedef void (*task_callback)(void *);

void enable_interrupt();
void disable_interrupt();
void default_handler();
void lower_sync_handler();
void curr_irq_handler();
void curr_sync_handler();

void task_init();
void add_task(task_callback cb,void *arg ,unsigned int priority);
void exec_task();

typedef struct task
{
unsigned long priority;
unsigned long duration;
void *arg;
task_callback callback;
char msg[20];
struct task *prev, *next;
} task;

#endif
23 changes: 23 additions & 0 deletions LAB3/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H


void delay(unsigned int clock);
void enable_uart_interrupt();
void disable_uart_interrupt();
void uart_init();
void uart_send_string(const char* str);
void uart_send_int(int num, int newline);
void uart_send_uint(unsigned int num,int newline);
void uart_send(const char c);
char uart_recv();
char uart_recv_raw();
void uart_hex(unsigned int d);
void uart_dec(unsigned int num);
void uart_handler(void *arg);
void test_uart_async();
char uart_async_recv();
void uart_async_send_string(char *str);
unsigned int uart_printf(char* fmt, ...);
typedef unsigned int Reg;
#endif
7 changes: 7 additions & 0 deletions LAB3/include/peripheral/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _PERIPHERAL_BASE_H
#define _PERIPHERAL_BASE_H

#define MMIO_BASE 0x3F000000
#define MAILBOX_BASE MMIO_BASE + 0xb880

#endif
12 changes: 12 additions & 0 deletions LAB3/include/peripheral/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _PERIPHERAL_GPIO_H
#define _PERIPHERAL_GPIO_H

#include "peripheral/base.h"

#define GPFSEL1 ((volatile unsigned int *)(MMIO_BASE+0x00200004))
#define GPSET0 ((volatile unsigned int *)(MMIO_BASE+0x0020001C))
#define GPCLR0 ((volatile unsigned int *)(MMIO_BASE+0x00200028))
#define GPPUD ((volatile unsigned int *)(MMIO_BASE+0x00200094))
#define GPPUDCLK0 ((volatile unsigned int *)(MMIO_BASE+0x00200098))

#endif
Loading