Tuesday, July 19, 2011

Cross Compiling MTD Utils

Cross-compiling MTD utils is tricky and eLinux wiki article is out-dated. So you may need some tweaks. As of this writing, we will use the most recent versions of :
  • mtd-utils - Jul. 4 2011 snapshot
  • e2fsprogs-1.41.14
  • lzo-2.05
  • zlib-1.2.5
As in the eLinux wiki, we take

/home/user/mtd

as build directory and

/home/user/mtd/install

as target path. First, you want extract all sources under /home/user/mtd, so your build directory looks like


zlib

Go to the source directory and execute configuration utility

~/mtd$ cd zlib-1.2.5
~/mtd/zlib-1.2.5$ ./configure --prefix=/usr

prefix option here is required if you want /usr as your target root for resulting output, otherwise /usr/local will be the target directory like most cases. We have chosen to use /usr. It is yours to decide. Before building anything you have to edit the Makefile.

CROSS = arm-none-linux-gnueabi-
CC = $(CROSS)gcc
LDSHARED = $(CROSS)gcc -shared ...
CPP = $(CROSS)gcc -E
AR = $(CROSS)ar rc
RANLIB = $(CROSS)ranlib

Then you are ready to go

~/mtd/zlib-1.2.5$ make
~/mtd/zlib-1.2.5$ make install DESTDIR=/home/user/mtd/install


lzo

lzo is cross build friendly. So it is straightforward to do.

~/mtd/lzo-2.05$ ./configure --host=arm-none-linux-gnueabi --prefix=/usr
~/mtd/lzo-2.05$ make
~/mtd/lzo-2.05$ make install DESTDIR=/home/user/mtd/install


e2fsprogs

Building e2fsprogs is same with lzo

~/mtd/e2fsprogs-1.41.14$ ./configure --host=arm-none-linux-gnueabi --prefix=/usr
~/mtd/e2fsprogs-1.41.14$ make
~/mtd/e2fsprogs-1.41.14$ make install DESTDIR=/home/user/mtd/install

But for some reason, you have to copy following two files to the target directory

~/mtd/e2fsprogs-1.41.14$ mkdir ../install/usr/include/uuid
~/mtd/e2fsprogs-1.41.14$ cp lib/uuid/uuid.h ../install/usr/include/uuid
~/mtd/e2fsprogs-1.41.14$ cp lib/libuuid.a ../install/usr/lib

Note that the target directories for the above cp operation are related to your choice of --prefix option before. If you have chosen not to change the default directory, then you have to do like:

~/mtd/e2fsprogs-1.41.14$ mkdir ../install/usr/local/include/uuid
~/mtd/e2fsprogs-1.41.14$ cp lib/uuid/uuid.h ../install/usr/local/include/uuid
~/mtd/e2fsprogs-1.41.14$ cp lib/libuuid.a ../install/usr/local/lib


mtd-utils

First, You have to edit Makefile and add following at the top of the file

INSTALLDIR = /home/user/mtd/install/usr
ZLIBCPPFLAGS = -I$(INSTALLDIR)/include
LZOCPPFLAGS = -I$(INSTALLDIR)/include
ZLIBLDFLAGS = -L$(INSTALLDIR)/lib
LZOLDFLAGS = -L$(INSTALLDIR)/lib
WITHOUT_XATTR = 1
CROSS = arm-none-linux-gnueabi-
LDFLAGS += $(ZLIBLDFLAGS) $(LZOLDFLAGS)

Again note that INSTALLDIR corresponds to --prefix option before. If you have chosen not to change the target, then your INSTALLDIR here should be

INSTALLDIR = /home/user/mtd/install/usr/local

Edit common.mk and change the CFLAGS,

CFLAGS ?= -O2 -g $(ZLIBCPPFLAGS) $(LZOCPPFLAGS)

and replace the BUILDDIR

ifndef BUILDDIR
#ifeq ($(origin CROSS),undefined)
# BUILDDIR := $(CURDIR)
#else
## Remove the trailing slash to make the directory name
# BUILDDIR := $(CURDIR)/$(CROSS:-=)
#endif
BUILDDIR := $(CURDIR)
endif

Now, you are ready to build mtd-utils

~/mtd/mtd-utils$ make
~/mtd/mtd-utils$ make install DESTDIR=/home/user/mtd/install

You can find some of precompiled files here.

