Making an SD card image for Nano Pi M4v2

Making an Image for sd boot nano pi m4v2, preferrably buildroot. The best way is to download buildroot images from,

https://drive.google.com/drive/folders/1gUzxDS1ekNw4MHwKglZgj_eUikvrP4WB/buildroot-images.tgz

to local PC. Extract buildroot folder

# tar -xvzf buildroot-images.tgz

Then copy it to top inside sd-fuse_rk3399 folder. From inside of sd-fuse_rk3399, run 

# ./mk-sd-image.sh buildroot

flash it to correct block device i.e. /dev/sdX, in my case it is /dev/sde

# (sudo pv -n out/rk3399-sd-buildroot-linux-4.4-arm64-20210620.img | sudo dd of=/dev/sde bs=1M ) 2>&1 | dialog --gauge "Running dd command (cloning), please wait..." 10 70 0

Observe dirty pages using below command

# sync & watch -d grep -e Dirty: -e Writeback: /proc/meminfo

Once dirty pages are finished. Replace your custom images in the SD card or in buildroot folder.


Custom kernel and uboot

analyse fusing.sh and mk-sd-image.sh. Put all the images as


4.4M boot.img

202K idbloader.img

55                            info.conf

18M kernel.img

383K MiniLoaderAll.bin

570         param4sd.txt

 0  parameter.txt

545    partmap.txt

3.8M  resource.img

681M rootfs.img

4.0M            trust.img

4.0M  uboot.img


Now run the shell script,

./mk-sd-image.sh buildroot


Important commands which run from this shell script are as below,

# sudo dd if=/dev/zero of=rk3399-sd-buildroot-linux-4.4-arm64-20210619.img bs=1024 count=0 seek=3906250

# losetup -f

# sudo losetup /dev/loop30 rk3399-sd-buildroot-linux-4.4-arm64-20210619.img

# sfdisk -u S -L -q out/rk3399-sd-buildroot-linux-4.4-arm64-20210619.img 2>/dev/null << EOF

>2048,,0x0C,-

>EOF


Here we are making a partition as non bootable, FAT32 with full size

start = 2048 byte 

size=,, (as much as possible)

type=0x0C (FAT32)

bootable=- non bootable


# ./fusing.sh /dev/loop30 buildroot

# sudo mkfs.exfat /dev/loop35p1 -n FriendlyARM

# sudo losetup -d /dev/loop30

Now your image is ready to flash.


Log:-----------------------------------------------------------------------------------------------

if [ $# -eq 0 ]; then
echo "Usage: $0 DEVICE <debian|buildroot|friendlycore-arm64|friendlydesktop-arm64|lubuntu|eflasher|friendlywrt>"
exit 0
fi
+ '[' 2 -eq 0 ']'

case $1 in
/dev/sd[a-z] | /dev/loop[0-9]* | /dev/mmcblk[0-9]*)
if [ ! -e $1 ]; then
echo "Error: $1 does not exist."
exit 1
fi
DEV_NAME=`basename $1`
BLOCK_CNT=`cat /sys/block/${DEV_NAME}/size` ;;&
/dev/sd[a-z])
DEV_PART=${DEV_NAME}1
REMOVABLE=`cat /sys/block/${DEV_NAME}/removable` ;;
/dev/mmcblk[0-9]* | /dev/loop[0-9]*)
DEV_PART=${DEV_NAME}p1
REMOVABLE=1 ;;
*)
echo "Error: Unsupported SD reader"
exit 0
esac
+ case $1 in
+ '[' '!' -e /dev/loop30 ']'
++ basename /dev/loop30
+ DEV_NAME=loop30
++ cat /sys/block/loop30/size
+ BLOCK_CNT=7812500
+ DEV_PART=loop30p1
+ REMOVABLE=1

if [ ${REMOVABLE} -le 0 ]; then
echo "Error: $1 is a non-removable device. Stop."
exit 1
fi
+ '[' 1 -le 0 ']'

