Linux
$ sudo apt install qemu
$ wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip
$ unzip 2018-11-13-raspbian-stretch.zip
2018-11-13-raspbian-stretch.img
由於 QEMU 和真實的 Pi 還是有些不同的,因此無法使用 2018-11-13-raspbian-stretch.zip 裡面提供的 kernel 來開機,需要自行編出 QEMU 用的 kernel
$ curl https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/kernel-qemu-4.4.34-jessie --output kernel-qemu-4.4.34-jessie
由於使用 apt 工具安裝的 QEMU 並不支援 Pi 3,因此我們要自己手動編譯 2.12.0 版本的 QEMU
$ sudo apt install gcc build-essential automake gcc-arm-linux-gnueabihf
$ wget https://download.qemu.org/qemu-2.12.0-rc3.tar.xz
$ tar xvf qemu-2.12.0-rc3.tar.xz
$ cd qemu-2.12.0-rc3
$ ./configure --target-list=arm-softmmu,aarch64-softmmu
--target-list
: 指令想要編譯的模擬器--prefix
: 指定程式要安裝的路徑./configure --help
得知$ make -j$(nproc)
$ make install
$ echo "export PATH=$(pwd)/aarch64-softmmu:\$PATH" >> ~/.bashrc
$ source ~/.bashrc
$ wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip
$ unzip 2018-11-13-raspbian-stretch.zip
2018-11-13-raspbian-stretch.img
由於 QEMU 和真實的 Pi 還是有些不同的,因此無法使用 2018-11-13-raspbian-stretch.zip 裡面提供的 kernel 來開機,需要自行編出 QEMU 用的 kernel,而在這邊參考了 dhruvvyas90/qemu-rpi-kernel 的編譯方式
$ git clone git://github.com/raspberrypi/linux.git --branch raspberrypi-kernel_1.20180619-1 --single-branch --depth 1
linux-arm.patch
$ cd linux
$ patch -p1 < ./linux-arm.patch
$ KERNEL=kernel7
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- versatile_defconfig
config_file
和 config_ip_tables
$ cat ./config_file >> .config
$ cat ./config_ip_tables >> .config
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bzImage dtbs
$ cd ..
$ cp linux/arch/arm/boot/zImage ./kernel7.img
$ cp linux/arch/arm/boot/dts/versatile-pb.dtb ./versatile-pb.dtb
pi
raspberry
Ctrl-A + X
有時候想要不藉由開機,希望直接從外面改裡面的程式,故使用之
losetup
設定 loop device
$ losetup -P -f --show XXX.img
$ losetup -l
$ mkdir one two
$ sudo mount -o loop /dev/loop0p1 ./one
$ sudo mount -o loop /dev/loop0p2 ./two
mount -l
查看若發生進不去 OS 的情況 (e.g. Kernel panic, …),就需要藉由外部 debugger 找尋錯誤
qemu-system-*
後面添加 -S -gdb tcp::1234
arm-linux-gnueabihf-gdb -q
進入 GDB(gdb) target remote localhost:1234
即可