Wednesday, September 10, 2014

Installing Debian Jessie and Kernel 3.16 on SD card for SAMA5D3 XPLAINED board

I wanted to rewrite a previous post using Jessie instead of Wheezy.  I am running a Ubuntu 14 virtually on my apple laptop using parallels.

First make sure you have the necessary dependencies installed (there may be some I missed):

sudo apt-get update
sudo apt-get install vim git build-essential device-tree-compiler lzma lzop u-boot-tools libncurses5-dev:amd64 libncurses5:i386 libstdc++6:i386

Linux Kernel


Now we build the kernel, I am using the latest kernel build by Robert C Nelson and then modifying it to my needs, his wiki page is here.

Now make sure you are in the master directory where you will put all of your linux build.  I just called my master.  The following steps will clone Roberts' git repository, checkout a branch called tmp and build the kernel (this will take some time).

cd ~/master
git clone https://github.com/RobertCNelson/armv7_devel.git
cd armv7_devel/
git checkout origin/v3.16.x-sama5-armv7 -b tmp
./build_kernel.sh

As the linux kernel is built it will prompt for configuration options.  This first time just exit, we will come back and make changes in a later post.  When done change back to the master directory.

U-boot Compile


To compile U-boot so that it will work with the SAMA5D3 do the following (run this from your master folder).  This relies on the cross compiler built with the kernel script.  Probably a good time to mention that the numbers below will change as scripts are updating.  So if there is a problem running any part of this blog search the directory for the updated numbers.  For example, "find ${DIR}/armv7_devel/dl -name gcc-linaro-arm-linux-gnueabi*_linux -type d" will give you the path used by export CROSS_COMPILE below.


git clone git://git.denx.de/u-boot.git
cd u-boot/
git checkout v2014.07 -b tmp
wget -c https://raw.githubusercontent.com/eewiki/u-boot-patches/master/v2014.07/0001-sama5d3_xplained-uEnv.txt-bootz-n-fixes.patch
patch -p1 < 0001-sama5d3_xplained-uEnv.txt-bootz-n-fixes.patch
export CROSS_COMPILE=~/master/armv7_devel/dl/gcc-linaro-arm-linux-gnueabihf-4.9-2014.08_linux/bin/arm-linux-gnueabihf-
export ARCH=arm
make distclean
make sama5d3_xplained_mmc_config
make

You will need to create a uEnv.txt file that setups the environment and gives some initial instructions, these are specific to the build that follows
vim uEnv.txt
<insert the following>
#Enable systemd in Debian
optargs=quiet init=/lib/systemd/systemd
console=ttyS0,115200
mmcroot=/dev/mmcblk0p2 ro
mmcrootfstype=ext4 rootwait fixrtc
Finally, while in the u-boot directory you can create a MAC address for the board as referenced in this link.  This is not necessary since the board will create one at random.
tools/gen_eth_addr
XX:XX:XX:XX:XX:XX
The result will be a mac address you can use for testing.  If you want to get real ones for deployment check out this link.  Save this MAC address for use later in the install.

Now you are done compiling the Uboot files so back out of the u-boot directory (cd ..)

Root File System


First is to install all the necessary packages.

sudo apt-get install debootstrap
sudo apt-get install binfmt-support
sudo apt-get install qemu-user-static

With everything installed we can now start to create the root file system

mkdir armhf-root
cd armhf-root
sudo debootstrap --foreign --arch=armhf jessie .

This will take a while to download all basic packages.  The next steps setup the root file system so that you can run it as if you were on the SAMA5D3 XPLAINED board :

sudo cp /usr/bin/qemu-arm-static usr/bin
sudo LC_ALL=C LANGUAGE=C LANG=C chroot . /debootstrap/debootstrap --second-stage
sudo LC_ALL=C LANGUAGE=C LANG=C chroot . dpkg --configure -a

Now it is just a matter of making modifications and additions to meet your needs.  First lets add a password for root (all these commands are run from the armhf-root directory):

sudo chroot . passwd

setup the package repository (./etc/apt/sources.list) which will vary based on your needs:

deb http://ftp.us.debian.org/debian/ jessie main contrib non-free #deb-src http://ftp.us.debian.org/debian/ jessie main contrib non-free deb http://ftp.us.debian.org/debian/ jessie-updates main contrib non-free #deb-src http://ftp.us.debian.org/debian/ jessie-updates main contrib non-free deb http://security.debian.org/ jessie/updates main contrib non-free #deb-src http://security.debian.org/ jessie/updates main contrib non-free #deb http://ftp.debian.org/debian jessie-backports main contrib non-free ##deb-src http://ftp.debian.org/debian jessie-backports main contrib non-free

and install some packages (these will be different depending on your needs):

LC_ALL=C LANGUAGE=C LANG=C chroot . apt-get update
LC_ALL=C LANGUAGE=C LANG=C chroot . apt-get install <package_name>

Some packages to add include locales, dialog, python3, python3-serial, python3-bson, vim, openssh-server, ntpdate, ppp, wvdial, usb-modeswitch, eject etc.

