Sunday, March 22, 2009

CeBootLin - Linux autoloader for WinCE on Loox 5XX (560/550)

Here is the "bootloader" for wince to boot linux automatically on startup of PDA. It is not real bootloader because it has nothing to do with boot sectors, it is just a wince program which is able to boot linux as soon as possible. I have implemented it in this way because I didn't want to brick my PDA meddling with boot sectors or something. Also I wanted to have a way to skip loading of linux and let wince load itself. So here we are. It works. The code is based on haret (thanks guys!) but I cut off the parts I not used. Because the laoder uses hardware registers (GPIO/CPLD for leds and keybaord) of Fujitsu Siemens Loox N560/C550, it will not work for other PDAs. When CeBootLin is installed and PDA is turned on, wince begins boot process loading applications defined under the HKEY_LOCAL_MACHINE\init registery key. This way it loads CeBootLin. CeBootLin waits for 1 second, blinking with keyboard led. During the keyboard blinking, user can press any button to stop CeBootLin from loading Linux. If no button has been pressed during keyboard blinking, CeBootLin will search for \CeBootLin\default.txt and use this haret script to load Linux.

So, in essence, you should complete the following steps in order to install CeBootLin:
1. Download CeBootLin.
2. Unpack the downloaded archive into the PDA root. The CeBootLin.exe must be reachable with the path \CeBootLin\CeBootLin.exe.
3. Place default.txt under the \CeBootLin directory. The default txt might looks like this (if the Andorid is what you are going to load with CeBootLin):

set MTYPE 1454
set KERNEL "\CeBootLin\zImage"
set CMDLINE "root=179:3 mem=62M rootdelay=3 boot_delay=0 init=/init console=tty0 fbcon=rotate:0 androidboot.console=tty0 android.checkjni=1"
set RAMADDR 0xA0200000
bootlinux

4. Place zImage under the directory \CeBootLin\.

5. Modify HKEY_LOCAL_MACHINE\init adding Launch82 ="CeBootLin.exe" and Depend82 = 14 00.


6. Modify HKEY_LOCAL_MACHINE\Loader\SystemPath adding a new directory \CeBootLin\ to the multi-string list.

7. Wait for 5 minutes (WinCE needs time to persist changes to registry).
8. Feel free to reboot your PDA

To test that CeBootLin is able to boot linux at all, run CeBootLin.exe manually.

It looks little bit complicated but later, setup program might be created to ship automatically bootable Andorid on Loox550. But before, Andorid must be polished.

Here is the source code of CeBootLinux. CeBootLinux is licensed with GNU GPL.

Sunday, March 15, 2009

Pictures of Linux and Android on Fujitsu-Siemens Loox N560


Today the desteny of WM5 is only to be replaced with Linux


Haret.. all you need is to press Run


Android on Loox N560


Installation of debian on Loox N560


Installation of debian on Loox N560

Memory tester for Loox N560/C550

Recently I've created a way to test memory for defects on Fujitsu-Siemens Loox N560/C550. Only one who upgraded (or wants to upgrade) PDA's memory from 64M to 128M needs this utility. Using this "util" it is possible to test memory immediately right after memory has been upgraded, without re-flushing WM5/WM6.

So do the following to test your PDA's memory:
1. Download either LooxMem128-v0.zip (to test 128Mb of memory) or LooxMem64-v0.zip (to test 64Mb of memory).
2. Unpack the downloaded archive onto SD.
3. Insert SD into your Loox and reboot it.
4. Don't let PDA fall asleep, start haret.exe which is in the directory where unpacked the archive to.
5. When haret.exe is started press Run.
6. Read the caution when Linux is booted.
7. Make up your mind and then either press RESET (insert a stylus into a hole on the bottom side of the PDA, if you don't want to test memory) or press enter..
WARNING: Poor memory chips might be damaged during the test (that is why you want to test your PDA's memory, isn't it?). None will be responsible for your actions except yourself!!

Note: You have to press any button during the testing process once per 5 or so minutes.
(In a case you need to port the utility on the other platform (not Loox C550/N560) the only thing you have to do is to replace zImage and modify default.txt to let the linux for your platform boot on the target device).)

