Building GNU libmatheval for ARM

Overview

GNU libmatheval is a library used for parsing and evaluating symbolic expressions input as text.

A simple and common use of this library is when you have a C program and you have mathematical expressions. For example,

x * y > x^2

contains two variables whose values can vary over time since they could be analog inputs and therefore you need to evaluate it often and execute different actions according to the resulting comparison.

This library can be quite useful in an embedded system that provides some control functionality, but it has the disadvantage that some of its requirements are not straight forward to cross-build and have significant memory and storage footprints.

I managed to build it and run it successfully on an Atmel AT91SAM9260 at 210MHz with 64MB RAM and a rootfs with 128MB NAND flash, so it should be sufficient for many embedded systems, but if you have a slower processor or less memory (it’s still common to find industrial embedded devices with significantly fewer processor horsepower and/or memory) you could have performance issues.
For this article, I’m using the more powerful Atmel Cortex-A5 ATSAMA5D35 with 512MB RAM.

Requirements

I used buildroot for building the toolchain and the rootfs for the evaluation board. Unfortunately, buildroot does not include libmatheval nor some of it’s requirements since they are not typically used in embedded systems.

Building a toolchain and a rootfs with buildroot is a straight-forward task that is well documented. By using it, you can easily build some of libmatheval’s requirements:

  • libtool
  • libgmp
  • libffi
  • pthreads
  • flex

The procedure that I’m describing here is for building libmatheval and the requirements that are not included in buildroot.

First, let’s export common environment variables that make our life easier:

export HOST=arm-linux
export BUILD=i386-linux
export ROOTFS=/home/projects/sama5/rootfs

Now we can start building the requirements:

libuninstring
Package: libunistring 0.9.3

This library is used for advanced text processing. The mathematical expressions we use need advanced text analysis.

./configure --prefix=$ROOTFS --host=$HOST --build=$BUILD --enable-threads=posix

Apply the following patch. The commented code should not be a problem since it’s for glibc < 2.4 and most embedded systems use uclibc or newlib and a newer GCC:

--- a/lib/localename.c  2010-04-03 11:43:57.000000000 +0200
+++ b/lib/localename.c  2014-01-04 23:36:13.392018313 +0100
@@ -2613,10 +2613,10 @@
            See .  */  
         const char *name =
           nl_langinfo (_NL_ITEM ((category), _NL_ITEM_INDEX (-1)));
-        if (name[0] == '\0')
+        /*if (name[0] == '\0')*/
           /* Fallback code for glibc < 2.4, which did not implement
              nl_langinfo (_NL_LOCALE_NAME (category)).  */  
-          name = thread_locale->__names[category];
+        /*name = thread_locale->__names[category];*/
         return name;
 #  endif
 #  if defined __APPLE__ && defined __MACH__ /* MacOS X */

Build and install in $ROOTFS

make
make  DESTDIR=$ROOTFS install

bdw-gc
Package: gc-7.2e

This is a C garbage collector for C malloc or C++ new. As with libunistring, this library is needed for the following requirement (Guile).

Configure, build and install in $ROOTFS:

./configure \
--prefix=$ROOTFS \
--host=$HOST \
--build=$BUILD \
--enable-threads=posix \
--disable-gcj-support

make

make  DESTDIR=$ROOTFS install

guile
Package: guile 1.8.8

Guile is a library, interpreter and compiler of Scheme, a symbolic programming language. It’s an ideal option for evaluating mathematical expressions, but it’s not the best companion for small embedded systems due to its memory and storage footprints. For example, the size of the shared lib libguile.so.17.4.0 in Guile 1.8.8 is ~3.1MB and ~790KB when stripped.

When building it, you must take care that the Guile version that you want to install in your embedded device must be equal or less than the version you have installed in your host since the cross-building process uses the native Guile. In my case I had guile 2.0.5 installed in my host, but I used guile 1.8.8 since I had several problems while trying to build 2.0.x versions.

First, apply this patch that removes the use of csqrt() and calculates it manually. I think this is a problem with my uclibc.

