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

From DAVE Developer's Wiki
< DESK-MX8M-L
Revision as of 11:02, 25 July 2023 by U0026 (talk | contribs) (Updates for DESK-MX8-L 4.0.0 release)

Jump to: navigation, search
History
ID# Issue Date Notes

16139

17/02/2022 First DESK-MX8M-L release

17546

24/02/2023 DESK-MX8M-L-2.0.0 release

17546

25/07/2023 DESK-MX8M-L-4.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-MX8M-L/General/Release_Notes_(DESK-MX8M-L)|DESK-MX8M-L Release Notes] kit by using a simple bash script.
Note: Starting from DESK-MX8M-L-2.0.0 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-MX8M-L 4.0.0 release with

  • a 32 GB microSD card [1]
  • a USB SD card reader
  • the binary files delivered along with the DESK-MX8M-L 4.0.0

The resulting card is partitioned as depicted here.

The script - named desk-mx8m-l-4.0.0_mksd.sh - can be realized with the following code:

#!/bin/bash

if [[ -z $1 || -z $2 || -z $3 || -z $4 ]]
then
	echo "$0 Usage:"
	echo "	$0 <device> <flash.bin> <binaries directory> <rootfs tar.bz2>"
	echo "	Example: $0 /dev/sdc flash.bin 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 flash.bin 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