if [ -z ${BLOCK_CNT} -o ${BLOCK_CNT} -le 0 ]; then
echo "Error: $1 is inaccessible. Stop fusing now!"
exit 1
fi
+ '[' -z 7812500 -o 7812500 -le 0 ']'

let DEV_SIZE=${BLOCK_CNT}/2
+ let DEV_SIZE=7812500/2
if [ ${DEV_SIZE} -gt 64000000 ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too large"
exit 1
fi
+ '[' 3906250 -gt 64000000 ']'

true ${MIN_SIZE:=600000}
+ true 600000
if [ ${DEV_SIZE} -le ${MIN_SIZE} ]; then
    echo "Error: $1 size (${DEV_SIZE} KB) is too small"
    echo "       please try another SD card."
    exit 1
fi
+ '[' 3906250 -le 600000 ']'

# ----------------------------------------------------------
# Get target OS

true ${TARGET_OS:=${2,,}}
+ true buildroot

RKPARAM=$(dirname $0)/${TARGET_OS}/parameter.txt
++ dirname ./fusing.sh
+ RKPARAM=./buildroot/parameter.txt
RKPARAM2=$(dirname $0)/${TARGET_OS}/param4sd.txt
++ dirname ./fusing.sh
+ RKPARAM2=./buildroot/param4sd.txt
case ${2,,} in
debian* | friendlywrt | buildroot* | friendlycore* | friendlydesktop* | lubuntu*)
;;
eflasher*)
[ -f ./${TARGET_OS}/idbloader.img ] && touch ${RKPARAM} ;;
*)
echo "Error: Unsupported target OS: ${TARGET_OS}"
exit -1;;
esac
+ case ${2,,} in

if [ -f "${RKPARAM}" -o -f "${RKPARAM2}" ]; then
        echo ""
else
ROMFILE=`./tools/get_pkg_filename.sh ${TARGET_OS}`
cat << EOF
Warn: Image not found for ${TARGET_OS}
----------------
you may download them from the netdisk (dl.friendlyarm.com) to get a higher downloading speed,
the image files are stored in a directory called images-for-eflasher, for example:
   tar xvzf /path/to/NETDISK/images-for-eflasher/${ROMFILE}
----------------
Or, download from http (Y/N)?
EOF

while read -r -n 1 -t 3600 -s USER_REPLY; do
if [[ ${USER_REPLY} = [Nn] ]]; then
echo ${USER_REPLY}
exit 1
elif [[ ${USER_REPLY} = [Yy] ]]; then
echo ${USER_REPLY}
break;
fi
done

if [ -z ${USER_REPLY} ]; then
echo "Cancelled."
exit 1
fi

./tools/get_rom.sh ${TARGET_OS} || exit 1
fi
+ '[' -f ./buildroot/parameter.txt -o -f ./buildroot/param4sd.txt ']'
+ echo ''


# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo "$0" "$@"
exit
fi
++ id -u
+ '[' 1000 -ne 0 ']'
+ echo 'Re-running script under sudo...'
Re-running script under sudo...
+ sudo ./fusing.sh /dev/loop30 buildroot
[sudo] password for n: 
if [ $# -eq 0 ]; then
echo "Usage: $0 DEVICE <debian|buildroot|friendlycore-arm64|friendlydesktop-arm64|lubuntu|eflasher|friendlywrt>"
exit 0
fi
+ '[' 2 -eq 0 ']'

case $1 in
/dev/sd[a-z] | /dev/loop[0-9]* | /dev/mmcblk[0-9]*)
if [ ! -e $1 ]; then
echo "Error: $1 does not exist."
exit 1
fi
DEV_NAME=`basename $1`
BLOCK_CNT=`cat /sys/block/${DEV_NAME}/size` ;;&
/dev/sd[a-z])
DEV_PART=${DEV_NAME}1
REMOVABLE=`cat /sys/block/${DEV_NAME}/removable` ;;
/dev/mmcblk[0-9]* | /dev/loop[0-9]*)
DEV_PART=${DEV_NAME}p1
REMOVABLE=1 ;;
*)
echo "Error: Unsupported SD reader"
exit 0
esac
+ case $1 in
+ '[' '!' -e /dev/loop30 ']'
++ basename /dev/loop30
+ DEV_NAME=loop30
++ cat /sys/block/loop30/size
+ BLOCK_CNT=7812500
+ DEV_PART=loop30p1
+ REMOVABLE=1

