用Qemu运行/调试arm linux
用Qemu运行/调试arm linux,这事情干过好几次了,久了就忘记了,每次都要重新去查资料,浪费不少时间。这次把整个过程弄成个脚本,放到github上,需要是拿下来运行一下即可,方便自己也方便有需要的朋友。这里解释一下脚本的内容(运行系统Ubuntu 16.04 LTS):
1. 设置一些环境变量,可以根据自己需要修改。
export ARCH=arm
export MACHINE=vexpress
export TARGET=arm-linux
export GDB_VERSION=8.0
export KERNEL_VERSION=4.4.1
export BUSYBOX_VERSION=1.25.1
export CROSS_COMPILE=arm-linux-gnueabihf-
2. 安装工具链等软件包
function prepare() {
apt-get -y install qemu gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libexpat1-dev libncurses5-dev
}
3. 下载Linux内核并解压
function download_linux() {
if [ -e linux-${KERNEL_VERSION}.tar.xz ]
then
echo linux-${KERNEL_VERSION}.tar.xz exist
else
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-${KERNEL_VERSION}.tar.xz
fi
if [ -e linux-${KERNEL_VERSION} ]
then
echo linux-${KERNEL_VERSION} exist
else
tar xf linux-${KERNEL_VERSION}.tar.xz
fi
}
4. 编译Linux内核
function build_linux() {
echo "build_linux ..."
cd linux-${KERNEL_VERSION}
make ${MACHINE}_defconfig
make zImage -j8
make modules -j8
make dtbs
cd -
}
5. 拷贝内核到独立目录
function copy_linux() {
rm -rf arm-linux-kernel
mkdir arm-linux-kernel
cp -fv linux-${KERNEL_VERSION}/arch/arm/boot/zImage arm-linux-kernel/
cp -fv linux-${KERNEL_VERSION}/arch/arm/boot/dts/*ca9.dtb arm-linux-kernel/
cp -fv linux-${KERNEL_VERSION}/.config arm-linux-kernel/
}
6. 下载并解压busybox
function download_busybox() {
if [ -e busybox-${BUSYBOX_VERSION}.tar.bz2 ]
then
echo busybox-${BUSYBOX_VERSION}.tar.bz2 exist
else
wget https://busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2
fi
if [ -e busybox-${BUSYBOX_VERSION} ]
then
echo busybox-${BUSYBOX_VERSION} exist
else
tar xf busybox-${BUSYBOX_VERSION}.tar.bz2
fi
}
7. 编译busybox
function build_busybox() {
echo "build_busybox..."
cd busybox-${BUSYBOX_VERSION}
make defconfig
make CROSS_COMPILE=${CROSS_COMPILE}
make install CROSS_COMPILE=${CROSS_COMPILE}
cd -
}
8. 制作rootfs
function make_rootfs() {
rm -rf tmpdir
rm -rf rootfs
mkdir -p rootfs/lib
mkdir -p rootfs/dev
cp -Pvf /usr/arm-linux-gnueabihf/lib/* rootfs/lib
cp busybox-${BUSYBOX_VERSION}/_install/* rootfs/ -rf
dd if=/dev/zero of=rootfs.ext3 bs=1M count=128
mkfs.ext3 rootfs.ext3
mkdir tmpdir
mount -t ext3 rootfs.ext3 tmpdir/ -o loop
cp -r rootfs/* tmpdir/
mknod tmpdir/dev/tty1 c 4 1
mknod tmpdir/dev/tty2 c 4 2
mknod tmpdir/dev/tty3 c 4 3
mknod tmpdir/dev/tty4 c 4 4
umount tmpdir
}
9. 下载并解压gdb
function download_gdb() {
if [ -e gdb-${GDB_VERSION}.tar.gz ]
then
echo gdb-${GDB_VERSION}.tar.gz exist
else
wget http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.gz
fi
if [ -e gdb-${GDB_VERSION} ]
then
echo gdb-${GDB_VERSION} exist
else
tar xf gdb-${GDB_VERSION}.tar.gz
fi
}
10. 编译gdb
function build_gdb() {
echo "build_gdb..."
cd gdb-${GDB_VERSION}
./configure --target=${TARGET} --enable-sim --prefix=/usr/local/
make
cp -fv gdb-${GDB_VERSION}/gdb/gdb /usr/local/bin/arm-linux-gdb
cd -
}
11. 生成gdb启动脚本
function gen_gdb_init() {
echo "file linux-${KERNEL_VERSION}/vmlinux" >gdbinit
echo "dir linux-${KERNEL_VERSION}" >>gdbinit
echo "target remote :1234" >>gdbinit
}
12. 汇总
function build() {
prepare
download_linux
build_linux
copy_linux
download_busybox
build_busybox
make_rootfs
download_gdb
build_gdb
gen_gdb_init
}
build
echo "Done"
github
还包括其它一些脚本和使用说明,一并放在https://github.com/xianjimli/qemu-arm-linux上。