BOOT=$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`

# 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 flash.bin]"
dd if=$BOOT of=$DRIVE bs=1k seek=32 conv=fsync

echo "[Done]"
200px-Emblem-important.svg.png

if you are making a uSD for MITO 8M Mini SOM, then the last line must be modified with dd if=$BOOT of=$DRIVE bs=1k seek=33.

Here is an example that shows how to use this script. We use here files delivered with DESK-MX8M-L 4.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
  • Linux kernel: Image
  • Device tree blob: imx8mp-mito8mplus-cb1008.dtb or imx8mm-mito8mmini-cb100a.dtb

Let's start with creating boot.scr. On MVM there is /home/dvdk/desk-mx-l/desk-mx8m-l-4.0.0_bootscript.txt file that we can use to create needed file.

Here below there is an example on how to create a boot.scr file from the desk-mx8m-l-4.0.0_bootscript.txt for booting from SD card:

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

setenv desk_release 'desk-mx8m-l-4.0.0'

if test 0x${cb_configid#} = 0x00001001;
then
	setenv fdtfile imx8mp-mito8mplus-cb1001.dtb
elif test 0x${cb_configid#} = 0x00001004;
then
	setenv fdtfile imx8mp-mito8mplus-cb1004.dtb
elif test 0x${cb_configid#} = 0x00001005;
then
	setenv fdtfile imx8mp-mito8mplus-cb1005.dtb
	# CM7 boot
	setenv cm7_loadaddr 0x48000000
	setenv cm7_tcm_start 0x7e0000
	setenv cm7_boot 'fatload mmc ${sd_dev}:1 ${cm7_loadaddr} stress_m7.bin;cp.b ${cm7_loadaddr} ${cm7_tcm_start} 20000; bootaux ${cm7_tcm_start}'
elif test 0x${cb_configid#} = 0x00001008;
then
	setenv fdtfile imx8mp-mito8mplus-cb1008.dtb
elif test 0x${cb_configid#} = 0x0000100a;
then
	setenv fdtfile imx8mm-mito8mmini-cb100a.dtb
else
	echo Invalid CB ConfigID! Autoreset ...
	sleep 30
	reset
fi

setenv bootfile Image
setenv mmc_loadk 'fatload mmc ${sd_dev}:1 ${loadaddr} ${bootfile}'
setenv mmc_loadfdt 'fatload mmc ${sd_dev}:1 ${fdt_addr} ${fdtfile}'
setenv mmcboot 'run mmc_loadk; run mmc_loadfdt; run bootk'
setenv bootargs 'console=ttymxc1,115200 earlycon=ec_imx6q,0x30890000,115200 root=/dev/mmcblk1p2'
setenv bootk 'booti ${loadaddr} - ${fdt_addr}'

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

run mmcboot

echo mmcboot FAILURE

and compile it using:

dvdk@vagrant:~$ mkimage -A ARM -T script -C none -n DESK-MX8M -d ~/desk-mx-l/desk-mx8m-l-4.0.0_bootscript.txt ~/desk-mx-l/boot.scr
Image Name:   DESK-MX8M
Created:      Tue Jul 25 12:24:35 2023
Image Type:   ARM Linux Script (uncompressed)
Data Size:    1371 Bytes = 1.34 KiB = 0.00 MiB
Load Address: 00000000
Entry Point:  00000000
Contents:
   Image 0: 1363 Bytes = 1.33 KiB = 0.00 MiB
dvdk@vagrant:~$

In the case of you don't have some file on MVM, please download it from mirro.dave.eu.

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

dvdk@vagrant:~$ mkdir -p ~/desk-mx-l/desk
dvdk@vagrant:~$ cd ~/desk-mx-l/desk
dvdk@vagrant:~/desk-mx-l/desk$ ls /tftpboot/desk-mx8m-l/ -l
total 34872
-rw-r--r-- 1 dvdk root 31351296 Jul 24 08:50 desk-mx8m-l-4.0.0_Image
-rw-r--r-- 1 dvdk root  1406688 Jul 24 08:48 desk-mx8m-l-4.0.0_imx8mm_flash.bin
-rw-r--r-- 1 dvdk root    43034 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mm-mito8mmini-cb100a.dtb
-rw-r--r-- 1 dvdk root    43022 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mm-mito8mmini.dtb
-rw-r--r-- 1 dvdk root  1433040 Jul 24 08:48 desk-mx8m-l-4.0.0_imx8mp_flash.bin
-rw-r--r-- 1 dvdk root    60443 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mp-mito8mplus-cb1001.dtb
-rw-r--r-- 1 dvdk root    62520 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mp-mito8mplus-cb1004.dtb
-rw-r--r-- 1 dvdk root    61258 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mp-mito8mplus-cb1005.dtb
-rw-r--r-- 1 dvdk root    61299 Jul 24 08:50 desk-mx8m-l-4.0.0_imx8mp-mito8mplus-cb1008.dtb
lrwxrwxrwx 1 root root       49 Jul 24 09:42 Image -> /tftpboot/desk-mx8m-l/desk-mx8m-l-4.0.0_Image
lrwxrwxrwx 1 root root       60 Jul 24 09:42 imx8mm_flash.bin -> /tftpboot/desk-mx8m-l/desk-mx8m-l-4.0.0_imx8mm_flash.bin
lrwxrwxrwx 1 root root       72 Jul 24 09:42 imx8mm-mito8mmini-cb100a.dtb -> /tftpboot/desk-mx8m-l/desk-mx8m-l-4.0.0_imx8mm-mito8mmini-cb100a.dtb
lrwxrwxrwx 1 root root       60 Jul 24 09:42 imx8mp_flash.bin -> /tftpboot/desk-mx8m-l/desk-mx8m-l-4.0.0_imx8mp_flash.bin
lrwxrwxrwx 1 root root       72 Jul 24 09:42 imx8mp-mito8mplus-cb1008.dtb -> /tftpboot/desk-mx8m-l/desk-mx8m-l-4.0.0_imx8mp-mito8mplus-cb1008.dtb
-rw-r--r-- 1 dvdk root  1152054 Jul 24 08:48 splash_image.bmp
dvdk@vagrant:~/desk-mx-l/desk$ mkdir binaries
dvdk@vagrant:~/desk-mx-l/desk$ cp /tftpboot/desk-mx8m-l/Image binaries/
dvdk@vagrant:~/desk-mx-l/desk$ cp /tftpboot/desk-mx8m-l/imx8mp-mito8mplus-cb1008.dtb binaries/
dvdk@vagrant:~/desk-mx-l/desk$ cp ~/desk-mx-l/boot.scr binaries/
dvdk@vagrant:~/desk-mx-l/desk$ cp /tftpboot/desk-mx8m-l/imx8mp_flash.bin flash.bin
dvdk@vagrant:~/desk-mx-l/desk$ 

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

  • Device file of the microSD card (/dev/sdb in the example)
  • Boot image
  • 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/desk$ cd ~/desk-mx-l/
dvdk@vagrant:~/desk-mx-l$ sudo umount /dev/sdb*
umount: /dev/sdb: not mounted.
umount: /dev/sdb1: not mounted.
dvdk@vagrant:~/desk-mx-l$ sudo ./desk-mx8m-l-4.0.0_mksd.sh /dev/sdb desk/flash.bin desk/binaries/ rfs/desk-mx8m-l-4.0.0_desk-image-qt6-desk-mx8mp.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, 0.285905 s, 3.7 MB/s
DISK SIZE - 31914983424 bytes
Checking that no-one is using this disk right now ... OK

Disk /dev/sdb: 29.74 GiB, 31914983424 bytes, 62333952 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 0x688f6e07.
/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 21.7 GiB.
/dev/sdb3: Done.

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

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1  *       32768 16777215 16744448    8G  c W95 FAT32 (LBA)
/dev/sdb2       16777216 62333951 45556736 21.7G 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 5694592 4k blocks and 1425408 inodes
Filesystem UUID: 393fccfd-00af-487c-ae97-a255d18ab558
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000

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

[Copying files...]
'desk/binaries/boot.scr' -> '/mnt/boot.scr'
'desk/binaries/desk-mx8m-l-4.0.0-rc2_bootscript.txt' -> '/mnt/desk-mx8m-l-4.0.0-rc2_bootscript.txt'
'desk/binaries/Image' -> '/mnt/Image'
'desk/binaries/imx8mp-mito8mplus-cb1008.dtb' -> '/mnt/imx8mp-mito8mplus-cb1008.dtb'
[Extracting rfs (this may take a while...)]
[Programming flash.bin]
1399+1 records in
1399+1 records out
1433040 bytes (1.4 MB, 1.4 MiB) copied, 0.30112 s, 4.8 MB/s
[Done]
dvdk@vagrant:~/desk-mx-l$

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