DESK-MX6UL-L/Development/How to create a bootable microSD card

From DAVE Developer's Wiki
Jump to: navigation, search
History
Version Issue Date Notes

1.0.1

Jun 2021 First DESK release
2.0.0 Mar 2022 DESK 3.0.0 release



How to create a bootable SD card[edit | edit source]

200px-Emblem-important.svg.png

The procedure described here was tested with a virtual machine such as the MVM. The USB controller of the MVM should be enabled by default. If you are using Linux-based distribution and are having trouble detecting a USB device in virtual machine, please make sure your user belongs to the vboxusers group.


This article shows how to create a bootable microSD for the DESK-MX6UL-L Release Notes kit by using a simple bash script.
Note: Note: Starting from DESK-MX6UL-L 1.0.1 release the support for the SPL has been introduced in U-Boot. Previous versions of this script will no longer produce a fully functional and bootable microSD card.

The procedure has been tested on a MVM released with DESK-MX6-L 3.0.0 release with

  • a 16 GB microSD card [1]
  • a USB SD card reader
  • the binary files delivered along with the DESK-MX6UL-L 3.0.0

The resulting card is partitioned as depicted here below:

  • bootable partition (mmcblk0p1, vfat) containing:
    • binary images (U-Boot and kernel images)
  • root file system partition (mmcblk0p2, ext3)
    • root file system binaries and init scripts

The script - named mksd.sh - can be realized with the following code:

#!/bin/bash

if [[ -z $1 || -z $2 || -z $3 || -z $4 || -z $5 ]]
then
	echo "$0 Usage:"
	echo "	$0 <device> <u-boot.img> <SPL> <binaries directory> <rootfs tar.bz2>"
	echo "	Example: $0 /dev/sdc u-boot.img SPL 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.img location!"
	exit
fi

if ! [[ -e $3 ]]
then
	echo "Incorrect SPL location!"
	exit
fi

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

