2015年12月26日 星期六

OpenEmbedded -- Creating and using an SDK


SDK包含了開發時所需的相關檔案,有些還包含了整個開發環境(tool/command),避免系統的tool/command跟SDK需要的不同造成的error,一個SDK應該包含以下內容:(YOCTO Slides)
  1. Compilers or cross-compilers
  2. Linkers
  3. Library headers
  4. Debuggers
  5. Custom utilities


這篇文章就是在介紹如何使用Openembedded建立一個generic SDK供他人開發application,使用指令bitbake -v meta-toolchain即可產生generic SDK,並產生一個install的script,放置於oe/oe-core/build/tmp-eglibc/deploy/sdk/底下,執行過程如下
brook@vista:/home/brook/oe/oe-core/build$ bitbake -v meta-toolchain

...

+ chmod +x /home/brook/oe/oe-core/build/tmp-eglibc/deploy/sdk/oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh

+ cat /home/brook/oe/oe-core/build/tmp-eglibc/deploy/sdk/oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.tar.bz2

+ rm /home/brook/oe/oe-core/build/tmp-eglibc/deploy/sdk/oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.tar.bz2


NOTE: Tasks Summary: Attempted 1685 tasks of which 1312 didn't need to be rerun and all succeeded.
brook@vista:/home/brook/oe/oe-core/build$ tree tmp-eglibc/deploy/sdk/
tmp-eglibc/deploy/sdk/
`-- oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh

0 directories, 1 file

接著直要將底下的script(oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh)給developer開發即可,-d後面帶安裝目錄,安裝過程需要SUDO權限,
brook@vista:/home/brook/oe/oe-core/build$ ./tmp-eglibc/deploy/sdk/oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh --help
Usage: oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh [-y] [-d <dir>]
  -y         Automatic yes to all prompts
  -d <dir>   Install the SDK to <dir>
======== Advanced DEBUGGING ONLY OPTIONS ========
  -S         Save relocation scripts
  -R         Do not relocate executables
  -D         use set -x to see what is going on
brook@vista:~$ /home/brook/oe/oe-core/build/tmp-eglibc/deploy/sdk/oecore-x86_64-armv7a-vfp-neon-toolchain-oe-core.0.sh -d /opt/oe -D
+ printf 'Enter target directory for SDK (default: /usr/local/oecore-x86_64): '
Enter target directory for SDK (default: /usr/local/oecore-x86_64): + '[' /opt/oe = '' ']'
+ echo /opt/oe
/opt/oe

...

+ '[' 0 = 0 ']'
+ /usr/bin/sudo rm /SSD/opt/oe/relocate_sdk.py /SSD/opt/oe/relocate_sdk.sh
+ echo 'SDK has been successfully set up and is ready to be used.'
SDK has been successfully set up and is ready to be used.
+ exit 0

要使用SDK之前,必須先source安裝目錄下開頭為environment-setup-的script,設置相關的環境變數,我的script內容如下
export PATH=/SSD/opt/oe/sysroots/x86_64-oesdk-linux/usr/bin:/SSD/opt/oe/sysroots/x86_64-oesdk-linux/usr/bin/armv
7a-vfp-neon-oe-linux-gnueabi:$PATH
export PKG_CONFIG_SYSROOT_DIR=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi
export PKG_CONFIG_PATH=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi/usr/lib/pkgconfig
export CONFIG_SITE=/SSD/opt/oe/site-config-armv7a-vfp-neon-oe-linux-gnueabi
export CC="arm-oe-linux-gnueabi-gcc  -march=armv7-a -mthumb-interwork -mfloat-abi=softfp -mfpu=neon -mthumb-inte
rwork --sysroot=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export CXX="arm-oe-linux-gnueabi-g++  -march=armv7-a -mthumb-interwork -mfloat-abi=softfp -mfpu=neon -mthumb-int
erwork --sysroot=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export CPP="arm-oe-linux-gnueabi-gcc -E  -march=armv7-a -mthumb-interwork -mfloat-abi=softfp -mfpu=neon -mthumb-interwork --sysroot=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export AS="arm-oe-linux-gnueabi-as "
export LD="arm-oe-linux-gnueabi-ld  --sysroot=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export GDB=arm-oe-linux-gnueabi-gdb
export STRIP=arm-oe-linux-gnueabi-strip
export RANLIB=arm-oe-linux-gnueabi-ranlib
export OBJCOPY=arm-oe-linux-gnueabi-objcopy
export OBJDUMP=arm-oe-linux-gnueabi-objdump
export AR=arm-oe-linux-gnueabi-ar
export NM=arm-oe-linux-gnueabi-nm
export M4=m4
export TARGET_PREFIX=arm-oe-linux-gnueabi-
export CONFIGURE_FLAGS="--target=arm-oe-linux-gnueabi --host=arm-oe-linux-gnueabi --build=x86_64-linux --with-libtool-sysroot=/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export CFLAGS=" -O2 -fexpensive-optimizations -frename-registers -fomit-frame-pointer"
export CXXFLAGS=" -O2 -fexpensive-optimizations -frename-registers -fomit-frame-pointer -fpermissive"
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
export CPPFLAGS=""
export OECORE_NATIVE_SYSROOT="/SSD/opt/oe/sysroots/x86_64-oesdk-linux"
export OECORE_TARGET_SYSROOT="/SSD/opt/oe/sysroots/armv7a-vfp-neon-oe-linux-gnueabi"
export OECORE_ACLOCAL_OPTS="-I /SSD/opt/oe/sysroots/x86_64-oesdk-linux/usr/share/aclocal"
export OECORE_DISTRO_VERSION="20151225"
export OECORE_SDK_VERSION="oe-core.0"
export PYTHONHOME=/SSD/opt/oe/sysroots/x86_64-oesdk-linux/usr
export ARCH=arm
export CROSS_COMPILE=arm-oe-linux-gnueabi-


以下是我用這SDK編譯一個檔案,並放入target中執行
brook@vista:~$ source /opt/oe/environment-setup-armv7a-vfp-neon-oe-linux-gnueabi
brook@vista:~$ cat main.c
#include <stdio.h>

int main(int argc, char *argv[])
{
        printf("hello\n");
        return 0;
}

brook@vista:~$ $CC -o brook-sdk main.c

D:\Projects>adb push z:\brook-sdk /
688 KB/s (5644 bytes in 0.008s)

D:\Projects>adb shell chmod +x /brook-sdk

D:\Projects>adb shell /brook-sdk
hello

開發環境架構圖


    參考資料:
  1. Yocto Project and OpenEmbedded development course
  2. Yocto Project Quick Start -- SDK Generation





2015年12月19日 星期六

OpenEmbedded - Writing Meta Data for adding Kernel Module package


OpenEmbedded User Manual - CH3, Writing Meta Data (Adding packages)之後,這一篇是教導如何新增Kernel Module package。 BB檔的寫法與OpenEmbedded User Manual - CH3, Writing Meta Data (Adding packages)雷同,只是我們這次iherit的class是module

/oe-core/meta/recipes-kernel/brook/brook_1.0.bb

inherit module
DESCRIPTION = "Brook Out Tree Kernel Module"
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://LICENSE;md5=5ff2bd2dd80c7cc542a6b8de3ee4ff87"

PR = "r1-${KERNEL_VERSION}"

# This DEPENDS is to serialize kernel module builds
DEPENDS = "virtual/kernel"


SRC_URI = "file://brook.patch \
"

S = "${WORKDIR}/brook-linux-${PV}"

這邊的SRC_URI我是使用patch格式,當然你也可以使用zip格式,但是zip格式需要考慮目錄名稱必須為"${WORKDIR}/brook-linux-${PV}",即bb檔中的S,但是如果是patch就不用管S目錄名稱為何了。

完整檔案結構如下

後面會陸續介紹每個檔案
    /oe-core/meta/recipes-kernel/brook
    |-- brook_1.0.bb
    |-- files
    |   `-- brook.patch
    `-- git
        |-- LICENSE
        |-- Makefile
        `-- main.c