Saturday, March 14, 2009

Linux Kernel for Fujitsu-Siemens Loox N560/C550 with support for CPLD and builtin leds

I've implemented support for CPLD and leds. htc-gpio driver is used with a custom list of CPLD registers/resources for Fujitsu-Siemens Loox 5XX (N560/C550). Now CPLD interface is available for all other drivers (existing and planned) of the linux/android kernel.

It is interesting how CPLD is implemented in the linux kernel. Actually CPLD is a chip that supports very simple custom logic (and that is the reason why it is much faster than CPU). CPLD chip has a set of input pins and output pins. All these pins are mapped to a region of memory address space. The most important work with CPLD is that modifying values of pins to control devices. This is done setting a value to 0 or 1. In fact, programming of GPIO (general purpose input/output of CPU) is done in the same way: a programmer writes code that sets a GPIO pin to either 0 or 1. Linux kernel already has support for GPIO which provides a set of functions for platform drivers. In addition, GPIO driver provides a way to map any virtual GPIO pin to some custom code that is responsible for handling of setters and getters for the mapped pins. And indeed, that is how htc-egpio driver does the job. The driver leverages gpiolib mapping CPLD registers to GPIO pins. Thanks to this approach, no addition functions are required to work with CPLD. It is enough to use gpio_set_value/.. methods.
After the CPLD driver had been implemented, it was possible to implement a driver for the leds of Loox N560/C550. The leds driver, I implemented, exports a set of functions that other drivers (wifi, gps, bluetooth, pm, ...) are supposed to use to control the leds. Also, the leds driver makes it possible to control the leds from userspace (shell scripts, java program, ...) using files under sysfs filesystem.

The following is the list of files exported by the leds driver and the possible content values for the each file:
/sys/devices/platform/loox5xx-leds.1/keyboard: on, off, any
- backlight for keyboard

/sys/devices/platform/loox5xx-leds.1/left_green: on, off, any
- green led on the left side. WM5 indicates WiFi activity with this led

/sys/devices/platform/loox5xx-leds.1/left_blue: on, off, any
- blue led on the left side. WM5 indicates Bluetooth activity with this led

/sys/devices/platform/loox5xx-leds.1/left_orange: on, off, any
- orange led on the left side. WM5 indicates GPS activity with this led on Loox N560

/sys/devices/platform/loox5xx-leds.1/right_green: on, off, any
- green led on the right side. WM5 allows programs to control this led using winapi.

/sys/devices/platform/loox5xx-leds.1/right_orange: on, off, blink, any
- orange led on the left side. WM5 indicates charging activity with this led

Where:
on - the led is on regardless of the kernel activity,
off - the led is off regardless of the kernel activity,
blink - the led is blinking regardless of the kernel activity,
any - the led is controlled by the kernel.

For instance, if the content of the right_orange file is set to 'on', then the orange led (on right side of PDA) will be on no matter what you do: plug or unplug your device to/from a cradle. The driver will hold on the state of the led until, you write 'any' into the file right_orange. With 'any' value in file, the first 'power' event will change state of the led. The interface of the driver makes it possible to implement any notification (low battery, new mail..) effects you can imagine involving all leds of the device. The following script demonstrates how to blink with the keyboard backlight led:

while true;
do echo on > /sys/devices/platform/loox5xx-leds.1/keyboard;
sleep 0.1;
echo off > /sys/devices/platform/loox5xx-leds.1/keyboard;
sleep 0.1;
# Condition for break...
done
echo any > /sys/devices/platform/loox5xx-leds.1/keyboard;

The current patch for the android cupcake kernel is located here.

