Linux
由於使用 apt 工具安裝的 QEMU 並不支援 Pi 3,因此我們要自己手動編譯 2.12.0 版本的 QEMU
apt-get install gcc build-essential automake gcc-arm-linux-gnueabihf vim git wget python pkg-config zlib1g-dev libglib2.0-dev libpixman-1-dev flex bison unzip libncurses5-dev
$ 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
$ git clone git://github.com/raspberrypi/linux.git --branch raspberrypi-kernel_1.20180619-1 --single-branch --depth 1
linux-arm.patch
$ wget https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/tools/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
$ wget https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/tools/config_file
$ wget https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/tools/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
$ cp linux/vmlinux ./vmlinux
有沒有發現上面編譯Raspberry Pi Kernel時有個參數CROSS_COMPILE=arm-linux-gnueabihf-
呢?
一般的編譯器
那甚麼是交叉編譯器呢?
你會將你在電腦上跑的程式直接放到手機上去跑嗎?不會吧
為什麼我們要交叉編譯器呢? Code Ref
額外補充
如果你在Docker中執行
$ vncviewer 0:5900
$ sudo apt-get install vncviewer
如果是在有圖型桌面環境下的話
如果是想要輸出到終端機的話
Raspberry Pi 預設帳號密碼
pi
raspberry
離開 QEMU
Ctrl-A + X
由於使用 apt 工具安裝的 GDB 並不支援 ARM,因此我們要自己手動編譯 8.1 版本的 GDB
$ tar zxvf gdb-8.1.tar.gz
$ cd gdb-8.1
$ ./configure --target=arm-linux-gnueabihf
--target-list
: 目標架構--prefix
: 指定程式要安裝的路徑./configure --help
得知$ make -j$(nproc)
$ sudo make install
$ echo "export PATH=$(pwd)/bin:\$PATH" >> ~/.bashrc
$ source ~/.bashrc
$ arm-linux-gnueabihf-gdb vmlinux
(gdb) target remote localhost:1234
qemu {OTHER_OPTION} -S -s(or -gdb tcp::1234)
(gdb) target remote localhost:1234
(gdb) b
(gdb) info b
or (gdb) i b
(gdb) continue
or (gdb) c
(gdb) list
or (gdb) l
(gdb) step
or (gdb) s
(gdb) si
(gdb) next
or (gdb) n
(gdb) until
or (gdb) u
(gdb) print
or (gdb) p
,如 p str(gdb) info register
or (gdb) i r
x/nyz