Friday, 7 February 2014

Building custom (real-time) kernel for Cubieboard 2

Thanks benngcp in review33 for providing a detail guide for building real time kernel:
http://www.review33.com/avforum/forum_message.php?topic=70130726214918

Below is a recap of his guide, I use two SD cards in this guide (first card with 8G or more space to store the Cubian and the kernel source, the other card with 4G or more to store the Mubox image and the compiled kernel files)

1. Follow the guide in https://github.com/cubieplayer/cubian/wiki/Install-Cubian, download Cubian (Cubian-base-r4-arm-a20.img.7z) in here: http://cubian.org/downloads/

2. Load to a micro SD card using Win32 DiskImager:
http://sourceforge.net/projects/win32diskimager/

3. Insert the micro SD card, LAN cable and power up.

4. Login, the default username and password are both "cubie" (no double quotes)


5. After login, you can reset the root password:
#sudo passwd root
(enter new password twice)

6. Then su to root and set correct time & timezone:
#su -
(enter the new password that you just assigned in step 5)

#dpkg-reconfigure tzdata


select the correct region and country, it will show you the local and UTC time.
If the date is incorrect, set it like:
date -s "7 FEB 2014 18:00:00"


7. Refresh package database:
#apt-get update --fix-missing

Note that:
  • It would take about 5-10 minutes, depends on your network bandwidth. 
  • Press "enter" key for all the questions.
Below step is optional:
#apt-get upgrade

8. Get required softwares
#apt-get install libnl1 libnl-dev libssl-dev wpasupplicant bridge-utils curl wget dkms build-essential unzip ncurses-dev uboot-mkimage libusb-1.0 libusb-dev pkg-config
  • It would take about 5-10 minutes, depends on your network bandwidth. 
  • Press "enter" key for all the questions.
  • Additional 150MB disk spaced is required.
9. Get the sunxi kernel source
#cd /root
#git clone git://github.com/cubieboard/linux-sunxi -b cubie/sunxi-3.4

  • It would take an hour or longer, depends on network condition. 
  • Additional 2GB disk spaced is required. (1GB download size, 1.7GB after decompression)


10. Modify kernel source (avoid compile error related to power management)
#cd /root/linux-sunxi
#nano arch/arm/mach-sun7i/pm/standby/mem_printk.c
(Use Ctrl+W to search by text, use Ctrl+C to show current row number)

10.1 goto line 168:
change: char digit_string[] = "0123456789ABCDEF";
to: char* digit_string = "0123456789ABCDEF";
(add "*" and remove [])

Before:
 
After:

10.2 goto line 213:
change: char digit_string[] = "0123456789ABCDEF";
to: char* digit_string = "0123456789ABCDEF";
(add "*" and remove [])

10.3 goto line 369:
change: char fill_ch[] = "       ";
to: char* fill_ch = "       ";

(add "*" and remove [])

Save and exit (Ctrl+X, enter Y and press enter key again)

 11. Download Realtime Patch 3.4.61
#wget https://www.kernel.org/pub/linux/kernel/projects/rt/3.4/older/patch-3.4.61-rt77.patch.xz
#unxz patch*

12. Apply the patch:
#patch -p1 < patch*
It shall take less than 10 seconds to complete.


13. Modify "fs/timerfd.c"
#nano fs/timerfd.c
goto line 388, change
hrtimer_wait_for_timer(&ctx->tmr);
to:
hrtimer_wait_for_timer(&ctx->t.tmr);
14. Get the cubieboard specific kernel configuration default values and open the kernel configuration menu page:
#git clone git://github.com/cubieboard/cubie_configs
#cp cubie_configs/kernel-configs/3.4/cubieboard2_defconfig .config
#make menuconfig


15. Do the following changes inside the menu:
15.1 Kernel Features -> Memory Split -> (change to) 1G/3G user/kernel split
15.2 Kernel Features -> Preemption Model -> (change to) Fully Preemptible Kernel (RT)

15.3 CPU power management -> CPU frequency scaling -> Default CPUFreq governor

(change to "performance")
(Exit and save the configuration)



16. Make the kernel
#alias make='make -j2'
#make
("-j2" means running the process in two threads, this could better utilize the CPU if your computer has multiple CPU/cores. Cubieboard2 is dual core, so using j2 will fully utilize the two cores)

The make process would take over an hour if there is no error, be patient...

Make sure the make process complete without error. If there is error, try to look at the cause of error, if it is something look familar that you've modified before, may be there was typo. If there is something new, try copy and paste the cause (file name, line number, error description) and search. Someone may have faced the same problem before.



