From faedbbe5521388345ba3c2480730439db0a80fe1 Mon Sep 17 00:00:00 2001 From: Christophe Priouzeau Date: Tue, 27 Oct 2020 12:19:10 +0100 Subject: [PATCH] SDCARD-RAW-TOOLS: check requisite tools are available on the system In addition, to script adaptation following shellcheck Signed-off-by: Christophe Priouzeau Change-Id: I49d9f9a3ebfe7be2d1b8e77c8bf6a71751dbf228 --- .../create_sdcard_from_flashlayout.sh | 242 ++++++++++-------- 1 file changed, 132 insertions(+), 110 deletions(-) diff --git a/recipes-devtools/sdcard-raw-tools/sdcard-raw-tools/create_sdcard_from_flashlayout.sh b/recipes-devtools/sdcard-raw-tools/sdcard-raw-tools/create_sdcard_from_flashlayout.sh index e41bd71..a426c37 100755 --- a/recipes-devtools/sdcard-raw-tools/sdcard-raw-tools/create_sdcard_from_flashlayout.sh +++ b/recipes-devtools/sdcard-raw-tools/sdcard-raw-tools/create_sdcard_from_flashlayout.sh @@ -14,10 +14,12 @@ # CREATED: 11/22/2017 15:03 # REVISION: --- #=============================================================================== -#TODO: Pre-requisite tools -# sgdisk -# du -# dd + +PRE_REQUISITE_TOOLS=" \ + sgdisk \ + du \ + dd \ +" unset FLASHLAYOUT_data unset FLASHLAYOUT_filename @@ -70,24 +72,36 @@ usage() { } debug() { - if [ $DEBUG ]; + if [ "$DEBUG" ]; then echo "" - echo "[DEBUG]: $@" + echo "[DEBUG]: $*" fi } function exec_print() { - if [ $DEBUG ]; + if [ "$DEBUG" ]; then echo "" - echo "[DEBUG]: $@" - $@ + echo "[DEBUG EXEC]: $*" + eval "$@" else - $@ 2> /dev/null > /dev/null + eval "$@" 2> /dev/null > /dev/null fi } +tools_check() { + for tools in ${PRE_REQUISITE_TOOLS}; do + if [ "$(command -v "$tools" | wc -l )" -eq 0 ]; then + echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + echo "[ERROR]: $tools cannot be found on your system." + echo " Please check corresponding package is installed or your PATH variable is set correctly." + echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + exit 1 + fi + done +} + # Read Flash Layout file and put information on array: FLASHLAYOUT_data function read_flash_layout() { local i=0 @@ -115,7 +129,7 @@ function read_flash_layout() { #Bin2boot FLASHLAYOUT_data[$i,$COL_BIN2BOOT]=${flashlayout_data[7]} - i=$(($i+1)) + i=$((i+1)) debug "READ: ${flashlayout_data[0]} ${flashlayout_data[1]} ${flashlayout_data[2]} ${flashlayout_data[3]} ..." fi @@ -158,17 +172,17 @@ function get_last_image_path() { ;; esac fi - i=$(($i+1)) + i=$((i+1)) done - if [ -n $last_image ]; + if [ -n "$last_image" ]; then - if [ -f $FLASHLAYOUT_filename_path/$last_image ]; + if [ -f "$FLASHLAYOUT_filename_path/$last_image" ]; then FLASHLAYOUT_prefix_image_path="$FLASHLAYOUT_filename_path" - elif [ -f $FLASHLAYOUT_filename_path/../$last_image ]; + elif [ -f "$FLASHLAYOUT_filename_path/../$last_image" ]; then FLASHLAYOUT_prefix_image_path="$FLASHLAYOUT_filename_path/.." - elif [ -f $FLASHLAYOUT_filename_path/../../$last_image ]; + elif [ -f "$FLASHLAYOUT_filename_path/../../$last_image" ]; then FLASHLAYOUT_prefix_image_path="$FLASHLAYOUT_filename_path/../.." else @@ -194,7 +208,7 @@ function calculate_number_of_partition() { then if [ "$selected" == "P" ] || [ "$selected" == "E" ] || [ "$selected" == "PD" ] || [ "$selected" == "DP" ]; then - num=$(($num+1)) + num=$((num+1)) fi fi done @@ -207,9 +221,9 @@ function calculate_number_of_partition() { function move_partition_offset() { ind=$1 new_offset=$2 - offset_hexa=$(printf "%x\n" $new_offset) + offset_hexa=$(printf "%x\n" "$new_offset") - for ((k=$ind;k> $FLASHLAYOUT_infoname + echo -n "=============" >> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" #empty line for ((i=0;i> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" # part name for ((i=0;i> $FLASHLAYOUT_infoname + printf "= %08s " "$partName" >> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" #empty for ((i=0;i> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" # partition number for ((i=0;i> $FLASHLAYOUT_infoname - j=$(($j+1)) + printf "= %08s%-2d " "mmcblk0p" $j>> "$FLASHLAYOUT_infoname" + j=$((j+1)) fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" j=1 for ((i=0;i> $FLASHLAYOUT_infoname - j=$(($j+1)) + printf "= (%-2d) " $j>> "$FLASHLAYOUT_infoname" + j=$((j+1)) fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" for ((i=0;i> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" for ((i=0;i> $FLASHLAYOUT_infoname + echo -n "=============" >> "$FLASHLAYOUT_infoname" fi fi done - echo "=" >> $FLASHLAYOUT_infoname + echo "=" >> "$FLASHLAYOUT_infoname" # print legend of partition j=1 for ((i=0;i> $FLASHLAYOUT_infoname - echo " Device: /dev/mmcblk0p$j" >> $FLASHLAYOUT_infoname - echo " Label: $partName" >> $FLASHLAYOUT_infoname - if [ -n "$bin2flash" ]; - then - echo " Image: $bin2flash" >> $FLASHLAYOUT_infoname - else - echo " Image:" >> $FLASHLAYOUT_infoname - fi - j=$(($j+1)) + { + echo "($j):" + echo " Device: /dev/mmcblk0p$j" + echo " Label: $partName" + if [ -n "$bin2flash" ]; + then + echo " Image: $bin2flash" + else + echo " Image:" + fi + } >> "$FLASHLAYOUT_infoname" + j=$((j+1)) fi fi done @@ -667,19 +684,19 @@ function print_populate_on_infofile() { then if [ "$selected" == "E" ]; then - echo "- Populate partition $partName (/dev/mmcblk0p$j)" >> $FLASHLAYOUT_infoname + echo "- Populate partition $partName (/dev/mmcblk0p$j)" >> "$FLASHLAYOUT_infoname" if [ -n "$bin2flash" ]; then - echo " dd if=$bin2flash of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> $FLASHLAYOUT_infoname + echo " dd if=$bin2flash of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> "$FLASHLAYOUT_infoname" else - echo " dd if= of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> $FLASHLAYOUT_infoname + echo " dd if= of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> "$FLASHLAYOUT_infoname" fi else - echo "- Populate partition $partName (/dev/mmcblk0p$j)" >> $FLASHLAYOUT_infoname - echo " dd if=$bin2flash of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> $FLASHLAYOUT_infoname + echo "- Populate partition $partName (/dev/mmcblk0p$j)" >> "$FLASHLAYOUT_infoname" + echo " dd if=$bin2flash of=/dev/mmcblk0p$j bs=1M conv=fdatasync status=progress" >> "$FLASHLAYOUT_infoname" fi - echo "" >> $FLASHLAYOUT_infoname - j=$(($j+1)) + echo "" >> "$FLASHLAYOUT_infoname" + j=$((j+1)) fi fi done @@ -687,7 +704,7 @@ function print_populate_on_infofile() { function create_info() { -cat > $FLASHLAYOUT_infoname << EOF +cat > "$FLASHLAYOUT_infoname" << EOF This file describe How to update manually the partition of SDCARD: 1. SDCARD schema of partition 2. How to populate each partition @@ -699,7 +716,7 @@ This file describe How to update manually the partition of SDCARD: EOF print_shema_on_infofile -cat >> $FLASHLAYOUT_infoname << EOF +cat >> "$FLASHLAYOUT_infoname" << EOF 2. How to populate each partition --------------------------------- @@ -707,7 +724,7 @@ EOF print_populate_on_infofile -cat >> $FLASHLAYOUT_infoname << EOF +cat >> "$FLASHLAYOUT_infoname" << EOF 3. How to update the kernel/devicetree -------------------------------------- @@ -758,10 +775,13 @@ function print_warning() { echo "" echo "???????????????????????????????????????????????????????????????????????????" echo "???????????????????????????????????????????????????????????????????????????" - for t in "`echo $WARNING_TEXT | tr '#' '\n'`"; + OLD_IFS=$IFS + IFS=$'\n' + for t in $(echo "$WARNING_TEXT" | tr '#' '\n'); do echo "$t" done + IFS=$OLD_IFS echo "[WARNING]: IT'S POSSIBLE, THE BOARD DOES NOT BOOT CORRECTLY DUE TO " echo " FILE(s) NOT PRESENT." echo "???????????????????????????????????????????????????????????????????????????" @@ -786,10 +806,12 @@ then echo "" usage else + tools_check + FLASHLAYOUT_filename=$1 - FLASHLAYOUT_filename_path=$(dirname $FLASHLAYOUT_filename) - FLASHLAYOUT_filename_name=$(basename $FLASHLAYOUT_filename) - FLASHLAYOUT_dirname=$(basename $FLASHLAYOUT_filename_path) + FLASHLAYOUT_filename_path=$(dirname "$FLASHLAYOUT_filename") + FLASHLAYOUT_filename_name=$(basename "$FLASHLAYOUT_filename") + FLASHLAYOUT_dirname=$(basename "$FLASHLAYOUT_filename_path") _extension="${FLASHLAYOUT_filename##*.}" if [ ! "$_extension" == "tsv" ]; @@ -801,19 +823,19 @@ else fi # File have a correct extension # - if echo $FLASHLAYOUT_dirname | grep -q flashlayout + if echo "$FLASHLAYOUT_dirname" | grep -q flashlayout then # add directory name as prefix for raw image new_filename=$(echo "$FLASHLAYOUT_dirname/$FLASHLAYOUT_filename_name" | sed -e "s|/|_|g") filename_for_raw_to_use="$FLASHLAYOUT_filename_path/$new_filename" else - filename_for_raw_to_use=$FLASHLAYOUT_filename + filename_for_raw_to_use="$FLASHLAYOUT_filename" fi - FLASHLAYOUT_rawname=$(basename $filename_for_raw_to_use | sed -e "s/tsv/raw/") - FLASHLAYOUT_infoname=$(basename $filename_for_raw_to_use | sed -e "s/tsv/how_to_update.txt/") + FLASHLAYOUT_rawname=$(basename "$filename_for_raw_to_use" | sed -e "s/tsv/raw/") + FLASHLAYOUT_infoname=$(basename "$filename_for_raw_to_use" | sed -e "s/tsv/how_to_update.txt/") # check if flashlayout have sdcard name - if [ $(grep -i $SDCARD_TOKEN $FLASHLAYOUT_filename| wc -l) -eq 0 ]; + if [ "$(grep -ic "$SDCARD_TOKEN" "$FLASHLAYOUT_filename")" -eq 0 ]; then echo "" echo "[WARNING]: THE FLASHLAYOUT NAME DOES NOT CONTAINS SDCARD REFERENCE." @@ -832,15 +854,15 @@ else FLASHLAYOUT_infoname=$FLASHLAYOUT_prefix_image_path/$FLASHLAYOUT_infoname # erase previous raw image - if [ -f $FLASHLAYOUT_rawname ]; + if [ -f "$FLASHLAYOUT_rawname" ]; then echo "" echo "[WARNING]: A previous raw image are present on this directory" echo "[WARNING]: $FLASHLAYOUT_rawname" echo "[WARNING]: would you like to erase it: [Y/n]" - read answer + read -r answer if [[ "$answer" =~ ^[Yy]+[ESes]* ]]; then - rm -f $FLASHLAYOUT_rawname $FLASHLAYOUT_infoname + rm -f "$FLASHLAYOUT_rawname" "$FLASHLAYOUT_infoname" fi fi @@ -851,8 +873,8 @@ else fi generate_empty_raw_image -generate_gpt_partition_table_from_flash_layout -populate_gpt_partition_table_from_flash_layout +generate_gpt_partition_table_from_flash_layout "" +populate_gpt_partition_table_from_flash_layout "" create_info print_info