forked from GrassLab/osc2021
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync lab5 to lab7 // Don't need VA in this lab
- Loading branch information
1 parent
972e6ad
commit d6548b2
Showing
81 changed files
with
4,886 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# osc2022 | ||
## Author | ||
|
||
| 學號 | GitHub 帳號 | 姓名 | Email | | ||
| --- | ----------- | --- | --- | | ||
|`309554042`| `JusticeLeee` | `李政毅` | [email protected] | | ||
|
||
## How to build | ||
|
||
```bash | ||
./make.sh | ||
``` | ||
## How to run | ||
|
||
```bash | ||
make run | ||
``` | ||
## How to burn it into pi3 | ||
```bash | ||
./rus.sh # write kernel to rpi3 through uart | ||
sudo screen /dev/ttyUSB0 115200 | ||
``` | ||
## Note | ||
|
||
copy some string.h source for use strcmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# | ||
# Copyright (C) 2018 bzt (bztsrc@github) | ||
# | ||
# Permission is hereby granted, free of charge, to any person | ||
# obtaining a copy of this software and associated documentation | ||
# files (the "Software"), to deal in the Software without | ||
# restriction, including without limitation the rights to use, copy, | ||
# modify, merge, publish, distribute, sublicense, and/or sell copies | ||
# of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
# DEALINGS IN THE SOFTWARE. | ||
# | ||
# | ||
|
||
CFILE = $(wildcard *.c) | ||
ASMFILE = $(wildcard *.S) | ||
OBJS = $(CFILE:.c=_c.o) | ||
OBJS += $(ASMFILE:.S=_s.o) | ||
CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -fno-stack-protector -Iinclude -Iinclude | ||
|
||
all: kernel8.img | ||
|
||
%_s.o: %.S | ||
aarch64-elf-gcc $(CFLAGS) -c $< -o $@ | ||
|
||
%_c.o: %.c | ||
aarch64-elf-gcc $(CFLAGS) -c $< -o $@ | ||
|
||
kernel8.img: $(OBJS) | ||
aarch64-elf-ld -nostdlib -nostartfiles $(OBJS) -T link.ld -o bootloader.elf | ||
aarch64-elf-objcopy -O binary bootloader.elf bootloader.img | ||
|
||
clean: | ||
rm kernel8.elf *.o >/dev/null 2>/dev/null || true | ||
|
||
run: | ||
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef _MINI_UART_H | ||
#define _MINI_UART_H | ||
|
||
void uart_init(void); | ||
char uart_getc(void); | ||
void uart_putc(char c); | ||
void uart_puts(char* str); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef _MM_H | ||
#define _MM_H | ||
|
||
#define PAGE_SHIFT 12 | ||
#define TABLE_SHIFT 9 | ||
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT) | ||
|
||
#define PAGE_SIZE (1 << PAGE_SHIFT) | ||
#define SECTION_SIZE (1 << SECTION_SHIFT) | ||
|
||
#define LOW_MEMORY (2 * SECTION_SIZE) | ||
|
||
#ifndef __ASSEMBLER__ | ||
void memzero(unsigned long src, unsigned long n); | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef _PER_BASE_H_ | ||
#define _PER_BASE_H_ | ||
|
||
#define PHY_BASE_ADDR 0x3F000000 | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef _PER_GPIO_H | ||
#define _PER_GPIO_H | ||
|
||
#include "peripherals/base.h" | ||
|
||
#define GPFSEL1 (PHY_BASE_ADDR + 0x00200004) | ||
#define GPSET0 (PHY_BASE_ADDR + 0x0020001C) | ||
#define GPCLR0 (PHY_BASE_ADDR + 0x00200028) | ||
#define GPPUD (PHY_BASE_ADDR + 0x00200094) | ||
#define GPPUDCLK0 (PHY_BASE_ADDR + 0x00200098) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef _PER_MINI_UART_H_ | ||
#define _PER_MINI_UART_H_ | ||
|
||
#include "peripherals/base.h" | ||
#define AUX_ENABLES (PHY_BASE_ADDR + 0x00215004) | ||
#define AUX_MU_IO_REG (PHY_BASE_ADDR + 0x00215040) | ||
#define AUX_MU_IER_REG (PHY_BASE_ADDR + 0x00215044) | ||
#define AUX_MU_IIR_REG (PHY_BASE_ADDR + 0x00215048) | ||
#define AUX_MU_LCR_REG (PHY_BASE_ADDR + 0x0021504C) | ||
#define AUX_MU_MCR_REG (PHY_BASE_ADDR + 0x00215050) | ||
#define AUX_MU_LSR_REG (PHY_BASE_ADDR + 0x00215054) | ||
#define AUX_MU_MSR_REG (PHY_BASE_ADDR + 0x00215058) | ||
#define AUX_MU_SCRATCH (PHY_BASE_ADDR + 0x0021505C) | ||
#define AUX_MU_CNTL_REG (PHY_BASE_ADDR + 0x00215060) | ||
#define AUX_MU_STAT_REG (PHY_BASE_ADDR + 0x00215064) | ||
#define AUX_MU_BAUD_REG (PHY_BASE_ADDR + 0x00215068) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef _SHELL_H | ||
#define _SHELL_H | ||
|
||
void buffer_clear(); | ||
void execute_command(char *); | ||
void init_shell(); | ||
|
||
void print_input_prompt(); | ||
void get_input(); | ||
|
||
void del_key(); | ||
void left_key(); | ||
void right_key(); | ||
void up_key(); | ||
void down_key(); | ||
|
||
void simple_shell(); | ||
|
||
void shell_hello(); | ||
void shell_help(); | ||
void shell_reboot(); | ||
void shell_boot_uart(); | ||
|
||
void shell_ls(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef _STDINT_H | ||
#define _STDINT_H | ||
|
||
typedef unsigned long long int uint64_t; | ||
typedef signed long long int int64_t; | ||
|
||
typedef unsigned int uint32_t; | ||
typedef signed int int32_t; | ||
|
||
typedef unsigned short int uint16_t; | ||
typedef signed short int int16_t; | ||
|
||
typedef unsigned char uint8_t; | ||
typedef signed char int8_t; | ||
|
||
uint8_t hex_to_int64(char*); | ||
uint64_t hex_to_int8(char); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef _STR_TOOL_H | ||
#define _STR_TOOL_H | ||
#include "stdint.h" | ||
|
||
typedef struct { | ||
char c_magic[6]; | ||
char c_ino[8]; | ||
char c_mode[8]; | ||
char c_uid[8]; | ||
char c_gid[8]; | ||
char c_nlink[8]; | ||
char c_mtime[8]; | ||
char c_filesize[8]; | ||
char c_devmajor[8]; | ||
char c_devminor[8]; | ||
char c_rdevmajor[8]; | ||
char c_rdevminor[8]; | ||
char c_namesize[8]; | ||
char c_check[8]; | ||
} cpio_newc_header; | ||
|
||
int strcmp(char*, char*); | ||
char* itoa(int64_t, int); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef _UTILS_H_ | ||
#define _UTILS_H_ | ||
|
||
void delay(unsigned long); | ||
void put32(unsigned long, unsigned int); | ||
unsigned int get32(unsigned long); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
SECTIONS | ||
{ | ||
. = 0x50000; | ||
.text : { KEEP(*(.text.boot)) *(.text) } | ||
.rodata : { *(.rodata) } | ||
.data : { *(.data) } | ||
|
||
. = ALIGN(0x8); | ||
bss_begin = .; | ||
.bss : { *(.bss*) } | ||
bss_end = .; | ||
_end = .; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "mini_uart.h" | ||
#include "utils.h" | ||
#include "stdint.h" | ||
|
||
union large_int_by_byte{ | ||
uint8_t int_by_byte[8]; | ||
uint64_t larg_int; | ||
}; | ||
|
||
void boot_main(){ | ||
uart_init(); | ||
|
||
union large_int_by_byte program; | ||
volatile uint8_t *program_addr = (volatile uint8_t *)0x80000; | ||
|
||
// read dirty message until end | ||
while(1) { | ||
uint8_t data; | ||
data = uart_getc(); | ||
if(data==4)break; | ||
} | ||
|
||
// read kernel size | ||
for(int i=0; i<8; i++) | ||
program.int_by_byte[i] = uart_getc(); | ||
|
||
for(uint64_t offset=0; offset<program.larg_int; offset++) | ||
program_addr[offset] = uart_getc(); | ||
|
||
asm volatile("br %x[program_addr]"::[program_addr]"r"(program_addr)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "utils.h" | ||
#include "peripherals/gpio.h" | ||
#include "peripherals/mini_uart.h" | ||
|
||
void uart_init(){ | ||
unsigned int selector; | ||
|
||
selector = get32(GPFSEL1); | ||
selector &= ~(7<<12); // clean gpio 14 | ||
selector |= 2<<12; // set alt5 at gpio14 | ||
selector &= ~(7<<15); // clean gpio 15 | ||
selector |= 2<<15; // set alt5 at gpio 15 | ||
put32(GPFSEL1, selector); | ||
|
||
put32(GPPUD, 0); // disable pull-up/down | ||
delay(150); // wait 150 cycles | ||
put32(GPPUDCLK0, (1<<14)|(1<<15)); // only modify GPIO 14 and 15 | ||
delay(150); | ||
put32(GPPUDCLK0, 0); // remove the clock | ||
|
||
put32(AUX_ENABLES, 1); // enable mini UART | ||
put32(AUX_MU_CNTL_REG, 0); // Disable transmitter and receiver during configuration | ||
put32(AUX_MU_IER_REG, 0); // Disable interrupt | ||
put32(AUX_MU_LCR_REG, 3); // Set the data size to 8 bit | ||
put32(AUX_MU_MCR_REG, 0); // Don’t need auto flow control | ||
put32(AUX_MU_BAUD_REG, 270); // Set baud rate to 115200 | ||
|
||
put32(AUX_MU_IIR_REG, 6); // No FIFO | ||
put32(AUX_MU_CNTL_REG, 3); // Enable the transmitter and receiver | ||
} | ||
|
||
void uart_putc(char c){ | ||
while(!(get32(AUX_MU_LSR_REG)&0x20)); | ||
put32(AUX_MU_IO_REG, c); | ||
} | ||
|
||
char uart_getc(void){ | ||
while(!(get32(AUX_MU_LSR_REG)&0x01)); | ||
return get32(AUX_MU_IO_REG); | ||
} | ||
|
||
void uart_puts(char* str){ | ||
while(*str){ | ||
uart_putc(*str); | ||
str += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.globl memzero | ||
memzero: | ||
str xzr, [x0], #8 | ||
subs x1, x1, #8 | ||
b.gt memzero | ||
ret |
Oops, something went wrong.