(optional) Do a disk cache write back to avoid data loss due to accident:
#sync

17. Make the boot image#make ARCH=arm -j2 uImage

The make process would take about 30 minutes if there is no error, be patient

(optional) Do a disk cache write back to avoid data loss due to accident:
#sync

18. Now we can work on the Mubox. Take another micro SD card (4G or larger), put it into a card reader and connect to Cubieboard2. It should be mounted as /dev/sdb1 if there is a partition.

19. Setup Mubox on this SD card (in card reader), just follow the official setup guide in here:
http://mubox.voyage.hk/cubieboard2
The SD card should be in /dev/sda (before partitioning) and /dev/sda1 (after partitioning and formating). So just replace those "/dev/sdX" with "/dev/sda" and "/dev/sdX1" with "/dev/sda1".

20. Now you should have this SD card in card reader loaded with Mubox, mount it in /media/voyage-mubox. Just unplug the card reader and re-insert it. The system shall auto mount it into /media/voyage-mubox. If not, run the following command manually
#mkdir /media/voyage-mubox
#mount /dev/sdb1 /media/voyage-mubox

21.  Copy uImage & kernel modules

(optional, backup before copying)
#cp /media/voyage-mubox/boot/uImage /media/voyage-mubox/boot/uImage.old
#cd /root/linux-sunxi
#cp arch/arm/boot/uImage /media/voyage-mubox/boot
#make -j2 modules_install INSTALL_MOD_PATH=/media/voyage-mubox

24. Generate the boot up script  #cd /root
#git clone git://github.com/cubieboard/sunxi-tools
#cd /root/sunxi-tools
#make

#cd
#cp /root/linux-sunxi/cubie_configs/sysconfig/linux/cubieboard2.fex .
#nano cubieboard2.fex