Tuesday, March 10, 2009

Cross-Packaging On Debian

Debian/Emdebian already has tons of packages. But sometimes you have a need to package or repackage a piece of software yourself. The following may be helpful if a platform you build for is not the platform you build on.

Use the following command to build a package from a repository of apt:
emsource --arch armel -b memtester

If you need to install a package for target on your host:

dpkg-cross -a armel -i zlib1g-dev_1.2.3.3.dfsg-13em1_armel.deb
The command dpkg -l | grep zlib1g will show you:

ii zlib1g-dev-armel-cross 1:1.2.3.3.dfsg-13em1

(dpkg-cross automatically converts package names and moves content under the /usr/arm-linux-gnueabi (for armel target))

You need to configure ~/.apt-cross/emsource to use emsource without privileges of root. The content of the file may look like:

workingdir: /home/akshaal/.apt-cross-working-dir

In a case you need to build a package from sources that have already been downloaded, use the following command:

dpkg-buildpackage -aarmel

Sunday, March 8, 2009

Install emdebian on ARM device

Here is how I've successfully installed emdebian on my Fujitsu-Siemens Loox N560. I assume there is an already installed emdebian-tools package and cross-compilers. Next thing is to run the following commands:
cd /tmp;
mkdir grip/
sudo debootstrap --arch=arm --foreign lenny grip/ http://www.emdebian.org/grip/
cd grip/
sudo tar -czf /tmp/emdebian-grip-arm-debootstrap.tgz .

Then SD card is to be partitioned with fdisk. For example it could be:

cfdisk /dev/sdX
mkfs.ext3 /dev/sdXy

where sdX - is a device for an SD card. And sdXy is a partition that is supposed to be used for linux on the SD card. Then mount the linux partition and untar the emdebian-grip-arm-debootstrap.tgz:

mount /dev/sdXy /mnt
cd /mnt
tar zxpvf /tmp/emdebian-grip-arm-debootstrap.tgz
ln -s bin/sh init
cd /tmp
umount /mnt

Now we can boot linux from the partition. When linux has booted and a command line appeared, run the following commands step by step:
cd /debootstrap
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
run ./debootstrap --second-stage
dpkg --configure -a
rm /init
cd /
ln -s sbin/init init

Now it is time to reboot linux again. This time a login prompt should work. Use root login and no password. Then configure basic settings:

echo 'nameserver 1.1.1.1' > /etc/resolv.conf
echo '127.0.0.1 localhost' > /etc/hosts
echo '172.16.0.2 myhost 172.16.0.2' >> /etc/hosts
echo 'myhost' > /etc/hostname

use actual nameserver IP instead of 1.1.1.1. Reboot PDA one more time. Then configure network:

ifconfig usb0 172.16.0.1 netmask 255.255.255.0
route add default gw 172.16.0.1
Make sure network is up and running. Then upgrade your installation:

echo 'deb http://www.emdebian.org/grip/ sid main' > /etc/apt/sources.list
apt-get update
apt-get dist-upgrade
apt-get install ...whateveryoulike...

And configure network properly.
cat > /etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug usb0
iface usb0 inet static
address 172.16.0.2
netmask 255.255.255.0
network 127.16.0.0
broadcast 172.16.0.255
gateway 172.16.0.1
dns-nameservers 1.1.1.1
dns-search your.domain

Linux Kernel for Fujitsu-Siemens Loox N560/C550 with usbnet working

After 3 days of struggle I am finally able to build a linux kernel for my PDA Fujitsu-Siemens Loox N560. The kernel has been downloaded from http://www.asm32.ru and patched (pxa27x_udc.c) to support for usbnet (host-to-PDA networking through a usb coard). Also I've made a diff file with differences between the current cupcake kernel and the kernel (by date 02-mar-2009) from asm32.ru site. That will be useful later when I need to apply support for Loox to a new upstream cupcake kernel. Here is the patch which also includes support for usbnet on Loox C550/N560. The patch already contains a kernel configuration file (.config), but usbnet support is not enabled there. Here is the other .config file which is differ from that included in the patch by supporting usbnet, having power management switched off and configuration for larger fonts in console.

