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




2015年9月29日 星期二

create an initramfs on mdm9x40


利用linux提供的script建立initramfs在先前的文章已經提過,可參考如何利用kvm/qemu練習linux module之new update,基本上就是呼叫gen_initramfs_list.sh建立file system清單,使用gen_init_cpio將清單轉成CPIO格式後,在使用gzip做壓縮。相關指令如下:
W=/home/brook/projects/9x40/apps_proc
RD=/home/brook/projects/initramfs
CPIO=/home/brook/projects/initramfs.cpio
SYSROOT=${W}/oe-core/build/tmp-eglibc/sysroots/
OUTPUT=/home/brook/projects/9x40-initramfs.img
sh ${W}/kernel/scripts/gen_initramfs_list.sh -d ${RD} > /tmp/gen_initramfs_list
${W}/oe-core/build/tmp-eglibc/sysroots/mdm9640/usr/src/kernel/usr/gen_init_cpio /tmp/gen_initramfs_list > ${CPIO}
gzip -c ${CPIO} > ${CPIO}.gz


我是copy recipes/linux-quic/linux-quic_git.bb裡面的do_deploy(),接著修改成另外一個script,來建立image,指令如下:
${SYSROOT}/x86_64-linux/usr/bin/mkbootimg --kernel ${SYSROOT}/mdm9640/boot/zImage-3.10.49 --dt ${SYSROOT}/mdm9640/boot/masterDTB --ramdisk ${CPIO}.gz --cmdline "dynamic_debug.verbose=1 root=/dev/ram rootfstype=ramfs console=ttyHSL0,115200,n8 androidboot.hardware=qcom ehci-hcd.park=3 msm_rtb.filter=0x37" --base 0x81C00000 --tags-addr 0x81900000 --pagesize 2048 --ramdisk_offset 0xd32000 --output ${OUTPUT}

這裡的重點是cmdline要改成"root=/dev/ram rootfstype=ramfs",否則在lk的lk/app/aboot/aboot.c:update_cmdline()會塞一些information給kernel,導致無法由initramfs開機。

相關檔案位置: https://www.codeaurora.org/cgit/quic/le/mdm/manifest/tree/?id=LNX.LE.5.0.1-57014-9x40
LNX.LE.5.0.1-57014-9x40.xml: repo manifest file,

https://www.codeaurora.org/cgit/quic/le/kernel/lk/tree/app/aboot/aboot.c?id=LNX.LE.5.0.1-57014-9x40
lk/app/aboot/aboot.c: unsigned char *update_cmdline(const char * cmdline)

https://www.codeaurora.org/cgit/quic/le/oe/recipes/tree/conf/machine/mdm9640.conf?h=LNX.LE.5.0.1_rb1.7
conf/machine/mdm9640.conf: MACHINE_KERNEL_TAGS_OFFSET = "0x81900000"

https://www.codeaurora.org/cgit/quic/le/oe/recipes/tree/recipes/linux-quic/linux-quic_git.bb?id=LNX.LE.5.0.1-57014-9x40
recipes/linux-quic/linux-quic_git.bb: do_deploy()



2015年6月20日 星期六

如何利用kvm/qemu練習linux module之new update


本文是如何利用kvm/qemu練習linux module的更新版。


brook@vista:~/qemu$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux
Cloning into 'linux'...
remote: Counting objects: 4153172, done.
remote: Compressing objects: 100% (548/548), done.
remote: Total 4153172 (delta 278), reused 0 (delta 0)
Receiving objects: 100% (4153172/4153172), 919.28 MiB | 2.32 MiB/s, done.
Resolving deltas: 100% (3423945/3423945), done.
Checking out files: 100% (49457/49457), done.
brook@vista:~/qemu$ cd linux/
brook@vista:~/qemu/linux$ git tag -l | tac | head -2
v4.1-rc8
v4.1-rc7
brook@vista:~/qemu/linux$ cp /boot/config-3.8.0-35-generic .config
brook@vista:~/qemu/linux$ make ARCH=i386 olddefconfig
  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