--- guile-1.8.8/libguile/numbers.c	2010-12-13 18:25:01.000000000 +0100
+++ guile-1.8.8.a/libguile/numbers.c	2014-01-20 00:01:38.106510706 +0100
@@ -6168,8 +6168,8 @@
   if (SCM_COMPLEXP (x))
     {
 #if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT && defined (SCM_COMPLEX_VALUE)
-      return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));
-#else
+        /*return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));*/
+        /*#else*/
       double re = SCM_COMPLEX_REAL (x);
       double im = SCM_COMPLEX_IMAG (x);
       return scm_c_make_polar (sqrt (hypot (re, im)),

Configure by specifying where to find ffi and bdw-gc:

 
CPPFLAGS="-I$ROOTFS/include -I$ROOTFS/usr/include" \
LDFLAGS="-L$ROOTFS/lib -L$ROOTFS/usr/lib" \
LIBFFI_CFLAGS="-I$ROOTFS/usr/include" \
LIBFFI_LIBS="-L$ROOTFS/usr/lib" \
BDW_GC_CFLAGS="-I$ROOTFS/usr/include" \
BDW_GC_LIBS="-L$ROOTFS/usr/lib" \
GUILE_FOR_BUILD=/usr/bin/guile \
./configure \
--prefix=$ROOTFS \
--host=$HOST \
--build=$BUILD

Edit the file libguile/Makefile.
Remove the flag -Werror to variable CFLAGS, otherwise it will complain that several variables are set but not used [-Werror=unused-but-set-variable].

 
CFLAGS = -g -O2 -Wall -Wmissing-prototypes

Build and install:

 
make
make install

libmatheval
Package: libmatheval 1.1.11

Now we can finally build libmatheval!

Edit the file Makefile.in and remove the directory tests from the building process:

SUBDIRS = doc lib

Configure, build and install:

CPPFLAGS="-I$ROOTFS/include -I$ROOTFS/usr/include" \
LDFLAGS="-L$ROOTFS/lib -L$ROOTFS/usr/lib" \
./configure \
--prefix=$ROOTFS \
--host=$HOST \
--build=$BUILD

make

make install

For compiling a program tha uses libmatheval you just need to add the linking flag -lmatheval and, of course, include the directories where it was installed, typically /your/rootfs/usr/include and /your/rootfs/usr/lib.

References:

Cyanogenmod in a Samsung Galaxy S3 mini using Linux

Nowadays is straight forward to install CyanogenMod, the Open Source firmware Android’s alternative, if you have a device that is officially supported.
But if you have a device that is not officially suported you could face some issues that are not well documented. And if you use Linux, finding documentation could be a bit harder.

In my case I have a Samsung Galaxy S3 mini that, of course, is not officially supported…

I tried several things that didn’t work before finding the right way.
In this article I explain the procedure that finally worked.

Requirements
ADT (Android Development Tools)
The Android SDK provides the API libraries and developer tools necessary to build, test, and debug apps for Android such as Eclipse. This is a big download (~497MB) and it seems a loss of time because what we only need is the Android Debug Bridge program, adb (~1.2MB).
Package: ADT bundle

Heimdall
Samsung devices come with a unique boot mode called Download Mode. Heimdall is a cross-platform, open source tool for interfacing with the Download Mode. You can directly download the binaries and try them, but for me it didn’t work, you can check the issues I had at the end of this article. Therefore, I downloaded the sources from GitHub and compiled them.
Package: Heimdall sources

unzip Heimdall-master.zip
cd Heimdall-master

# First compile libpit, but do not install it.
# This library will be used later for building heimdall
cd libpit
./configure
make
cd ..

# Now build heimdall
cd heimdall
./configure
make
sudo make install

ClockworkMod
It is a custom recovery for Android phones and tablets that allows to perform several advanced recovery, restoration, installation and maintenance operations on Android devices. It is one of the most common ways used to gain root access, back up device data, install custom ROMs, kernels and more.
Package: recovery-clockwork-6.0.2.7-golden.img

CyanogenMode for Galaxy S3 mini
The Galaxy S3 mini is not officially supported by the CyanogenMod team, but the good guys at maclaw.pl have a working CyanogenMod version for this device.
Package: You can download from here. At the time I did this procedure the most recent version was cm10.1_golden.maclaw.20130928.zip, but there are more recent and stables versions.

Installation

The procedure for installing CyanogenMod for the Galaxy S3 mini is similar to the one for the Galaxy S3, but the subtle details that differ can make you spend a lot of time.
This procedure is partially based on the official guide for installing CyanogenMod for the Galaxy S3

Connect the device to the USB adapter, boot the device normally and using adb, reboot the device into download mode:

adb reboot download

Copy ClockworkMod Recovery file into the Heimdall directory.
Open a terminal and run the following command from the Heimdall directory. Notice the argument –Kernel2 that is specific to the Galaxy S3 mini. Adjust the file name according to your version.

heimdall flash --Kernel2 recovery-clockwork-6.0.2.7-golden.img --no-reboot

A blue transfer bar will appear on the device showing the recovery being transferred.
You need now to manually reboot the phone into ClockworkMod Recovery mode by holding Home & Volume Up & Power.
At this point the Galaxy S3 mini has ClockworkMod Recovery installed.

Place the CyanogenMod rom .zip file on the root of the SD card using adb. Adjust the file name according to your version.

adb push cm10.1_golden.maclaw.20130928.zip /sdcard/

Once the device boots into the ClockworkMod Recovery, use the physical volume buttons to move up and down.

Select the option to wipe data/factory reset.
Select install zip from sdcard.
Select choose zip from sdcard.
Select the CyanogenMod file you placed on the sdcard. You will then need to then confirm that you do wish to flash this file.
Once the installation has finished, return back to the main menu, and select the reboot system now option. The device should now boot into CyanogenMod.

That’s it!

It’s been some weeks since I install CyanogenMod on my Galaxy S3 mini and I can say that is quite stable. I haven’t had any freezes nor strange behaviors. The most common apps work as expected. The only issue I have experience is a video freeze when running YouTube after a few minutes of playback. This issue was know and it should be fixed in more recent versions.

———

The following section is only relevant if you have problems with you SIM and carrier. It is not directly related to the Galaxy S3 mini nor CyanogenMod, but I didn’t want to write a dedicated article just for this.

SIM network unlocking

If you have a phone from carrier A and you want to use your old SIM that belongs to carrier B, but when you switch on the phone you get the message SIM network unlock code, it means that the phone is locked to carrier A and you need a code to unlocking. Unfortunately, I didn’t find how to get this code for free…

This means you have to pay for having the code. Any company that provides this service will ask for the telephone’s country, its carrier and its IMEI number that is a unique ID that identifies the phone. As a matter of fact, the IMEI number is an ID that resides in the modem, that’s why it doesn’t change even if you flash your phone.

Once you have your network unlock code (received by email) you could unlock the phone by following this procedure:

Switch on the phone with the Non Accepted SIM card
The phone will ask for the code
Enter Unlock code

However, this could not always work. In my case, I had to do the following:

Switch on your phone with the Non Accepted SIM card
The phone will ask for the code
Enter Unlock code, it won't work. It will say Network unlock unsuccessful. I tried it 3 times
Enter unfreeze code that arrive in the email too
Enter Unlock code again

And this time it worked!
This means I have a completely rooted phone that is not tied to any carrier.

References

Unofficial site for the CyanogenMod Galaxy S3 mini
Official CyanogenMod guide for Galaxy S3
Heimdall error
ClockworkMod’s official page
ClockworkMod guide

Cross-compiling OpenVPN

openvpn_logo

OpenVPN is an open source software application used for creating SSL/TLS VPN that provides secure point-to-point connections in routed or bridged configurations and remote access facilities.

Cross-compiling the latest version 2.3.2 is an easy procedure, no makefiles of other files have to be modified:

Download package
openvpn-2.3.2.tar.gz

Requirements
This library is built on top of OpenSSL and LZO. It’s quite probable that you already have both packages installed in your embedded device since they’re quite common.

Environment configuration
As normal, set the environment variables that make our life easier. Change them according to your needs. These settings are for an ARM based processor:

$ export HOST=arm-linux
$ export BUILD=i386-linux
$ export PREFIX=/home/projects/rootfs/usr/local

Configuration
I already have OpenSSL and LZO installed in my embedded system (buildroot installed them for me), however I had to specify where the LZO header files and library were located:

LZO_CFLAGS="-I$PREFIX/usr/include" 
LZO_LIBS="-L$PREFIX/usr/lib -llzo2" 
./configure 
--target=arm-linux 
--host=arm-linux 
--build=i686-pc-linux-gnu 
--prefix=$PREFIX 
--enable-static 
--enable-shared 
--enable-small 
--disable-plugins 
--disable-debug

Compilation and installation

make
make install

The last step will install some documentation that you can get rid of, a header file openvpn-plugin.h that you will need only if you compiile an OpenVPn plugin and the binary openvpn that is what you need.

Here are some tutorials for configuring an OpenVPN client/server and learning the basic ideas behind it:

That’s it. Have fun!