How to create a bootable microSD card (XUELK)

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box
AXEL ULite-top.png Applies to AXEL ULite
SBC Lynx-top.png Applies to SBC Lynx

This article shows how to create a bootable microSD card for the XUELK kit by using a simple bash script. The procedure has been tested on a Linux PC running Ubuntu 12 distribution with

  • a 16 GB microSD card [1]
  • the binary files delivered along with the XUELK 1.1.3.

The resulting card is partitioned as depicted here.

The script - named mksd.sh - looks like this:

#!/bin/bash

if [[ -z $1 || -z $2 || -z $3 || -z $4 ]]
then
	echo "$0 Usage:"
	echo "	$0 <device> <u-boot.imx> <binaries directory> <rootfs tar.bz2>"
	echo "	Example: $0 /dev/sdc u-boot.imx binaries/ rootfs.tar.bz2"
	exit
fi

if [ "$(whoami)" != "root" ]
then
	echo "you must be root to run this script!"
	exit
fi

if ! [[ -b $1 ]]
then
	echo "$1 is not a valid block device!"
	exit
fi

if ! [[ -e $2 ]]
then
	echo "Incorrect u-boot.imx location!"
	exit
fi

if ! [[ -d $3 ]]
then
	echo "Incorrect Binaries location!"
	exit
fi

if ! [[ -f $4 ]]
then
	echo "Incorrect rootfs location!"
	exit
fi

DRIVE=$1
if [[ "$DRIVE" == *"mmcblk"* ]]
then
	echo "You're using a mmc device, I need to fix partition names"
	PART="p"
else
	PART=""
fi
UBOOT=$2
BINARIES=$3
RFS=$4

echo "All data on "$DRIVE" now will be destroyed! Continue? [y/n]"
read ans
if ! [ $ans == 'y' ]
then
	exit
fi

echo "[Partitioning $1...]"

dd if=/dev/zero of=$DRIVE bs=1024 count=1024

SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`

echo DISK SIZE - $SIZE bytes

CYLINDERS=`echo $SIZE/255/63/512 | bc`

{
	echo 10,1380,0x0c,*
	echo 1390,,83,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE
sudo partprobe

echo "[Making filesystems...]"
mkfs.vfat -F 32 -n BOOT "$DRIVE$PART"1 #> /dev/null
mkfs.ext3 -L ROOTFS "$DRIVE$PART"2 #> /dev/null

echo "[Copying files...]"

binaries_dir=${BINARIES%/}
mount "$DRIVE$PART"1 /mnt
cp -av --no-preserve=ownership $binaries_dir/* /mnt/
umount "$DRIVE$PART"1

echo "[Extracting rfs (this may take a while...)]"
mount "$DRIVE$PART"2 /mnt
tar jxf $RFS -C /mnt > /dev/null
chmod 755 /mnt
umount "$DRIVE$PART"2

echo "[Programming u-boot.imx]"
dd if=$UBOOT of=$DRIVE bs=1k seek=1

echo "[Done]"

Here is an example that shows how to use this script. Let's assume that the binary files were downloaded in the xuelk-1.1.3 subdirectory of the working directory. Before invoking the script, the following files has to be renamed in order to make them compatible with the default U-Boot environment variables:

  • bootscript: boot.scr
  • Linux kernel: uImage
  • Device tree blob: imx6ul-lynx.dtb.

This is the list of the binary files that will be used by the script:

sysadmin@stagesw:~/devel/sbclynx/uSD$ ll xuelk-1.1.3/
total 51436
drwxrwxr-x 2 sysadmin sysadmin     4096 Mar 15 14:12 ./
drwxrwxr-x 3 sysadmin sysadmin     4096 Mar 14 18:03 ../
-rw-rw-r-- 1 sysadmin sysadmin      668 Mar 14 14:11 boot.scr
-rw-rw-r-- 1 sysadmin sysadmin    31436 Mar 12 20:53 imx6ul-lynx.dtb
-rw-rw-r-- 1 sysadmin sysadmin  6765480 Mar 12 20:54 uImage
-rw-rw-r-- 1 sysadmin sysadmin   507948 Mar 12 21:03 xuelk-1.1.2_mx6ul_lynx_u-boot.imx
-rw-rw-r-- 1 sysadmin sysadmin 45346549 Mar 15 12:19 xuelk-1.1.3_lynx-image-networking-sbc-lynx.tar.bz2

You can now run the script, by passing the following parameters:

  • Device file of the microSD card (/dev/sdc in the example)
  • U-Boot image
  • Path of the directory containing the bootscript file, the Linux kernel image, and the device tree blob
  • Archive of the target's root file system (compressed as .tar.bz2 file).
sysadmin@stagesw:~/devel/sbclynx/uSD$ sudo ./mksd.sh /dev/sdc xuelk-1.1.3/xuelk-1.1.2_mx6ul_lynx_u-boot.imx xuelk-1.1.3 xuelk-1.1.3/xuelk-1.1.3_lynx-image-networking-sbc-lynx.tar.bz2 
[sudo] password for sysadmin: 
All data on /dev/sdc now will be destroyed! Continue? [y/n]
y
[Partitioning /dev/sdc...]
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 3.65572 s, 287 kB/s
Disk /dev/sdc doesn't contain a valid partition table
DISK SIZE - 15976103936 bytes
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdc: 1942 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdc: unrecognized partition table type
Old situation:
No partitions found
New situation:
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdc1   *     10    1389    1380   11084850    c  W95 FAT32 (LBA)
/dev/sdc2       1390    1941     552    4433940   83  Linux
/dev/sdc3          0       -       0          0    0  Empty
/dev/sdc4          0       -       0          0    0  Empty
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
[Making filesystems...]
mkfs.vfat 3.0.12 (29 Oct 2011)
mke2fs 1.42 (29-Nov-2011)
Filesystem label=ROOTFS
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
277440 inodes, 1108485 blocks
55424 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1136656384
34 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[Copying files...]
`xuelk-1.1.3/boot.scr' -> `/mnt/boot.scr'
`xuelk-1.1.3/imx6ul-lynx.dtb' -> `/mnt/imx6ul-lynx.dtb'
`xuelk-1.1.3/uImage' -> `/mnt/uImage'
`xuelk-1.1.3/xuelk-1.1.2_mx6ul_lynx_u-boot.imx' -> `/mnt/xuelk-1.1.2_mx6ul_lynx_u-boot.imx'
`xuelk-1.1.3/xuelk-1.1.3_lynx-image-networking-sbc-lynx.tar.bz2' -> `/mnt/xuelk-1.1.3_lynx-image-networking-sbc-lynx.tar.bz2'
[Extracting rfs (this may take a while...)]
[Programming u-boot.imx]
496+1 records in
496+1 records out
507948 bytes (508 kB) copied, 2.20883 s, 230 kB/s
[Done]



[1] In case you have a different size, you'll need to change the sfdisk parameters accordingly.