ubuntu上建立交叉编译环境,在qemu上运行ARM镜像

时间:2021-12-31 08:53:31

以下内容,仅供参考。如果需要详细解释的,可能不适合你花时间;如果看着Linux的命令和蹩脚英语的内容不反胃的话,您可以花点时间。

premise: 
(a) directory ~/embeddedLinux  will hold download Linux kernel, busybox, crosstool-ng
(b) directory ~/arm-busybox will hold the generated busybox root fs image
(c) directory ~/x-tools will hold the cross toolchains from crosstool-ng
(d) directory ~/ will hold the final zImage and rootfs.img.gz.
(e) qemu will be started at directory ~/



############################################################################
1. download crosstool-ng 1.22.0
############################################################################
ocean@ocean-bel:~$ mkdir embeddedLinux
ocean@ocean-bel:~$ ls
Desktop    Downloads      examples.desktop  Pictures  Templates  Workspace
Documents  embeddedLinux  Music             Public    Videos
ocean@ocean-bel:~$ cd embeddedLinux/
ocean@ocean-bel:~/embeddedLinux$ ls
ocean@ocean-bel:~/embeddedLinux$ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.22.0.tar.bz2
--2016-11-12 11:23:38--  http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.22.0.tar.bz2
Resolving crosstool-ng.org (crosstool-ng.org)... 140.211.15.129
Connecting to crosstool-ng.org (crosstool-ng.org)|140.211.15.129|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1621476 (1.5M) [application/x-bzip]
Saving to: ‘crosstool-ng-1.22.0.tar.bz2’

crosstool-ng-1.22.0 100%[===================>]   1.55M   585KB/s    in 2.7s    

2016-11-12 11:23:41 (585 KB/s) - ‘crosstool-ng-1.22.0.tar.bz2’ saved [1621476/1621476]

ocean@ocean-bel:~/embeddedLinux$ ls -al
total 1592
drwxrwxr-x  2 ocean ocean    4096 Nov 12 11:23 .
drwxr-xr-x 18 ocean ocean    4096 Nov 12 11:23 ..
-rw-rw-r--  1 ocean ocean 1621476 Nov 20  2015 crosstool-ng-1.22.0.tar.bz2
ocean@ocean-bel:~/embeddedLinux$ 
ocean@ocean-bel:~/embeddedLinux$ 
ocean@ocean-bel:~/embeddedLinux$ 
ocean@ocean-bel:~/embeddedLinux$ 
ocean@ocean-bel:~/embeddedLinux$ tar xjvf crosstool-ng-1.22.0.tar.bz2 
ocean@ocean-bel:~/embeddedLinux$ cd crosstool-ng/
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ls
bootstrap  configure     contrib  ct-ng.comp  docs     LICENSES    Makefile.in  README.md  scripts   TODO
config     configure.ac  COPYING  ct-ng.in    kconfig  licenses.d  patches      samples    steps.mk


ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ sudo apt-get install ncurses-dev bison texinfo flex autoconf automake libtool libexpat1-dev libncurses5-dev patch curl cvs build-essential subversion gawk python-dev gperf

ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./configure --enable-local


ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ make


ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ls
bootstrap   config.log     configure.ac  ct-ng       docs      licenses.d   patches   README.md  steps.mk
config      config.status  contrib       ct-ng.comp  kconfig   Makefile     paths.mk  samples    TODO
config.gen  configure      COPYING       ct-ng.in    LICENSES  Makefile.in  paths.sh  scripts
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ 

ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./ct-ng help
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./ct-ng list-samples
Status  Sample name
  MKDIR config.gen
  IN    config.gen/arch.in
  IN    config.gen/kernel.in
  IN    config.gen/cc.in
  IN    config.gen/binutils.in
  IN    config.gen/libc.in
  IN    config.gen/debug.in