if [ ${REMOVABLE} -le 0 ]; then
echo "Error: $1 is a non-removable device. Stop."
exit 1
fi
+ '[' 1 -le 0 ']'

if [ -z ${BLOCK_CNT} -o ${BLOCK_CNT} -le 0 ]; then
echo "Error: $1 is inaccessible. Stop fusing now!"
exit 1
fi
+ '[' -z 7812500 -o 7812500 -le 0 ']'

let DEV_SIZE=${BLOCK_CNT}/2
+ let DEV_SIZE=7812500/2
if [ ${DEV_SIZE} -gt 64000000 ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too large"
exit 1
fi
+ '[' 3906250 -gt 64000000 ']'

true ${MIN_SIZE:=600000}
+ true 600000
if [ ${DEV_SIZE} -le ${MIN_SIZE} ]; then
    echo "Error: $1 size (${DEV_SIZE} KB) is too small"
    echo "       please try another SD card."
    exit 1
fi
+ '[' 3906250 -le 600000 ']'

# ----------------------------------------------------------
# Get target OS

true ${TARGET_OS:=${2,,}}
+ true buildroot

RKPARAM=$(dirname $0)/${TARGET_OS}/parameter.txt
++ dirname ./fusing.sh
+ RKPARAM=./buildroot/parameter.txt
RKPARAM2=$(dirname $0)/${TARGET_OS}/param4sd.txt
++ dirname ./fusing.sh
+ RKPARAM2=./buildroot/param4sd.txt
case ${2,,} in
debian* | friendlywrt | buildroot* | friendlycore* | friendlydesktop* | lubuntu*)
;;
eflasher*)
[ -f ./${TARGET_OS}/idbloader.img ] && touch ${RKPARAM} ;;
*)
echo "Error: Unsupported target OS: ${TARGET_OS}"
exit -1;;
esac
+ case ${2,,} in

if [ -f "${RKPARAM}" -o -f "${RKPARAM2}" ]; then
        echo ""
else
ROMFILE=`./tools/get_pkg_filename.sh ${TARGET_OS}`
cat << EOF
Warn: Image not found for ${TARGET_OS}
----------------
you may download them from the netdisk (dl.friendlyarm.com) to get a higher downloading speed,
the image files are stored in a directory called images-for-eflasher, for example:
   tar xvzf /path/to/NETDISK/images-for-eflasher/${ROMFILE}
----------------
Or, download from http (Y/N)?
EOF

while read -r -n 1 -t 3600 -s USER_REPLY; do
if [[ ${USER_REPLY} = [Nn] ]]; then
echo ${USER_REPLY}
exit 1
elif [[ ${USER_REPLY} = [Yy] ]]; then
echo ${USER_REPLY}
break;
fi
done

if [ -z ${USER_REPLY} ]; then
echo "Cancelled."
exit 1
fi

./tools/get_rom.sh ${TARGET_OS} || exit 1
fi
+ '[' -f ./buildroot/parameter.txt -o -f ./buildroot/param4sd.txt ']'
+ echo ''


# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo "$0" "$@"
exit
fi
++ id -u
+ '[' 0 -ne 0 ']'

# ----------------------------------------------------------
# Get host machine
ARCH=
+ ARCH=
if uname -mpi | grep aarch64 >/dev/null; then
# EMMC=.emmc
ARCH=aarch64/
fi
+ uname -mpi
+ grep aarch64

# ----------------------------------------------------------
# Fusing idbloader, bootloader, trust to card

true ${BOOT_DIR:=./prebuilt}
+ true ./prebuilt

