├── .gitignore ├── 00_crosscompiler ├── OLVASSEL.md └── README.md ├── 01_bareminimum ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── kernel8.img ├── link.ld └── start.S ├── 02_multicorec ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── kernel8.img ├── link.ld ├── main.c └── start.S ├── 03_uart1 ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── start.S ├── uart.c └── uart.h ├── 04_mailboxes ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 05_uart0 ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 06_random ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── rand.c ├── rand.h ├── start.S ├── uart.c └── uart.h ├── 07_delays ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 08_power ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── power.c ├── power.h ├── start.S ├── uart.c └── uart.h ├── 09_framebuffer ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── homer.h ├── kernel8.img ├── lfb.c ├── lfb.h ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 0A_pcscreenfont ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── font.psf ├── font.sfn ├── gpio.h ├── kernel8.img ├── lfb.c ├── lfb.h ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 0B_readsector ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sd.c ├── sd.h ├── start.S ├── uart.c └── uart.h ├── 0C_directory ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── fat.c ├── fat.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sd.c ├── sd.h ├── start.S ├── uart.c └── uart.h ├── 0D_readfile ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── fat.c ├── fat.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sd.c ├── sd.h ├── start.S ├── uart.c └── uart.h ├── 0E_initrd ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── initrd.c ├── initrd.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 0F_executionlevel ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 10_virtualmemory ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── mmu.c ├── mmu.h ├── start.S ├── uart.c └── uart.h ├── 11_exceptions ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── exc.c ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── mmu.c ├── mmu.h ├── start.S ├── uart.c └── uart.h ├── 12_printf ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sprintf.c ├── sprintf.h ├── start.S ├── uart.c └── uart.h ├── 13_debugger ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── dbg.c ├── dbg.h ├── disasm.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sprintf.c ├── sprintf.h ├── start.S ├── uart.c └── uart.h ├── 14_raspbootin64 ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── start.S ├── uart.c └── uart.h ├── 15_writesector ├── Makefile ├── Makefile.clang ├── Makefile.gcc ├── OLVASSEL.md ├── README.md ├── delays.c ├── delays.h ├── gpio.h ├── kernel8.img ├── link.ld ├── main.c ├── mbox.c ├── mbox.h ├── sd.c ├── sd.h ├── start.S ├── uart.c └── uart.h ├── LICENSE ├── OLVASSEL.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | -------------------------------------------------------------------------------- /01_bareminimum/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /01_bareminimum/Makefile.clang: -------------------------------------------------------------------------------- 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 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a53+nosimd 27 | 28 | all: clean kernel8.img 29 | 30 | start.o: start.S 31 | clang --target=aarch64-elf $(CFLAGS) -c start.S -o start.o 32 | 33 | kernel8.img: start.o 34 | ld.lld -m aarch64elf -nostdlib start.o -T link.ld -o kernel8.elf 35 | llvm-objcopy -O binary kernel8.elf kernel8.img 36 | 37 | clean: 38 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 39 | 40 | run: 41 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 42 | -------------------------------------------------------------------------------- /01_bareminimum/Makefile.gcc: -------------------------------------------------------------------------------- 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 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 27 | 28 | all: clean kernel8.img 29 | 30 | start.o: start.S 31 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 32 | 33 | kernel8.img: start.o 34 | aarch64-elf-ld -nostdlib -nostartfiles start.o -T link.ld -o kernel8.elf 35 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 36 | 37 | clean: 38 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 39 | 40 | run: 41 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 42 | -------------------------------------------------------------------------------- /01_bareminimum/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 01 - A legszükségesebbek 2 | ==================================== 3 | 4 | Rendben, nem fogunk semmi érdekeset csinálni, csak kipróbáljuk a környezetet. A keletkezett kernel8.img-nek 5 | be kell tudnia bootolni a Raspberry Pi-n, ahol végtelen ciklusban várakoztatja a CPU magokat. Ezt ellenőrizheted a 6 | következő paranccsal: 7 | 8 | ```sh 9 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 10 | ... kimenet törölve az átláthatóság miatt, utolsó sor: ... 11 | 0x0000000000080004: 17ffffff b #-0x4 (addr 0x80000) 12 | ``` 13 | 14 | Start 15 | ----- 16 | 17 | Amikor a vezérlés átadódik a kernel8.img-nek, a környezet még nem alkalmas C kód futtatására. Ezért mindenképp 18 | szükség van egy kis Assembly bevezetőre. Mivel ez az első oktatóanyag nagyon egyszerű, nem is lesz más, nincs 19 | még C kódunk. 20 | 21 | Ne feledkezzünk meg róla, hogy 4 CPU magunk van. Most mind ugyanazt a végtelen ciklust hajtja végre. 22 | 23 | 24 | Makefile 25 | -------- 26 | 27 | A Makefile-unk végtelenü legyszerű. Lefordítjuk a start.S-t, ez az egyetlen forrás fájlunk. Aztán a szerkesztési 28 | fázisban a linker.ld szrkript segítségével linkeljük. Végezetül pedig a keletkező elf futtahatót nyers programfájllá 29 | kovertáljuk. 30 | 31 | Linker szkript 32 | -------------- 33 | 34 | Nem túl meglepő módon ez is egyszerű. Be kell állítanunk a bázis címet, ahová a kernel8.img töltődik, és mindent 35 | ide rakunk, mivel csak egy szekciónk van. Fontos megjegyezni, hogy AArch64 kód esetén a betöltési cím **0x80000** 36 | és nem **0x8000**, mint AArch32 esetében. 37 | 38 | -------------------------------------------------------------------------------- /01_bareminimum/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 01 - Bare Minimum 2 | ========================== 3 | 4 | Okay, we're not going to do anything here, just test our toolchain. The resulting kernel8.img should 5 | boot on Raspberry Pi, and stop the CPU cores in an infinite loop. You can check that by running 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 9 | ... output removed for clarity, last line: ... 10 | 0x0000000000080004: 17ffffff b #-0x4 (addr 0x80000) 11 | ``` 12 | 13 | Start 14 | ----- 15 | 16 | When the control is passed to kernel8.img, the environment is not ready for C. Therefore we must 17 | implement a small preamble in Assembly. As this first tutorial is very simple, that's all we have, no C 18 | for now. 19 | 20 | Note that CPU has 4 cores. All of them are running the same infinite loop for now. 21 | 22 | Makefile 23 | -------- 24 | 25 | Our Makefile is very simple. We compile start.S, as this is our only source. Then in linker phase we 26 | link it using the linker.ld script. Finally we convert the resulting elf executable into a raw image. 27 | 28 | Linker script 29 | ------------- 30 | 31 | Not surpisingly simple too. We just set the base address where our kernel8.img will be loaded, and we 32 | put the only section we have there. Important note, for AArch64 the load address is **0x80000**, and 33 | not **0x8000** as with AArch32. 34 | 35 | -------------------------------------------------------------------------------- /01_bareminimum/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/01_bareminimum/kernel8.img -------------------------------------------------------------------------------- /01_bareminimum/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { *(.text.boot) } 30 | 31 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 32 | } 33 | -------------------------------------------------------------------------------- /01_bareminimum/start.S: -------------------------------------------------------------------------------- 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 | .section ".text.boot" 27 | 28 | .global _start 29 | 30 | _start: 31 | 1: wfe 32 | b 1b 33 | -------------------------------------------------------------------------------- /02_multicorec/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /02_multicorec/Makefile.clang: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -mcpu=cortex-a53+nosimd 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | clang --target=aarch64-elf $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | clang --target=aarch64-elf $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | ld.lld -m aarch64elf -nostdlib start.o $(OBJS) -T link.ld -o kernel8.elf 40 | llvm-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 47 | -------------------------------------------------------------------------------- /02_multicorec/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -d in_asm 47 | -------------------------------------------------------------------------------- /02_multicorec/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/02_multicorec/kernel8.img -------------------------------------------------------------------------------- /02_multicorec/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /02_multicorec/main.c: -------------------------------------------------------------------------------- 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 | void main() 27 | { 28 | while(1); 29 | } 30 | -------------------------------------------------------------------------------- /03_uart1/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /03_uart1/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio 47 | -------------------------------------------------------------------------------- /03_uart1/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 03 - UART1, Auxilary mini UART 2 | ========================================== 3 | 4 | Ezúttal a hírhedt Helló Világ példát vesszük elő. Előbb az UART1-re írjuk meg, mivel azt egyszerűbb programozni, 5 | mivel fix órafrekvenciája van. 6 | 7 | FIGYELEM: qemu nem irányítja át alapból az UART1-et a terminálra, csak az UART0-át, ezért `-serial null -serial stdio`-t használj. 8 | 9 | Gpio.h 10 | ------ 11 | 12 | Van egy új fejléc fájlunk. Ebben definiáljuk az MMIO címét, és a GPIO vezérlő szavainak címeit. Ez egy nagyon 13 | népszerű fejléc lesz, majd minden eszközhöz kelleni fog. 14 | 15 | Uart.h, uart.c 16 | -------------- 17 | 18 | Egy nagyon minimális változat. 19 | 20 | `uart_init()` inicializálja az UART csipet, és soros vonalat leképezi a GPIO lábakra. 21 | 22 | `uart_send(c)` kiküld egy karatert a soros vonalra. 23 | 24 | `uart_getc()` fogad egy karatert. A kocsivissza karakter (13) automatikusan újsor karakterré (10) konvertálódik. 25 | 26 | `uart_puts(s)` kiír egy szöveget. Újsor karakternél kiküld egy kocsivissza karatert is (13 + 10). 27 | 28 | Main 29 | ---- 30 | 31 | Először is meg kell hívni az uart inicializáló kódját. Aztán kiküldjük, "Helló Világ!". Ha beszereztél USB 32 | soros kábelt, akkor ennek meg kell jelennie a minicom ablakában. Ezután minden, minicom-ban leütött karaktert 33 | visszaküld és kiír. Ha nem kapcsoltad ki a helyi visszhangot (local echo), akkor ez azt jelenti, hogy minden 34 | leütött karaktert duplán fog kiírni a minicom. 35 | 36 | -------------------------------------------------------------------------------- /03_uart1/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 03 - UART1, Auxilary mini UART 2 | ======================================= 3 | 4 | It is time for the famous Hello World example. We're going to write on the UART1 first, as it's easier to program 5 | as it has a fixed clocked frequency. 6 | 7 | NOTE: qemu does not redirect UART1 to terminal by default, only UART0, so you have to use `-serial null -serial stdio`. 8 | 9 | Gpio.h 10 | ------ 11 | 12 | We have a new header file. This defines the base MMIO address, and the GPIO controller's addresses. This file 13 | going to be very popular, as many devices need it. 14 | 15 | Uart.h, uart.c 16 | -------------- 17 | 18 | A very minimal implementation. 19 | 20 | `uart_init()` initializes the device and maps it to the GPIO ports. 21 | 22 | `uart_send(c)` sends a character over the serial line. 23 | 24 | `uart_getc()` receives a character. The carrige return character (13) will be converted into a newline character (10). 25 | 26 | `uart_puts(s)` prints out a string. On newline, a carrige return character will also be sent (13 + 10). 27 | 28 | Main 29 | ---- 30 | 31 | First, we have to call the uart initialization code. Then, it'll return "Hello World!". If you've purchased an USB 32 | serial cable, you should see it on minicom's screen. After that every character typed in minicom will be 33 | echoed back. If you haven't turned off local echo, that means you'll see every pressed key twice. 34 | 35 | -------------------------------------------------------------------------------- /03_uart1/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/03_uart1/kernel8.img -------------------------------------------------------------------------------- /03_uart1/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /03_uart1/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | 28 | void main() 29 | { 30 | // set up serial console 31 | uart_init(); 32 | 33 | // say hello 34 | uart_puts("Hello World!\n"); 35 | 36 | // echo everything back 37 | while(1) { 38 | uart_send(uart_getc()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /03_uart1/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | -------------------------------------------------------------------------------- /04_mailboxes/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /04_mailboxes/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio 47 | -------------------------------------------------------------------------------- /04_mailboxes/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 04 - Levelesládák 2 | ============================= 3 | 4 | Mielőtt nekiugranánk az UART0-ának, szükségünk lesz a levelesládára. Ezért ebben az oktatóanyagban bemutatom a 5 | mailbox interfészt. Arra használjuk, hogy lekérdezzük az alaplap egyedi sorszámát, majd kiírjuk azt. 6 | 7 | FIGYELEM: qemu nem irányítja át alapból az UART1-et a terminálra, csak az UART0-át, ezért `-serial null -serial stdio`-t használj. 8 | 9 | Uart.h, uart.c 10 | -------------- 11 | 12 | `uart_hex(d)` kiír egy bináris értéket hexadecimális formátumban. 13 | 14 | Mbox.h, mbox.c 15 | -------------- 16 | 17 | A levelesláda interfésze. Először értékekkel feltöltjük az `mbox` tömböt, aztán meghívjuk a `mbox_call(ch)`-t, 18 | hogy értesüljön róla a GPU, megadva közben a levelesláda csatornáját. 19 | Ebben a példában a [property csatornát](https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface) 20 | használtam, aminek az üzenete a következőképp néz ki: 21 | 22 | ``` 23 | 0. üzenet teljes hossza bájtban, (x+1)*4 24 | 1. MBOX_REQUEST mágikus szám, kérés típusú üzenetet jelent 25 | 2-x. parancsok 26 | x+1. MBOX_TAG_LAST mágikus szám, nincs további parancs jelölése 27 | ``` 28 | 29 | Ahol minden egyes parancs szerkezete a következő: 30 | 31 | ``` 32 | n+0. parancs azonosító 33 | n+1. adatterület mérete bájtban 34 | n+2. nulla 35 | n+3. opcionális adatterület 36 | ``` 37 | 38 | Main 39 | ---- 40 | 41 | Lekérjük az alaplap egyedi szériaszámát, majd kiírjuk a soros konzolra. 42 | -------------------------------------------------------------------------------- /04_mailboxes/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 04 - Mailboxes 2 | ======================= 3 | 4 | Before we could go on with UART0, we need mailboxes. So in this tutorial we introduce the mailbox interface. 5 | We'll use it to query the board's serial number and print that out on UART1. 6 | 7 | NOTE: qemu does not redirect UART1 to terminal by default, only UART0, so you have to use `-serial null -serial stdio`. 8 | 9 | Uart.h, uart.c 10 | -------------- 11 | 12 | `uart_hex(d)` prints out a binary value in hexadecimal format. 13 | 14 | Mbox.h, mbox.c 15 | -------------- 16 | 17 | The mailbox interface. First we fill up the message in `mbox` array, then we call 18 | `mbox_call(ch)` to pass it to the GPU, specifying the mailbox channel. 19 | In this example we have used the [property channel](https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface), 20 | which requires the message to be formatted as: 21 | 22 | ``` 23 | 0. size of the message in bytes, (x+1)*4 24 | 1. MBOX_REQUEST magic value, indicates request message 25 | 2-x. tags 26 | x+1. MBOX_TAG_LAST magic value, indicates no more tags 27 | ``` 28 | 29 | Where each tag looks like: 30 | 31 | ``` 32 | n+0. tag identifier 33 | n+1. value buffer size in bytes 34 | n+2. must be zero 35 | n+3. optional value buffer 36 | ``` 37 | 38 | Main 39 | ---- 40 | 41 | We query the board's serial number and then we display it on the serial console. 42 | -------------------------------------------------------------------------------- /04_mailboxes/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/04_mailboxes/kernel8.img -------------------------------------------------------------------------------- /04_mailboxes/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /04_mailboxes/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_LAST 0 45 | 46 | int mbox_call(unsigned char ch); 47 | -------------------------------------------------------------------------------- /04_mailboxes/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /05_uart0/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /05_uart0/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /05_uart0/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 05 - UART0, PL011 2 | ============================= 3 | 4 | Ebben az okatatóanyagban ugyanazt csináljuk, mint a 4-esben, de most a szériaszámot az UART0-ra küldjük ki. 5 | Emiatt ez a példa könnyen használható qemu-val is: 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 9 | My serial number is: 0000000000000000 10 | ``` 11 | 12 | Uart.h, uart.c 13 | -------------- 14 | 15 | Mielőtt a frekvenciaosztót megadhatnánk, be kell állítanunk egy fix órajelet a PL011 csipben. Ezt levelesládán 16 | keresztül tehetjük meg, ugyanazon a property csatornán keresztül, amit már korábban is használtunk. Ettől eltekintve 17 | ez az interfész teljesen azonos az UART1-ével. 18 | 19 | Main 20 | ---- 21 | 22 | Lekérjük az alaplap egyedi szériaszámát, majd kiírjuk a soros konzolra. 23 | -------------------------------------------------------------------------------- /05_uart0/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 05 - UART0, PL011 2 | ========================== 3 | 4 | This tutorial does the same as tutorial 04, but it prints the serial number on UART0. As such, it can be used 5 | easily with qemu, like 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 9 | My serial number is: 0000000000000000 10 | ``` 11 | 12 | Uart.h, uart.c 13 | -------------- 14 | 15 | Before we could use a rate divisor value, we must establish a valid clock rate for the PL011. It's done 16 | via mailboxes, with the same property channel we used earlier. Otherwise this interface is identical to the 17 | UART1 one. 18 | 19 | Main 20 | ---- 21 | 22 | We query the board's serial number and then we display it on the serial console. 23 | -------------------------------------------------------------------------------- /05_uart0/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/05_uart0/kernel8.img -------------------------------------------------------------------------------- /05_uart0/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /05_uart0/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /05_uart0/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /06_random/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /06_random/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /06_random/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 06 - Hardveres Véletlenszám Generátor 2 | ================================================= 3 | 4 | Ez egy egyszerű okatatóanyag lesz. Lekérjük az aktuális értéket (az egyéként nem dokumentált) 5 | hardveres véletlenszám generátorból. Ez arra használható többek között, hogy egy egyszerű, megfelelő 6 | kockadobást szimuláljunk bármilyen játékban. Ez azért fontos, mert hardveres támogatás nélkül csak 7 | kizárólag pszeudo-véletlen állítható elő. 8 | 9 | Rand.h, rand.c 10 | -------------- 11 | 12 | `rand_init()` inicializálja a hardvert. 13 | 14 | `rand(min,max)` visszaad egy min és max közötti véletlen számot. 15 | 16 | Main 17 | ---- 18 | 19 | Lekérjük a véletlen számot, és kiírjuk a soros konzolra. 20 | -------------------------------------------------------------------------------- /06_random/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 06 - Hardware Random Number Generator 2 | ============================================== 3 | 4 | This going to be an easy tutorial. We query a number from the (undocumented) hardware random 5 | number generator. You can use this to implement a simple, but accurate dice throw in any game. 6 | It is important as without hardware support you can only generate pseudo-random numbers. 7 | 8 | Rand.h, rand.c 9 | -------------- 10 | 11 | `rand_init()` initializes the hardware. 12 | 13 | `rand(min,max)` returns a random number between min and max. 14 | 15 | Main 16 | ---- 17 | 18 | We query a random value and then we display it on the serial console. 19 | -------------------------------------------------------------------------------- /06_random/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/06_random/kernel8.img -------------------------------------------------------------------------------- /06_random/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /06_random/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "rand.h" 28 | 29 | void main() 30 | { 31 | // set up serial console and random number generator 32 | uart_init(); 33 | rand_init(); 34 | 35 | uart_puts("Here goes a random number: "); 36 | uart_hex(rand(0,4294967295)); 37 | uart_puts("\n"); 38 | 39 | // echo everything back 40 | while(1) { 41 | uart_send(uart_getc()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /06_random/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETCLKRATE 0x38002 44 | #define MBOX_TAG_LAST 0 45 | 46 | int mbox_call(unsigned char ch); 47 | -------------------------------------------------------------------------------- /06_random/rand.h: -------------------------------------------------------------------------------- 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 | void rand_init(); 27 | unsigned int rand(unsigned int min, unsigned int max); 28 | -------------------------------------------------------------------------------- /06_random/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /07_delays/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /07_delays/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /07_delays/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 07 - Késleltetések 2 | ============================== 3 | 4 | Roppant fontos, hogy a megfelelő időtartamot késleltessünk, amikor alacsony szintű hardverrel bánunk. 5 | Ebben az okatatóanyagban három megközelítést nézünk meg. Az egyik CPU órajel függő (akkor hasznos, ha 6 | a várakozási idő órajelciklusban van megadva), a másik kettő mikroszekundum (másodperc milliomod része) alapú. 7 | 8 | Delays.h, delays.c 9 | ------------------ 10 | 11 | `wait_cycles(n)` ez nagyon faék, n-szer lefuttatjuk a `nop` (nincs utasítás) utasítást. 12 | 13 | `wait_msec(n)` ez a megvalósítás ARM rendszer regisztereket használ (minden AArch64 CPU-n elérhető). 14 | 15 | `wait_msec_st(n)` ez pedig BCM specifikus, ami a Rendszer Időzítő perifériát használja (nincs emulálva qemu-n). 16 | 17 | Main 18 | ---- 19 | 20 | Különböző implementációkkal várakozunk a konzolra írások között. 21 | -------------------------------------------------------------------------------- /07_delays/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 07 - Delays 2 | ==================== 3 | 4 | It is very important to wait precise amounts of time while you're interfacing with low level hardware. 5 | In this tutorial we'll cover there ways. One is CPU frequency dependent (and useful if wait time is given 6 | in CPU clock cycles), the other two are microsec (millionth of a second) based. 7 | 8 | Delays.h, delays.c 9 | ------------------ 10 | 11 | `wait_cycles(n)` this is a very straightforward thing, we execute the 'nop' instruction n times. 12 | 13 | `wait_msec(n)` this implementation uses ARM system registers (available on all AArch64 CPUs). 14 | 15 | `wait_msec_st(n)` is a BCM specific implementation, which uses the System Timer peripheral (not available on qemu). 16 | 17 | Main 18 | ---- 19 | 20 | We use different wait implementations between printing strings on serial console. 21 | -------------------------------------------------------------------------------- /07_delays/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /07_delays/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/07_delays/kernel8.img -------------------------------------------------------------------------------- /07_delays/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /07_delays/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETCLKRATE 0x38002 44 | #define MBOX_TAG_LAST 0 45 | 46 | int mbox_call(unsigned char ch); 47 | -------------------------------------------------------------------------------- /07_delays/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /08_power/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /08_power/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /08_power/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 08 - Energiagazdálkodás 2 | =================================== 3 | 4 | Beépített rendszerek esetén nagyon fontos az energiafogyasztás. A Raspberry Pi 3-on ezért roppant szofisztikált 5 | interfészt találunk. Az egyes eszközöket külön-külön ki be kapcsolgathatjuk. Van egy hátulütő azonban, a GPIO 6 | VCC lábai direktbe vannak kötve az áramforrásra, magyarán nem lehet programból vezérelni őket. Ez azt jelenti, 7 | ha eszközöket kötsz rá, akkor neked kell megoldanod ezen eszközök kikapcsolását (mondjuk egy tranzisztorral, 8 | amit egy másik GPIO adatláb vezérel). 9 | 10 | Power.h, power.c 11 | ---------------- 12 | 13 | Az energiagazdálkodás az egyik periféria, amit a qemu egyáltalán nem emulál. Igazi vason szépen megy. 14 | 15 | `power_off()` leállítja az alaplapot egy, majdnem nulla energiafogyasztási szintre. 16 | 17 | `reset()` újraindítja a gépet. Ezt is a PMC kezeli, és mivel nincs a Raspberry-n fizikai reset gomb, roppant 18 | hasznos tud lenni. 19 | 20 | Main 21 | ---- 22 | 23 | Megjelenítünk egy egyszerű menüt, majd várunk a felhasználóra. A válaszától függően vagy újraindítjuk a gépet, 24 | vagy kikapcsoljuk. 25 | -------------------------------------------------------------------------------- /08_power/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 08 - Power management 2 | ============================== 3 | 4 | For embedded systems, power consumption is critical. The Raspberry Pi 3 has a very sophisticated 5 | PM interface. You can turn each device on and off idependently. There's a catch though, the GPIO 6 | VCC pins are hardwired, there's no way to turn them off programatically. This means if you connect 7 | some devices to them, you'll have to implement a way to turn those devices off (with a transistor 8 | connected to a data GPIO pin for example). 9 | 10 | Power.h, power.c 11 | ---------------- 12 | 13 | The power management controller is one of the peripherals that are not emulated properly by qemu. 14 | Works on real hardware though. 15 | 16 | `power_off()` shutdowns the board to a almost zero power consumption state. 17 | 18 | `reset()` reboots the machine. Also handled by the PMC, and since the Raspberry Pi does not have 19 | a hardware reset button, it's very useful. 20 | 21 | Main 22 | ---- 23 | 24 | We display a simple menu, and wait for user input. Depending on the input, we reboot the system or 25 | power it off. 26 | -------------------------------------------------------------------------------- /08_power/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /08_power/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/08_power/kernel8.img -------------------------------------------------------------------------------- /08_power/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /08_power/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "power.h" 28 | 29 | void main() 30 | { 31 | char c; 32 | 33 | // set up serial console 34 | uart_init(); 35 | 36 | while(1) { 37 | uart_puts(" 1 - power off\n 2 - reset\nChoose one: "); 38 | c=uart_getc(); 39 | uart_send(c); 40 | uart_puts("\n\n"); 41 | if(c=='1') power_off(); 42 | if(c=='2') reset(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /08_power/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /08_power/power.h: -------------------------------------------------------------------------------- 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 | void power_off(); 27 | void reset(); 28 | -------------------------------------------------------------------------------- /08_power/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /09_framebuffer/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /09_framebuffer/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /09_framebuffer/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/09_framebuffer/kernel8.img -------------------------------------------------------------------------------- /09_framebuffer/lfb.h: -------------------------------------------------------------------------------- 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 | void lfb_init(); 27 | void lfb_showpicture(); 28 | -------------------------------------------------------------------------------- /09_framebuffer/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /09_framebuffer/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "lfb.h" 28 | 29 | void main() 30 | { 31 | // set up serial console and linear frame buffer 32 | uart_init(); 33 | lfb_init(); 34 | 35 | // display a pixmap 36 | lfb_showpicture(); 37 | 38 | // echo everything back 39 | while(1) { 40 | uart_send(uart_getc()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /09_framebuffer/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /09_framebuffer/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /0A_pcscreenfont/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0A_pcscreenfont/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /0A_pcscreenfont/font.psf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0A_pcscreenfont/font.psf -------------------------------------------------------------------------------- /0A_pcscreenfont/font.sfn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0A_pcscreenfont/font.sfn -------------------------------------------------------------------------------- /0A_pcscreenfont/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0A_pcscreenfont/kernel8.img -------------------------------------------------------------------------------- /0A_pcscreenfont/lfb.h: -------------------------------------------------------------------------------- 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 | void lfb_init(); 27 | void lfb_print(int x, int y, char *s); 28 | void lfb_proprint(int x, int y, char *s); 29 | -------------------------------------------------------------------------------- /0A_pcscreenfont/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0A_pcscreenfont/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "lfb.h" 28 | 29 | void main() 30 | { 31 | // set up serial console and linear frame buffer 32 | uart_init(); 33 | lfb_init(); 34 | 35 | // display an ASCII string on screen with PSF 36 | lfb_print(80, 80, "Hello World!"); 37 | 38 | // display a UTF-8 string on screen with SSFN 39 | lfb_proprint(80, 120, "Hello 多种语言 Многоязычный többnyelvű World!"); 40 | 41 | // echo everything back 42 | while(1) { 43 | uart_send(uart_getc()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0A_pcscreenfont/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0A_pcscreenfont/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /0B_readsector/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0B_readsector/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 0B - Szektor Beolvasás 2 | ================================== 3 | 4 | Ezidáig minden adatot (kép, font) hozzálinkeltünk a kernelhez. Itt az ideje, hogy adatot olvassunk be 5 | az SD kártyáról. Ebben az oktatóanyagban egy igazi meghajtót implementálunk a szektor beolvasásához. 6 | 7 | Sd.h, sd.c 8 | ------------ 9 | 10 | Nos, jó lenne, ha lenne levelesláda szektorok olvasására és írására, de nincs. Ezért nekünk kell direktben 11 | beszélni az EMMC-vel, ami trükkös és unalmas feladat. Különböző kártyákkal is törődnünk kell. De végül, 12 | rendelkezésre áll két funkció. 13 | 14 | `sd_init()` inicializálja az EMMC-t SD kártya olvasáshoz. 15 | 16 | `sd_readblock(lba,buffer,num)` beolvas num blokkot (szektort) az SD kártyáról a buffer-be lba-tól kezdve. 17 | 18 | Main 19 | ---- 20 | 21 | A blokkot a bss szegmens utánra töltjük be, majd kidumpoljuk a konzolra. A beolvasás funkció részletes 22 | üzeneteket ír ki arról, hogy épp mit kommunikál az EMMC vezérlővel. 23 | -------------------------------------------------------------------------------- /0B_readsector/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 0B - Read Sector 2 | ========================= 3 | 4 | So far we have linked our data (pixmap, font) to the kernel image. It is time to read data from the 5 | SD card. For this tutorial we're implementing a real driver for read sector function. 6 | 7 | Sd.h, sd.c 8 | ------------ 9 | 10 | Well, it would be nice to have a mailbox for reading and writing sectors, but there isn't. So we have to 11 | talk directly to the EMMC, which is tricky and boring. We have to handle all kinds of cards. But finally, 12 | we have two functions. 13 | 14 | `sd_init()` initialize EMMC for SD card read. 15 | 16 | `sd_readblock(lba,buffer,num)` read num blocks (sectors) from the SD card into buffer starting at sector lba. 17 | 18 | Main 19 | ---- 20 | 21 | We read a block after the bss segment in memory, and then we dump it to the console. The read function will 22 | display detailed information on the EMMC communication. 23 | -------------------------------------------------------------------------------- /0B_readsector/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /0B_readsector/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0B_readsector/kernel8.img -------------------------------------------------------------------------------- /0B_readsector/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0B_readsector/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "sd.h" 28 | 29 | // get the end of bss segment from linker 30 | extern unsigned char _end; 31 | 32 | void main() 33 | { 34 | // set up serial console 35 | uart_init(); 36 | 37 | // initialize EMMC and detect SD card type 38 | if(sd_init()==SD_OK) { 39 | // read the master boot record after our bss segment 40 | if(sd_readblock(0,&_end,1)) { 41 | // dump it to serial console 42 | uart_dump(&_end); 43 | } 44 | } 45 | 46 | // echo everything back 47 | while(1) { 48 | uart_send(uart_getc()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0B_readsector/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0B_readsector/sd.h: -------------------------------------------------------------------------------- 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 SD_OK 0 27 | #define SD_TIMEOUT -1 28 | #define SD_ERROR -2 29 | 30 | int sd_init(); 31 | int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num); 32 | -------------------------------------------------------------------------------- /0B_readsector/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | void uart_dump(void *ptr); 32 | -------------------------------------------------------------------------------- /0C_directory/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0C_directory/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 0C - Könyvtárak 2 | =========================== 3 | 4 | Most hogy már tudunk szektort beolvasni, ideje értelmezni a fájlrendszert. Ez az oktatóanyag megtanítja, 5 | hogy hogyan listázzuk ki egy FAT16 vagy FAT32 partíció gyökérkönyvtárát. 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -drive file=test.dd,if=sd,format=raw -serial stdio 9 | ... kimenet törölve az átláthatóság miatt ... 10 | MBR disk identifier: 12345678 11 | FAT partition starts at: 00000008 12 | ... kimenet törölve az átláthatóság miatt ... 13 | FAT type: FAT16 14 | FAT number of root diretory entries: 00000200 15 | FAT root directory LBA: 00000034 16 | ... kimenet törölve az átláthatóság miatt ... 17 | Attrib Cluster Size Name 18 | ...L.. 00000000 00000000 EFI System 19 | ....D. 00000003 00000000 FOLDER 20 | .....A 00000171 0000C448 BOOTCODEBIN 21 | .....A 0000018A 000019B3 FIXUP DAT 22 | .....A 0000018E 00001B10 KERNEL8 IMG 23 | .....A 00000192 000005D6 LICENC~1BRO 24 | .....A 00000193 002B2424 START ELF 25 | ``` 26 | 27 | Fat.h, fat.c 28 | ------------ 29 | 30 | Ez elég egyszerű és jól dokumentált művelet. Beolvassuk az MBR-t, megkeressük a partíciónkat, majd 31 | betöltjük az első szektorát (Volume Boot Record). Ebben van az ún. BIOS Parameter Block, ami leírja 32 | a FAT fájlrendszert. 33 | 34 | `fat_getpartition()` betölti és értelmezi a partíció első szektorát. 35 | 36 | `fat_listdirectory()` kilistázza a fájlrendszer gyökérkönyvtárának tartalmát. 37 | 38 | Main 39 | ---- 40 | 41 | Miután inicializájuk az EMMC-t szektorok olvasásához, beolvassuk az első szektort a partícióról. Ha a BPB 42 | egy érvényes FAT fájlrendszert ír le, akkor kilistázzuk a fájlrendszer gyükérkönyvtárát. 43 | -------------------------------------------------------------------------------- /0C_directory/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 0C - Directory 2 | ======================= 3 | 4 | Now that we can load a sector from the storage, it is time to parse it as a file system. This 5 | tutorial will show you how to list the root directory entries of a FAT16 or FAT32 partition. 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -drive file=test.dd,if=sd,format=raw -serial stdio 9 | ... output removed for clarity ... 10 | MBR disk identifier: 12345678 11 | FAT partition starts at: 00000008 12 | ... output removed for clarity ... 13 | FAT type: FAT16 14 | FAT number of root diretory entries: 00000200 15 | FAT root directory LBA: 00000034 16 | ... output removed for clarity ... 17 | Attrib Cluster Size Name 18 | ...L.. 00000000 00000000 EFI System 19 | ....D. 00000003 00000000 FOLDER 20 | .....A 00000171 0000C448 BOOTCODEBIN 21 | .....A 0000018A 000019B3 FIXUP DAT 22 | .....A 0000018E 00001B10 KERNEL8 IMG 23 | .....A 00000192 000005D6 LICENC~1BRO 24 | .....A 00000193 002B2424 START ELF 25 | ``` 26 | 27 | Fat.h, fat.c 28 | ------------ 29 | 30 | This is easy and pretty well documented. We have to read the MBR, locate our partition, and load 31 | it's first sector (Volume Boot Record). That has the BIOS Parameter Block, which describes the FAT 32 | file system. 33 | 34 | `fat_getpartition()` load and check the boot record of the first MBR partition. 35 | 36 | `fat_listdirectory()` list root directory entries on the volume. 37 | 38 | Main 39 | ---- 40 | 41 | Once we initialize EMMC to read sectors, we load the boot record of the first partition. If the BPB 42 | describes a valid FAT partition, we list the root directory entries in it. 43 | -------------------------------------------------------------------------------- /0C_directory/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /0C_directory/fat.h: -------------------------------------------------------------------------------- 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 | int fat_getpartition(void); 27 | void fat_listdirectory(void); 28 | -------------------------------------------------------------------------------- /0C_directory/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0C_directory/kernel8.img -------------------------------------------------------------------------------- /0C_directory/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0C_directory/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0C_directory/sd.h: -------------------------------------------------------------------------------- 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 SD_OK 0 27 | #define SD_TIMEOUT -1 28 | #define SD_ERROR -2 29 | 30 | int sd_init(); 31 | int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num); 32 | -------------------------------------------------------------------------------- /0C_directory/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | void uart_dump(void *ptr); 32 | -------------------------------------------------------------------------------- /0D_readfile/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0D_readfile/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /0D_readfile/fat.h: -------------------------------------------------------------------------------- 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 | int fat_getpartition(void); 27 | unsigned int fat_getcluster(char *fn); 28 | char *fat_readfile(unsigned int cluster); 29 | -------------------------------------------------------------------------------- /0D_readfile/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0D_readfile/kernel8.img -------------------------------------------------------------------------------- /0D_readfile/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0D_readfile/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0D_readfile/sd.h: -------------------------------------------------------------------------------- 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 SD_OK 0 27 | #define SD_TIMEOUT -1 28 | #define SD_ERROR -2 29 | 30 | int sd_init(); 31 | int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num); 32 | -------------------------------------------------------------------------------- /0D_readfile/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | void uart_dump(void *ptr); 32 | -------------------------------------------------------------------------------- /0E_initrd/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0E_initrd/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /0E_initrd/initrd.h: -------------------------------------------------------------------------------- 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 | void initrd_list(char *buf); 27 | -------------------------------------------------------------------------------- /0E_initrd/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0E_initrd/kernel8.img -------------------------------------------------------------------------------- /0E_initrd/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0E_initrd/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "initrd.h" 28 | 29 | // import our bitchunk from rd.o 30 | extern volatile unsigned char _binary_ramdisk_start; 31 | 32 | void main() 33 | { 34 | // set up serial console 35 | uart_init(); 36 | 37 | // list contents of an archive 38 | initrd_list((char*)&_binary_ramdisk_start); 39 | 40 | // echo everything back 41 | while(1) { 42 | uart_send(uart_getc()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0E_initrd/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0E_initrd/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | void uart_dump(void *ptr); 32 | -------------------------------------------------------------------------------- /0F_executionlevel/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /0F_executionlevel/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /0F_executionlevel/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 0F - Futási szintek 2 | =============================== 3 | 4 | Mielőtt rátérhetnénk a virtuális memóriára, beszélnünk kell a futási szintekről. Minden szintnek saját 5 | lapfordító tára van, emiatt életbevágó, hogy tudjuk, melyik szinten futunk éppen. Ezért ebben az oktatóanyagban 6 | megbizonyosodunk róla, hogy rendszerfelügyeleti szinten (supervisor) azaz EL1-en vagyunk-e. Qemu alatt a gép 7 | indulhat egyből EL1-en, az igazi Raspberry Pi vason azonban általában virtualizációs szinten (hypervisor) azaz EL2-n 8 | ébredünk. Qemu alatt a szintváltást a "-d int" kapcsolóval debuggolhatjuk. 9 | 10 | ```sh 11 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio -d int 12 | Exception return from AArch64 EL2 to AArch64 EL1 PC 0x8004c 13 | Current EL is: 00000001 14 | ``` 15 | 16 | Start 17 | ----- 18 | 19 | Hozzáadtam egy pár Assembly sort, ami átállítja a futási szintet, ha nem rendszerfelügyeleti szinten lennénk. 20 | De mielőtt ezt megtehetnénk, hozzáférést kell biztosítani a számláló regiszterekhez (counter, amit a wait_msec() 21 | használ), valamint megmondjuk a CPU-nak, hogy AArch64 módban fut az EL1. Végezetül egy kivételkezelőből való 22 | visszatérést hazudunk, hogy ténylegesen szintet váltsunk. 23 | 24 | FIGYELEM: a teljesség kedvéért hozzáadtam az EL3-at is az [Issue #6](https://github.com/bztsrc/raspi3-tutorial/issues/6) 25 | miatt, bár normális körülmények között a Raspberry EL2-n futtatja a kernel8.img-t. Bizonyos config.txt parancsokkal elérhető, 26 | hogy EL3-on induljon (köszönet [@btauro](https://github.com/btauro)-nak az infóért). 27 | 28 | Main 29 | ---- 30 | 31 | Lekérjük az aktuális futási szintet, és kiírjuk a soros konzolra. 32 | -------------------------------------------------------------------------------- /0F_executionlevel/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 0F - Execution levels 2 | ============================== 3 | 4 | Before we can go on to virtual memory, we have to talk about execution levels. Each level has it's own 5 | memory translation tables, therefore it's cruital to know which one we are using. So in this tutorial we're 6 | make sure of it, we are at supervisor level, EL1. Qemu may start machine at EL1, but real Raspberry Pi hardware 7 | normally boots at hypervisor level, EL2. Under qemu use "-d int" to debug the level change. 8 | 9 | ```sh 10 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio -d int 11 | Exception return from AArch64 EL2 to AArch64 EL1 PC 0x8004c 12 | Current EL is: 00000001 13 | ``` 14 | 15 | Start 16 | ----- 17 | 18 | I've added a little bit more Assembly code for changing the execution level if we're not at supervisor level. 19 | But before we can do that, we have to grant access for the counter registers (used by wait_msec()), and tell 20 | the CPU we want AArch64 mode in EL1. Finally, we fake an exception return to change the level for real. 21 | 22 | NOTE: For completeness, I've added code for EL3 too because of [Issue #6](https://github.com/bztsrc/raspi3-tutorial/issues/6), 23 | although normally Raspberry runs kernel8.img in EL2. With some config.txt options, you can make it run in EL3 (thanks 24 | [@btauro](https://github.com/btauro) for the info). 25 | 26 | Main 27 | ---- 28 | 29 | We query the current execution level and then we display it on the serial console. 30 | -------------------------------------------------------------------------------- /0F_executionlevel/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/0F_executionlevel/kernel8.img -------------------------------------------------------------------------------- /0F_executionlevel/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /0F_executionlevel/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | 28 | void main() 29 | { 30 | unsigned long el; 31 | 32 | // set up serial console 33 | uart_init(); 34 | 35 | // read the current level from system register 36 | asm volatile ("mrs %0, CurrentEL" : "=r" (el)); 37 | 38 | uart_puts("Current EL is: "); 39 | uart_hex((el>>2)&3); 40 | uart_puts("\n"); 41 | 42 | // echo everything back 43 | while(1) { 44 | uart_send(uart_getc()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0F_executionlevel/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /0F_executionlevel/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /10_virtualmemory/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /10_virtualmemory/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /10_virtualmemory/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/10_virtualmemory/kernel8.img -------------------------------------------------------------------------------- /10_virtualmemory/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | . = ALIGN(4096); 32 | PROVIDE(_data = .); 33 | .data : { *(.data .data.* .gnu.linkonce.d*) } 34 | .bss (NOLOAD) : { 35 | . = ALIGN(16); 36 | __bss_start = .; 37 | *(.bss .bss.*) 38 | *(COMMON) 39 | __bss_end = .; 40 | } 41 | . = ALIGN(4096); 42 | _end = .; 43 | 44 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 45 | } 46 | __bss_size = (__bss_end - __bss_start)>>3; 47 | -------------------------------------------------------------------------------- /10_virtualmemory/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /10_virtualmemory/mmu.h: -------------------------------------------------------------------------------- 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 | void mmu_init(); 27 | -------------------------------------------------------------------------------- /10_virtualmemory/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /11_exceptions/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /11_exceptions/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /11_exceptions/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/11_exceptions/kernel8.img -------------------------------------------------------------------------------- /11_exceptions/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | . = ALIGN(4096); 32 | PROVIDE(_data = .); 33 | .data : { *(.data .data.* .gnu.linkonce.d*) } 34 | .bss (NOLOAD) : { 35 | . = ALIGN(16); 36 | __bss_start = .; 37 | *(.bss .bss.*) 38 | *(COMMON) 39 | __bss_end = .; 40 | } 41 | . = ALIGN(4096); 42 | _end = .; 43 | 44 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 45 | } 46 | __bss_size = (__bss_end - __bss_start)>>3; 47 | -------------------------------------------------------------------------------- /11_exceptions/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "mmu.h" 28 | 29 | void main() 30 | { 31 | unsigned int r; 32 | 33 | // set up serial console 34 | uart_init(); 35 | 36 | // set up paging 37 | mmu_init(); 38 | 39 | // generate a Data Abort with a bad address access 40 | r=*((volatile unsigned int*)0xFFFFFFFFFF000000); 41 | // make gcc happy about unused variables :-) 42 | r++; 43 | 44 | // echo everything back 45 | while(1) { 46 | uart_send(uart_getc()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /11_exceptions/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /11_exceptions/mmu.h: -------------------------------------------------------------------------------- 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 | void mmu_init(); 27 | -------------------------------------------------------------------------------- /11_exceptions/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /12_printf/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /12_printf/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 12 - Printf 2 | ==================== 3 | 4 | Before we can improve our exception handler, we are going to need some functions very well known from the C library. 5 | Since we are programming bare metal, we don't have libc, therefore we have to implement printf() on our own. 6 | 7 | ```sh 8 | $ qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 9 | Hello World! 10 | This is character 'A', a hex number: 7FFF and in decimal: 32767 11 | Padding test: '00007FFF', ' -123' 12 | ``` 13 | 14 | Sprintf.h, sprintf.c 15 | -------------------- 16 | 17 | The interesting part. We heavily rely on our compiler's features to handle variable length argument list. As usual 18 | in these tutorials, it's not a fully featured, but rather a bare minimum implementation. Supports '%s', '%c', 19 | '%d' and '%x'. Padding is limited, only right alignment with leading zeros for hex and spaces for decimal. 20 | 21 | `sprintf(dst, fmt, ...)` same as printf, but stores result in a string 22 | 23 | `vsprintf(dst, fmt, va)` a variant that receives an argument list parameter instead of a variable length list of arguments. 24 | 25 | 26 | Uart.h, uart.c 27 | ------------- 28 | 29 | `printf(fmt, ...)` the good old C library function. Uses the sprintf function above and then outputs the string 30 | in the same way as uart_puts() did. Since we have '%x', uart_hex() became unnecessary, therefore removed. 31 | 32 | Start 33 | ----- 34 | 35 | Although we are not going to use floats and doubles, gcc built-ins might. So we have to enable the FPU 36 | coprocessor to avoid "undefined instruction" exceptions. Also, in a lack of a proper exception handler, 37 | we have a dummy `exc_handler` stub this time. 38 | 39 | Main 40 | ---- 41 | 42 | We test our printf implementation. 43 | -------------------------------------------------------------------------------- /12_printf/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/12_printf/kernel8.img -------------------------------------------------------------------------------- /12_printf/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /12_printf/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | 28 | void main() 29 | { 30 | // set up serial console 31 | uart_init(); 32 | 33 | // test our printf implementation 34 | printf( "Hello %s!\n" 35 | "This is character '%c', a hex number: %x and in decimal: %d\n" 36 | "Padding test: '%8x', '%8d'\n", 37 | "World", 'A', 32767, 32767, 0x7FFF, -123); 38 | 39 | // echo everything back 40 | while(1) { 41 | uart_send(uart_getc()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /12_printf/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /12_printf/sprintf.h: -------------------------------------------------------------------------------- 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 | unsigned int sprintf(char *dst, char* fmt, ...); 27 | unsigned int vsprintf(char *dst,char* fmt, __builtin_va_list args); 28 | -------------------------------------------------------------------------------- /12_printf/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void printf(char *fmt, ...); 30 | -------------------------------------------------------------------------------- /13_debugger/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /13_debugger/dbg.h: -------------------------------------------------------------------------------- 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 breakpoint asm volatile("brk #0") 27 | -------------------------------------------------------------------------------- /13_debugger/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/13_debugger/kernel8.img -------------------------------------------------------------------------------- /13_debugger/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /13_debugger/main.c: -------------------------------------------------------------------------------- 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 | #include "uart.h" 27 | #include "dbg.h" 28 | 29 | void main() 30 | { 31 | // set up serial console 32 | uart_init(); 33 | 34 | // test our debugger 35 | breakpoint; 36 | 37 | // echo everything back 38 | while(1) { 39 | uart_send(uart_getc()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /13_debugger/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /13_debugger/sprintf.h: -------------------------------------------------------------------------------- 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 | unsigned int sprintf(char *dst, char* fmt, ...); 27 | unsigned int vsprintf(char *dst,char* fmt, __builtin_va_list args); 28 | -------------------------------------------------------------------------------- /13_debugger/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void printf(char *fmt, ...); 30 | -------------------------------------------------------------------------------- /14_raspbootin64/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /14_raspbootin64/Makefile.gcc: -------------------------------------------------------------------------------- 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 | SRCS = $(wildcard *.c) 27 | OBJS = $(SRCS:.c=.o) 28 | CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles 29 | 30 | all: clean kernel8.img 31 | 32 | start.o: start.S 33 | aarch64-elf-gcc $(CFLAGS) -c start.S -o start.o 34 | 35 | %.o: %.c 36 | aarch64-elf-gcc $(CFLAGS) -c $< -o $@ 37 | 38 | kernel8.img: start.o $(OBJS) 39 | aarch64-elf-ld -nostdlib -nostartfiles start.o $(OBJS) -T link.ld -o kernel8.elf 40 | aarch64-elf-objcopy -O binary kernel8.elf kernel8.img 41 | 42 | clean: 43 | rm kernel8.elf *.o >/dev/null 2>/dev/null || true 44 | 45 | run: 46 | qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial stdio 47 | -------------------------------------------------------------------------------- /14_raspbootin64/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/14_raspbootin64/kernel8.img -------------------------------------------------------------------------------- /14_raspbootin64/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_GETSERIAL 0x10004 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /14_raspbootin64/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | -------------------------------------------------------------------------------- /15_writesector/Makefile: -------------------------------------------------------------------------------- 1 | Makefile.gcc -------------------------------------------------------------------------------- /15_writesector/OLVASSEL.md: -------------------------------------------------------------------------------- 1 | Oktatóanyag 15 - Szektor Kiírás 2 | =============================== 3 | 4 | Térjünk egy kicsit vissza a 0B oktatóanyaghoz, és egészítsük ki az SD kártya írással. Hogy leteszteljük, 5 | egy újraindítás számlálót fogunk készíteni. Beolvassunk egy szektort (akárcsak a 0B-ben), növelünk egy 6 | számlálót a bufferben, majd visszaírjuk a kártyára. Így minden alkalommal, mikor újraindítjuk a gépet, 7 | a számláló eggyel növekedni fog. 8 | 9 | A számlálóhoz a második szektor utolsó 4 bájtját választottam. Nem akartam az első szektort írni, mivel 10 | az esetleg elronthatja a Master Boot Record-ot, indíthatatlanná téve a kártyánkat. A második szektor 11 | biztonságosabb, habár ha EFI Partíciós Táblát használsz (mint én), akkor a számláló tönkreteheti azt is. 12 | Ezért a legutolsó 4 bájtot választottam, remélve, hogy a tábla rövidebb, mint 508 bájt. Ha ez nem állja 13 | meg a helyét, akkor változtasd meg a COUNTER_SECTOR értékét, hogy egy biztosan használaton kívüli szektorra 14 | mutasson. 15 | 16 | Szeretném külön megköszönni [@DamianOslebo](https://github.com/DamianOslebo) segítségét az alapos tesztelésért 17 | és amiért kiszúrt egy hibás parancs definíciót. 18 | 19 | Sd.h, sd.c 20 | ------------ 21 | 22 | Új parancsokat (CMD_WRITE_SINGLE, CMD_WRITE_MULTI) és új státuszokat (SR_WRITE_AVAILABLE, INT_WRITE_RDY) 23 | adtam hozzá a meghajtóprogramunkhoz. Ezeket fogjuk használni a *READ* megfelelőik helyett. 24 | 25 | `sd_writeblock(buffer,lba,num)` kiír egy blokkot (szektort) a bufferből az SD kártyára az lba-tól kezdve. 26 | 27 | Main 28 | ---- 29 | 30 | A blokkot a bss szegmens utánra töltjük be, növeljük a számlálót, majd vissza kiírjuk a kártyára. Ha minden 31 | rendben ment, akkor a számláló aktuális értékét kiírjuk a soros konzolra. 32 | -------------------------------------------------------------------------------- /15_writesector/README.md: -------------------------------------------------------------------------------- 1 | Tutorial 15 - Write Sector 2 | ========================== 3 | 4 | Let's go back to tutorial 0B for a moment, and add a function to write the SD card. To test it, we'll implement 5 | a boot counter. We're going to read a sector into memory (same as in 0B), increase a counter in the sector buffer, 6 | then save it back to the SD card. This way every time we reboot this image, the counter should be increased. 7 | 8 | For the counter I've choosen the last 4 bytes of the 2nd sector. I did not want to use the first sector, as that 9 | could mess up the Master Boot Record and render our card unbootable. Second sector is safer, although if you're 10 | using an EFI Partitioning Table (like I do), then the counter could mess up that too. So I've choosen the last 4 bytes 11 | of the sector hoping the table is shorter than 508 bytes. If this not the case for you, then change the COUNTER_SECTOR 12 | define to point to a surely unused sector on your SD card. 13 | 14 | I'd would like to say thanks to [@DamianOslebo](https://github.com/DamianOslebo) for thgroughful testing and spotting 15 | a bad constant in the command defines. 16 | 17 | Sd.h, sd.c 18 | ------------ 19 | 20 | I've added new commands (CMD_WRITE_SINGLE, CMD_WRITE_MULTI) and new status flags (SR_WRITE_AVAILABLE, 21 | INT_WRITE_RDY) to our driver. We'll use them instead of their *READ* counterparts. 22 | 23 | `sd_writeblock(buffer,lba,num)` write num blocks (sectors) from the buffer into the SD card starting at sector lba. 24 | 25 | Main 26 | ---- 27 | 28 | We read a block after the bss segment in memory, and increase a counter in it, and save it back to the card. If 29 | everything went well, we print the actual counter value on the console. 30 | -------------------------------------------------------------------------------- /15_writesector/delays.h: -------------------------------------------------------------------------------- 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 | void wait_cycles(unsigned int n); 27 | void wait_msec(unsigned int n); 28 | unsigned long get_system_timer(); 29 | void wait_msec_st(unsigned int n); 30 | -------------------------------------------------------------------------------- /15_writesector/kernel8.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bztsrc/raspi3-tutorial/43af6f21b0697eeea775500b8fd268c1435389da/15_writesector/kernel8.img -------------------------------------------------------------------------------- /15_writesector/link.ld: -------------------------------------------------------------------------------- 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 | SECTIONS 27 | { 28 | . = 0x80000; 29 | .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } 30 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } 31 | PROVIDE(_data = .); 32 | .data : { *(.data .data.* .gnu.linkonce.d*) } 33 | .bss (NOLOAD) : { 34 | . = ALIGN(16); 35 | __bss_start = .; 36 | *(.bss .bss.*) 37 | *(COMMON) 38 | __bss_end = .; 39 | } 40 | _end = .; 41 | 42 | /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } 43 | } 44 | __bss_size = (__bss_end - __bss_start)>>3; 45 | -------------------------------------------------------------------------------- /15_writesector/mbox.h: -------------------------------------------------------------------------------- 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 | /* a properly aligned buffer */ 27 | extern volatile unsigned int mbox[36]; 28 | 29 | #define MBOX_REQUEST 0 30 | 31 | /* channels */ 32 | #define MBOX_CH_POWER 0 33 | #define MBOX_CH_FB 1 34 | #define MBOX_CH_VUART 2 35 | #define MBOX_CH_VCHIQ 3 36 | #define MBOX_CH_LEDS 4 37 | #define MBOX_CH_BTNS 5 38 | #define MBOX_CH_TOUCH 6 39 | #define MBOX_CH_COUNT 7 40 | #define MBOX_CH_PROP 8 41 | 42 | /* tags */ 43 | #define MBOX_TAG_SETPOWER 0x28001 44 | #define MBOX_TAG_SETCLKRATE 0x38002 45 | #define MBOX_TAG_LAST 0 46 | 47 | int mbox_call(unsigned char ch); 48 | -------------------------------------------------------------------------------- /15_writesector/sd.h: -------------------------------------------------------------------------------- 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 SD_OK 0 27 | #define SD_TIMEOUT -1 28 | #define SD_ERROR -2 29 | 30 | int sd_init(); 31 | int sd_readblock(unsigned int lba, unsigned char *buffer, unsigned int num); 32 | int sd_writeblock(unsigned char *buffer, unsigned int lba, unsigned int num); 33 | -------------------------------------------------------------------------------- /15_writesector/uart.h: -------------------------------------------------------------------------------- 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 | void uart_init(); 27 | void uart_send(unsigned int c); 28 | char uart_getc(); 29 | void uart_puts(char *s); 30 | void uart_hex(unsigned int d); 31 | void uart_dump(void *ptr); 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2017 bzt (bztsrc@github) 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, copy, 7 | modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 18 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 19 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 | DEALINGS IN THE SOFTWARE. 22 | --------------------------------------------------------------------------------