01-对readme.md的搬运和翻译
本文是对Readme.md文件的翻译,原文放在文章最后,GitHub地址在上一篇文章中.
下面的非黑色字体都是我自己加上去的
给树莓派"裸机"写一个操作系统
简介
作为RealVNC的技术CEO,我不再写代码了.但是最近我意识到了我是多么的怀念它.
由于最近新冠疫情导致的全国性的"封锁",(我也因此不用通勤了),我发现我一天多出来了几个小时.我把这段时间留给自己,并且用来实现我儿时的目标--写一个可以在商用的硬件上写一个裸机操作系统.
"裸机"是什么意思
当我们买电脑,平板或者手机的时候,里面经常会预装一些软件.当你第一次启动设备时,你应该会对看到Windows,Mac OS,IOS和安卓甚至Linux感到很熟悉.这些都是操作系统--为你和我这种普通人而把电脑芯片变为可以开箱即用所设计的软件.它们通过在屏幕上绘制图案,处理来自像键盘和鼠标等设备的信息,使用网络设备将你的电脑连接到网上,允许我们播放声音等许多工作来帮助我们和设备进行交互.
世界上的程序员开发能运行在操作系统之上的软件.这些软件通过操作系统与硬件打交道,这样这些复杂的代码就不用一遍又一遍的被实现.结果就是对硬件不怎么了解也可以称为一个程序员.操作系统做了最困难的工作才让我们用上了像脸书,Instagram, WhatsApp, TikTok这样的软件.
可以说没有操作系统,计算机硬件做不了什么有用的事.它们只是坐在这然后等待命令.所以,为什么只有软件巨头们像微软,苹果,谷歌来告诉大多数电脑它们启动的时候应该怎么做呢?我们不行吗?当然,我们可以,这就是裸机编程.
硬件的选择
如果你对可以告诉计算机去做什么感到兴奋,那么你需要对硬件有一定的兴趣.要完成你的任务的电脑芯片叫作CPU(中央处理单元)--它是每个计算机设备跳动的心脏.多年来,许多公司都设计了这样的CPU,但是只有AMD和Intel两家的CPU被广泛采用.这些芯片都有它们的优点和缺点.如果你有智能手机,它们大概率在ARM设计的芯片上运行.如果你有台运行Windows的笔记本,那么它大概率运行在Intel的芯片上.你会想对两种架构最终都能有所了解,但是我为这个项目选择了ARM公司的设备.
最新的树莓派4B是一款低功耗电脑,有1.5G四核64位A72处理器.这是一款世界上数百万人都在用的设备,所以为其编写裸机代码是非常令人兴奋的事情.想象一下某人某天可能用你写的操作系统!树莓派还有一些有用的附加硬件可以在这一过程中帮助我们.
硬件要求
你需要一些硬件来开始编写你的操作系统:
1.带有专用电源线的树莓派和HDMI线
2.一个通过HDMI与树莓派相连的显示屏 / TV
3.用来启动树莓派的micro-SD卡
4.一个用来开编写代码的电脑,比如Windows或者Mac 笔记本(开发设备)
你需要确认你能用你的开发设备读写micro-SD卡.对我来讲,这意味着要买一个SD卡读适配器,因为micro-SD卡对于我笔记本电脑的插槽来讲太小了.你也可能需要一个,甚至一个USB SD卡读写器,如果你的电脑没有内置的话.
其他十分有用的硬件,你离开它你甚至不能工作:
1.一副眉毛镊子(我从我妻子那里借来的!)-对从树莓派的插槽里面插入或者拔出micro-SD卡很有用
2.一根USB转TTL线,在你的操作系统把信息显示在屏幕上之前你可以看到它在做什么
软件要求
如果你不能使其他人的操作系统运行,那么你就写不了自己的操作系统.所以我用Raspbian--树莓派官网推荐的操作系统.我使用了它们网站上提供的,非常简洁的镜像制作工具来做到完成.
连接好您的树莓派4并且确保它启动到了Raspbian.网上有很多资源来帮助你完整这件事.启动Raspbian能测试你的硬件设置是否正常工作.注意:因为我把树莓派连接到我的(不明亮的)TV,我需要在SD卡中的config.txt 文件里面进行编辑(设置hdmi_safe=1)来确保我能看到屏幕.没有这个,它就不亮.如果你仍然遇到了问题,去查看树莓派网站上config.txt其他选项的视频.
在Raspbian运行之前不要继续!
树莓派4在ARM的A72处理器上运行,而你的电脑大大概率运行在Intel的处理器上.因此你需要一些软件来帮助你编译代码以运行在不同的架构上.这叫做交叉编译.
在Linux上使用ARM的编译器
下载并解压ARM的gcc编译器.由于某些原因,我不会在这里详细展开,你需要使用"AArch64 ELF bare-metal target".因为我使用Windows10的WSL来模拟Ubuntu,我下载了x86_64 Linux 的交叉编译器.
我也提倡安装GNU make - 你待会就会需要它的.因为我用WSL,所以我只用打出"sudo apt install make" 然后输入我的密码.
在MAC OS X 上使用 Clang (苹果芯片或者Intel的芯片)
从应用商店下载并安装 XCode.它将给你提供大量的开发工具,包括make.我推荐使用 Homebrew来安装LLVM,对于我,Homebrew应安装好了,所以我只要敲下"brew install llvm"
LLVM可以满足你对于使用MAC在树莓派裸机上编程的所有需求.它甚至可以在我的运行在苹果芯片上的M1 Macbook pro上运行.这台电脑运行ARM的处理器而不是Intel的.
直接在树莓派4上编译
你可以从这里阅读关于在树莓派上编译的更多信息.(见下一篇PRI-BUILD.md的翻译)
现在你可以去写自己的操作系统了.
致谢
这些代码并非都是我的原创产品,而是收到其他杰出的贡献者启发和整理的.
感谢:
Zoltan Baldaszti的“裸机树莓派3教程”(github) https://github.com/bztsrc/raspi3-tutorial
如果你也想在这里得到认可,请联系我们!