function fusing_bin() {
[ -z $2 -o ! -f $1 ] && return 1

echo "---------------------------------"
echo "$1 fusing"
echo "dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2"
dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2 conv=fdatasync
ddret=$?
}

# umount all at first
set +e
+ set +e
umount /dev/${DEV_NAME}* > /dev/null 2>&1
+ umount /dev/loop30 /dev/loop30p1 /dev/loop30p2
set -e
+ set -e
if [ ! -f ${TARGET_OS}/idbloader.img -a ! -f ${TARGET_OS}/trust.img ]; then
fusing_bin ${BOOT_DIR}/idbloader.img  64
fusing_bin ${BOOT_DIR}/uboot.img      16384
fusing_bin ${BOOT_DIR}/trust.img      24576
fi
+ '[' '!' -f buildroot/idbloader.img -a '!' -f buildroot/trust.img ']'

#<Message Display>
echo "---------------------------------"
+ echo ---------------------------------
---------------------------------
echo "Bootloader image is fused successfully."
+ echo 'Bootloader image is fused successfully.'
Bootloader image is fused successfully.
echo ""
+ echo ''


# ----------------------------------------------------------
# partition card & fusing filesystem

true ${SD_UPDATE:=$(dirname $0)/tools/sd_update}
++ dirname ./fusing.sh
+ true ./tools/sd_update

[[ -z $2 && ! -f "${RKPARAM}" ]] && exit 0
+ [[ -z buildroot ]]

echo "---------------------------------"
+ echo ---------------------------------
---------------------------------
echo "${TARGET_OS^} filesystem fusing"
+ echo 'Buildroot filesystem fusing'
Buildroot filesystem fusing
echo "Image root: `dirname ${RKPARAM}`"
++ dirname ./buildroot/parameter.txt
+ echo 'Image root: ./buildroot'
Image root: ./buildroot
echo
+ echo


PARTMAP=$(dirname $0)/${TARGET_OS}/partmap.txt
++ dirname ./fusing.sh
+ PARTMAP=./buildroot/partmap.txt
PARAM4SD=$(dirname $0)/${TARGET_OS}/param4sd.txt
++ dirname ./fusing.sh
+ PARAM4SD=./buildroot/param4sd.txt

# ----------------------------------------------------------
# Prepare image for sd raw img
#     emmc boot: need parameter.txt, do not need partmap.txt
#     sdraw: all need parameter.txt and partmap.txt

if [ ! -f "${PARTMAP}" ]; then
if [ -d ${TARGET_OS}/sd-boot ]; then
      (cd ${TARGET_OS}/sd-boot && { \
                cp partmap.txt ../; \
        })
       fi
fi
+ '[' '!' -f ./buildroot/partmap.txt ']'

if [ ! -f "${PARAM4SD}" ]; then
if [ -d ${TARGET_OS}/sd-boot ]; then
       (cd ${TARGET_OS}/sd-boot && { \
               cp param4sd.txt ../; \
       })
       fi
fi
+ '[' '!' -f ./buildroot/param4sd.txt ']'

if [ ! -f "${PARTMAP}" ]; then
echo "File not found: ${PARTMAP}, please download the latest version of the image files from http://dl.friendlyarm.com/nanopct4"
exit 1
fi
+ '[' '!' -f ./buildroot/partmap.txt ']'

if [ ! -f "${PARAM4SD}" ]; then
echo "File not found: ${PARAM4SD}, please download the latest version of the image files from http://dl.friendlyarm.com/nanopct4"
exit 1
fi
+ '[' '!' -f ./buildroot/param4sd.txt ']'

