ubuntu 启动linux arm
QEMU启动ARM linux内核linux下安装如下工具sudo apt-get install qemu libncurses5-dev gcc-arm-linux-gnueabi build-essential到官网下载linux busybox代码编译文件系统cd busybox-1.28.0export ARCH=armexport CROSS_COMPI...
嵌入式行业真的没前途吗?
Windows与Ubuntu跨系统复制粘贴
打开终端,手动输入以下命令,再重启ubuntu系统就可以了
即通过安装VMtools实现了Windows与Ubuntu跨系统复制粘贴,也实现了Ubuntu窗口自适应
sudo apt-get autoremove open-vm-tools
sudo apt-get install open-vm-tools
sudo apt-get install open-vm-tools-desktop
配置虚拟网络编辑器
VMnet0 ---> 桥接模式 --> ubuntu和windows连接外网都使用的是自己独立的IP地址
VMnet1 ---> 主机模式 --> windows可以上网,ubuntu不能上网,windows和ubuntu可以进行网络通信
VMnet8 ---> NAT模式 --> ubuntu和windows连接外网都使用的是同一个IP地址
QEMU启动ARM linux内核
linux下安装如下工具
sudo apt-get install qemu libncurses5-dev gcc-arm-linux-gnueabi build-essential
到官网下载linux busybox代码
编译文件系统
cd busybox-1.28.0
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
make menuconfig
进入menuconfig后,配置成静态编译
busybox settings->
build option->
[*] Build static binary (no shared libs)
make install可编译完成,在busybox-1.28.0目录下会有一个_install目录生成,这个目录是编译好的文件系统需要的一些命令的集合
正确应为,按如上直接make install 下面有错误
make install ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
把_install目录复制到linux-4.1.48目录下,进入_install目录进,先后创建etc,dev等目录
mkdir etc
mkdir dev
mkdir mnt
mkdir -p etc/init.d/
在_install/etc/init.d/ 目录下创建一个rcS文件,并写入如下内容
mkdir -p /proc
mkdir -p /tmp
mkdir -p /sys
mkdir -p /mnt
/bin/mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
需要修改_install/etc/init.d/rcS需要可执行权限,可chmod命令来修改,比如
chmod +x _install/etc/init.d/rcS
在_install/etc目录新创建一个 fstab文件,并写入如下内容
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev tmpfs default 0 0
debugfs /sys/kernel/debug debugfs defaults 0 0
在_install/etc目录新创建一个 inittab文件,并写入如下内容
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
在_install/dev目录下创建如下设备节点,需要root需要权限
cd _install/dev/
sudo mknod console c 5 1
sudo mknod null c 1 3
编译内核
cd linux-4.1.48/
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
make vexpress_defconfig
make menuconfig
配置initramfs,在 initramfs source file中填入_install ,并把Default kernel command string清空
General setup-》
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support (_install) Initramfs source file(s)
Boot options --->
() Default kernel command string 前面括号是空的
配置Memory split 为3G/1G user/kernel split,并打开高端内存
Kernel Features --->
Memory split (3G/1G user/kernel split) --->
[*] High Memory Support
将之前配置的文件保存起来的正确姿势是:
make savedefconfig -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
在源代码根目录下生成defconfig
直接执行以下命令保存更新vexpress_defconfig 配置
mv defconfig arch/arm/configs/vexpress_defconfig
开始编译内核
make bzImage -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
make dtbs
/bin/sh: 1: bc: not found
kernel/time/Makefile:30: recipe for target 'kernel/time/timeconst.h' failed
make[2]: *** [kernel/time/timeconst.h] Error 127
scripts/Makefile.build:403: recipe for target 'kernel/time' failed
make[1]: *** [kernel/time] Error 2
Makefile:950: recipe for target 'kernel' failed
make: *** [kernel] Error 2
root@kali:~/Code/linux-4.1.48# sudo apt-get install bc
安装工具,重新编译即可
编译Linux内核版本4.0.0出现include/linux/compiler-gcc.h:106:30: fatal error: linux/compiler-gcc7.h错误
gdb版本问题,主机为交叉编译为7的版本,内核源码没有对应为7的版本的头文件
arm-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabi/7/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Debian 7.2.0-11' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-multiarch --disable-sjlj-exceptions --with-arch=armv4t --with-float=soft --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --includedir=/usr/arm-linux-gnueabi/include
Thread model: posix
gcc version 7.2.0 (Debian 7.2.0-11)
复制本地主机已跑linux内核源码中的/usr/src/linux-headers-xxxx-generic/include/linux/compiler-gcc.h 到include/linux/compiler-gcc7.h
这里用了我另一个内核版本工程的头文件(在同样环境已编译过版本为4.1.48)
cp /usrsrc/linux-headers-xxxxx-generic/include/linux/compiler-gcc.h include/linux/compiler-gcc7.h
或
cp compiler-gcc.h(已编译ok的4.1.48) include/linux/compiler-gcc7.h
运行QEMU 来模拟4核Cortex-A9的 Versatile Express平台,这个平台是ARM公司提供的,给客户用来基于这个平台做产品原型开发
qemu-system-arm -M vexpress-a9 -smp 4 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic
Failed to execute /linuxrc (error -8)
Starting init: /sbin/init exists but couldn't execute it (error -8)
Starting init: /bin/sh exists but couldn't execute it (error -8)
Kernel panic - not syncing: No working init found. Try passing init= option to kernel. See Linux Documentation/init.txt for guidance.
CPU: 3 PID: 1 Comm: swapper/0 Not tainted 4.1.48 #1
Hardware name: ARM-Versatile Express
发现跑不起来,百度了一下可能是文件格式的问题,于是用file查一下文件格式,发现是X86
~/Code/linux-4.1.48# file _install/bin/busybox
_install/bin/busybox: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e25da6533f4b73917a90a4be1386444cd8fb4ede, stripped
于是重新编译一下busybox生成文件系统
make install ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
再检查一下文件系统,这一下为ARM
~/Code/busybox-1.28.0# file _install/bin/busybox
_install/bin/busybox: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=6faba135dc21de7ac18d0569ffa1427e1346f957, stripped
最后跑起来了,按ENTER键则进入文件系统
input: AT Raw Set 2 keyboard as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
ledtrig-cpu: registered to indicate activity on CPUs
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
aaci-pl041 10004000.aaci: FIFO 512 entries
oprofile: using arm/armv7-ca9
NET: Registered protocol family 17
9pnet: Installing 9P2000 support
Registering SWP/SWPB emulation handler
rtc-pl031 10017000.rtc: setting system clock to 2018-01-06 11:42:18 UTC (1515238938)
ALSA device list:
#0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
Freeing unused kernel memory: 1400K
tmpfs: No value for mount option 'default'
mount: mounting tmpfs on /dev failed: Invalid argument
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
Please press Enter to activate this console.
/ #
/ #
/ # ls
bin etc mnt sbin tmp
dev linuxrc proc sys usr
/ #
再开一个终端输入
killall qemu-system-arm
就可以关闭QEMU平台
编译内核包含调试信息
Kernel hacking >
Compile-time checks and compiler options
[*] Compile the kernel with debug info
qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -S -s
-S表示QEMU虚拟机会冻结CPU,直到远程GDB输入命令
-s表示在默认的1234端口接受GDB远程调试
~/Code/linux-4.1.48# qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -S -s
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
安装arm gdb工具
更新源
vi /etc/apt/sources.list
deb http://ftp.de.debian.org/debian jessie main
apt-get install gdb-arm-none-eabi
调试:
arm-none-eabi-gdb --tui vmlinux
ps:target remote localhost:1234 这里为
target remote kali:1234
除了上述用gdb自带的界面来调试外,还可以用eclipse来调试内核
安装c/c++版本 eclipse
apt-get install eclipse-cdt
以root权限开启eclipse并打开已有工程
配置debug配置选项:注意选择gdb为:arm-none-eabi-gdb
开始调试,确保以下命令执行,即调试状态
qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -S -s
其中上面的运行变量可以修改为16进制查看
在Eclipse中调试时,显示变量的16进制值
可用快捷键这里为F6为next F5为step into
点击菜单“Window”-“Preferences”,如图:
使用单步调试时会看到执行顺序在C语言源代码里面跳来跳去,是GCC对代码进行了优化造成的,优化后的代码执行顺序与源代码的顺序就有出入了。提高运行效率与代码紧凑度,对于调试学习内核不好
1,优化级别从-O2改为-O1
修改内核源代码根目录下的Makefile
all: vmlinux
ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
KBUILD_CFLAGS += -Os
else
KBUILD_CFLAGS += -O1
endif
CONFIG_CC_OPTIMIZE_FOR_SIZE默认是不定义的,所以修改红色部分为-O1,优化级别为1级,只做基本的优化
2 禁止inline后会是一次真正的函数调用
打开CONFIG_DEBUG_SECTION_MISMATCH配置宏
更多推荐
所有评论(0)