一、需求
- Linux kernel:以5.1.7為例
- buildroot
- qemu
- toolchain:以linaro為例也可使用Arch linux aarch64-linux-gnu-gcc來編譯(但需要linaro)
二、下載toolchain
在linaro官網的下載區https://www.linaro.org/downloads/,進行下載。以最新第七版為例https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/,下載linux版本即可,然後找個地方解壓縮。
三、下載或編譯Qemu
在arch linux中用pacman安裝qemu。
1 | $ sudo pacman -Sy qemu |
或是去https://www.qemu.org/下載qemu,或是用git clone。
1 2 3 4 | $ git clone git: //git .qemu.org /qemu .git qemu.git $ cd qemu.git $ . /configure --target-list=aarch64-softmmu $ make |
之後執行檔會放在qemu.git/aarch64-softmmu/qemu-system-aarch64。
Arch Linux的package預設包含aarch64-softmmu這個target。
四、建立rootfs
其實有很多方法可以建立(busybox),而這裡用buildroot來做。首先取得buildroot。
1 2 3 | git clone git: //git .buildroot.net /buildroot buildroot.git $ cd buildroot.git $ make menuconfig |
以下為範例:
在Target Packages --->選取busybox與[*] Show packages that are also provided by busybox 。
在Filesystem images中選擇[*] cpio the root filesystem (for use as an initial RAM filesystem)。
在toolchain option中選擇外部toolchain如下:
設好外部toolchain路徑gcc版本與linux header版本,必將SSP和RPC開啟。可以檢查.config中是否有:
BR2_TOOLCHAIN_EXTERNAL_HAS_SSP=y
BR2_TOOLCHAIN_EXTERNAL_INET_RPC=y
最後:
$ make
四、下載編譯kernel
去https://www.kernel.org/下載最新kernel。
1 2 3 4 | $ cd linux-5.x.x $ mkdir out_AArch64 $ make ARCH=arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=. /out_AARCH64 defconfig $ make ARCH=arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=. /out_AARCH64 menuconfig |
在.config中修改以下:
1 2 | CONFIG_CROSS_COMPILE= "aarch64-linux-gnu-" # needs to match your cross-compiler prefix CONFIG_INITRAMFS_SOURCE= "path/to/buildroot.git/output/images/rootfs.cpio" # points at your buildroot image |
最後編譯:
1 | $ make ARCH=arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=. /out_AARCH64 Image -j8 |
五、Qemu執行
在out_AArch64資料夾中。1 | $qemu-system-aarch64 -kernel arch /arm64/boot/Image -machine virt -cpu cortex-a57 -machine type =virt -smp 4 -append "console=ttyAMA0" -nographic |
1 | $qemu-system-aarch64 -kernel arch /arm64/boot/Image -machine virt -cpu cortex-a57 -machine type =virt -smp 4 -append "console=ttyAMA0" -nographic -S -s |
在另一個視窗。
1 | $aarch64-linux-gnu-gdb vmlinux |
預設登入帳密:root無密碼。
六、其他
無法開機時請先確認toolchain正確與否。