scripts/kconfig/conf  --olddefconfig Kconfig
.config:550:warning: symbol value 'm' invalid for ACPI_PCI_SLOT
.config:553:warning: symbol value 'm' invalid for ACPI_HOTPLUG_MEMORY
.config:665:warning: symbol value 'm' invalid for HOTPLUG_PCI_ACPI
.config:4522:warning: symbol value 'm' invalid for FB_VESA
.config:5062:warning: symbol value 'm' invalid for USB_ISP1760_HCD
.config:6150:warning: symbol value 'm' invalid for VME_BUS
#
# configuration written to .config
#
brook@vista:~/qemu/linux$ make ARCH=i386 all
scripts/kconfig/conf  --silentoldconfig Kconfig
  SYSTBL  arch/x86/syscalls/../include/generated/asm/syscalls_32.h
  SYSHDR  arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h
  SYSHDR  arch/x86/syscalls/../include/generated/uapi/asm/unistd_64.h
...(略)
  IHEX    firmware/yam/1200.bin
  IHEX    firmware/yam/9600.bin
brook@vista:~/qemu/linux$ cd ..
brook@vista:~/qemu$ git clone git://busybox.net/busybox.git
Cloning into 'busybox'...
remote: Counting objects: 91770, done.
remote: Compressing objects: 100% (23149/23149), done.
remote: Total 91770 (delta 71829), reused 86760 (delta 68061)
Receiving objects: 100% (91770/91770), 21.68 MiB | 805 KiB/s, done.
Resolving deltas: 100% (71829/71829), done.
brook@vista:~/qemu$ cd busybox
brook@vista:~/qemu/busybox$ make defconfig
scripts/kconfig/conf -d Config.in
*
* Busybox Configuration
...(略)
  Use the klogctl() interface (FEATURE_KLOGD_KLOGCTL) [Y/n/?] (NEW) y
logger (LOGGER) [Y/n/?] (NEW) y
brook@vista:~/qemu/busybox$ sed -i 's/.*CONFIG_STATIC.*/CONFIG_STATIC=y/' .config
brook@vista:~/qemu/busybox$ make CFLAGS="-m32" LDFLAGS="-m32" all
scripts/kconfig/conf -s Config.in
#
# using defaults found in .config
#
...(略)
  DOC     busybox.1
  DOC     BusyBox.html
brook@vista:~/qemu/busybox$ file busybox
busybox: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x87675efbb7f7f810a462113cb2913bab73ffb1b6, stripped
brook@vista:~/qemu/busybox$ cd ..x
brook@vista:~/qemu$ ./create_initrd_by_linux_script.sh
+ INITD=initrd
+ rm -rf initrd
+ mkdir -p initrd/sbin initrd/bin initrd/sys initrd/tmp initrd/dev initrd/proc
+ mkdir -p initrd/usr/sbin initrd/usr/bin initrd/etc/init.d
+ install -m 0755 busybox/busybox initrd/bin
+ install -m 0755 init initrd/
+ ln -s ../bin/busybox initrd/sbin/mdev
+ ln -s busybox initrd/bin/sh
+ ln -s busybox initrd/bin/mkdir
+ ln -s busybox initrd/bin/mount
+ ./linux/scripts/gen_initramfs_list.sh -d initrd
+ ./linux/usr/gen_init_cpio /tmp/brook_initramfs_list
brook@vista:~/qemu$ qemu-system-i386 -kernel linux/arch/x86/boot/bzImage -initrd initrd.img


create_initrd_by_linux_script.sh

#!/bin/bash
INITD="initrd"
rm -rf ${INITD}
mkdir -p ${INITD}/sbin ${INITD}/bin ${INITD}/sys ${INITD}/tmp ${INITD}/dev ${INITD}/proc
mkdir -p ${INITD}/usr/sbin ${INITD}/usr/bin ${INITD}/etc/init.d
install -m 0755 busybox/busybox ${INITD}/bin
install -m 0755 init ${INITD}/
ln -s ../bin/busybox ${INITD}/sbin/mdev

ln -s busybox ${INITD}/bin/sh
ln -s busybox ${INITD}/bin/mkdir
ln -s busybox ${INITD}/bin/mount

./linux/scripts/gen_initramfs_list.sh -d ${INITD} > /tmp/brook_initramfs_list
./linux/usr/gen_init_cpio /tmp/brook_initramfs_list > initrd.img







熱門文章