|
| 1 | +========================= |
| 2 | +Lab 0: Environment Setup |
| 3 | +========================= |
| 4 | + |
| 5 | +Introduction |
| 6 | +============= |
| 7 | +The first step of creating a masterpiece is preparing the tool. |
| 8 | +You're going to implement a 64-bit kernel on ARM CPU. |
| 9 | +Hence, you need a toolchain to help you finish the jobs. |
| 10 | + |
| 11 | +In this lab, you'll set up the working environment for future development. |
| 12 | + |
| 13 | +Goals of this lab |
| 14 | +================= |
| 15 | + |
| 16 | +* Understand cross-platform development. |
| 17 | + |
| 18 | +* Setup the working environment. |
| 19 | + |
| 20 | +* Test your hardware. |
| 21 | + |
| 22 | +.. note:: |
| 23 | + This lab is an introductory lab, it won't be taken as part of your final grade. |
| 24 | + But you still need to do the ``required`` stuff, or you'll be in trouble in the next lab. |
| 25 | + |
| 26 | +Cross-platform development |
| 27 | +========================== |
| 28 | + |
| 29 | +Cross compiler |
| 30 | +--------------- |
| 31 | +rpi3 uses ARM Cortex-A53 CPU. |
| 32 | +You need a cross-compiler either using C/C++/Rust. |
| 33 | + |
| 34 | +``required`` Install the cross compiler on your host computer. |
| 35 | + |
| 36 | +``question`` What's the RAM size of Raspberry Pi 3B+? |
| 37 | + |
| 38 | +``question`` What's the cache size and level of Raspberry Pi 3B+? |
| 39 | + |
| 40 | +.. note:: You're doing 64-bit programming, make sure you choose the right cross compiler. |
| 41 | + |
| 42 | +Bare Metal Programming |
| 43 | +---------------------- |
| 44 | +Some features and standard library of a programming language rely on operating system support. |
| 45 | +Hence you cannot naively use them. |
| 46 | + |
| 47 | +Also, you should set the corresponding compiler flags to generate correct code. |
| 48 | + |
| 49 | +Linker |
| 50 | +------- |
| 51 | +You might not notice the existence of linker. |
| 52 | +It's because the compiler uses the default linker script for you. (``ld --verbose`` to check the content) |
| 53 | +But in bare metal programming, you should set the memory layout yourself. |
| 54 | + |
| 55 | +This is an incomplete linker script for you, you should extend it in the following lab. |
| 56 | + |
| 57 | +.. code-block:: none |
| 58 | + :linenos: |
| 59 | +
|
| 60 | + SECTIONS |
| 61 | + { |
| 62 | + . = 0x80000; |
| 63 | + .text : { *(.text) } |
| 64 | + } |
| 65 | + |
| 66 | +``question`` Explain each line of the above linker script. |
| 67 | + |
| 68 | +QEMU |
| 69 | +----------- |
| 70 | +In cross-platform development, |
| 71 | +it's easier to validate on emulators first to get better control. |
| 72 | +You can use QEMU to test your code first before validating them on your real rpi3. |
| 73 | + |
| 74 | +.. warning:: |
| 75 | + Although QEMU provides a machine option for rpi3, it doesn't act the same as real rpi3. |
| 76 | + You should validate your code on rpi3, too. |
| 77 | + |
| 78 | +``required`` Install ``qemu-system-aarch64`` as an emulator for rpi3. |
| 79 | + |
| 80 | +From source code to kernel image |
| 81 | +===================================== |
| 82 | + |
| 83 | +You have the basic knowledge of the toolchain for cross-platform development. |
| 84 | +Now, it's time to practice them. |
| 85 | + |
| 86 | +From source code to object files |
| 87 | +-------------------------------- |
| 88 | + |
| 89 | +Source code is compiled or assembled to be object file by cross compiler. |
| 90 | + |
| 91 | +.. code-block:: c |
| 92 | +
|
| 93 | + .section ".text" |
| 94 | + _start: |
| 95 | + wfe |
| 96 | + b _start |
| 97 | +
|
| 98 | +Assemble the assembly to object file by the following command. |
| 99 | + |
| 100 | +.. code-block:: none |
| 101 | +
|
| 102 | + aarch64-linux-gnu-gcc -c a.S |
| 103 | +
|
| 104 | +From object files to ELF |
| 105 | +------------------------ |
| 106 | + |
| 107 | +Linker links object files to a ELF file |
| 108 | +It contains debugging information for debugger. |
| 109 | +It also could be load by QEMU and some bootloaders. |
| 110 | + |
| 111 | +Save the provided linker script as linker.ld and run the following command to link the object file. |
| 112 | + |
| 113 | +.. code-block:: none |
| 114 | +
|
| 115 | + aarch64-linux-gnu-ld -T linker.ld -o kernel8.elf a.o |
| 116 | +
|
| 117 | +
|
| 118 | +From elf to kernel image |
| 119 | +--------------------------- |
| 120 | + |
| 121 | +To run your code on real rpi3, |
| 122 | +you need to make the elf file a raw binary image. |
| 123 | +Also, the default name of it should be kernel8.img. |
| 124 | +You can use objcopy to translate elf to raw binary. |
| 125 | + |
| 126 | +.. code-block:: none |
| 127 | +
|
| 128 | + aarch64-linux-gnu-objcopy -O binary kernel8.elf kernel8.img |
| 129 | +
|
| 130 | +Check on QEMU |
| 131 | +------------- |
| 132 | + |
| 133 | +After building, you can use QEMU to see the dumped assembly. |
| 134 | + |
| 135 | +.. code-block:: none |
| 136 | +
|
| 137 | + qemu-system-aarch64 -M raspi3 -kernel kernel8.img -display none -d in_asm |
| 138 | +
|
| 139 | +``required`` Build your first kernel image and check it by QEMU. |
| 140 | + |
| 141 | + |
| 142 | +Deploy to REAL rpi3 |
| 143 | +=================== |
| 144 | + |
| 145 | +Flash bootable image to SD card |
| 146 | +-------------------------------- |
| 147 | +To prepare a bootable image for rpi3, you have to prepare at least the following stuff. |
| 148 | + |
| 149 | +* A FAT16/32 partition contains |
| 150 | + |
| 151 | + * Firmware for GPU |
| 152 | + |
| 153 | + * Kernel image |
| 154 | + |
| 155 | +There are two ways to do it. |
| 156 | + |
| 157 | +1. |
| 158 | + We already prepared a bootable image. |
| 159 | + You can use the following command to flash it to your sd card. |
| 160 | + |
| 161 | + .. code-block:: none |
| 162 | +
|
| 163 | + dd if=nctuos.img of=/dev/sdb |
| 164 | +
|
| 165 | + .. warning:: /dev/sdb should be replaced by your sd card device, you can check it by `lsblk` |
| 166 | + |
| 167 | + It's already partition and contains a FAT32 filesystem with firmware inside. |
| 168 | + You can mount the partition to check. |
| 169 | + |
| 170 | +2. |
| 171 | + Partition the disk and prepare the booting firmware yourself. |
| 172 | + You can download the firmware from |
| 173 | + https://github.com/raspberrypi/firmware/tree/master/boot |
| 174 | + |
| 175 | + bootcode.bin, fixup.dat and start.elf are essentials. |
| 176 | + More information about pi3's booting could be checked on official website |
| 177 | + https://www.raspberrypi.org/documentation/configuration/boot_folder.md |
| 178 | + https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/README.md |
| 179 | + |
| 180 | + Finally, put firmware and your kernel image into the FAT partition. |
| 181 | + |
| 182 | + .. note:: |
| 183 | + Besides using mkfs.fat -F 32 to FAT32 you should also set the partition type. |
| 184 | + |
| 185 | + |
| 186 | +``required`` Use either one of the methods to set up your sd card. |
| 187 | + |
| 188 | +Interact with rpi3 |
| 189 | +------------------ |
| 190 | +Use the provided kernel8.img and connect TX, RX, GND to the corresponding pin on rpi3. |
| 191 | +After power on, you can read and write data from /dev/ttyUSB0 (Linux). |
| 192 | +You can use putty or screen with baud rate 115200 to interact with your rpi3. |
| 193 | + |
| 194 | +.. code-block:: none |
| 195 | +
|
| 196 | + screen /dev/ttyUSB0 115200 |
| 197 | +
|
| 198 | +
|
| 199 | +Debugging |
| 200 | +========== |
| 201 | + |
| 202 | +Debug on QEMU |
| 203 | +------------- |
| 204 | + |
| 205 | +Debugging on QEMU is a relatively easy way to validate your code. |
| 206 | +QEMU could show the content of memory and registers or expose them to debugger. |
| 207 | +You can use the following command waiting for gdb connection. |
| 208 | + |
| 209 | +.. code-block:: none |
| 210 | +
|
| 211 | + qemu-system-aarch64 -M raspi3 -kernel kernel8.img -display none -S -s |
| 212 | +
|
| 213 | +Then you can use the following command in gdb to load debugging information and connect to QEMU. |
| 214 | + |
| 215 | +.. code-block:: none |
| 216 | +
|
| 217 | + file kernel8.elf |
| 218 | + target remote :1234 |
| 219 | +
|
| 220 | +.. note:: |
| 221 | + Your gdb should also be cross-platform gdb. |
| 222 | + |
| 223 | + |
| 224 | +Debug on real rpi3 |
| 225 | +------------------- |
| 226 | + |
| 227 | +If you'd like to debug on real rpi3, you could either use print log or using JTAG. |
| 228 | +We don't provide JTAG in this course, you can try it if you have one. |
| 229 | +https://metebalci.com/blog/bare-metal-raspberry-pi-3b-jtag/ |
0 commit comments