Wednesday, January 19, 2011

Creating, testing (and flashing -- TBD) a custom kernel

In my last post, I announced that creating and uploading a custom kernel works. Now I will explain how this can be done.

Thanks to MAPGPS from http://www.ardrone-flyers.com for testing my tools.

You need:

- Linux! I used Ubuntu 10.10
- ARDrone linux kernel source (get it from [1])
- ARDrone linux kernel config (get it from [2])
- The Sourcery G++ toolchain (get it from [3] or [4] )
- My plftool, usb_flash tool (see below)
- The old parrot flash tool (for ardrone_usb_bootloader.bin)
- libusb-0.1 (linux) or libusb-win32 (windows)
- zlib


Getting & Compiling plftool + usb_flash tool


These tools are not yet released. Thus I will not provide binaries for windows, instead you have to compile them by yourself. For windows, you need to use mingw32.

Getting the sources

The sources are stored at my google code project. In order to get them, you need to install a svn client. For linux you then call something like this:

svn checkout http://ardrone-tool.googlecode.com/svn/projects/libplf/trunk libplf
svn checkout http://ardrone-tool.googlecode.com/svn/projects/usb_flash/trunk usb_flash
svn checkout http://ardrone-tool.googlecode.com/svn/projects/plftool/trunk plftool

Compiling

Now that you got the sources, you need to compile them. First start with libplf as this is needed by all projects. The library contains the code to read and write .plf files.

cd libplf 
make
cd ..

When compiling with mingw32 call "make -f Makefile.w32" instead.

If libplf was compiled succesfully you should get either a libplf.so or libplf.dll file.
Next compile plftool and usb_flash:

cd plftool
make
cd ..
cd usb_flash
make
cd ..

When compiling with mingw32, call "make -f Makefile.w32" instead.

This should create plftool(.exe) and usb_flash(.exe).

For Linux, if you do not want to install the libplf.so file, you need to call plftool and usb_flash with LD_LIBRARY_PATH=<path_to_the_folder_where_libplf.so_is_located>  prefix.

E.g.
LD_LIBRARY_PATH=/home/scorp2kk/projects/ardrone/tools/libplf plftool 


For Windows, you need to add the folder where libplf.dll is located to your PATH variable before calling usb_flash or plftool.

Patching the kernel


As Parrot's published source code does not match the kernel delivered with firmware > 1.3.3, a patch is required to ensure correct behaviour. This patch mainly affects p6_sdhci.c.

You can get the patch from here. To apply it, just call:

cd linux-2.6.27
patch -p1 < p6_sdhci-patch1.patch


Compiling the kernel

Now as the kernel is patched, you should use Parrot's original config [2] and cross-compile the sources with Sourcery G++. Basically you need to do something like this (don't forget to add the Sourcery' bin directory to your PATH):

cp ARDrone_Version_20100809_1_2-busybox.config .config
make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- zImage 

After a few minutes (or hours) make informs you that the zImage file is available:

Kernel: arch/arm/boot/Image is ready
Kernel: arch/arm/boot/zImage is ready

Creating a kernel.plf file

If you got your zImage, it is time to build a kernel.plf file from it. This is done by plftool. plftool expects a build-ini file that describes the plf file. A sample ini file can be found at plftool/samples/kernel. Using the sample kernel.ini, you only have to modify the "File=" parameters in section "[zImage]" and "[BootParams]" so they point to the zImage file and to a text file, the boot parameters are located in (e.g. called bootparams.txt).

To create the kernel.plf from the kernel.ini, just call:

plftool -b kernel.ini -o kernel.plf 

The tool should output something similar to this and create the kernel.plf file.

Creating kernel.plf based on this config:
File [HdrVersion: 11 EntryPoint: 0x40800000; Version: 0.0.0
TargetPlat=4, TargetAppl=78, HwCompat=0, LangZone=0]
  zImage     @ 0x40800000 (zImage)
  initrd     @ ---------- (no initrd)
  bootparams @ 0x40700000 (bootparams.txt)

 *** kernel.plf created ***

Testing the kernel.plf file

If  kernel.plf file was created, it is time to test this file. First inspect it with plftool:

plftool -d -i kernel.plf

Have a look at dwFileType, dwEntryPoint and Section->LoadAddr.

If it looks okay, it is time to start (not flash!) the kernel:
  1. Copy ardrone_usb_bootloader.bin to the folder of usb_flash
  2. Connect the drone to your PC via USB (the LED should be RED)
  3. Connect the BATTERY to your drone (the LED becomes GREEN) 
  4. Check if the device was found (with lsusb)
  5. Start usb_flash in TEST mode:
usb_flash -t kernel.plf

This will load (NOT flash) the kernel over USB. In general, it is helpful to observe A.R. Drone's serial port, as the whole boot sequence is shown there. usb_flash outputs on success:


...

Mode: kernel test

*** VERIFICATION ***
Verifying ardrone_usb_bootloader.bin
Verifying test_kernel.plf


*** DOWNLOADING USB BOOTLOADER ***
Try [00/10] to connect to VID: 0x19cf PID: 0x1000
Found a possible device:
- Manufacturer: Parrot SA
- Product: P6 USB Stage1
- Serialnumber: ?
- Number of configurations: 1
Sending bootloader (0x5b20 bytes)
- Send Hello P6 (0xA3)
Success!!!

