Guidelines for pit hopping in QEMU MIPS environment

Posted by jebster on Fri, 14 Jan 2022 11:16:19 +0100

  1. qemu installation

    There are many ways to install. For ubuntu/debian users, you can directly install apt

    Official website: https://www.qemu.org/download/

    sudo apt-get install zlib1g-dev
    sudo apt-get install libglib2.0-0
    sudo apt-get install libglib2.0-dev
    sudo apt-get install libtool
    sudo apt-get install libsdl1.2-dev
    sudo apt-get install libpixman-1-dev
    sudo apt-get install autoconf
    sudo apt-get install qemu 
    sudo apt-get install qemu-user-static
    sudo apt-get install qemu-system
    

    Or use the source installation method

    https://github.com/qemu/qemu

  2. Configuration of build root cross compiling environment

Download builderoot

   wget http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2
   tar -jxvf buildroot-snapshot.tar.bz2
   cd buildroot

Configure builderoot

   sudo apt-get install libncurses-dev patch
   make clean
   make menuconfig

In the interface that appears, "Target Architecture" is changed to "MIPS", and the big end and the small end can choose by themselves. In addition, in "Toolschain", the version of "Kernel Headers" should be changed to the version of their own system

You can use {uname -r} to query

My kernel version is 4.13.0-36-generic

The sad thing is that there is no 4.13 in the Kernel Headers option x……

At this time, select "Manually specified Linux version" and manually specify it as 4.13 in the "Custom kernel headers series" below x

Execute the following command after exiting

   sudo apt-get install texinfo
   sudo apt-get install bison
   sudo apt-get install flex
   sudo make

During compilation... It will be slow Wait patiently ==

One thing to note is that I encountered a problem when compiling

Some of these files should have been linux-4.13 when downloaded tar. XZ but there seems to be something wrong with Makefile. The access becomes Linux - tar. xz

My approach to this issue is:

 1. Manual Download linux-4.13.tar.xz

 2. Manually save as

    ```buildroot/output/build/linux-headers/.stmap_download```

    and

    ``buildroot/dl/linux/linux-.tar.xz``

3. Continue compiling

After compilation, the elf file under mips architecture can be compiled by using the mipsel Linux GNU GCC instruction

At the same time, the dependency libraries required for mips program execution are also saved in / usr / mipsel Linux GNU / lib

  1. Network environment configuration of qemu virtual machine

    Install dependencies first

    sudo apt-get install bridge-utils uml-utilities

    Here, I choose to use the bridge method to interconnect the host computer with the virtual machine, and then link the virtual machine with the external network through NAT

    Note that if NAT is not set, the virtual machine cannot access the Internet, which is also a problem not mentioned in many articles (I stayed in the pit for a long time)

    sudo brctl addbr br0
    sudo ifconfig br0 192.168.122.1/24 up
    sudo tunctl -t tap0
    sudo ifconfig tap0 192.168.122.11/24 up
    sudo brctl addif br0 tap0
    

    Download the image of qemu virtual machine

    https://people.debian.org/~aurel32/qemu/mips/
    

    The kernel and image I chose were vmlinux-3.2.0-4-4kc-malta and debian_wheezy_mips_standard.qcow2

    The default user name and password are root/root

    Then configure DHCP and DNS services locally

    sudo dnsmasq --strict-order --except-interface=lo --interface=br0 --listen-address=192.168.122.1 --bind-interfaces --dhcp-range=192.168.122.2,192.168.122.254 --conf-file="" --pid-file=/var/run/qemu-dhcp-virbr0.pid --dhcp-leasefile=/var/run/qemu-dhcp-virbr0.leases --dhcp-no-override

    Now use

    sudo qemu-system-mips64 -M malta -kernel vmlinux-3.2.0-4-4kc-malta -hda debian_wheezy_mips_standard.qcow2 -append "root=/dev/sda1 console=tty0" -netdev tap,id=tapnet,ifname=tap0,script=no -device rtl8139,netdev=tapnet -nographic

    Command to start the virtual machine, so that the virtual machine can communicate with the host computer

    Next, configure the Internet connection

    Turn on port forwarding

    sudo sysctl -w net.ipv4.ip_forward=1
    sudo sysctl -p /etc/sysctl.conf
    

    Configure iptables

    sudo iptables -t nat -A POSTROUTING -s "192.168.122.0/255.255.255.0" ! -d "192.168.122.0/255.255.255.0" -j MASQUERADE
    sudo iptables -N vm-service
    sudo iptables -A vm-service -j ACCEPT
    sudo iptables -A FORWARD -s 192.168.122.0/24 -j   vm-service
    

    Then use the above command to start the virtual machine, and you can communicate with the external network

     
  2. QEMU mipsel runs dynamically compiled programs

    QEMU mipsel usually runs programs. When encountering dynamically compiled programs, it will prompt that the dependent library is missing

    The methods given in the book and on the Internet are chroot, but there are several holes when using this command

    First copy the lib library compiled by builderoot to the program folder

    Then execute the chroot command

       cp $(which qemu-mipsel) .
       sudo chroot . ./qemu-mipsel hello
    

    The lib library has been copied to the directory, but the dependent library is still missing during execution

    At this time, the high probability is that qemu mipsel lacks a dependency Library One solution is to view and copy the qemu dependency library through the ldd instruction. The other method I recommend is to directly use the statically compiled qemu mipsel static

       cp $(which qemu-mipsel-static) .
       sudo chroot . ./qemu-mipsel-static hello
    

So far My qemu installation and pit climbing are over. If there is any problem, please forgive me (tell me)

Topics: debian