From 6e575328247330399c33df94754a755eae5646a1 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 3 Jun 2020 15:02:12 +0800 Subject: [PATCH] update readme --- README.md | 60 +++++- loader/Makefile | 33 ---- loader/raspbootocm.cc | 428 ------------------------------------------ loader/src/gpio.h | 45 ----- loader/src/link.ld | 46 ----- loader/src/main.c | 80 -------- loader/src/mbox.c | 69 ------- loader/src/mbox.h | 47 ----- loader/src/start.S | 72 ------- loader/src/uart.c | 113 ----------- loader/src/uart.h | 30 --- 11 files changed, 58 insertions(+), 965 deletions(-) delete mode 100644 loader/Makefile delete mode 100644 loader/raspbootocm.cc delete mode 100644 loader/src/gpio.h delete mode 100644 loader/src/link.ld delete mode 100644 loader/src/main.c delete mode 100644 loader/src/mbox.c delete mode 100644 loader/src/mbox.h delete mode 100644 loader/src/start.S delete mode 100644 loader/src/uart.c delete mode 100644 loader/src/uart.h diff --git a/README.md b/README.md index f3575d31..6d2819c4 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ | -----------| -------------- | ---- | -------------------------- | | 0856009 | JingWangTW | 王靖 | jingwang.cs08g@nctu.edu.tw | -## LAB 2 Project +## LAB 6 Project -### How to build +### Build ```bash make ``` @@ -16,4 +16,60 @@ make ### Run on QEMU ```bash make run +``` + +### Clean and Rebuild Project +```bash +make force +``` + +### Run with Debugger +```bash +make debug + +# on the other shell +aarch64-linux-gnu-gdb +file build/kernel8.elf +directory . +target remote localhost:1234 + +``` + +## Directory structure +``` +. +├── LICENSE +├── Makefile # make file +├── README.md +├── include # header files +│   ├── allocator.h # header file for allocator.c +│   ├── command.h +│   ├── ctype.h +│   ├── framebuffer.h +│   ├── gpio.h +│   ├── img_data.h +│   ├── mailbox.h +│   ├── math.h # header file for math.c +│   ├── mem.h # header file for mem.c +│   ├── shell.h +│   ├── stdarg.h # define stdarg +│   ├── string.h +│   ├── time.h +│   ├── type.h # define some common type +│   └── uart.h +├── link.ld +└── src # source files + ├── allocator.c # register for fixed-size, vaired-size allocator, allocate and free memory from the both allocator + ├── command.c + ├── ctype.c + ├── framebuffer.c + ├── mailbox.c + ├── main.c + ├── math.c + ├── mem.c # buddy system + ├── shell.c + ├── start.S # startup booting process + ├── string.c + ├── time.c + └── uart.c ``` \ No newline at end of file diff --git a/loader/Makefile b/loader/Makefile deleted file mode 100644 index 0c71f6b5..00000000 --- a/loader/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -CC = aarch64-linux-gnu-gcc -LD = aarch64-linux-gnu-ld -OBJ_CPY = aarch64-linux-gnu-objcopy -EMULATOR = qemu-system-aarch64 - -CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles - -SRCDIR = src - -SRCS = $(wildcard $(SRCDIR)/*.c ) -OBJS = $(patsubst $(SRCDIR)/%.c, %.o, $(SRCS)) -LINK_SCRIPT = src/link.ld - -VPATH = $(SRCDIR) -vpath %.c %.S $(SRCDIR) - -all: clean kernel8.img - -clean: - @rm kernel8.elf kernel8.img *.o >/dev/null 2>/dev/null || true - -kernel8.img: start.o $(OBJS) - $(LD) -nostdlib -nostartfiles $^ -T $(LINK_SCRIPT) -o kernel8.elf - $(OBJ_CPY) -O binary kernel8.elf $@ - -start.o: start.S - $(CC) $(CFLAGS) -c $< -o $@ - -%.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ - -run: kernel8.img - $(EMULATOR) -M raspi3 -kernel kernel8.img -serial stdio \ No newline at end of file diff --git a/loader/raspbootocm.cc b/loader/raspbootocm.cc deleted file mode 100644 index b669b5c8..00000000 --- a/loader/raspbootocm.cc +++ /dev/null @@ -1,428 +0,0 @@ -/* raspbootcom.cc - upload kernel.img via serial port to the RPi */ -/* Copyright (C) 2013 Goswin von Brederlow - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#define _BSD_SOURCE /* See feature_test_macros(7) */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define BUF_SIZE 65536 - -struct termios old_tio, new_tio; - -void do_exit ( int fd, int res ) -{ - // close FD - if ( fd != -1 ) - close ( fd ); - // restore settings for STDIN_FILENO - if ( isatty ( STDIN_FILENO ) ) - { - tcsetattr ( STDIN_FILENO, TCSANOW, &old_tio ); - } - exit ( res ); -} - -// open serial connection -int open_serial ( const char * dev ) -{ - // The termios structure, to be configured for serial interface. - struct termios termios; - - // Open the device, read/write, not the controlling tty, and non-blocking I/O - int fd = open ( dev, O_RDWR | O_NOCTTY | O_NONBLOCK ); - if ( fd == -1 ) - { - // failed to open - return -1; - } - // must be a tty - if ( !isatty ( fd ) ) - { - fprintf ( stderr, "%s is not a tty\n", dev ); - do_exit ( fd, EXIT_FAILURE ); - } - - // Get the attributes. - if ( tcgetattr ( fd, &termios ) == -1 ) - { - perror ( "Failed to get attributes of device" ); - do_exit ( fd, EXIT_FAILURE ); - } - - // So, we poll. - termios.c_cc[VTIME] = 0; - termios.c_cc[VMIN] = 0; - - // 8N1 mode, no input/output/line processing masks. - termios.c_iflag = 0; - termios.c_oflag = 0; - termios.c_cflag = CS8 | CREAD | CLOCAL; - termios.c_lflag = 0; - - // Set the baud rate. - if ( ( cfsetispeed ( &termios, B115200 ) < 0 ) || ( cfsetospeed ( &termios, B115200 ) < 0 ) ) - { - perror ( "Failed to set baud-rate" ); - do_exit ( fd, EXIT_FAILURE ); - } - - // Write the attributes. - if ( tcsetattr ( fd, TCSAFLUSH, &termios ) == -1 ) - { - perror ( "tcsetattr()" ); - do_exit ( fd, EXIT_FAILURE ); - } - return fd; -} - -// send kernel to rpi -void send_kernel ( int fd, const char * file ) -{ - int file_fd; - off_t off; - uint32_t size; - ssize_t pos; - char * p; - bool done = false; - - // Set fd blocking - if ( fcntl ( fd, F_SETFL, 0 ) == -1 ) - { - perror ( "fcntl()" ); - do_exit ( fd, EXIT_FAILURE ); - } - - // Open file - if ( ( file_fd = open ( file, O_RDONLY ) ) == -1 ) - { - perror ( file ); - do_exit ( fd, EXIT_FAILURE ); - } - - // Get kernel size - off = lseek ( file_fd, 0L, SEEK_END ); - if ( off > 0x200000 ) - { - fprintf ( stderr, "kernel too big\n" ); - do_exit ( fd, EXIT_FAILURE ); - } - size = htole32 ( off ); - lseek ( file_fd, 0L, SEEK_SET ); - - fprintf ( stderr, "### sending kernel %s [%zu byte]\n", file, (size_t) off ); - - // send kernel size to RPi - p = (char *) &size; - pos = 0; - while ( pos < 4 ) - { - ssize_t len = write ( fd, &p[pos], 4 - pos ); - if ( len == -1 ) - { - perror ( "write()" ); - do_exit ( fd, EXIT_FAILURE ); - } - pos += len; - } - // wait for OK - char ok_buf[2]; - p = ok_buf; - pos = 0; - while ( pos < 2 ) - { - ssize_t len = read ( fd, &p[pos], 2 - pos ); - if ( len == -1 ) - { - perror ( "read()" ); - do_exit ( fd, EXIT_FAILURE ); - } - pos += len; - } - if ( ok_buf[0] != 'O' || ok_buf[1] != 'K' ) - { - fprintf ( stderr, "error after sending size\n" ); - do_exit ( fd, EXIT_FAILURE ); - } - - while ( !done ) - { - char buf[BUF_SIZE]; - ssize_t pos = 0; - ssize_t len = read ( file_fd, buf, BUF_SIZE ); - switch ( len ) - { - case -1: - perror ( "read()" ); - do_exit ( fd, EXIT_FAILURE ); - case 0: - done = true; - } - while ( len > 0 ) - { - ssize_t len2 = write ( fd, &buf[pos], len ); - if ( len2 == -1 ) - { - perror ( "write()" ); - do_exit ( fd, EXIT_FAILURE ); - } - len -= len2; - pos += len2; - } - } - - // Set fd non-blocking - if ( fcntl ( fd, F_SETFL, O_NONBLOCK ) == -1 ) - { - perror ( "fcntl()" ); - do_exit ( fd, EXIT_FAILURE ); - } - - fprintf ( stderr, "### finished sending\n" ); - - return; -} - -int main ( int argc, char * argv[] ) -{ - int fd, max_fd = STDIN_FILENO; - fd_set rfds, wfds, efds; - char buf[BUF_SIZE]; - size_t start = 0; - size_t end = 0; - bool done = false, leave = false; - int breaks = 0; - - printf ( "Raspbootcom V1.0\n" ); - - if ( argc != 3 ) - { - printf ( "USAGE: %s \n", argv[0] ); - printf ( "Example: %s /dev/ttyUSB0 kernel/kernel.img\n", argv[0] ); - exit ( EXIT_FAILURE ); - } - - // Set STDIN non-blocking and unbuffered - if ( fcntl ( STDIN_FILENO, F_SETFL, O_NONBLOCK ) == -1 ) - { - perror ( "fcntl()" ); - exit ( EXIT_FAILURE ); - } - if ( isatty ( STDIN_FILENO ) ) - { - // get the terminal settings for stdin - if ( tcgetattr ( STDIN_FILENO, &old_tio ) == -1 ) - { - perror ( "tcgetattr" ); - exit ( EXIT_FAILURE ); - } - - // we want to keep the old setting to restore them a the end - new_tio = old_tio; - - // disable canonical mode (buffered i/o) and local echo - new_tio.c_lflag &= ( ~ICANON & ~ECHO ); - - // set the new settings immediately - if ( tcsetattr ( STDIN_FILENO, TCSANOW, &new_tio ) == -1 ) - { - perror ( "tcsetattr()" ); - do_exit ( -1, EXIT_FAILURE ); - } - } - - while ( !leave ) - { - // Open device - if ( ( fd = open_serial ( argv[1] ) ) == -1 ) - { - // udev takes a while to change ownership - // so sometimes one gets EPERM - if ( errno == ENOENT || errno == ENODEV || errno == EACCES ) - { - fprintf ( stderr, "\r### Waiting for %s...\r", argv[1] ); - sleep ( 1 ); - continue; - } - perror ( argv[1] ); - do_exit ( fd, EXIT_FAILURE ); - } - fprintf ( stderr, "### Listening on %s \n", argv[1] ); - - // select needs the largeds FD + 1 - if ( fd > STDIN_FILENO ) - { - max_fd = fd + 1; - } - else - { - max_fd = STDIN_FILENO + 1; - } - - done = false; - start = end = 0; - while ( !done || start != end ) - { - // Watch stdin and dev for input. - FD_ZERO ( &rfds ); - if ( !done && end < BUF_SIZE ) - FD_SET ( STDIN_FILENO, &rfds ); - FD_SET ( fd, &rfds ); - - // Watch fd for output if needed. - FD_ZERO ( &wfds ); - if ( start != end ) - FD_SET ( fd, &wfds ); - - // Watch stdin and dev for error. - FD_ZERO ( &efds ); - FD_SET ( STDIN_FILENO, &efds ); - FD_SET ( fd, &efds ); - - // Wait for something to happend - if ( select ( max_fd, &rfds, &wfds, &efds, NULL ) == -1 ) - { - perror ( "select()" ); - do_exit ( fd, EXIT_FAILURE ); - } - else - { - // check for errors - if ( FD_ISSET ( STDIN_FILENO, &efds ) ) - { - fprintf ( stderr, "error on STDIN\n" ); - do_exit ( fd, EXIT_FAILURE ); - } - if ( FD_ISSET ( fd, &efds ) ) - { - fprintf ( stderr, "error on device\n" ); - do_exit ( fd, EXIT_FAILURE ); - } - // RPi is ready to recieve more data, send more - if ( FD_ISSET ( fd, &wfds ) ) - { - ssize_t len = write ( fd, &buf[start], end - start ); - if ( len == -1 ) - { - perror ( "write()" ); - do_exit ( fd, EXIT_FAILURE ); - } - start += len; - if ( start == end ) - start = end = 0; - // shift buffer contents - if ( end == BUF_SIZE ) - { - memmove ( buf, &buf[start], end - start ); - end -= start; - start = 0; - } - } - // input from the user, copy to RPi - if ( FD_ISSET ( STDIN_FILENO, &rfds ) ) - { - ssize_t len = read ( STDIN_FILENO, &buf[end], BUF_SIZE - end ); - switch ( len ) - { - case -1: - perror ( "read()" ); - do_exit ( fd, EXIT_FAILURE ); - case 0: - done = true; - leave = true; - } - end += len; - } - // output from the RPi, copy to STDOUT - if ( FD_ISSET ( fd, &rfds ) ) - { - char buf2[BUF_SIZE]; - ssize_t len = read ( fd, buf2, BUF_SIZE ); - switch ( len ) - { - case -1: - perror ( "read()" ); - do_exit ( fd, EXIT_FAILURE ); - case 0: - done = true; - } - // scan output for tripple break (^C^C^C) - // send kernel on tripple break, otherwise output text - const char * p = buf2; - while ( p < &buf2[len] ) - { - const char * q = index ( p, '\x03' ); - if ( q == NULL ) - q = &buf2[len]; - if ( p == q ) - { - ++breaks; - ++p; - if ( breaks == 3 ) - { - if ( start != end ) - { - fprintf ( stderr, "Discarding input after tripple break\n" ); - start = end = 0; - } - send_kernel ( fd, argv[2] ); - breaks = 0; - } - } - else - { - while ( breaks > 0 ) - { - ssize_t len2 = write ( STDOUT_FILENO, "\x03\x03\x03", breaks ); - if ( len2 == -1 ) - { - perror ( "write()" ); - do_exit ( fd, EXIT_FAILURE ); - } - breaks -= len2; - } - while ( p < q ) - { - ssize_t len2 = write ( STDOUT_FILENO, p, q - p ); - if ( len2 == -1 ) - { - perror ( "write()" ); - do_exit ( fd, EXIT_FAILURE ); - } - p += len2; - } - } - } - } - } - } - close ( fd ); - } - - do_exit ( -1, EXIT_SUCCESS ); -} \ No newline at end of file diff --git a/loader/src/gpio.h b/loader/src/gpio.h deleted file mode 100644 index 30e8abc9..00000000 --- a/loader/src/gpio.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - * - */ - -#define MMIO_BASE 0x3F000000 - -#define GPFSEL0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200000 ) ) -#define GPFSEL1 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200004 ) ) -#define GPFSEL2 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200008 ) ) -#define GPFSEL3 ( (volatile unsigned int *) ( MMIO_BASE + 0x0020000C ) ) -#define GPFSEL4 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200010 ) ) -#define GPFSEL5 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200014 ) ) -#define GPSET0 ( (volatile unsigned int *) ( MMIO_BASE + 0x0020001C ) ) -#define GPSET1 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200020 ) ) -#define GPCLR0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200028 ) ) -#define GPLEV0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200034 ) ) -#define GPLEV1 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200038 ) ) -#define GPEDS0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200040 ) ) -#define GPEDS1 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200044 ) ) -#define GPHEN0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200064 ) ) -#define GPHEN1 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200068 ) ) -#define GPPUD ( (volatile unsigned int *) ( MMIO_BASE + 0x00200094 ) ) -#define GPPUDCLK0 ( (volatile unsigned int *) ( MMIO_BASE + 0x00200098 ) ) -#define GPPUDCLK1 ( (volatile unsigned int *) ( MMIO_BASE + 0x0020009C ) ) diff --git a/loader/src/link.ld b/loader/src/link.ld deleted file mode 100644 index 1259029c..00000000 --- a/loader/src/link.ld +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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. - * - */ - -SECTIONS -{ - . = 0x80000 - 1024; - PROVIDE(_code = .); - .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } - PROVIDE(_data = .); - .data : { *(.data .data.* .gnu.linkonce.d*) } - .bss (NOLOAD) : { - . = ALIGN(16); - __bss_start = .; - *(.bss .bss.*) - *(COMMON) - __bss_end = .; - } - _end = .; - - /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } -} -__bss_size = (__bss_end - __bss_start)>>3; -__loader_size = (_end - _code)>>3; diff --git a/loader/src/main.c b/loader/src/main.c deleted file mode 100644 index 9b453721..00000000 --- a/loader/src/main.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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. - * - */ - -#include "uart.h" - -void main ( ) -{ - int size = 0; - char * kernel = (char *) 0x80000; - - // set up serial console - uart_init ( ); - - // say hello. To reduce loader size I removed uart_puts() -again: - uart_send ( 'R' ); - uart_send ( 'B' ); - uart_send ( 'I' ); - uart_send ( 'N' ); - uart_send ( '6' ); - uart_send ( '4' ); - uart_send ( '\r' ); - uart_send ( '\n' ); - // notify raspbootcom to send the kernel - uart_send ( 3 ); - uart_send ( 3 ); - uart_send ( 3 ); - - // read the kernel's size - size = uart_getc ( ); - size |= uart_getc ( ) << 8; - size |= uart_getc ( ) << 16; - size |= uart_getc ( ) << 24; - - // send negative or positive acknowledge - if ( size < 64 || size > 1024 * 1024 ) - { - // size error - uart_send ( 'S' ); - uart_send ( 'E' ); - goto again; - } - uart_send ( 'O' ); - uart_send ( 'K' ); - - // read the kernel - while ( size-- ) - *kernel++ = uart_getc ( ); - - // restore arguments and jump to the new kernel. - asm volatile( - "mov x0, x10;" - "mov x1, x11;" - "mov x2, x12;" - "mov x3, x13;" - // we must force an absolute address to branch to - "mov x30, 0x80000; ret" ); -} diff --git a/loader/src/mbox.c b/loader/src/mbox.c deleted file mode 100644 index d3aff2cf..00000000 --- a/loader/src/mbox.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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. - * - */ - -#include "gpio.h" - -/* mailbox message buffer */ -volatile unsigned int __attribute__ ( ( aligned ( 16 ) ) ) mbox[8]; - -#define VIDEOCORE_MBOX ( MMIO_BASE + 0x0000B880 ) -#define MBOX_READ ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x0 ) ) -#define MBOX_POLL ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x10 ) ) -#define MBOX_SENDER ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x14 ) ) -#define MBOX_STATUS ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x18 ) ) -#define MBOX_CONFIG ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x1C ) ) -#define MBOX_WRITE ( (volatile unsigned int *) ( VIDEOCORE_MBOX + 0x20 ) ) -#define MBOX_RESPONSE 0x80000000 -#define MBOX_FULL 0x80000000 -#define MBOX_EMPTY 0x40000000 - -/** - * Make a mailbox call. Returns 0 on failure, non-zero on success - */ -int mbox_call ( unsigned char ch ) -{ - unsigned int r = ( ( (unsigned int) ( (unsigned long) &mbox ) & ~0xF ) | ( ch & 0xF ) ); - /* wait until we can write to the mailbox */ - do - { - asm volatile( "nop" ); - } while ( *MBOX_STATUS & MBOX_FULL ); - /* write the address of our message to the mailbox with channel identifier */ - *MBOX_WRITE = r; - /* now wait for the response */ - while ( 1 ) - { - /* is there a response? */ - do - { - asm volatile( "nop" ); - } while ( *MBOX_STATUS & MBOX_EMPTY ); - /* is it a response to our message? */ - if ( r == *MBOX_READ ) - /* is it a valid successful response? */ - return mbox[1] == MBOX_RESPONSE; - } - return 0; -} diff --git a/loader/src/mbox.h b/loader/src/mbox.h deleted file mode 100644 index d90e3e05..00000000 --- a/loader/src/mbox.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - * - */ - -/* a properly aligned buffer */ -extern volatile unsigned int mbox[36]; - -#define MBOX_REQUEST 0 - -/* channels */ -#define MBOX_CH_POWER 0 -#define MBOX_CH_FB 1 -#define MBOX_CH_VUART 2 -#define MBOX_CH_VCHIQ 3 -#define MBOX_CH_LEDS 4 -#define MBOX_CH_BTNS 5 -#define MBOX_CH_TOUCH 6 -#define MBOX_CH_COUNT 7 -#define MBOX_CH_PROP 8 - -/* tags */ -#define MBOX_TAG_GETSERIAL 0x10004 -#define MBOX_TAG_SETCLKRATE 0x38002 -#define MBOX_TAG_LAST 0 - -int mbox_call ( unsigned char ch ); diff --git a/loader/src/start.S b/loader/src/start.S deleted file mode 100644 index dc88415d..00000000 --- a/loader/src/start.S +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - * - */ - -.section ".text.boot" - -.global _start - -_start: - // save arguments in registers (we will need them later for the new kernel) - // I choosed x10-x13 because instructions generated from C by gcc does not - // touch them. You can check that with "aarch64-elf-objdump -d kernel8.elf" - mov x10, x0 - mov x11, x1 - mov x12, x2 - mov x13, x3 - - // read cpu id, stop slave cores - mrs x1, mpidr_el1 - and x1, x1, #3 - cbz x1, 2f - // cpu id > 0, stop -1: wfe - b 1b -2: // cpu id == 0 - - // relocate our code from load address to link address - ldr x1, =0x80000 - ldr x2, =_start - ldr w3, =__loader_size -1: ldr x4, [x1], #8 - str x4, [x2], #8 - sub w3, w3, #1 - cbnz w3, 1b - - // set stack before our code - ldr x1, =_start - mov sp, x1 - - // clear bss - ldr x1, =__bss_start - ldr w2, =__bss_size -3: cbz w2, 4f - str xzr, [x1], #8 - sub w2, w2, #1 - cbnz w2, 3b - - // jump to relocated C code, should not return -4: bl main-1024 - // for failsafe, halt this core too - b 1b diff --git a/loader/src/uart.c b/loader/src/uart.c deleted file mode 100644 index 073e68ce..00000000 --- a/loader/src/uart.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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. - * - */ - -#include "gpio.h" -#include "mbox.h" - -/* PL011 UART registers */ -#define UART0_DR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201000 ) ) -#define UART0_FR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201018 ) ) -#define UART0_IBRD ( (volatile unsigned int *) ( MMIO_BASE + 0x00201024 ) ) -#define UART0_FBRD ( (volatile unsigned int *) ( MMIO_BASE + 0x00201028 ) ) -#define UART0_LCRH ( (volatile unsigned int *) ( MMIO_BASE + 0x0020102C ) ) -#define UART0_CR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201030 ) ) -#define UART0_IMSC ( (volatile unsigned int *) ( MMIO_BASE + 0x00201038 ) ) -#define UART0_ICR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201044 ) ) - -/** - * Set baud rate and characteristics (115200 8N1) and map to GPIO - */ -void uart_init ( ) -{ - register unsigned int r; - - /* initialize UART */ - *UART0_CR = 0; // turn off UART0 - - /* set up clock for consistent divisor values */ - mbox[0] = 9 * 4; - mbox[1] = MBOX_REQUEST; - mbox[2] = MBOX_TAG_SETCLKRATE; // set clock rate - mbox[3] = 12; - mbox[4] = 8; - mbox[5] = 2; // UART clock - mbox[6] = 4000000; // 4Mhz - mbox[7] = 0; // clear turbo - mbox[8] = MBOX_TAG_LAST; - mbox_call ( MBOX_CH_PROP ); - - /* map UART0 to GPIO pins */ - r = *GPFSEL1; - r &= ~( ( 7 << 12 ) | ( 7 << 15 ) ); // gpio14, gpio15 - r |= ( 4 << 12 ) | ( 4 << 15 ); // alt0 - *GPFSEL1 = r; - *GPPUD = 0; // enable pins 14 and 15 - r = 150; - while ( r-- ) - { - asm volatile( "nop" ); - } - *GPPUDCLK0 = ( 1 << 14 ) | ( 1 << 15 ); - r = 150; - while ( r-- ) - { - asm volatile( "nop" ); - } - *GPPUDCLK0 = 0; // flush GPIO setup - - *UART0_ICR = 0x7FF; // clear interrupts - *UART0_IBRD = 2; // 115200 baud - *UART0_FBRD = 0xB; - *UART0_LCRH = 0b11 << 5; // 8n1 - *UART0_CR = 0x301; // enable Tx, Rx, FIFO -} - -/** - * Send a character - */ -void uart_send ( unsigned int c ) -{ - /* wait until we can send */ - do - { - asm volatile( "nop" ); - } while ( *UART0_FR & 0x20 ); - /* write the character to the buffer */ - *UART0_DR = c; -} - -/** - * Receive a character - */ -char uart_getc ( ) -{ - /* wait until something is in the buffer */ - do - { - asm volatile( "nop" ); - } while ( *UART0_FR & 0x10 ); - /* read it and return */ - return (char) ( *UART0_DR ); -} diff --git a/loader/src/uart.h b/loader/src/uart.h deleted file mode 100644 index 8e54a463..00000000 --- a/loader/src/uart.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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. - * - */ - -void uart_init ( ); -void uart_send ( unsigned int c ); -char uart_getc ( ); -void uart_puts ( char * s ); -void uart_hex ( unsigned int d );