Skip to content
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
26 changes: 26 additions & 0 deletions Lab2/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "GDB debug - custom",
"type": "cppdbg",
"request": "launch",
"program": "~/Desktop/my_OSC/OSC/Lab2/bootloader.elf",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "gdb-multiarch",
"miDebuggerServerAddress": "127.0.0.1:1234"
}
]
}
9 changes: 9 additions & 0 deletions Lab2/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"sched.h": "c",
"allocator.h": "c",
"type_traits": "c",
"stddef.h": "c",
"devicetree.h": "c"
}
}
42 changes: 42 additions & 0 deletions Lab2/Allocator/allocator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "header/allocator.h"
#include "../header/mini_uart.h"
#include "../header/utils.h"

#define MEM_START 0x10000000

unsigned long *malloc_cur = (unsigned long *)MEM_START;

void *malloc(size_t size)
{
align(&size,8);
unsigned long *malloc_ret = malloc_cur;
malloc_cur+=(unsigned int)size;
return malloc_ret;
}
/*

Initial Memory Layout:
+-----------------------+
| Address 0x10000000 | <--- malloc_cur starts here
+-----------------------+

Step 1: Call malloc(sizeof("Hello")) - Request 6 bytes, aligned to 8 bytes
+-----------------------+ 0x10000000
| Reserved 8 bytes | <-- malloc_ret points here, now referred as 'a'
| (Uninitialized) | malloc_cur moved to 0x10000008
+-----------------------+ 0x10000008

Step 2: Initialize Memory with "Hello"
+-----------------------+ 0x10000000
| 'H' (a[0]) |
| 'e' (a[1]) |
| 'l' (a[2]) |
| 'l' (a[3]) |
| 'o' (a[4]) |
| '\0'(a[5]) |
| Padding (2 bytes) | <--- Memory alignment padding
+-----------------------+ 0x10000008

Continue Allocating...

*/
3 changes: 3 additions & 0 deletions Lab2/Allocator/header/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stddef.h>

void* malloc(size_t size);
4 changes: 4 additions & 0 deletions Lab2/Bootloader/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kernel=bootloader.img
arm_64bit=1

initramfs initramfs.cpio 0x20000000
23 changes: 23 additions & 0 deletions Lab2/Bootloader/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SECTIONS
{
. = 0x60000;

text_begin = .;
.text.relo : { *(.text.relocate) }

boot_entry = .;
.text.boot : { *(.text.boot) }

.text : { *(.text) }

.rodata : { *(.rodata) }
. = ALIGN(0x1000);

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

bss_begin = .;
.bss (NOLOAD) : { *(.bss) }
. = ALIGN(0x1000);
bss_end = .;
}
47 changes: 47 additions & 0 deletions Lab2/Bootloader/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "../header/mini_uart.h"
#include "../header/utils.h"
#include <stddef.h>

#define BUFFER_MAX_SIZE 256

extern char *_dtb;

void load_img()
{
char *const kernel_addr = (char *)0x80000;
char buffer[BUFFER_MAX_SIZE];
size_t index = 0;

while (1)
{
buffer[index] = uart_recv();

if (buffer[index] == '\n')
break;

index++;
}

buffer[index + 1] = '\0';
utils_newline2end(buffer);
uart_send('\r');

unsigned int img_size = utils_str2uint_dec(buffer);

char *current = kernel_addr;
while (img_size--)
{
*current = uart_recv_raw();
current++;
uart_send('.');
}

((void (*)(char *))kernel_addr)(_dtb);
}

void bootloader_main(void)
{
uart_init();
uart_send_string("Please sent the kernel image size:");
load_img();
}
19 changes: 19 additions & 0 deletions Lab2/Bootloader/relocate.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section ".text.relocate"
.globl _start_relocate

_start_relocate:
adr x10, .
ldr x12, =text_begin
adr x13, bss_end

moving_relocate:
cmp x10, x13
b.eq end_relocate
ldr x14, [x10], #8
str x14, [x12], #8
b moving_relocate

