用Qemu运行/调试arm linux【转】

时间:2024-05-24 08:03:25

转自:https://blog.****.net/absurd/article/details/78984244

用Qemu运行/调试arm linux,这事情干过好几次了,久了就忘记了,每次都要重新去查资料,浪费不少时间。这次把整个过程弄成个脚本,放到github上,需要是拿下来运行一下即可,方便自己也方便有需要的朋友。这里解释一下脚本的内容(运行系统Ubuntu 16.04 LTS):

. 设置一些环境变量,可以根据自己需要修改。
export ARCH=arm
export MACHINE=vexpress
export TARGET=arm-linux
export GDB_VERSION=8.0
export KERNEL_VERSION=4.4.
export BUSYBOX_VERSION=1.25.
export CROSS_COMPILE=arm-linux-gnueabihf- . 安装工具链等软件包
function prepare() {
apt-get -y install qemu gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libexpat1-dev libncurses5-dev
} . 下载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
} . 编译Linux内核
function build_linux() {
echo "build_linux ..."
cd linux-${KERNEL_VERSION}
make ${MACHINE}_defconfig
make zImage -j8
make modules -j8
make dtbs
cd -
} . 拷贝内核到独立目录
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上。 参考资料(非常感谢)
http://files.meetup.com/1590495/debugging-with-qemu.pdf
http://blog.****.net/nxcxl88/article/details/53244754
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.****.net/absurd/article/details/78984244

用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-
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 安装工具链等软件包

function prepare() {
apt-get -y install qemu gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libexpat1-dev libncurses5-dev
}
  • 1
  • 2
  • 3

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4. 编译Linux内核

function build_linux() {
echo "build_linux ..."
cd linux-${KERNEL_VERSION}
make ${MACHINE}_defconfig
make zImage -j8
make modules -j8
make dtbs
cd -
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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/
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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 -
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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 -
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

11. 生成gdb启动脚本

function gen_gdb_init() {
echo "file linux-${KERNEL_VERSION}/vmlinux" >gdbinit
echo "dir linux-${KERNEL_VERSION}" >>gdbinit
echo "target remote :1234" >>gdbinit
}
  • 1
  • 2
  • 3
  • 4
  • 5

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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

github

还包括其它一些脚本和使用说明,一并放在https://github.com/xianjimli/qemu-arm-linux上。

参考资料(非常感谢)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.****.net/absurd/article/details/78984244