2019年6月23日 星期日

Build the Linux Kernel and Busybox for ARM and run them on QEMU



安裝corss-compiler
brook@vista:~/qemu$ wget http://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz 
brook@vista:~/qemu$ tar xvf gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
brook@vista:~/qemu$ sudo mv gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf /opt/

編譯kernel
brook@vista:~/qemu$ cd linux-arm
brook@vista:~/qemu/linux-arm$ export ARCH=arm
brook@vista:~/qemu/linux-arm$ export CROSS_COMPILE=arm-linux-gnueabihf-
brook@vista:~/qemu/linux-arm$ export PATH=/opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf/bin:$PATH
brook@vista:~/qemu/linux-arm$ make vexpress_defconfig
brook@vista:~/qemu/linux-arm$ make -j8

編譯busybox
brook@vista:~/qemu/busybox$ export ARCH=arm
brook@vista:~/qemu/busybox$ export CROSS_COMPILE=arm-linux-gnueabihf-
brook@vista:~/qemu/busybox$ make O=/home/brook/qemu/busybox-build menuconfig
--> change to static busybox

brook@vista:~/qemu/busybox$ cd ../busybox-build
brook@vista:~/qemu/busybox-build$ make -j8
brook@vista:~/qemu/busybox-build$ cp busybox ../initrd-arm/bin/busybox

generate initrd-image
brook@vista:~/qemu$ ./linux-arm/usr/gen_initramfs_list.sh -d initrd-arm > /tmp/brook_initramfs_list
brook@vista:~/qemu$ ./linux-arm/usr/gen_init_cpio /tmp/brook_initramfs_list > initrd-arm.img


執行QEMU
brook@vista:~/qemu$ /usr/local/bin/qemu-system-arm -M vexpress-a9 -m 512M -kernel ./linux-arm/arch/arm/boot/zImage -dtb ./linux-arm/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -initrd ./initrd-arm.img -nographic -append "console=ttyAMA0"



    參考資料:
  1. http://albert-oma.blogspot.com/2017/12/qemu-arm-linux.html, [QEMU] 模擬一個實際的 ARM Linux 嵌入式系統
  2. https://www.centennialsoftwaresolutions.com/blog/build-the-linux-kernel-and-busybox-for-arm-and-run-them-on-qemu, Build the Linux Kernel and Busybox for ARM and run them on QEMU





2019年6月15日 星期六

Build QEMU 4 on ubuntu 16.04


brook@vista$ ./configure --target-list="arm-softmmu,i386-softmmu,x86_64-softmmu,arm-linux-user,i386-linux-user,x86_64-linux-user" --enable-debug --enable-sdl --enable-kvm --enable-curl --enable-snappy --enable-tools

ERROR: User requested feature sdl
       configure was not able to find it.
       Install SDL2-devel

brook@vista$ sudo apt-get install libsdl2-2.0-0

brook@vista$ ./configure --target-list="arm-softmmu,i386-softmmu,x86_64-softmmu,arm-linux-user,i386-linux-user,x86_64-linux-user" --enable-debug --enable-sdl --enable-kvm --enable-curl --enable-snappy --enable-tools
Install prefix    /usr/local
BIOS directory    /usr/local/share/qemu
firmware path     /usr/local/share/qemu-firmware
binary directory  /usr/local/bin
library directory /usr/local/lib
module directory  /usr/local/lib/qemu
libexec directory /usr/local/libexec
include directory /usr/local/include
config directory  /usr/local/etc
local state directory   /usr/local/var
Manual directory  /usr/local/share/man
ELF interp prefix /usr/gnemul/qemu-%M
Source path       /home/brook/qemu4/qemu-4.0.0-rc4
GIT binary        git
GIT submodules
C compiler        cc
Host C compiler   cc
C++ compiler      c++
Objective-C compiler cc
ARFLAGS           rv
CFLAGS            -g