It is possible to build the kernel with any toolchain for arm of your choice. I've tried crosstool-ng, emdebian and the one that is included in the android repo. I have decided to leverage emdebian toolchain for I use debian on my PC. The following is the command to build the kernel using emdebian toolchain:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
Of course you have to have emdebian toolchain installed. You can install emdebian something like it is shown below:

apt-get install emdebian-tool
emsetup --arch arm
Then add:

deb http://www.emdebian.org/debian/ unstable main

into your /etc/apt/sources.list and run:
apt-get install linux-kernel-headers-arm-cross gcc-4.2-arm-linux-gnu

When the kernel is built and ready you can copy it on an SD card and run it with haret with the following default.txt:

set MTYPE 1454
set KERNEL zImage
set CMDLINE "root=179:2 mem=60M rootdelay=3 boot_delay=0 init=/init console=tty0 fbcon=rotate:0 androidboot.console=tty0 android.checkjni=1"
set RAMADDR 0xA0200000
bootlinux
That is important! When you've placed zImage on an SD card, you will insert the card in your PDA. And then you must reboot your PDA! And only after you have rebooted your PDA with the SD card inside, you should ask haret to boot linux. That is very important because a linux kernel will not be loaded correctly (or it will but with only 10% chances) if one of the following conditions is true:
1. PDA has fallen asleep or suspended.
2. You have just inserted an SD card in your PDA and not rebooted it yet.
It looks like it is a bug in wince...

P.S. There you can download cupcake kernel http://source.android.com/download

P.P.S. When you build kernel with usbnet, don't set CONFIG_USB_ETH_RNDIS to 'y'! Or your PC kernel (2.6.x) will not find your PDA... neither cdc_ether nor cdc_subset will claim your PDA.

Friday, March 6, 2009

Async Related Posts Widget

I have finally implemented a feature I wanted to have on my blog. It is Related Posts Widget, but not just Yet Another Related Posts Widget. It differs form all other widgets that show related posts. It has features that no other widget has (yet at least):
1. It sorts related posts not only by date, but by relevance!
2. It is small. It is fast. It is asynchronous!. It is just AJAX :).
3. It is easy to install. It is very crucial point for me because I maintain a bunch of blogs and I don't want to waste my time doing coplicated setups for each blog over and over again.

