How to create a bootable microSD card (XUELK)

From DAVE Developer's Wiki
Revision as of 13:54, 15 March 2017 by U0001 (talk | contribs) (Created page with "U-boot file <code>u-boot.imx</code> must be stored into first raw sector of the SD card using <code>dd</code> command The following script can be used to create a bootable mic...")

(diff) ← Older revision | Approved revision (diff) | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

U-boot file u-boot.imx must be stored into first raw sector of the SD card using dd command The following script can be used to create a bootable microSD card. Is has been tested with 16 GB microSD cards. In case you have a different size, you'll need to change the sfdisk parameters accordingly. The resulting card is partitioned as depicted in the previous section.

#!/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 to program a bootable microSD card, containing the binaries delivered along with the XUELK 1.1.3.