02-对PRI-BUILD.md的搬运和翻译
这篇文章是对于PRI-BUILD.md的翻译,GitHub原文放在文章末尾
非黑色字体以及图片均为我自己添加
在树莓派4上进行编译
在不使用其他编译设备的情况下跟着这篇教程对树莓派进行编译时可能的(并不十分简单)
可能最简单路线就是首先用树莓派的操作系统(Beta)对你的系统进行重新建立镜像,然后使用预先置入的交叉编译器:
从64位镜像列表里面下载压缩好的 .img 镜像文件,使用最新的更新.
解压,然后使用树莓派镜像工具(https://downloads.raspberrypi.org/raspios_arm64/images )将其写入你的SD卡,从选项中选择 "Use costum"并且指向你下载好的文件

启动树莓派并按照设置向导来确保你能连上网.
碰碰运气,运行 sudo apt update
你需要在ARM的网站上下载交叉编译器.
你需要找到是 AArch64 ELF bare-metal target (aarch64-none-elf) .如果这个链接不知道怎么着断掉了,那么你可以谷歌搜 "Arm GNU-A linux hosted cross compilers"
然后打开文档文件,使用命令 " tar -xf <filename>" .你最后会得到一个gcc目录(虽然名字比较长),这里面包含一个bin文件夹,里面放着一个gcc可执行文件(同样,名字有点长).记住这个路径.
注意:你现在可以避免给树莓派重装镜像,而是自己建立交叉编译器. (https://wiki.osdev.org/GCC_Cross-Compiler)
现在,让我们干点活:
使用 git 来克隆这个仓库 : git clone https://github.com/isometimes/rpi4-osdev.git
你想要编译的那一部分相关的设备: 我喜欢利用 part5-framebuffer 进行测试(它可以被看见,所以你知道它什么时候成功工作)
把 Makefile.gcc 粘贴到 Makefile中.
编辑Makefile来确保 GCCPATH 变量指向能找到你的交叉编译器的那个子文件夹
在命令行中敲下 make 应该可以正确无误的工作
如果你想使用这个启动,那么你需要按照本教程所讲的复制 kernel8.img 文件到您准备好的SD卡上.为了对其进行测试,我做了以下的事(注意:它会破坏你安装的操作系统,除非你进行了备份,之后你可以把它挪回原位):
1. sudo cp kernel8.img /boot
2. 然后编辑 /etc/config.txt 只包含这几行(对于 part5-framebuffer,不然完整的阅读教程,以对其他部分的设置进行必要的更改
hdmi_group=1
hdmi_mode=16
core_freq_min=500
重新启动后,您应该看到part5-framebuffer演示程序启动了!

Writing a "bare metal" operating system for Raspberry Pi 4
Building on the RPi4 itself
It's possible (but not super-simple) to follow this tutorial on the Raspberry Pi without need for an additional build device.
Perhaps the easiest route is to firstly re-image your Pi to use the 64-bit Raspberry Pi OS (Beta), and then use a pre-built cross-compiler:
* Download a zipped _.img_ image file from the [64-bit image list](https://downloads.raspberrypi.org/raspios_arm64/images/), picking the newest update
* Unzip it and use the [Raspberry Pi Imager](https://www.raspberrypi.org/software/) to write it to your SD card, selecting "Use custom" from the options and pointing it at your downloaded _.img_ file
* Boot the Pi and follow the setup wizard to ensure you have a working Internet connection
* Just for luck, run `sudo apt update`
You'll then need to download a cross-compiler from the Arm website.
What you're looking for is the current [AArch64 ELF bare-metal target (aarch64-none-elf)](https://developer.arm.com/-/media/Files/downloads/gnu-a/10.2-2020.11/binrel/gcc-arm-10.2-2020.11-aarch64-aarch64-none-elf.tar.xz). If this link is somehow broken, you can use Google to search for "Arm GNU-A linux hosted cross compilers".
Then unpack the archive using `tar -xf <filename>`. You'll end up with a _gcc_ directory (albeit with a slightly longer name), which itself contains a _bin_ subdirectory, wherein you'll find the _gcc_ executable (again - with a longer name!). Remember this path.
Note: you can avoid re-imaging the Pi, by instead [building a cross-compiler yourself](https://wiki.osdev.org/GCC_Cross-Compiler).
Now let's build something:
* Use `git` to clone this repo: `git clone https://github.com/isometimes/rpi4-osdev.git`
* Decide which part you want to build - I like testing with _part5-framebuffer_ (it's visual, so you'll know when it works!)
* Copy the _Makefile.gcc_ to _Makefile_
* Edit the _Makefile_ and ensure the `GCCPATH` variable points to the _bin_ subdirectory where your cross-compiler is to be found
* Type `make` at the command line and it should build without errors
If you want to then boot with this, you'll need to copy the _kernel8.img_ file to a prepped SD card as the tutorial discusses. For the purposes of testing this process, I did the following (NOTE: it will trash your OS install unless you backup the old files so you can move them back later):
* `sudo cp kernel8.img /boot`
* Then edit _/boot/config.txt_ to include only these lines (for _part5-framebuffer_ anyway, otherwise read the tutorial in full for any necessary config changes for other parts...):
```
hdmi_group=1
hdmi_mode=16
core_freq_min=500
```
Reboot and you should see the _part5-framebuffer_ demo firing up!
[Go to part1-bootstrapping >](./part1-bootstrapping/)