Simple C to XML serializer 0.0.2 released in GitHub

Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post here.

I’ve improved it a little bit and released it in GitHub. You can check it out here.

This program is in its first stages and currently it only converts the most widely used C-structs constructs to XML. However, it could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar.

The version I released in GitHub adds the support for structs/unions defined inside a macro.

I’ll try to explain how this feature works:
Suppose you have a file, test3.h, with a macro that defines the following struct:

#define def_struct_data(prefix,number,code_number)
struct prefix##driver_data
{
    task_status_t tsk_sts;

    struct buttons_platform_data pdata[number];
    struct prefix##an_event event;

    struct prefix##buttons_settings
    {
        uint8_t thresholds[3];
        uint8_t pressed_threshold;
        struct prefix##buttons_mask hash_table[code_number];
    }__attribute__((packed)) settings;

    uint8_t raw[((number) / 8) + 1];
    uint8_t raw_mem[((number) / 8) + 1];

}__attribute__((packed));

As you probably know, the syntax of a C macro is not valid for a C compiler, that’s one of the reasons for using the C pre-compiler: it converts the macro to something that the compiler understands.
In the SC2XML case, applies the same condition. Therefore, you have to create a very simple C file that includes the file that you want to serialize, in this case test3.h, along with the use of the macro and execute sc2xml with that file:

Assume that the name of your simple C file is test3.stub.h. It should looks like this:

#include "test3.h"
def_struct_data(prefix,number,code_number)

def_struct_data is the name of your macro.

Execute sc2xml:

$ sc2xml test3.stub.h

sc2xml 0.0.2

INFO: *** Parsing file data/test3.gen.h ***

The final generated XML file, test3.h.xml, will look like this:



    
        prefixdriver_data
        
            tsk_sts
        
        
            pdata
        
        
            event
        
        
            prefixbuttons_settings
            
                thresholds
            
            
                pressed_threshold
            
            
                hash_table
            
            __attribute__ ( ( packed ) ) 
            settings 
        
        
            raw
        
        
            raw_mem
        
        __attribute__ ( ( packed ) )
    

Now you can see that the XML file shows exactly the struct defined in the macro def_struct_data but with nice XML tags!

I hope this project could be useful, specially if you want a non-trivial example that defines a parser and grammar with lex and yacc.

Updating the Samsung Galaxy i7500 using Linux

The Galaxy i7500 was the first android phone built by Samsung. I got this phone some while ago and I wanted to update to a more recent android version (the original firmware ships version 1.6), but unfortunately, Samsung doesn’t give any support nor updates. Thus, if you want to update the firmware, you can use a non-official firmware.

In this post I try to explain how to do this when your host/PC is a Linux machine.
The procedure described here is partially based in the quite uselful Easy guide to update to Galaxo or GAOSP! written by DaSchmarotzer that explains how to update this phone but using a Windows based machine.

Download

Get all the needed files:

Put everything in place

First of all, create a backup of your data. The SD card (pictures, music, etc) won’t be touched, but other important info like your contacts will be erased. If you have a Gmail account you just can synchronize your contacts for uploading them to your account and once you finish the update you can synchronize it again for downloading them to the phone.

Unpack the android SDK wherever you want. I put all the downloaded files in /home/android/.

Decompress the android SDK. The directory /home/android/android-sdk-linux_x86 will be created.

Change to the directory /home/android/android-sdk-linux_x86/tools

Move the file fastboot.zip inside the /home/android/android-sdk-linux_x86/tools directory and decompress it. A new file called fastboot will be created.
Give it execution permissions:

chmod +x fastboot

Connect your phone via USB and mount its SD card.
Copy the chosen ROM and Gapps files to the phone’s SD card.
Umount the SD card.
Turn off the phone, but leave it connected to the PC using the USB cable.

Now we have all the downloaded files in the required locations.

Manipulate the i7500 flash with fastboot

We have to tell the phone’s bootloader how to load our files. fastboot can write a new bootloader image (recovery-6.5.1.img) that allow us to do exactly that plus several other operations.

Before executing fastboot, we need to tell udev through its rules that we’re the owner of the usb device with vendors ID 18d1 (Google Inc.) and 04e8 (Samsung Electronics Co., Ltd).
You have to do these steps as root.
Change username to the username you use for logging in your system.