[L..]   alphaev56-unknown-linux-gnu
[L..]   alphaev67-unknown-linux-gnu
[L..]   arm-bare_newlib_cortex_m3_nommu-eabi
[L..]   arm-cortex_a15-linux-gnueabi
[L.X]   arm-cortexa5-linux-uclibcgnueabihf
[L..]   arm-cortex_a8-linux-gnueabi
[L.X]   arm-cortexa9_neon-linux-gnueabihf
[L..]   armeb-unknown-eabi
[L..]   armeb-unknown-linux-gnueabi
[L..]   armeb-unknown-linux-uclibcgnueabi
[L..]   arm-unknown-eabi
[L..]   arm-unknown-linux-gnueabi
[L.X]   arm-unknown-linux-musleabi
[L..]   arm-unknown-linux-uclibcgnueabi
[L.X]   arm-unknown-linux-uclibcgnueabihf
[L..]   armv6-rpi-linux-gnueabi
[L..]   armv7-rpi2-linux-gnueabihf
[L..]   avr
[L..]   i586-geode-linux-uclibc
[LB.]   i586-mingw32msvc,i686-none-linux-gnu
[L..]   i686-nptl-linux-gnu
[L.X]   i686-w64-mingw32
[L..]   m68k-unknown-elf
[L..]   m68k-unknown-uclinux-uclibc
[L..]   mips64el-n32-linux-uclibc
[L..]   mips64el-n64-linux-uclibc
[L..]   mips-ar2315-linux-gnu
[L..]   mipsel-sde-elf
[L..]   mipsel-unknown-linux-gnu
[L..]   mips-malta-linux-gnu
[L..]   mips-unknown-elf
[L..]   mips-unknown-linux-uclibc
[L.X]   i686-w64-mingw32,nios2-spico-elf
[L..]   powerpc-405-linux-gnu
[L..]   powerpc64-unknown-linux-gnu
[L..]   powerpc-860-linux-gnu
[L..]   powerpc-e300c3-linux-gnu
[L..]   powerpc-e500v2-linux-gnuspe
[L..]   powerpc-unknown-linux-gnu
[LB.]   powerpc-unknown-linux-uclibc
[L..]   powerpc-unknown_nofpu-linux-gnu
[L.X]   s390-ibm-linux-gnu
[L..]   s390x-ibm-linux-gnu
[L..]   sh4-unknown-linux-gnu
[L..]   sparc-unknown-linux-gnu
[L.X]   x86_64-w64-mingw32,x86_64-pc-linux-gnu
[L..]   x86_64-unknown-linux-gnu
[L..]   x86_64-unknown-linux-uclibc
[L.X]   x86_64-w64-mingw32
[L..]   xtensa-unknown-linux-uclibc
 L (Local)       : sample was found in current directory
 G (Global)      : sample was installed with crosstool-NG
 X (EXPERIMENTAL): sample may use EXPERIMENTAL features
 B (BROKEN)      : sample is currently broken




ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./ct-ng arm-unknown-linux-uclibcgnueabi
  IN    config.gen/arch.in
  IN    config.gen/kernel.in
  IN    config.gen/cc.in
  IN    config.gen/binutils.in
  IN    config.gen/libc.in
  CONF  config/config.in
#
# configuration written to .config
#

***********************************************************

Initially reported by: Thomas Petazzoni
URL: https://plus.google.com/+ThomasPetazzoni

***********************************************************

Now configured for "arm-unknown-linux-uclibcgnueabi"
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ 


ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./ct-ng menuconfig
  --> Paths and misc options 
                 ---> Local tarballs directory (default is ${HOME}/src)
                 ---> Prefix directory (default the Prefix directory will be ${HOME}/x-tools/${CT_TARGET})
                 ---> Maximum log level to see: DEBUG 
  --> Toolchain options
                 ---> Tuple's alias: arm-linux
                 
  --> Debug facilities
                 * do not select 'dmalloc' and 'duma'. select 'gdb', 'ltrace' and 'strace'
                 ---> gdb: do not select 'cross-gdb' and 'native-gdb'. select 'gdbserver' and 'Build a static gdbserver'
                 

  Save and Exit.  The .config file will be saved.
                 
               
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ pwd
/home/ocean/embeddedLinux/crosstool-ng
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ls
bootstrap   config.log     configure.ac  ct-ng       docs      licenses.d   patches   README.md  steps.mk
config      config.status  contrib       ct-ng.comp  kconfig   Makefile     paths.mk  samples    TODO
config.gen  configure      COPYING       ct-ng.in    LICENSES  Makefile.in  paths.sh  scripts
ocean@ocean-bel:~/embeddedLinux/crosstool-ng$ ./ct-ng build



Note:  the cross tool chains will be found at /home/ocean/x-tools


---add cross toolchains to the source file /etc/bash.bashrc
ocean@ocean-bel:~$ vi /etc/bash.bashrc 
...
# Added path for cross compiling toolchains
export PATH=$PATH:/home/ocean/x-tools/arm-unknown-linux-uclibcgnueabi/bin

ocean@ocean-bel:~$ source /etc/bash.bashrc 



############################################################################
2.  download linux kernel from kernel.org and use 'vexpress_defconfig'
############################################################################
(1) download linux kernel from kernel.org
  wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.8.4.tar.gz

(2) use vexpress_defconfig

ocean@ocean-bel:~/embeddedLinux/linux-4.8.4$ make vexpress_defconfig ARCH=arm CROSS_COMPILE=arm-linux-  
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  SHIPPED scripts/kconfig/zconf.hash.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
#
# configuration written to .config
#