22 comments:

  1. Hi,
    Thank you so much for this. I am trying in the last couple of weeks to cross-compile mtd-utils. I went through your steps and every thing was just smooth until I reached the step of building the mtd-utils (make).
    The error i got is:

    -----------------------------
    tom@ubuntu:~/mtd/mtd-utils$ make
    CHK include/version.h
    CC compr_lzo.o
    CC compr.o
    CC rbtree.o
    LD mkfs.jffs2
    /home/tom/ToolChain/arm-2008q3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -llzo2
    collect2: ld returned 1 exit status
    make: *** [/home/tom/mtd/mtd-utils/mkfs.jffs2] Error 1
    --------------------------
    Can you please help me with this ... thanks

    ReplyDelete
  2. liblzo2.a should be found in the

    /mtd/install/lib

    directory if you successfully make and install the lzo package. Please double check the lzo compile procedure. Be sure not to forget to run "make install" especially.

    ReplyDelete
  3. Thank you so much.. The cross compiling is done successfully. However, when I go to the install/sbin directory (which has the cross-compiled files) and try to run one of the files, it says: cannot execute binary file. Is this normal or I did something wrong.

    ReplyDelete
  4. The file you just compiled is for target system(ARM machine) to run not for your host machine. So you are supposed to run it on your target. If you still have some error on your target then you have to check the libraries it requires to run.

    ReplyDelete
  5. Thank you so much.. It is really helpful
    I appreciate it

    ReplyDelete
  6. Hi again,
    I don't know if you are familiar with this or not..

    I am trying to use nanddump to dump my android device (since it has yaffs2 file system). I did the cross-compiling and transfer the nanddump binary file to the target (android), but it keeps giving me './nanddump: not found'.
    Maybe it requires some libraries. I read something about dynamic linking while cross-compiling the required files. Maybe this will help:
    --------
    tom@ubuntu:~/mtd/install/sbin$ readelf -d nanddump

    Dynamic section at offset 0x6860 contains 24 entries:
    Tag Type Name/Value
    0x00000001 (NEEDED) Shared library: [libc.so.6]
    0x0000000c (INIT) 0x8848
    0x0000000d (FINI) 0xce84
    0x00000019 (INIT_ARRAY) 0x16854
    0x0000001b (INIT_ARRAYSZ) 4 (bytes)
    0x0000001a (FINI_ARRAY) 0x16858
    0x0000001c (FINI_ARRAYSZ) 4 (bytes)
    0x00000004 (HASH) 0x8128
    0x00000005 (STRTAB) 0x8520
    0x00000006 (SYMTAB) 0x8270
    0x0000000a (STRSZ) 353 (bytes)
    0x0000000b (SYMENT) 16 (bytes)
    0x00000015 (DEBUG) 0x0
    0x00000003 (PLTGOT) 0x16948
    0x00000002 (PLTRELSZ) 304 (bytes)
    0x00000014 (PLTREL) REL
    0x00000017 (JMPREL) 0x8718
    0x00000011 (REL) 0x86f8
    0x00000012 (RELSZ) 32 (bytes)
    0x00000013 (RELENT) 8 (bytes)
    0x6ffffffe (VERNEED) 0x86d8
    0x6fffffff (VERNEEDNUM) 1
    0x6ffffff0 (VERSYM) 0x8682
    0x00000000 (NULL) 0x0
    ----------


    If you have any knowledge about this, please let me know.

    ReplyDelete
  7. It looks like the glibc library (libc.so.6) is the only library required. If you copied the library and its alias already, maybe this article will help you:

    http://www.eyrie.org/~eagle/notes/rpath.html

    By the way, are you sure you put the nanddump on your $PATH? Otherwise you have to run "./nanddump" rather than just "nanddump".

    ReplyDelete
  8. When i try to compile mtd utils i have this error:

    root@sinapse:/home/luca/Thesis/Programs/compiled-mtd-utils/mtd-utils-f238f2e# make
    CHK include/version.h
    CC ftl_format.o

    make: arm-none-linux-gnueabi-gcc: Command not found
    make: *** [/home/luca/Thesis/Programs/compiled-mtd-utils/mtd-utils-f238f2e/ftl_format.o] Error 127


    can you help me???

    ReplyDelete
  9. The message says you don't have the cross-compiler in your system. If you have one, then chances are either your compiler executables are not on the PATH or your compiler has different name, e.g., arm-linux-gnueabi-gcc. So, please check it.

    ReplyDelete
  10. Thanks a lot.
    If i have not the cross compiler how can i install it???
    Thanks

    ReplyDelete
  11. Don't mention it. CodeSourcery Lite would be the simplest choice

    https://sourcery.mentor.com/sgpp/lite/arm/portal/subscription?@template=lite

    Or you can build you own cross-compiler from the scratch

    http://crosstool-ng.org/

    There are several flavors there, so be sure to choose the right one you want. In this case, probably GNU/Linux version is the one.

    ReplyDelete
  12. thanks.
    I downloaded it , but now how can install it. There is not make file

    ReplyDelete
  13. You can download the manual too. Getting Started Guide would be a good start.

    ReplyDelete
  14. i put the compiler in /usr/sbin but now i have this problem:

    root@sinapse:/home/luca/Thesis/Programs/compiled-mtd-utils/mtd-utils-f238f2e# make
    CHK include/version.h
    CC ftl_format.o
    arm-none-linux-gnueabi-gcc: error trying to exec 'cc1': execvp: No such file or directory

    ReplyDelete
  15. You have to install the software not to copy them. Getting Started Guide will show you how to install it.

    ReplyDelete
  16. really thanks but i can't find it...

    ReplyDelete
  17. Please check out the Documentation section and find the "read this first" line:

    https://sourcery.mentor.com/sgpp/lite/arm/portal/release1803

    ReplyDelete
  18. I installed the compiler from the .bin package but i have always the same error :

    root@sinapse:/home/luca/Thesis/Programs/compiled-mtd-utils/mtd-utils-f238f2e# make
    CHK include/version.h
    CC ftl_format.o
    make: arm-none-linux-gnueabi-gcc: Command not found
    make: *** [/home/luca/Thesis/Programs/compiled-mtd-utils/mtd-utils-f238f2e/ftl_format.o] Error 127

    ReplyDelete
  19. Dear Sinapse,

    After the install, you are supposed to enlist the toolchain root (~/CodeSourcery/arm-2011.03/bin) onto you $PATH before you use it.

    ReplyDelete
  20. compr_lzo.c:31:23: error: lzo/lzo1x.h: No such file or directory
    compr_lzo.c: In function 'jffs2_lzo_cmpr':
    compr_lzo.c:53: error: 'lzo_uint' undeclared (first use in this function)
    compr_lzo.c:53: error: (Each undeclared identifier is reported only once
    compr_lzo.c:53: error: for each function it appears in.)
    compr_lzo.c:53: error: expected ';' before 'compress_size'
    compr_lzo.c:56: warning: implicit declaration of function 'lzo1x_999_compress'
    compr_lzo.c:56: error: 'compress_size' undeclared (first use in this function)
    compr_lzo.c:58: error: 'LZO_E_OK' undeclared (first use in this function)
    compr_lzo.c: In function 'jffs2_lzo_decompress':
    compr_lzo.c:74: error: 'lzo_uint' undeclared (first use in this function)
    compr_lzo.c:74: error: expected ';' before 'dl'
    compr_lzo.c:76: warning: implicit declaration of function 'lzo1x_decompress_safe'
    compr_lzo.c:76: error: 'dl' undeclared (first use in this function)
    compr_lzo.c:78: error: 'LZO_E_OK' undeclared (first use in this function)
    compr_lzo.c: In function 'jffs2_lzo_init':
    compr_lzo.c:97: error: 'LZO1X_999_MEM_COMPRESS' undeclared (first use in this function)
    make: *** [/home/anusha/vasu_wspace/project/mtd-utils/mtd-utils/compr_lzo.o] Error 1


    please help me

    ReplyDelete
  21. ./configure --build=i686-pc-linux --prefix=/home/ashish/projects/mpc8280/kernel/mtd/install --host=powerpc-linux
    configure: loading site script /opt/eldk-5.3/powerpc/site-config-powerpc-linux
    configure: Configuring LZO 2.06
    checking build system type... i686-pc-linux-gnu
    checking host system type... powerpc-unknown-linux-gnu
    checking target system type... powerpc-unknown-linux-gnu
    checking whether to enable maintainer-specific portions of Makefiles... no
    checking for powerpc-linux-gcc... powerpc-linux-gcc -m32 -mhard-float --sysroot=/opt/eldk-5.3/powerpc/sysroots/powerpc-linux
    checking whether the C compiler works... no
    configure: error: in `/home/ashish/projects/mpc8280/kernel/mtd/lzo-2.06':
    configure: error: C compiler cannot create executables
    See `config.log' for more details

    I did not understand why this error occurs? Others are cross compiling well..

    ReplyDelete
  22. I am trying to boot from jffs2 - changed the uboot parameters but kind of confused about the whole boot process. Can anyone help ?. I would like to know how jffs2 gets programmed ?. Is it through nfs ?. Can someone point me to some web sites to undersand this in detail ?

    ReplyDelete