Checksum returned: 0x5256 Expected: 0x5256 => OK
Starting the bootloader...

*** INSTALLER DOWNLOAD ***
Closing usb connection...
Try [00/10] to connect to VID: 0x19cf PID: 0x1000
Found a possible device:
- Manufacturer: Parrot SA
- Product: P6 USB Stage1
- Serialnumber: ?
- Number of configurations: 1
loading installer
Uploading file: test_kernel.plf
Error Code returned: 0x00000000  ==> OK
*** INSTALLATION DONE ***

If everything works, the kernel boots up, WiFi ist started and you should be able to connect to the drone with telnet.

Check the kernel version with

uname -a

If the default kernel starts without problems, you can now begin to modify the kernel config.

Flashing the kernel to NAND works but I still need to modify the plftool.. so stay tuned.


Happy hacking :-)


[1] https://projects.ardrone.org/documents/show/19
[2] https://projects.ardrone.org/documents/show/18
[3] https://projects.ardrone.org/documents/show/20
[4] http://www.codesourcery.com/sgpp/lite/arm/portal/release858

10 comments:

  1. Hi !
    I own an ardrone. I would like to custom it to add some things connected to usb.
    I need to compil the kernel to add some modules, howewer, the .config file i found here is incomplete.

    Im looking for a complete .config file, Please give me any link ;)

    ReplyDelete
  2. Hi François-Karim,

    the .config refereced by [2] is complete. Did you specify the ARCH variable?

    Best regards

    ReplyDelete
  3. Hi es,
    Thanks for your tools and explanation. I was able to compile a new kernel!

    Is it also possible to only compile the USB module/driver?
    I think you did it here:
    http://embedded-software.blogspot.com/2010/12/ar-drone-usb.html
    can you tell us how you compiled the USB module?
    or did you compiled the whole kernel for this?
    Best regards

    ReplyDelete
  4. Hi again:
    I figured some things out.
    first do:
    ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/20100809/linux-2.6.27$ make config

    put "Enable loadable module support" to Yes

    than I did: ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/20100809/linux-2.6.27$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- modules

    and have this output:

    CHK include/linux/version.h
    make[1]: `include/asm-arm/mach-types.h' is up to date.
    CHK include/linux/utsrelease.h
    CALL scripts/checksyscalls.sh
    :1097:2: warning: #warning syscall fadvise64 not implemented
    :1265:2: warning: #warning syscall migrate_pages not implemented
    :1321:2: warning: #warning syscall pselect6 not implemented
    :1325:2: warning: #warning syscall ppoll not implemented
    :1365:2: warning: #warning syscall epoll_pwait not implemented
    Building modules, stage 2.
    MODPOST 0 modules


    Anny idea why the modules are not build?

    kind regards MicroUAV

    ReplyDelete
  5. MAKE MODULES dwc_otg.ko

    cd ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/20100809/linux-2.6.27
    cp ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/ARDrone_version20100809_1_2_configurations_patches/ARDrone_Version_20100809_1_2-kernel.config ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/20100809/linux-2.6.27/.config

    PATH=/home/mavlab/CodeSourcery/Sourcery_G++_Lite/bin:$PATH
    export PATH
    make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- menuconfig

    You need to enable modules for dwc_otg (put leter "M"):
    make menuconfig
    System Type --->
    Parrot drivers --->
    PARROT6 USB driver (Synopsys)

    make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- modules

    modulle find under:

    cd ~/ARDroneSDK/ardrone_onboard/Linux_kernel_source_code/20100809/linux-2.6.27/drivers/parrot/usb/dwc_otg/dwc_otg.ko

    MicroUAV

    ReplyDelete
  6. Hi there,

    I saw the new parrot asteroid car stereo also uses the plf file format for updates.
    They look like they'd contain a full android system but I just don't know how to extract them.

    Is there an easy way to find this out?
    That's the specific file:

    http://download.parrot.com/Parrot_Asteroid_soft_update/asteroid_update_fr_FR.plf

    thank you!

    ReplyDelete
  7. can any one pls help me?
    i tried make libplf...but im getting like

    nimmakal@wp-sata45:~/Desktop/zzz1/libplf$ make
    gcc -shared -o libplf.so plf.o crc32.o gzip.o -lz
    /usr/bin/ld: plf.o: relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
    plf.o: could not read symbols: Bad value
    collect2: ld returned 1 exit status
    make: *** [libplf.so] Error 1
    nimmakal@wp-sata45:~/Desktop/zzz1/libplf$
    patching is ok...in compiling it is saying arm-none-linux-gnueabi:command not found.... wat i hav 2 do???????pls help me

    ReplyDelete
  8. Hi ES, you have explained the whole process in a very edifying way.

    ReplyDelete
  9. Now as the kernel is patched, you should use Parrot's original config [2] and cross-compile the sources with Sourcery G++. Basically you need to do something like this (don't forget to add the Sourcery' bin directory to your PATH)
    automateandvalidate

    ReplyDelete
  10. Hi,

    I have lost one of the main library in OS (libc) and so the kernel cannot boot up! So I cannot connect to drone using USB (it is not recognized as a device)

    Do you know how can I change its firmware? (I guess the only way is to program the processor. do you know how can I do it?)

    Or is there any way to access the internal falsh directly?

    Regards,
    Mahdi

    ReplyDelete