Makefile

ifneq ($(KERNELRELEASE),)
# obj-m := <module_name>.o
# <module_name>-y := <src1>.o <src2>.o ...

        EXTRA_CFLAGS += -DYAFFS_OUT_OF_TREE

        obj-m := brook.o

        brook-objs := main.o
else
        PWD := $(shell pwd)

modules:
 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules

modules_install:
 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules_install

clean:
 $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean
endif

這是一般的Kernel Module寫法,沒有特別的地方,Out Tree build時KERNELRELEASE為空字串,所以會用下面那段。

License的內容就只有"License: GPL"這個字串

main.c

Module的主程式,只是在init與exit時印出簡單字串而已。
#include <linux/module.h>

MODULE_AUTHOR("Brook");
MODULE_DESCRIPTION("Kernel module for demo");
MODULE_LICENSE("GPL");

static int __init brook_init(void)
{
        printk(KERN_INFO "Brook Module Init\n");
        return 0;
}
module_init(brook_init);

static void __exit brook_exit(void)
{
        printk(KERN_INFO "Brook Module Exit\n");
        return;
}
module_exit(brook_exit);


brook.patch

這是我把檔案全部放在git目錄底下,做source control,並用git format-patch產生的,將產生後的檔案放置files/brook.patch。




2015年12月13日 星期日

mkbootimg -- pack boot images utils


mkbootimg是Android project的一部分,用來封裝boot image的,其使用參數如下:
usage: mkbootimg
       --kernel <filename>
       --ramdisk <filename>
       [ --second <2ndbootloader-filename> ]
       [ --cmdline <kernel-commandline> ]
       [ --board <boardname> ]
       [ --base <address> ]
       [ --pagesize <pagesize> ]
       [ --ramdisk_offset <ramdisk_offset> ]
       [ --dt <filename> ]
       [ --tags-addr <address> ]
       -o|--output <filename>