(modify the usb detect type from 1 to 0 to solve the CPU overload problem, search "
usb_detect_type=1")
[usbc0]
usb_detect_type=0


Save and exit


#./fex2bin cubieboard2.fex /media/voyage-mubox/boot/script.bin 


25. Done, power off and put the SD card in card reader to the SD card slot of Cubieboard2:
#sync
#poweroff
Then power on again, you shall see Mubox login, enter username=root, password=voyage to login. You may want to set the correct timezone again (on this SD card):
#dpkg-reconfigure tzdata
select the correct region and country, it will show you the local and UTC time.
If the date is incorrect, set it like:
date -s "7 FEB 2014 18:00:00"







If you want to install the Mubox onto NAND drive, do the below steps.
101. Boot up Mubox SD-card and login (username: root, password: voyage)
 (now run the below using the "root" account)

102. Add Cubian APT source:
#wget -O cubian.key http://packages.cubian.org/cubian.gpg.key
#apt-key add cubian.key

Edit /etc/apt/sources.list
#nano /etc/apt/sources.list
and add this line (anywhere)
deb http://packages.cubian.org/ wheezy main
Save and exit

103. Install required tools:
#apt-get install cubian-nandinstall dosfstools cubian-resizefs

104. Fix the "cubian-nandinstall" script:
#nano /usr/sbin/cubian-nandinstall
Find the line
echoRed "!!! This tool must be run on SD-card system"
Remove the "exit 2" line below that line.
Save and exit.


105. Run the copy script, it will erase your NAND drive and copy the content on SD card (mubox) to the NAND drive.
#cubian-nandinstall
poweroff, remove the SD card and power on again. It should boot from the NAND now.

13 comments:

  1. Hello,

    Very very good, and your work will help me a lot, I have only one question, it will work with the Desktop version?

    Thanks

    ReplyDelete
    Replies
    1. No, I afraid this won't work with the desktop verison, if the "desktop version" means x86 architecture.

      I have an Atom small form factor motherboard with Voyage MPD installed and I just compiled the real-time kernel for it. I will write another blog post about the real time kernel patch for Voyage MPD.

      Delete
    2. Thanks again for replaying,

      but I mean in the site how had the cubians, http://cubian.org/downloads/

      the desktop version may work?

      Delete
    3. Oh I see what you mean!
      Yes, that's exactly what I am using in Step 1, the Cubian built for A20. In the guide above I used release 4, now the latest version is release 5:

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hey. I did exactly everything in your marvellous tutorial except that i compiled for cubieboard 1.
    But my cubieboard takes only the unpatched kernels and until now none of my patched kernels worked. Can u give me a hint? is it the CB1? Is it just uncapable of this?

    ReplyDelete
    Replies
    1. Hi, sorry for late reply.
      I think the above setup guide not suitable for cubieboard 1st generation nor the 3rd generation because there are tiny differences. You can use it as a baseline but for some steps I suppose you need to match with the cubieboard 1st gen. specific configuration files.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi, this is a great and useful article. I am getting the following error while compiling the PREEMPT_RT patched Kernel for linux-sunxi. Any ideas to debug these errors?

    The Kernel source is :
    https://github.com/linux-sunxi/linux-sunxi.git --branch sunxi-3.4

    Patch Applied:
    https://www.kernel.org/pub/linux/kernel/projects/rt/3.4/older/patch-3.4.61-rt77.patch.xz


    mm/page_alloc.c: In function ‘__free_pages_ok’:
    mm/page_alloc.c:772:2: error: implicit declaration of function ‘local_lock_irqsave’ [-Werror=implicit-function-declaration]
    local_lock_irqsave(pa_lock, flags);

    mm/page_alloc.c:772:21: error: ‘pa_lock’ undeclared (first use in this function)
    local_lock_irqsave(pa_lock, flags);


    mm/page_alloc.c:778:2: error: implicit declaration of function ‘local_unlock_irqrestore’ [-Werror=implicit-function-declaration]
    local_unlock_irqrestore(pa_lock, flags);

    mm/page_alloc.c: In function ‘drain_pages’:
    mm/page_alloc.c:1234:3: error: implicit declaration of function ‘local_lock_irqsave_on’ [-Werror=implicit-function-declaration]
    cpu_lock_irqsave(cpu, flags);

    mm/page_alloc.c:236:24: error: ‘pa_lock’ undeclared (first use in this function)
    local_lock_irqsave_on(pa_lock, flags, cpu)

    mm/page_alloc.c:1234:3: note: in expansion of macro ‘cpu_lock_irqsave’
    cpu_lock_irqsave(cpu, flags);

    mm/page_alloc.c:1243:3: error: implicit declaration of function ‘local_unlock_irqrestore_on’ [-Werror=implicit-function-declaration]
    cpu_unlock_irqrestore(cpu, flags);

    mm/page_alloc.c: In function ‘free_hot_cold_page’:
    mm/page_alloc.c:1359:21: error: ‘pa_lock’ undeclared (first use in this function)
    local_lock_irqsave(pa_lock, flags);

    mm/page_alloc.c: In function ‘buffered_rmqueue’:
    mm/page_alloc.c:1510:22: error: ‘pa_lock’ undeclared (first use in this function)
    local_lock_irqsave(pa_lock, flags);

    mm/page_alloc.c:1542:3: error: implicit declaration of function ‘local_spin_lock_irqsave’ [-Werror=implicit-function-declaration]
    local_spin_lock_irqsave(pa_lock, &zone->lock, flags);

    mm/page_alloc.c: In function ‘__zone_pcp_update’:
    mm/page_alloc.c:236:24: error: ‘pa_lock’ undeclared (first use in this function)
    local_lock_irqsave_on(pa_lock, flags, cpu)

    mm/page_alloc.c:3993:3: note: in expansion of macro ‘cpu_lock_irqsave’
    cpu_lock_irqsave(cpu, flags);

    CC kernel/exit.o
    mm/page_alloc.c: In function ‘page_alloc_init’:
    mm/page_alloc.c:5028:2: error: implicit declaration of function ‘local_irq_lock_init’ [-Werror=implicit-function-declaration]
    local_irq_lock_init(pa_lock);

    mm/page_alloc.c:5028:22: error: ‘pa_lock’ undeclared (first use in this function)
    local_irq_lock_init(pa_lock);

    mm/page_alloc.c: At top level:
    mm/page_alloc.c:232:8: warning: ‘DEFINE_LOCAL_IRQ_LOCK’ declared ‘static’ but never defined [-Wunused-function]
    static DEFINE_LOCAL_IRQ_LOCK(pa_lock);

    cc1: some warnings being treated as errors
    make[1]: *** [mm/page_alloc.o] Error 1
    make: *** [mm] Error 2
    make: *** Waiting for unfinished jobs....

    ReplyDelete
  6. i have do work it.but have so much error,could you help me

    ReplyDelete
  7. Solution is simple, include #include in locallock.c in linux-sunxi/mm/
    and compilation end with successful

    ReplyDelete
  8. Include is #include < linux/locallock.h>

    ReplyDelete