Skip to content

Commit 1c89f74

Browse files
authored
Merge pull request #1 from naihsin/Lab2
Add Lab2
2 parents 4165842 + ee65cce commit 1c89f74

30 files changed

Lines changed: 1337 additions & 16 deletions

README.md

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,55 @@
66
| ---------- | ----------- | ------- | -------------------------- |
77
| `A091513` | `naihsin` | `張乃心` | s109164507@m109.nthu.edu.tw |
88

9-
## How to build
10-
11-
**WIP**
12-
13-
## How to run
14-
15-
**WIP**
16-
17-
## How to burn it into pi3
18-
19-
**WIP**
20-
21-
## Architecture
22-
23-
**WIP**
9+
## How to build loader
10+
```bash
11+
cd loader
12+
make
13+
```
14+
15+
## How to build kernel
16+
```bash
17+
cd kernel
18+
make
19+
```
20+
21+
## How to build cpio archive file
22+
```bash
23+
cd rootfs
24+
find . | cpio -o -H newc > ../initramfs.cpio
25+
```
26+
27+
## How to run loader
28+
```bash
29+
qemu-system-aarch64 -M raspi3 -kernel bootloader.img -initrd initramfs.cpio -serial null -serial pty
30+
screen /dev/<dev_name>
31+
```
32+
33+
## How to run kernel
34+
```bash
35+
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -initrd initramfs.cpio -serial null -serial pty
36+
screen /dev/<dev_name>
37+
```
38+
39+
## How to burn and run it into pi3
40+
- Copy bootloader.img to your SD card
41+
- Copt initramfs.cpio to your SD card
42+
- Restart raspi3
43+
- Waiting bootloader shell
44+
- Type <loadimg> command
45+
- Open another terminal to send kernel.img
46+
- ```bash python3 sender.py ```
47+
- Tab to raspi3 console, check for the recving kernel.img
48+
- Type <jumpimg> commmand
49+
- You will see the new kernel shell
50+
- Type <cpio> command
2451

2552
## Directory structure
2653

27-
**WIP**
54+
| File / Directory | Content |
55+
| --------------| ----------------------------------------------------- |
56+
| include | C header file |
57+
| kernel | Kernel main program, lib, makefile etc ... |
58+
| loader | Loader main program, lib, makefile etc ... |
59+
| initramfs.cpio | Cpio Archive file |
60+
| sender.py | Python script for sending kernel.img |

include/bootshell.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int strcmp(char*, char*);
2+
int strlen (char*);
3+
void boot_shell();
4+
void command();
5+
void command_help();
6+
void command_notfound();
7+
void command_loadimg();
8+
void command_jumpimg();

include/cpio.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//#define CPIO_ARCHIVE_LOCATION 0x8000000
2+
#define CPIO_ARCHIVE_LOCATION 0x20000000
3+
/* Magic identifiers for the "cpio" file format. */
4+
#define CPIO_HEADER_MAGIC "070701"
5+
#define CPIO_FOOTER_MAGIC "TRAILER!!!"
6+
#define CPIO_ALIGNMENT 4
7+
8+
struct cpio_header {
9+
char c_magic[6]; /* Magic header '070701'. */
10+
char c_ino[8]; /* "i-node" number. */
11+
char c_mode[8]; /* Permisions. */
12+
char c_uid[8]; /* User ID. */
13+
char c_gid[8]; /* Group ID. */
14+
char c_nlink[8]; /* Number of hard links. */
15+
char c_mtime[8]; /* Modification time. */
16+
char c_filesize[8]; /* File size. */
17+
char c_devmajor[8]; /* Major dev number. */
18+
char c_devminor[8]; /* Minor dev number. */
19+
char c_rdevmajor[8];
20+
char c_rdevminor[8];
21+
char c_namesize[8]; /* Length of filename in bytes. */
22+
char c_check[8]; /* Checksum. */
23+
};
24+
25+
/**
26+
* Stores information about the underlying implementation.
27+
*/
28+
struct cpio_info {
29+
/// The number of files in the CPIO archive
30+
unsigned int file_count;
31+
/// The maximum size of a file name
32+
unsigned int max_path_sz;
33+
};
34+
35+
36+
/**
37+
* Retrieves information about the provided CPIO archive
38+
* @param[in] archive The location of the CPIO archive
39+
* @param[out] info A CPIO info structure to populate
40+
* @return Non-zero on error.
41+
*/
42+
int cpio_info(void *archive, struct cpio_info *info);
43+
44+
/**
45+
* Writes the list of file names contained within a CPIO archive into
46+
* a provided buffer
47+
* @param[in] archive The location of the CPIO archive
48+
* @param[in] buf A memory location to store the CPIO file list to
49+
* @param[in] buf_len The length of the provided buf
50+
*/
51+
void cpio_ls(void *archive, char buf[100][100], unsigned long buf_len);
52+
53+
void cpio_get_file(void *archive, char name[], char content[]);