So, lets install the widget in a blog. The installation process constists of two simple modifications to the blog HTML Template. Go to Settings -> Layout -> HTML Template. But Before you start doing any modification, make a backup of your template (Download full template)! Now you are ready. The option Expend Widget Templates should be checked. Then follow the steps below:
1. Find string <data:post.body/> in your template code. Right after(!) this string, insert the snipped below:
<div caption='Related posts:' id='akRelatedPosts' max='8'/>
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<script language='javascript' type='text/javascript'>
(function(_1,_2){var _3={};var _4=_2.length;var _5=function(_6){var _7=&quot;([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?&quot;;var d=_6.match(new RegExp(_7));var _9=0;var _a=new Date(d[1],0,1);if(d[3]){_a.setMonth(d[3]-1)}if(d[5]){_a.setDate(d[5])}if(d[7]){_a.setHours(d[7])}if(d[8]){_a.setMinutes(d[8])}if(d[10]){_a.setSeconds(d[10])}if(d[12]){_a.setMilliseconds(Number(&quot;0.&quot;+d[12])*1000)}if(d[14]){_9=(Number(d[16])*60)+Number(d[17]);_9*=((d[15]==&quot;-&quot;)?1:-1)}_9-=_a.getTimezoneOffset();time=(Number(_a)+(_9*60*1000));return Number(time)};var _b=function(){var _c=[];for(var i in _3){_c.push(_3[i])}if(_c.length&lt;1){return}var _e=document.getElementById(&quot;akRelatedPosts&quot;);if(!_e){return}var _f=_e.getAttribute(&quot;max&quot;)||5;_c=_c.sort(function(a,b){var _12=b.weight-a.weight;if(_12!=0){return _12}return b.date-a.date});var s=&quot;&lt;ul&gt;&quot;;for(var i in _c){if(_f--&lt;1){break}var _14=_c[i];s+=&quot;&lt;li&gt;&lt;a href=&#39;&quot;+_14.href+&quot;&#39;&gt;&quot;+_14.title+&quot;&lt;/a&gt;&lt;/li&gt;&quot;}s+=&quot;&lt;/ul&gt;&quot;;var _15=_e.getAttribute(&quot;caption&quot;);s=&quot;&lt;span class=&#39;caption&#39;&gt;&quot;+_15+&quot;&lt;/span&gt;&quot;+s;_e.innerHTML=s};var _16=function(_17){for(var i in _17.feed.entry){var _19=_17.feed.entry[i];var _1a=_19.id[&quot;$t&quot;];var _1b=_3[_1a];if(_1b){_1b.weight++}else{var _1c;for(var _1d in _19.link){if(_19.link[_1d].rel==&quot;alternate&quot;){_1c=_19.link[_1d].href;break}}if(_1c==_1){continue}_1b={weight:1,title:_19.title[&quot;$t&quot;],date:_5(_19.published[&quot;$t&quot;]),href:_1c};_3[_1a]=_1b}}if(--_4==0){_b()}};var _1e=function(_1f){var _20;try{_20=new XMLHttpRequest()}catch(excp1){try{_20=new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;)}catch(excp2){try{_20=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;)}catch(excp3){_20=false}}}var _21=function(){if(_20.readyState==4){if(_20.status==200){_16(eval(&quot;(&quot;+_20.responseText+&quot;)&quot;))}}};_20.open(&quot;GET&quot;,_1f,true);_20.onreadystatechange=_21;_20.send(null)};for(var _22 in _2){_1e(_2[_22])}} <!-- = = = = = endoffunc = = = = = = -->
)(&quot;<data:blog.url/>&quot;,[
<b:loop values='data:post.labels' var='label'>
&quot;/feeds/posts/default/-/<data:label.name/>?alt=json&amp;max-results=5&quot;<b:if cond='data:label.isLast != &quot;true&quot;'>,</b:if>
</b:loop>
]);
</script>
</b:if>
You should end up with something like this (click on the picture to open a full-size version):
Pay attention to caption='...' and max='8' You may consider to edit these values. A value for caption defines text to be shown above a list of related posts. And max defines a number of displayed relevant posts.
2. This step is optional. Everything should start working without it, but the widget will not be nice-looking. That is why you may want to complete it. So find the string ]]></b:skin>. And insert the following css snippet before(!) the string:
#akRelatedPosts {
padding-top: 20px;
}

#akRelatedPosts .caption {
font-weight: bold;
}

#akRelatedPosts ul {
margin: 0;
}
If you think you have knowledge of css, you can tweak the styles below as you like. Finally you should see a picture like the following (click on picture to see full-size version):
That is all. You can save your template and check your posts.

Thursday, March 5, 2009

Cross-site request

It is handy to test javascript snippets without uploading them on a production server. But it is hard to test AJAX request like that. The reason for it is that firefox doesn't allow a cross-site request for a sake of user's security.
uncaught exception: Access to restricted URI denied (NS_ERROR_DOM_BAD_URI)
But if you know what you are doing you can temporarley switch this behavior off. Open about:config page and set signed.applets.codebase_principal_support to true. Also your script must have the following line executed before any cross-site request:
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
And don't forget to set signed.applets.codebase_principal_support to false value back and remove the line above from script when you have done with testing!!