if ! [[ -f $5 ]]
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
SPL=$3
BINARIES=$4
RFS=$5

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`

# check if we're running an old (e.g. 2.20.x) or new (e.g. 2.24.x) sfdisk
sfdisk --help | grep -- -H

if [ "$?" -eq "0" ]
then
	{
		echo 40,1380,0x0c,*
		echo 1420,,83,-
	} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE
else
{
    echo 16M,8176M,0x0c,*
    echo 8192M,,83,-
} | sfdisk $DRIVE
fi

partprobe


echo "[Making filesystems...]"
mkfs.vfat -F 32 -n BOOT "$DRIVE$PART"1 #> /dev/null
mkfs.ext3 -F -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 SPL]"
dd if=$SPL of=$DRIVE bs=512 seek=2 conv=fsync

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

echo "[Done]"

Here is an example that shows how to use this script. We use here files delivered with DESK-MX6UL-L 3.0.0 release. 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
  • splash image: splash_image.bmp
  • Linux kernel: desk-mx6ul-l-3.0.0_uImage
  • Device tree blob: desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb.

We will copy them into ~/desk-mx-l/desk directory. Needed binary files can be found on MVM in the following location:

dvdk@vagrant:~$ ll /tftpboot/desk-mx-l/
total 12180
drwxr-xr-x 2 dvdk root    4096 Feb 27 18:54 ./
drwxr-xr-x 3 dvdk root    4096 Feb 27 18:52 ../
-rw-r--r-- 1 dvdk root   32441 Feb 27 18:54 desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb
-rw-r--r-- 1 dvdk root   31479 Feb 27 18:54 desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb006c.dtb
-rw-r--r-- 1 dvdk root   32518 Feb 27 18:54 desk-mx6ul-l-3.0.0_imx6ul-lynx-som0013-cb002f.dtb
-rw-r--r-- 1 dvdk root   44032 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_spi_SPL
-rw-r--r-- 1 dvdk root  600280 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_spi_u-boot.img
-rw-r--r-- 1 dvdk root   44032 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_SPL
-rw-r--r-- 1 dvdk root  600280 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_u-boot.img
-rw-r--r-- 1 dvdk root   44032 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_spi_SPL
-rw-r--r-- 1 dvdk root  564844 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_spi_u-boot.img
-rw-r--r-- 1 dvdk root   44032 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_SPL
-rw-r--r-- 1 dvdk root  564244 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_u-boot.img
-rw-r--r-- 1 dvdk root   44032 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_usb_SPL
-rw-r--r-- 1 dvdk root  457216 Feb 27 18:52 desk-mx6ul-l-3.0.0_mx6uldesk_lynx_usb_u-boot.img
-rw-r--r-- 1 dvdk root 8189464 Feb 27 18:54 desk-mx6ul-l-3.0.0_uImage
dvdk@vagrant:~$ ll ~/desk-mx-l/desk-mx6ul-l-3.0.0_boot.scr
-rw-rw-r-- 1 dvdk dvdk 936 Feb 28 07:36 /home/dvdk/desk-mx-l/desk-mx6ul-l-3.0.0_boot.scr
dvdk@vagrant:~$ cp /tftpboot/desk-mx-l/desk-mx6ul-l-3.0.0_uImage ~/desk-mx-l/desk/desk-mx6ul-l-3.0.0_uImage
dvdk@vagrant:~$ cp /tftpboot/desk-mx-l/desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb ~/desk-mx-l/desk/desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb
dvdk@vagrant:~$ cp /tftpboot/desk-mx-l/splash_image.bmp ~/desk-mx-l/desk/
dvdk@vagrant:~$ cp ~/desk-mx-l/desk-mx6ul-l-3.0.0_boot.scr ~/desk-mx-l/desk/boot.scr

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

  • Device file of the microSD card (/dev/sdb in the example)
  • U-Boot image
  • SPL
  • Path of the directory containing the bootscript file, the Linux kernel image, and the device tree blob files
  • Archive of the target's root file system (compressed as .tar.bz2 file).
dvdk@vagrant:~/desk-mx-l$ sudo ./desk-mx6ul-l-3.0.0_mksd.sh /dev/sdb /tftpboot/desk-mx-l/desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_u-boot.img /tftpboot/desk-mx-l/desk-mx6ul-l-3.0.0_mx6uldesk_axelulite_SPL ~/desk-mx-l/desk ./rfs/desk-mx6ul-l-3.0.0_dave-image-devel-desk-mx6ul.tar.bz2 
All data on /dev/sdb now will be destroyed! Continue? [y/n]
y
[Partitioning /dev/sdb...]
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 2.05358 s, 511 kB/s
DISK SIZE - 15931539456 bytes
Checking that no-one is using this disk right now ... OK

Disk /dev/sdb: 14.86 GiB, 15931539456 bytes, 31116288 sectors
Disk model: Transcend       
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

>>> Created a new DOS disklabel with disk identifier 0x3adbf72f.
/dev/sdb1: Created a new partition 1 of type 'W95 FAT32 (LBA)' and of size 8 GiB.
/dev/sdb2: Created a new partition 2 of type 'Linux' and of size 6.9 GiB.
/dev/sdb3: Done.

New situation:
Disklabel type: dos
Disk identifier: 0x3adbf72f

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1  *       32768 16777215 16744448    8G  c W95 FAT32 (LBA)
/dev/sdb2       16777216 31116287 14339072  6.9G 83 Linux

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[Making filesystems...]
mkfs.fat 4.1 (2017-01-24)
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 1792384 4k blocks and 448800 inodes
Filesystem UUID: 3f65294a-899d-417c-9092-e8bf209ec6b2
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

[Copying files...]
'/home/dvdk/desk-mx-l/desk/boot.scr' -> '/mnt/boot.scr'
'/home/dvdk/desk-mx-l/desk/desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb' -> '/mnt/desk-mx6ul-l-3.0.0_imx6ul-axelulite-cb003a.dtb'
'/home/dvdk/desk-mx-l/desk/desk-mx6ul-l-3.0.0_uImage' -> '/mnt/desk-mx6ul-l-3.0.0_uImage'
'/home/dvdk/desk-mx-l/desk/splash_image.bmp' -> '/mnt/splash_image.bmp'
[Extracting rfs (this may take a while...)]
[Programming SPL]
86+0 records in
86+0 records out
44032 bytes (44 kB, 43 KiB) copied, 0.0930599 s, 473 kB/s
[Programming u-boot.img]
586+1 records in
586+1 records out
600280 bytes (600 kB, 586 KiB) copied, 1.64853 s, 364 kB/s
[Done]

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

bootscr[edit | edit source]

Once you got the new binaries compiled from your modified sources, they have to be installed on first SD partition preserving the original file names used into boot.scr U-Boot bootscript.

Here below there is an example on how to create a boot.scr file from the bootscript.txt for booting from SD card:

echo 'bootscript generated with command "mkimage -A ARM -T script -C none -n DESK-MX6UL -d bootscript.txt boot.scr"'

setenv desk_release 'desk-mx6ul-l-3.0.0'

if test 0x${som_configid#} = 0x00000013 && test 0x${cb_configid#} = 0x0000002f;
then
    setenv fdtfile ${desk_release}_imx6ul-lynx-som0013-cb002f.dtb
elif test 0x${cb_configid#} = 0x0000003a
then
	setenv fdtfile ${desk_release}_imx6ul-axelulite-cb003a.dtb
elif test 0x${cb_configid#} = 0x0000006c
then
	setenv fdtfile ${desk_release}_imx6ul-axelulite-cb006c.dtb
else
    echo Invalid CB! Autoreset ...
    sleep 30
	reset
fi

setenv bootfile ${desk_release}_uImage

setenv mmc_loadk 'fatload mmc ${mmcdev}:1 ${loadaddr} ${bootfile}'
setenv mmc_loadfdt 'fatload mmc ${mmcdev}:1 ${fdtaddr} ${fdtfile}'

echo Booting DESK-MX6UL via mmcboot with ${fdtfile} as device tree

run mmcboot

echo mmcboot FAILURE

and compile it using:

mkimage -A ARM -T script -C none -n DESK-MX6UL -d bootscript.txt boot.scr

Then copy the boot.scr into the <binaries_dir> directories used by the script to create the SD card.