基本上就是將kenrnel、ramdisk、device tree等封裝成一個檔案,讓boot loader能將其載入RAM中,並正確執行。檔案的layout如下(就如bootimg.h中註解所提到的,https://www.codeaurora.org/cgit/quic/femto/platform/system/core/tree/mkbootimg/bootimg.h?h=LNX.LE.5.0.1-57023-9x40)
/*
** +-----------------+ 
** | boot header     | 1 page
** +-----------------+
** | kernel          | n pages  
** +-----------------+
** | ramdisk         | m pages  
** +-----------------+
** | second stage    | o pages
** +-----------------+
** | device tree     | p pages
** +-----------------+
**
** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size
** p = (dt_size + page_size - 1) / page_size
**
** 0. all entities are page_size aligned in flash
** 1. kernel and ramdisk are required (size != 0)
** 2. second is optional (second_size == 0 -> no second)
** 3. load each element (kernel, ramdisk, second) at
**    the specified physical address (kernel_addr, etc)
** 4. prepare tags at tag_addr.  kernel_args[] is
**    appended to the kernel commandline in the tags.
** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
** 6. if second_size != 0: jump to second_addr
**    else: jump to kernel_addr
*/

其中boot header會有一些information,讓LK能正確的將各個section load到正確的Address上,boot header定義如下
typedef struct boot_img_hdr boot_img_hdr;

#define BOOT_MAGIC "ANDROID!"
#define BOOT_MAGIC_SIZE 8
#define BOOT_NAME_SIZE 16
#define BOOT_ARGS_SIZE 512

struct boot_img_hdr
{
    unsigned char magic[BOOT_MAGIC_SIZE];

    unsigned kernel_size;  /* size in bytes */
    unsigned kernel_addr;  /* physical load addr */

    unsigned ramdisk_size; /* size in bytes */
    unsigned ramdisk_addr; /* physical load addr */

    unsigned second_size;  /* size in bytes */
    unsigned second_addr;  /* physical load addr */

    unsigned tags_addr;    /* physical addr for kernel tags */
    unsigned page_size;    /* flash page size we assume */
    unsigned dt_size;      /* device tree in bytes */
    unsigned unused;       /* future expansion: should be 0 */
    unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
    
    unsigned char cmdline[BOOT_ARGS_SIZE];

    unsigned id[8]; /* timestamp / checksum / sha1 / etc */
};

第一個欄位magic用以識別這個image是否為有效,後續會帶著kernel、ramdisk、device tree跟second的size大小,以及要load的address。 以下是我的platform的FLASH與其RAM的layout,如下圖

基本上,main function就是將這些檔案pack成一個檔案,並做page alignment,有興趣再去看code吧。

下面這段是我寫的umkbootimg,讀取boot header資訊後印出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>

#include "bootimg.h"

static void *load_file(const char *fn, unsigned sz)
{
    char *data;
    int fd;

    data = 0;
    fd = open(fn, O_RDONLY);
    if(fd < 0) return 0;

    data = (char*) malloc(sz);
    if(data == 0) goto oops;

    if(read(fd, data, sz) != sz) goto oops;
    close(fd);

    return data;

oops:
    close(fd);
    if(data != 0) free(data);
    return 0;
}

int usage(void)
{
    fprintf(stderr,"usage: umkbootimg <filename>\n");
    return 1;
}


int main(int argc, char **argv)
{
    boot_img_hdr *hdr;

    char *img_name = 0;
    void *img_data = 0;
    unsigned sz;

    if (argc < 2) {
        return usage();
    }

    hdr = (boot_img_hdr *) load_file(argv[1], sizeof(boot_img_hdr));
    hdr->magic[BOOT_MAGIC_SIZE] = 0;
    printf("magic:%s\n", hdr->magic);
    printf("kernel_size:%u/0x%08x\n", hdr->kernel_size, hdr->kernel_size);
    printf("kernel_addr:%u/0x%08x\n", hdr->kernel_addr, hdr->kernel_addr);

    printf("ramdisk_size:%u/0x%08x\n", hdr->ramdisk_size, hdr->ramdisk_size);
    printf("ramdisk_addr:%u/0x%08x\n", hdr->ramdisk_addr, hdr->ramdisk_addr);

    printf("second_size:%u/0x%08x\n", hdr->second_size, hdr->second_size);
    printf("second_addr:%u/0x%08x\n", hdr->second_addr, hdr->second_addr);

    printf("tags_addr:%u/0x%08x\n", hdr->tags_addr, hdr->tags_addr);

    printf("page_size:%u/0x%08x\n", hdr->page_size, hdr->page_size);

    printf("dt_size:%u/0x%08x\n", hdr->dt_size, hdr->dt_size);

    printf("name:%s\n", hdr->name);

    printf("cmdline:%s\n", hdr->cmdline);
    return 0;
}


    參考資料:
  • https://www.codeaurora.org/cgit/quic/femto/platform/system/core/tree/mkbootimg/bootimg.h?h=LNX.LE.5.0.1-57023-9x40