如何为 Raspberry pi 3 编译 ARMv8 代码



我一直在按照这里的教程为我的树莓派编写一个基本的操作系统。我已经阅读了文档并更改了寄存器,以便它们应该运行,但由于树莓派 3 是 64 位的,因此使用 ARMv8,因此 lsl 是越界的。我正在使用 mac,所以使用 YAGARTO,并且真的不知道如何或在哪里获得另一个 64 位编译器。

任何感兴趣的人的代码:

.section .init
.globl _start
_start:
ldr r0,=0x3f200000

mov r1,#1
lsl r1,#21
str r1,[r0,#16]

mov r1,#1
lsl r1,#47
str r1,[r0,#28]
loop$: 
b loop$

我建议在这里看看David Welch的aarch64 RaspberryPi 3示例。为了方便起见,他的引导加载程序允许上传 .hex 格式的文件。
作为记录,您可以使用以下命令从已编译的可执行文件创建 .hex:

aarch64-none-objcopy program.elf -O ihex example.elf example.hex

关于工具链,据我所知,Linaro和ARM只为Linux和mingw提供工具链。但是你可以使用个人为OSX构建的工具链,比如Sergio Benitez提供的这个。

更新:一个方便的替代方法是根据此处描述的出色过程在SD卡上安装u-boot。

假设您将在/opt 中安装 aarch64-none 工具链,以便 aarch64-none-gcc 的路径为:

/opt/aarch64-none/bin/aarch64-none-gcc

适合您需求的简化程序是:

在SD卡上创建一个最小的配置.txt,

enable_uart=1
arm_control=0x200
kernel=u-boot.bin

将引导代码.bin、.dat修复和 start.elf 复制到 SD 卡,

构建 u-boot,

wget ftp://ftp.denx.de/pub/u-boot/u-boot-2019.01.tar.bz2
tar Jxf u-boot-2019.01.tar.bz2
cd u-boot-2019.01
make CROSS_COMPILE=/opt/aarch64-none/bin/aarch64-none- ARCH=arm64  rpi_3_defconfig all

将 u-boot.bin 复制到 SD 卡 - 它现在应该包含以下文件:

2019-04-08  06:03 PM               108 config.txt
2019-04-08  04:11 PM         2,873,444 start.elf
2019-04-08  04:11 PM            52,296 bootcode.bin
2019-04-08  04:11 PM             6,701 fixup.dat
2019-04-08  04:08 PM           479,872 u-boot.bin

将SD卡安装到Raspberry-pi3中, 假设您安装了串行到 USB 加密狗,并且终端仿真器配置为使用以下设置使用 USB 串行端口:

115200 bps, 8 data bits, 1 stop bit, no parity, no hardware flow control

您现在可以打开设备电源,并尽快按CTRL+C中断启动过程:

U-Boot 2019.01 (Apr 08 2019 - 16:07:23 -0400)
DRAM:  948 MiB
RPI 3 Model B (0xa22082)
MMC:   mmc@7e202000: 0, sdhci@7e300000: 1
Loading Environment from FAT... *** Warning - bad CRC, using default environment
In:    serial
Out:   vidconsole
Err:   vidconsole
Net:   No ethernet found.
starting USB...
USB0:   scanning bus 0 for devices... 3 USB Device(s) found
scanning usb for storage devices... 0 Storage Device(s) found
Hit any key to stop autoboot:  0
U-Boot> <INTERRUPT>

引导延迟环境变量设置为 2(秒),您需要将其设置为 -1(无限),以避免每次引导都使用CTRL+C

U-Boot> printenv bootdelay
bootdelay=2
U-Boot> setenv bootdelay -1
U-Boot> saveenv
Saving Environment to FAT... OK
U-Boot>

如果输入重置命令,pi 将重新启动,但 u-boot 将停止:

U-Boot> reset
resetting ...
U
‡t-Boot 2019.01 (Apr 08 2019 - 16:07:23 -0400)
DRAM:  948 MiB
RPI 3 Model B (0xa22082)
MMC:   mmc@7e202000: 0, sdhci@7e300000: 1
Loading Environment from FAT... OK
In:    serial
Out:   vidconsole
Err:   vidconsole
Net:   No ethernet found.
starting USB...
USB0:   scanning bus 0 for devices... 3 USB Device(s) found
scanning usb for storage devices... 0 Storage Device(s) found
U-Boot>

您现在可以使用所有可用的 u-boot 命令来检查/更改内存,以及加载/执行程序:

创建名为hello-aarch64.s的文件,其中包含以下内容:

.title "hello-aarch64.s"
.arch  armv8-a
.equ   AUX_MU_IO_REG, 0x3F215040  
.equ   AUX_MU_LSR_REG, 0x3F215054 
.text
.section .text.startup,"ax"    
.globl Reset_Handler
Reset_Handler:    stp  x29, x30,  [sp, #-16]!        
adr  x19, msg
ldr  x20,= AUX_MU_IO_REG
ldr  x21,= AUX_MU_LSR_REG
loop:             ldrb w0, [x19], 1                  
cbz  w0, done               
wait:             ldrb w1, [x21]
tbz  w1, #5, wait
strb w0, [x20]
b    loop
done:             ldp  x29,x30,  [sp], #16                 
ret
.balign 16
msg:              .asciz "Hello, aarch64 bare-metal world!rn"
.end

因为我们将从 u-boot 调用程序并且不想让它崩溃,所以该程序应该符合 ARM 64 位体系结构的 ARM 过程调用标准 - 这在这里有点矫枉过正,因为我们没有调用任何函数,但这并不重要。

可以使用以下命令编译程序并创建 s 记录文件:

CROSS_COMPILE= /opt/aarch64-none/bin/aarch64-none-
AS=${CROSS_COMPILE}as
LD=${CROSS_COMPILE}ld
OBJCOPY=${CROSS_COMPILE}objcopy
OBJDUMP=${CROSS_COMPILE}objdump
${AS} -o hello-aarch64.o hello-aarch64.s
${LD} -e Reset_Handler --section-start .text=0x00200000 -Map=hello-aarch64.map -o hello-aarch64.elf hello-aarch64.o 
${OBJCOPY} hello-aarch64.elf -O srec hello-aarch64.srec

现在可以上传和执行程序: 在 u-boot 中输入以下命令:

U-Boot> loads
## Ready for S-Record download ...

从终端模拟器中,将hello-aarch64.srec文件发送到 you-boot(没有 x 调制解调器,没有 kermit,只是文件原样)。

## First Load Addr = 0x00200000
## Last  Load Addr = 0x00200067
## Total Size      = 0x00000068 = 104 Bytes
## Start Addr      = 0x00200000
U-Boot>

使用 u-boot 中的go命令来执行程序(go命令实际上是call命令)。

U-Boot> go 0x00200000
## Starting application at 0x00200000 ...
Hello, aarch64 bare-metal world!
## Application terminated, rc = 0x0
U-Boot>

就是这样,您现在应该有一个学习 aarch64 汇编语言的良好标准环境。

很抱歉很冗长,但目标是为需要的人提供一个极简但完整的程序。

最新更新