Note: the vexpress_defconfig can be found under linux-4.8.4/arch/arm/configs/


(3) build kernel

ocean@ocean-bel:~/embeddedLinux/linux-4.8.4$ make all ARCH=arm CROSS_COMPILE=arm-linux- CONFIG_DEBUG_INFO=y


(4) find the generated zImage under arch/arm/boot/
ocean@ocean-bel:~/embeddedLinux/linux-4.8.4/arch/arm/boot$ ls
bootp       dts      Image  initramfs   Makefile
compressed  hello.c  init   install.sh  zImage


(5) copy zImage to ~/
ocean@ocean-bel:~$ cp embeddedLinux/linux-4.8.4/arch/arm/boot/zImage .



############################################################################
3. build busybox
############################################################################
(1) download busybox
ocean@ocean-bel:~$  wget http://busybox.net/downloads/busybox-1.25.1.tar.bz2
 
 
(2) use 'defconfig'
ocean@ocean-bel:~$  make ARCH=arm CROSS_COMPILE=arm-linux- defconfig
 
(3) make menuconfig and set option to set busybox as a static binary
ocean@ocean-bel:~$  make ARCH=arm CROSS_COMPILE=arm-linux- menuconfig
 
  --> Busybox Settings ---> Build Options ---> [*] Build BusyBox as a static binary (no shared libs)

  --> Networking Utilities ---> []Enable IPv6 support
                           ---> []nslookup
  
  
(4) build busybox and put the generated busybox files to '/home/ocean/arm-busybox'
ocean@ocean-bel:~$  make CONFIG_PREFIX=/home/ocean/arm-busybox ARCH=arm CROSS_COMPILE=arm-linux- install



(5) locate at directory '/home/ocean/arm-busybox'
 ocean@ocean-bel:~/arm-busybox$ pwd
 /home/ocean/arm-busybox

 (a) mkdir -p proc sys dev etc etc/init.d
 (b) create file etc/init.d/rcS
  ocean@ocean-bel:~/arm-busybox/etc/init.d$ ls -al
  total 12
  drwxrwxr-x 2 ocean ocean 4096 Nov 13 20:21 .
  drwxrwxr-x 3 ocean ocean 4096 Nov 12 17:20 ..
  -rwxrwxr-x 1 ocean ocean  154 Nov 13 20:21 rcS
  ocean@ocean-bel:~/arm-busybox/etc/init.d$ cat rcS 
  #!/bin/sh

  mount -t proc none /proc
  mount -t sysfs none /sys

  echo -e "\n Boot took $(cut -d '' -f1 /proc/uptime) seconds\n"

  /sbin/mdev -s
  #exec /bin/sh


  (c) chmod +x etc/init.d/rcS
  
  
(6) create root fs image file and generate rootfs.img.gz to ~/
  --commands to generate the fs image file-----
  find . | cpio -o --format=newc > ../rootfs.img
  cd ..
  gzip -c rootfs.img > rootfs.img.gz
  ----------------------------------------------
  
  ocean@ocean-bel:~/arm-busybox$ pwd
  /home/ocean/arm-busybox
  ocean@ocean-bel:~/arm-busybox$ ls
  bin  dev  etc  linuxrc  proc  sbin  sys  usr  
  ocean@ocean-bel:~/arm-busybox$ find . | cpio -o --format=newc > ../rootfs.img
  2265 blocks
  ocean@ocean-bel:~/arm-busybox$ cd ..
  ocean@ocean-bel:~$ gzip -c rootfs.img > rootfs.img.gz
  ocean@ocean-bel:~$ ls
  arm-busybox  Downloads         Music     rootfs.img     Videos
  Desktop      embeddedLinux     Pictures  rootfs.img.gz  x-tools
  Documents    examples.desktop  Public    Templates      zImage
  ocean@ocean-bel:~$ 


############################################################################
4. start qemu

ref link: http://ronubo.blogspot.be/2016/02/linux-on-arm-emulator-bringup-building.html
notes: in the kernel 4.x version, qemu should be passed with 
dtb (which is built from the correspondig   
arch/arm/boot/dts/<your_board_variant>.dts) 
############################################################################

For vexpress board: 

qemu-system-arm -M vexpress-a9  -m 512M -kernel zImage -initrd rootfs.img.gz  -serial stdio -append "root=/dev/ram rdinit=/bin/sh console=tty1" -dtb /home/ocean/embeddedLinux/linux-4.8.4/arch/arm/boot/dts/vexpress-v2p-ca9.dtb