# write ext4 image
${SD_UPDATE} -d /dev/${DEV_NAME} -p ${PARTMAP}
+ ./tools/sd_update -d /dev/loop30 -p ./buildroot/partmap.txt
----------------------------------------------------------------
[/dev/loop30] capacity = 3814MB, 4000000000 bytes
current /dev/loop30 partition:
MBR.0 start : 0x003bc00000 size 0x00b2ab2800  kB
MBR.1 start : 0x0006000000 size 0x0035c00000  kB
----------------------------------------------------------------
parsing ./buildroot/partmap.txt:
part.0 flash=mmc,1:loader:idb:0x8000,0x280000:idbloader.img:[RAW] ./buildroot/idbloader.img
part.1 flash=mmc,1:env:env:0x3f8000,0x8000::[RAW] 
part.2 flash=mmc,1:parm:parm:0x400000,0x400000:param4sd.txt:[RAW] ./buildroot/param4sd.txt
part.3 flash=mmc,1:uboot:raw:0x800000,0x400000:uboot.img:[RAW] ./buildroot/uboot.img
part.4 flash=mmc,1:trust:raw:0xc00000,0x400000:trust.img:[RAW] ./buildroot/trust.img
part.5 flash=mmc,1:misc:raw:0x1000000,0x400000::[RAW] 
part.6 flash=mmc,1:resc:raw:0x1400000,0xc00000:resource.img:[RAW] ./buildroot/resource.img
part.7 flash=mmc,1:kern:raw:0x2000000,0x2000000:kernel.img:[RAW] ./buildroot/kernel.img
part.8 flash=mmc,1:boot:raw:0x4000000,0x2000000:boot.img:[RAW] ./buildroot/boot.img
part.9 flash=mmc,1:data:fat:0x3bc00000,0x0::[MBR] 
part.10 flash=mmc,1:rootfs:ext4:0x6000000,0x35c00000:rootfs.img:[MBR] ./buildroot/rootfs.img
----------------------------------------------------------------
create new MBR 2:
[MBR.0] start : 0x003bc00000 size 0x0000000000 
[MBR.1] start : 0x0006000000 size 0x0035c00000 
----------------------------------------------------------------
copy from: ./buildroot to /dev/loop30
 [RAW. 0]:      201 KB | ./buildroot/idbloader.img > 100% : done.
 [RAW. 2]:        1 KB | ./buildroot/param4sd.txt  > 100% : done.
 [RAW. 3]:     4096 KB | ./buildroot/uboot.img     > 100% : done.
 [RAW. 4]:     4096 KB | ./buildroot/trust.img     > 100% : done.
 [RAW. 6]:     3835 KB | ./buildroot/resource.img  > 100% : done.
 [RAW. 7]:    18016 KB | ./buildroot/kernel.img    > 100% : done.
 [RAW. 8]:     4406 KB | ./buildroot/boot.img      > 100% : done.
 [MBR. 1]:   696340 KB | ./buildroot/rootfs.img    > 100% : done.
----------------------------------------------------------------
if [ $? -ne 0 ]; then
echo "Error: filesystem fusing failed, Stop."
exit 1
fi
+ '[' 0 -ne 0 ']'

if [ -z ${ARCH} ]; then
partprobe /dev/${DEV_NAME} -s 2>/dev/null
fi
+ '[' -z ']'
+ partprobe /dev/loop30 -s
/dev/loop30: msdos partitions 2 1
if [ $? -ne 0 ]; then
echo "Warning: Re-reading the partition table failed"

else
case ${TARGET_OS} in
debian* | buildroot* | friendlycore* | friendlydesktop* | lubuntu* | friendlywrt*)
sleep 1
#resize2fs -f /dev/${DEV_PART};;
esac
fi
+ '[' 0 -ne 0 ']'
+ case ${TARGET_OS} in
+ sleep 1

echo "---------------------------------"
+ echo ---------------------------------
---------------------------------
echo "${TARGET_OS^} is fused successfully."
+ echo 'Buildroot is fused successfully.'
Buildroot is fused successfully.
echo "All done."
+ echo 'All done.'
All done.

+ exit
-----------------------------------------------------------------------------------------------


Important links:








Comments

Post a Comment

Popular posts from this blog

dev_get_platdata understanding

Getting started with pinctrl subsystem linux

How to take systrace in android