end_relocate:
ldr x14, =boot_entry
br x14

32 changes: 32 additions & 0 deletions Lab2/Bootloader/send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import serial
import os
import time
from tqdm import tqdm

def send_file(serial_port, file_path, baud_rate=115200, timeout=0.5, sleep_time=0.002):

tty = serial.Serial(serial_port, baud_rate, timeout=timeout)

file_size = os.path.getsize(file_path)

tty.write(str(file_size).encode('utf-8'))
tty.write("\n".encode('utf-8'))
time.sleep(sleep_time)

pbar = tqdm(total=file_size, unit="B", unit_scale=True, desc="Sending")

start_time = time.time()

with open(file_path, "rb") as fp:
for byte in iter(lambda: fp.read(1), b''):
tty.write(byte)
pbar.update(len(byte))
time.sleep(sleep_time)

pbar.close()

total_time = time.time() - start_time

print(f"Transfer completed in {total_time:.2f} seconds. Speed: {file_size / total_time:.2f} Bytes/s")

send_file("/dev/ttyUSB0", "kernel8.img")
32 changes: 32 additions & 0 deletions Lab2/Bootloader/start.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.section ".text.boot"
.globl _start_boot

_start_boot:
ldr x19, =_dtb
str x0, [x19]

mrs x20, mpidr_el1
and x20, x20,#0xFF
cbz x20, master

hang:
b hang

master:
adr x20, bss_begin
adr x21, bss_end
sub x21, x21, x20
bl memzero

mov sp, #0x3F000000
bl bootloader_main

memzero:
str xzr, [x20], #8
subs x21, x21, #8
b.gt memzero
ret

.global _dtb
.section .data
_dtb: .dc.a 0x0
67 changes: 67 additions & 0 deletions Lab2/CPIO/cpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "header/cpio.h"
#include "../header/mini_uart.h"
#include "../header/utils.h"

void cpio_ls()
{
char *addr = cpio_addr;

while (utils_str_compare((char *)(addr + sizeof(cpio_header)), "TRAILER!!!") != 0)
{
cpio_header *header = (cpio_header *)addr;
unsigned long pathname_size = hex2dec(header->c_namesize); //path_size
unsigned long file_size = hex2dec(header->c_filesize); //file_size

unsigned long headerPathname_size = sizeof(cpio_header) + pathname_size; //total_size

align(&headerPathname_size,4);
align(&file_size,4);

uart_send_string(addr + sizeof(cpio_header)); // print the file name
uart_send_string("\n");

addr += (headerPathname_size + file_size);
}
}

char *findFile(char *name)
{
char *addr = cpio_addr;
while (utils_str_compare((char *)(addr + sizeof(cpio_header)), "TRAILER!!!") != 0)
{
if ((utils_str_compare((char *)(addr + sizeof(cpio_header)), name) == 0))
return addr;

cpio_header *header = (cpio_header *)addr;
unsigned long pathname_size = hex2dec(header->c_namesize);
unsigned long file_size = hex2dec(header->c_filesize);
unsigned long headerPathname_size = sizeof(cpio_header) + pathname_size;

align(&headerPathname_size,4);
align(&file_size,4);
addr += (headerPathname_size + file_size);
}
}

void cpio_cat(char *filename)
{
char *target = findFile(filename);

if (target)
{
cpio_header *header = (cpio_header *)target;
unsigned long pathname_size = hex2dec(header->c_namesize);
unsigned long file_size = hex2dec(header->c_filesize);
unsigned long headerPathname_size = sizeof(cpio_header) + pathname_size;

align(&headerPathname_size,4);
align(&file_size,4);

char *file_content = target + headerPathname_size;

uart_send_string(file_content);
uart_send_string("\n");
}
else
uart_send_string("Not found the file\n");
}
22 changes: 22 additions & 0 deletions Lab2/CPIO/header/cpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
typedef struct cpio_header
{
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_header;

extern char * cpio_addr;
void cpio_ls();
void cpio_cat(char *filename);
char * findFile(char *name);
Loading