Then you can modify different files to fit your needs as well.  For example:
modify etc/fstab using the following:
sudo sh -c "echo '/dev/mmcblk0p2 / auto errors=remount-ro 0 1' >> etc/fstab"
sudo sh -c "echo '/dev/mmcblk0p1 /boot/uboot auto defaults 0 2' >> etc/fstab"
and modify etc/inittab using the following:
sudo sh -c "echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt102' >> etc/inittab"

Or modify the network interfaces:

sudo vim etc/network/interfaces

and add the following (the hwaddress portion is optional, the board will create one at random), use the MAC address created previously in the u-boot section:
allow-hotplug eth0
iface eth0 inet dhcp
hwaddress ether XX:XX:XX:XX:XX:XX
After this you can make other changes and modifications as needed, just remember to always use chroot . when calling an application.

when done remove qemu-arm-static so it doesn't cause problems when you copy over to the SD card.
sudo rm usr/bin/qemu-arm-static
After this you can make other changes and modifications as needed, just remember to always use chroot . when calling an application.

Create SD Card

With these three components we can now create an SD card for use with the SAMA5D3 Xplained.  So start with an SD card preferably with class 10 for better reliability.  My card is an 8 Gbyte SD card.  I  connected the SD card to my parallels Ubuntu.  You may wish to monitor dmesg to identify the SD card.  Mine was /dev/sdb.

The following commands clear the SD card and then setup the partitions on the SD card (one for boot and one for roots).  The sfdisk command will change the terminal prompt which should be expected just type in the last three lines.
export DISK=/dev/sdb
sudo dd if=/dev/zero of=${DISK} bs=1M count=16
sudo sfdisk --in-order --Linux --unit M ${DISK} <<-__EOF__
1,48,0xE,*
,,,-
__EOF__
After that name the partitions as follows:
sudo mkfs.vfat -F 16 ${DISK}1 -n boot
sudo mkfs.ext4 ${DISK}2 -L rootfs
wait 10 seconds and then eject the SD card and reinsert it again.  On some systems it will mount automatically, ubuntu mounts both partitions at /media/username where username is the username of the current login user.  If it doesn't mount you can mount it yourself with the following:
sudo mkdir -p /media/<useranem>/boot/
sudo mkdir -p /media/<username>/rootfs/
sudo mount ${DISK}1 /media/<username>/boot/
sudo mount ${DISK}2 /media/<username>/rootfs/

Copy Files to SD Card


Now we want to copy everything over to our SD card.  Your location for these files may be different depending on where you placed them in your file system.
Check the file location before running these commands.  <username> below should be replaced with your username if using ubuntu or the whole path should be modified to match your settings.  I am running all these commands from my master folder that contains u-boot, armv7_devel, and armhf-root.

First we copy over the u-boot related files
sudo cp -v ./u-boot/spl/u-boot-spl.bin /media/<username>/boot/BOOT.BIN
sudo cp -v ./u-boot/u-boot.img /media/<username>/boot/
sudo cp -v ./u-boot/uEnv.txt /media/<username>/boot/
Now we copy over the root file system:
sudo rsync -axHAX --progress armhf-root/ /media/<username>/rootfs/
then we copy over the kernel image (zImage), the device tree files (dtbs), and any modules related files (the export kernel_version command should make it a little easier to update with a new build in the future).

export kernel_version=3.16.1-sama5-armv7-r6
sudo cp -v armv7_devel/deploy/${kernel_version}.zImage /media/<username>/boot/zImage
sudo mkdir -p /media/boot/dtbs/
sudo tar xvo -f armv7_devel/deploy/${kernel_version}-dtbs.tar.gz -C /media/<username>/boot/dtbs/
sudo tar xv -f armv7_devel/deploy/${kernel_version}-modules.tar.gz -C /media/<username>/rootfs/
After that just unmount the SD card with the following:
sync
sudo umount /media/<username>/boot
sudo umount /media/<username>/rootfs
And then remove the SD card, power down the SAMA5D3 Xplained and pull jumper JP5.  Then insert the SD card into the SAMA5D3 and power up.  Use a serial cable to connect to the SAMA5D3 board and watch it boot up.  Once it is up you may wish to get the modules up and running with the following command:
depmod -a

That was a very basic build and you will quickly want to make changes.  First things is to make your changes.  You can update u-boot, the linux kernel, or the root file system and then just copy over the new files to the SD card.  For the device tree files and the root files system you may wish to delete (remove) the files first before making the changes.  Just using sudo rm -r is a quick and dirty way to remove, if you are doing something for deployment you may wish to do a full clean of the partition as previously described in this post.










2 comments:

  1. Hi, I have a big problem ... I followed this guide and the previous ones but I can not start the operating system from the SD card, the LCD screen of 4.3 "is white all the time and does not boot, I can not verder anything from the serial console. Any ideas on how I could fix or what could be wrong?

    ReplyDelete
    Replies
    1. I have not played at all with the LCD display, the eewiki I followed refers to video but it doesn't look like it is functional yet. You should see output on the SAMA5D3 XPLAINED serial debug console out of the box. It should boot up by default using the internal NAND flash. You can remove jumper J5 to try and force boot up from the SD card.

      Delete