include/gpio.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2018 bzt (bztsrc@github)
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
* DEALINGS IN THE SOFTWARE.
23+
*
24+
*/
25+
26+
#define MMIO_BASE 0x3F000000
27+
28+
#define GPFSEL0 ((volatile unsigned int*)(MMIO_BASE+0x00200000))
29+
#define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
30+
#define GPFSEL2 ((volatile unsigned int*)(MMIO_BASE+0x00200008))
31+
#define GPFSEL3 ((volatile unsigned int*)(MMIO_BASE+0x0020000C))
32+
#define GPFSEL4 ((volatile unsigned int*)(MMIO_BASE+0x00200010))
33+
#define GPFSEL5 ((volatile unsigned int*)(MMIO_BASE+0x00200014))
34+
#define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
35+
#define GPSET1 ((volatile unsigned int*)(MMIO_BASE+0x00200020))
36+
#define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
37+
#define GPLEV0 ((volatile unsigned int*)(MMIO_BASE+0x00200034))
38+
#define GPLEV1 ((volatile unsigned int*)(MMIO_BASE+0x00200038))
39+
#define GPEDS0 ((volatile unsigned int*)(MMIO_BASE+0x00200040))
40+
#define GPEDS1 ((volatile unsigned int*)(MMIO_BASE+0x00200044))
41+
#define GPHEN0 ((volatile unsigned int*)(MMIO_BASE+0x00200064))
42+
#define GPHEN1 ((volatile unsigned int*)(MMIO_BASE+0x00200068))
43+
#define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
44+
#define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))
45+
#define GPPUDCLK1 ((volatile unsigned int*)(MMIO_BASE+0x0020009C))

include/myshell.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int strcmp(char*, char*);
2+
int strlen (char*);
3+
void my_shell();
4+
void command();
5+
void command_help();
6+
void command_hello();
7+
void command_reboot();
8+
void command_notfound();
9+
void command_cpio();
10+
void command_cpiols();

include/string.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "../include/type.h"
2+
3+
int strcmp ( char * s1, char * s2 );
4+
void strset ( char * s1, int c, int size );
5+
int strlen ( char * s );
6+
void itoa ( int x, char str[], int d);
7+
void itohex_str ( uint64_t d, int size, char * s );
8+
void reverse ( char *s );

include/type.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef TYPE_H
2+
#define TYPE_H
3+
4+
typedef int int32_t;
5+
typedef long long int int64_t;
6+
7+
typedef unsigned int uint32_t;
8+
typedef unsigned long long int uint64_t;
9+
10+
#endif

include/uart.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2018 bzt (bztsrc@github)
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
* DEALINGS IN THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef UART_H
27+
#define UART_H
28+
29+
/* Auxilary mini UART registers */
30+
#define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
31+
#define AUX_MU_IO ((volatile unsigned int*)(MMIO_BASE+0x00215040))
32+
#define AUX_MU_IER ((volatile unsigned int*)(MMIO_BASE+0x00215044))
33+
#define AUX_MU_IIR ((volatile unsigned int*)(MMIO_BASE+0x00215048))
34+
#define AUX_MU_LCR ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
35+
#define AUX_MU_MCR ((volatile unsigned int*)(MMIO_BASE+0x00215050))
36+
#define AUX_MU_LSR ((volatile unsigned int*)(MMIO_BASE+0x00215054))
37+
#define AUX_MU_MSR ((volatile unsigned int*)(MMIO_BASE+0x00215058))
38+
#define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
39+
#define AUX_MU_CNTL ((volatile unsigned int*)(MMIO_BASE+0x00215060))
40+
#define AUX_MU_STAT ((volatile unsigned int*)(MMIO_BASE+0x00215064))
41+
#define AUX_MU_BAUD ((volatile unsigned int*)(MMIO_BASE+0x00215068))
42+
43+
#define PM_RSTC ((volatile unsigned int*)0x3F10001C)
44+
#define PM_WDOG ((volatile unsigned int*)0x3F100024)
45+
#define PM_PASSWORD (0x5a000000)
46+
/**
47+
* Set baud rate and characteristics (115200 8N1) and map to GPIO
48+
*/
49+
void uart_init();
50+
51+
/**
52+
* Send a character
53+
*/
54+
void uart_send(unsigned int c);
55+
/**
56+
* Receive a character
57+
*/
58+
char uart_getc();
59+
60+
/**
61+
* Display a string
62+
*/
63+
void uart_puts(char *s);
64+
65+
#endif
66+

initramfs.cpio

1 KB
Binary file not shown.

kernel/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SRCS = $(wildcard *.c)
2+
OBJS = $(SRCS:.c=.o)
3+
CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
4+
CC = aarch64-linux-gnu-gcc
5+
LINKER = aarch64-linux-gnu-ld
6+
OBJ_CPY = aarch64-linux-gnu-objcopy
7+
8+
all: clean kernel8.img
9+
10+
start.o: start.S
11+
$(CC) $(CFLAGS) -c start.S -o start.o
12+
13+
%.o: %.c
14+
$(CC) $(CFLAGS) -c $< -o $@
15+
16+
kernel8.img: start.o $(OBJS)
17+
$(LINKER) -nostdlib -nostartfiles start.o $(OBJS) -T linker.ld -o kernel8.elf
18+
$(OBJ_CPY) -O binary kernel8.elf kernel8.img
19+
20+
clean:
21+
rm kernel8.elf *.o >/dev/null 2>/dev/null || true
22+
23+
run:
24+
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio

0 commit comments

Comments
 (0)