...
module support    no
host CPU          x86_64
host big endian   no
target list       arm-softmmu i386-softmmu x86_64-softmmu arm-linux-user i386-linux-user x86_64-linux-user
gprof enabled     no
sparse enabled    no
strip binaries    no
profiler          no
static build      no
SDL support       yes (2.0.4)
SDL image support no
GTK support       no
GTK GL support    no
VTE support       no
TLS priority      NORMAL
GNUTLS support    no
libgcrypt         no
nettle            no
libtasn1          no
PAM               no
iconv support     yes
curses support    yes
virgl support     no
curl support      yes
mingw32 support   no
Audio drivers     pa oss
Block whitelist (rw)
Block whitelist (ro)
VirtFS support    no
Multipath support no
VNC support       yes
VNC SASL support  no
VNC JPEG support  yes
VNC PNG support   yes
xen support       no
brlapi support    no
bluez  support    no
Documentation     no
PIE               yes
vde support       no
netmap support    no
Linux AIO support no
ATTR/XATTR support yes
Install blobs     yes
KVM support       yes
HAX support       no
HVF support       no
WHPX support      no
TCG support       yes
TCG debug enabled yes
TCG interpreter   no
malloc trim support yes
RDMA support      no
PVRDMA support    no
fdt support       git
membarrier        no
preadv support    yes
fdatasync         yes
madvise           yes
posix_madvise     yes
posix_memalign    yes
libcap-ng support no
vhost-net support yes
vhost-crypto support yes
vhost-scsi support yes
vhost-vsock support yes
vhost-user support yes
Trace backends    log
spice support     no
rbd support       no
xfsctl support    no
smartcard support no
libusb            no
usb net redir     no
OpenGL support    no
OpenGL dmabufs    no
libiscsi support  no
libnfs support    no
build guest agent yes
QGA VSS support   no
QGA w32 disk info no
QGA MSI support   no
seccomp support   no
coroutine backend ucontext
coroutine pool    yes
debug stack usage no
mutex debugging   yes
crypto afalg      no
GlusterFS support no
gcov              gcov
gcov enabled      no
TPM support       yes
libssh2 support   no
TPM passthrough
TPM emulator
QOM debugging     yes
Live block migration yes
lzo support       no
snappy support    yes
bzip2 support     no
lzfse support     no
NUMA host support no
libxml2           no
tcmalloc support  no
jemalloc support  no
avx2 optimization yes
replication support yes
VxHS block device no
bochs support     yes
cloop support     yes
dmg support       yes
qcow v1 support   yes
vdi support       yes
vvfat support     yes
qed support       yes
parallels support yes
sheepdog support  yes
capstone          internal
docker            yes
libpmem support   no
libudev           yes
default devices   yes

brook@vista$  make all 
  GEN     arm-softmmu/config-devices.mak.tmp
  GEN     arm-softmmu/config-devices.mak
  GEN     i386-softmmu/config-devices.mak.tmp
...

brook@vista$  sudo make all install
[sudo] password for brook:
make[1]: Entering directory '/home/brook/qemu4/qemu-4.0.0-rc4/slirp'

brook@vista$  /usr/local/bin/qemu-system-x86_64 -kernel linux/arch/x86/boot/bzImage -initrd initrd.img -curses 


    參考資料:
  • https://blog.csdn.net/candcplusplus/article/details/78320602, QEMU 2.10.1 编译安装



2019年6月1日 星期六

Protocol Buffers - for C++


Protocol buffers基本上我把他想成是一個資料庫應用的延伸, 這話該如何說呢? 基本上他就是透過名為proto的meta file, 用以描述資料形態與內容(做encode/decode).

How does the Protocol Buffers work?

先定義你的message結構, 如
message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

再用protoc將該檔案轉成code, 目前支援(C++, Java and Python), 如下例子, 將brook.proto轉成C++(brook.pb.cc與brook.pb.h).
brook@vista:~/protobuf/CPP$ protoc --cpp_out=. brook.proto
brook@vista:~/protobuf/CPP$ ls
brook.pb.cc  brook.pb.h  brook.proto

這些code會包含存取每一個欄位的API(如下由doxygen產生的圖), 如email()與set_email(), 用以serialize/parse資料,

接著我們就可以用這些API寫一段code, 做serialize/parse資料
#include <iostream>
#include <fstream>
#include "brook.pb.h"
using namespace std;

static void _Serialize(void)
{
        Person person;
        person.set_name("John Doe");
        person.set_id(1234);
        person.set_email("jdoe@example.com");
        fstream output("myfile", ios::out | ios::binary);
        person.SerializeToOstream(&output);
}

static void _Parse(void)
{
        Person person;
        fstream input("myfile", ios::in | ios::binary);
        person.ParseFromIstream(&input);
        cout << "Name: " << person.name() << endl;
        cout << "E-mail: " << person.email() << endl;
}

int main(int argc, char *argv[])
{
        _Serialize();
        _Parse();
        return 0;
}

brook@vista:~/protobuf/CPP$ g++ main.c brook.pb.cc -lprotobuf
brook@vista:~/protobuf/CPP$ ./a.out
Name: John Doe
E-mail: jdoe@example.com


    參考資料:
  • protocol buffer簡介, https://developers.google.com/protocol-buffers/docs/overview





熱門文章