Writing a "bare metal" operating system for Raspberry Pi 4
Introduction
As a tech CEO @RealVNC, I don't write code any more. And I've recently realised just how much I miss it.
Currently in the throes of a nationwide "lockdown" due to Covid-19 (and having been spared my usual commute), I've found myself with more hours in the day. I have taken this time for myself and used it to fulfil a childhood ambition - to write a **bare metal** operating system that runs on commercial hardware.
What does bare metal mean?
When we buy a computer or a tablet/smartphone it typically comes with some basic software pre-installed. You'll likely be familiar with watching Microsoft Windows, Mac OS, iOS, Android or maybe even Linux start up as you power the device (or **boot** it) for the first time. These are all operating systems - software designed to make computer chips work out of the box for mere mortals like you and me. They help us interact with the machine by drawing to a screen, processing messages from devices like keyboards & mice, working with networking hardware to connect you to the Internet, allowing us to playback sound and much, much more.
Software developers around the world then build applications (apps) that run on top of these operating systems. These apps talk to the hardware via the operating system (**OS**), so this complex code doesn't have to be written over and over again. As a result, it's possible to be a software developer without knowing much about hardware at all! It's the OS that does the hard work that allows us to use apps like Facebook, Instagram, WhatsApp, TikTok etc.
It's fair to say that _computers can't do anything useful without an OS_. They just sit there waiting to be told what to do. So, why is it that only software giants like Microsoft, Apple and Google get to tell the majority of computers what to do as they're being switched on? Why can't we? Well, we can, and that is what bare metal programming is.
Choice of hardware
If you're excited by the prospect of telling a computer what to do then you need an interest in hardware. The computer chip that's going to do your bidding is called a **CPU** (Central Processing Unit) - it's the beating heart of every computer device. Lots of companies have designed such CPUs over the years, but two - Intel and Arm - are most widely adopted. These each have their advantages and disadvantages. If you own a smartphone, it's highly likely that it's running on a chip designed by Arm. If you own a laptop running Microsoft Windows then it's likely to be running on an Intel chip. You'll want to develop an understanding of both **architectures** eventually, but I've chosen an Arm device for this project.
The new [Raspberry Pi 4 Model B](https://www.raspberrypi.org/products/raspberry-pi-4-model-b/) is a low-cost computer that runs on a 1.5 GHz 64-bit quad-core Arm Cortex-A72 processor. It's a device that many millions of people worldwide use, and so it's exciting to write bare metal code for it. Imagine that somebody else might one day use your OS! The **RPi4** also has some useful attached hardware that will help us along the way.
Hardware prerequisites
You'll need some hardware to get started with writing your OS:
* An RPi4 with a dedicated power supply and HDMI lead
* A monitor/TV connected to the RPi4 via HDMI
* A micro-SD card to boot the RPi4 from
* A computer to write your code on e.g. a Windows/Mac laptop (the **dev machine**)
You'll need to make sure that you can write to the micro-SD card using your dev machine. For me, that meant buying an SD card adapter, because the micro-SD card was too small for the slot in my laptop. You may need the same, or even a USB SD card reader too if your laptop doesn't have one built-in.
Other incredibly useful hardware that you simply can't do without:
* A pair of eyebrow tweezers (I borrowed these from my wife!) - useful to insert/remove the micro-SD card into the tiny slot on the RPi4
* A [USB to serial TTL cable](https://www.amazon.co.uk/gp/product/B01N4X3BJB/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1) - useful to see what your OS is doing long before you can write information to the screen
Software prerequisites
If you can't get someone else's OS running, you likely won't be able to write your own. So I started by flashing the SD card with Raspbian - Raspberry Pi's recommended OS. I used the very neat [Imager tool](https://www.raspberrypi.org/downloads/) that they make available on their website to do this.
Hook up your RPi4 and make sure it boots into Raspbian. There are plenty of resources online to help you achieve/troubleshoot this. Getting Raspbian up will test that your hardware setup is working properly. Note: because I connected my RPi4 to my (not brilliant) TV, I needed to make an edit in the _config.txt_ file on the SD card (setting the `hdmi_safe` parameter to 1) to ensure that I could see the screen. Without that, it was just black. If you're still having trouble, check out the other _config.txt_ video options [here on the Raspberry Pi website](https://www.raspberrypi.com/documentation/computers/config_txt.html#video-options).
Don't proceed until you get Raspbian running!
The RPi4 runs on an Arm Cortex-A72 processor. Your dev machine is likely running on an Intel processor. You'll therefore need some software that helps you build code to run on a different architecture. This is called a **cross-compiler**.
Using Arm's compiler for Linux
Download and unpack [Arm's gcc compiler](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads). For reasons that I won't go into here, you'll need to use the "AArch64 ELF bare-metal target". Since I'm using WSL on Windows 10 to emulate Ubuntu, I downloaded the x86_64 Linux hosted cross-compiler.
I also advocate installing GNU make - you'll need it soon enough. Because I'm using WSL, for me that was simply a matter of typing `sudo apt install make` and entering my password.
Using clang on Mac OS X (Apple Silicon or Intel)
Download and install XCode from the App Store. This will give you a raft of developer tools for free, including `make`.
I recommend using [Homebrew](https://docs.brew.sh/Installation) to install LLVM. For me, Homebrew was already installed, so this was a simple case of typing `brew install llvm`.
LLVM will give you all you need to start building for Raspberry Pi bare metal on Mac. It even works on my M1 MacBook Pro with Apple Silicon, which runs an ARM processor rather than an Intel processsor.
Building directly on a Raspberry Pi 4
You can read more about how to build on the Pi itself [here](./RPI-BUILD.md).
_Now you're ready to start writing your OS!_
[Go to part1-bootstrapping >](./part1-bootstrapping/)
Acknowledgements
Not all of the code here is my original work, but has been collated from and inspired by some fantastic contributors.
Thanks go to:
* Zoltan Baldaszti's "Bare metal Raspberry Pi 3 tutorials" [(github)](https://github.com/bztsrc/raspi3-tutorial/)
Do reach out if you would like to be acknowledged here too!