A shell function of the firmware updating at present stage

Hey! Guys, I wrote a bash function for firmware updating, may it helps y’all at present stage:

#!/bin/bash  
update_firmware(){
    if ! [[ -x $(which uuid) ]];
    then 
        echo "Installing uuid......"
        sudo apt-get install uuid -y
    fi
    if ! [[ -x $(which wget) ]];
    then
        echo "Install wget......"
        sudo apt-get install wget -y
    fi
    if ! [[ -x $(which flashcp) ]];
    then
        echo "Install mtd-utils......"
        sudo apt-get install mtd-utils -y
    fi
    spl_file="u-boot-spl.bin.normal.$(uuid).out"
    u_boot_file="visionfive2_fw_payload.$(uuid).img"
    releases_page_file="$(uuid)$(uuid)"
    vf2_repo_url="https://github.com/starfive-tech/VisionFive2"
    fw_release_url="${vf2_repo_url}/releases"
    fw_url="${fw_release_url}/download"
    
    echo "Download releases page......"
    wget -nv -O ${releases_page_file} ${fw_release_url}
    latest_version=$( \
        grep -oP 'VF2_v\d+.\d+.\d+' ${releases_page_file} | \
        head -1 \
    )   
    
    echo "The latest version: ${latest_version}"

    echo "Download SPL file......"
    wget -nv -O ${spl_file} \
        ${fw_url}/${latest_version}/u-boot-spl.bin.normal.out
    echo "Download U-Boot file......"
    wget -nv -O ${u_boot_file} \
        ${fw_url}/${latest_version}/visionfive2_fw_payload.img
    
    devices=$(ls /dev)
    echo "Update SPL of SD & eMMC......"
    if [[ $devices == *"mmcblk0p1"* ]];
    then
        echo "'/dev/mmcblk0p1'(eMMC) exists, updating......"
        sudo dd if=${spl_file} of=/dev/mmcblk0p1 conv=fsync 
    fi
    if [[ $devices == *"mmcblk1p1"* ]];
    then
        echo "'/dev/mmcblk1p1'(SD card) exists, updating......"
        sudo dd if=${spl_file} of=/dev/mmcblk1p1 conv=fsync 
    fi

    echo "Update U-Boot of SD & eMMC......"
    if [[ $devices == *"mmcblk0p2"* ]];
    then
        echo "'/dev/mmcblk0p2'(eMMC) exists, updating......"
        sudo dd if=${u_boot_file} of=/dev/mmcblk0p2 conv=fsync 
    fi
    if [[ $devices == *"mmcblk1p2"* ]];
    then
        echo "'/dev/mmcblk1p2'(SD card) exists, updating......"
        sudo dd if=${u_boot_file} of=/dev/mmcblk1p2 conv=fsync 
    fi

    echo "Update SPL & U-Boot of Flash......"
    sudo flashcp -v ${spl_file}    /dev/mtd0
    sudo flashcp -v ${u_boot_file} /dev/mtd1

    echo "Finished!"
    
    read -p "Do you wish to delete downloaded files?([Y]y/Nn)" del_yn
    if [[ $del_yn =~ ^[Yy]$ ]] || [[ $del_yn == "" ]] ;
    then
        echo "Remove ${spl_file}, ${u_boot_file}, ${releases_page_file}......"
        rm -rf ${spl_file} ${u_boot_file} ${releases_page_file}
    fi
}

.bashrc (8.8 KB)

3 Likes