Create the file /etc/udev/rules.d/50.android.rules with the following content

SUBSYSTEM=="usb", SYSFS{idVendor}=="18d1", MODE="0666", OWNER="username"

Create the file /etc/udev/rules.d/90.android.rules with the following content

SUBSYSTEM=="usb", SYSFS{idVendor}=="04e8", MODE="0666", OWNER="username"

Reload udev’s rules

$ reload udev

As a normal user, add the /tools directory where fastboot is located to your PATH environment variable.

$ export PATH=$PATH:/home/android/android-sdk-linux_x86/tools

Change to the /home/android/android-sdk-linux_x86/tools directory and write the following command without pressing enter.

$ fastboot flash recovery recovery-6.5.1.img 

You’ll press it after booting the phone in fastboot mode. Do so by pressing and holding both the Call button (bottom left) and the Power button (bottom right) at the same time until you see on the phone that you are in fastboot mode. You’ll have about 15 seconds to press enter on the shell. If it works, you’ll see in the shell this output:

sending 'recovery' (3080 KB)... OKAY
writing 'recovery'... OKAY

In the phone’s display you’ll see something like this:

Android fastboot

Android fastboot

At this point we have a new bootloader image in the phone’s flash that allow us to do custom operations.

Turn off your phone. I had problems for turning it off, so I removed the battery and put it again.

Updating to the new firmware

Boot the phone, but this time in recovery mode. Do so by holding the Lower volume button, the Call button and the Power button. If it works, you’ll see a green menu with lots of choices:

Android recovery mode

Android recovery mode

Select the Nandroid backup option and press the Home button to confirm. Then just follow the instructions on-screen. I chose slot 1 when asked which slot to use.

Select Wipe data/Factory reset. It’s going to erase all your applications and settings, but it shouldn’t touch anything saved on your SD card. It’s important to do so, else the phone will not work properly.

Select Apply any zip. Select either the GAOSP or the Galaxo zip files, depending on your choice. This will take a bit since it was to copy 60 or 49MB, respectively.

Select Fix package permissions.

Select Apply any zip again but this time pick the Gapps, which will restore the Google Applications on your phone.

Now you can reboot by selecting the first option Reboot system.

Notice: It could happen that the phone freezes during the booting procedure in the android or gaosp boot screen. Wait for ~10 minutes, and then if it’s still stuck, pull out the battery and boot again.
If it still doesn’t work, repeat the procedure described in this section Updating to the new firmware.

Btw, sorry for the images quality. I’ll change them with better ones.

That’s it!

References

A simple C to XML serializer

Some days ago I was asked for writing a program that could read any given C struct or union from a header file and serialize it to XML.

I decided to write a program, called SC2XML: Simple C to XML serializer, flexible enough for adding later new C constructs like enums and typedefs.

In order to serialize any valid C struct/union I needed a C grammar and something that could scan and parse the file following that grammar. The best tools for doing this are lex and yacc. In Linux, Flex and Bison are the Open Source implementations of these old but quite useful Unix tools.
I strongly recommend these tools if you need to create a compiler, an interpreter or even a shell (you can have a look at a project called tarasca that I wrote some years ago as a proof of concept of a Cisco-like shell).

Since writing an ANSI C grammar is not a easy task, I downloaded the lex and yacc implementations of the ANSI C grammar written by Jutta Degener and modified it because it misses a few things like user-defined data types.

The first version of this program could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar (see the header comment of scanner.l and parser.y for details).

The sc2xml package needs, as requirements, the following two standard libraries:

  • glib-2
  • libxml2

The package already provides the files generated by Flex and Bison called lex.yy.c, parser.c and parser.tab.h. These files are created from scanner.l and parser.y. Thus, if you modified the last two files, you will also need Flex and Bison.

You can configure, compile and install sc2xml using the traditional way:

./configure
make
sudo make install

This version, as it is normal with a first alpha release, has some limitations and bugs. At them moment, it doesn’t support some constructs like

  • Anonymous structs/unions
  • Multi-line pre-processor directives and macros

You can have a look at the README for more details about the current limitations and bugs.

If I have the time or there is the interest for expanding this program, I would post it in sourceforge or gitorious. Meanwhile, you can download the source code from here: sc2xml-0.0.1.tar.gz