20180212_jeffrey - silenceuncrio/diary GitHub Wiki

0915

review

1045

試著改寫 FirmwareUpgrade.sh 讓它直接呼叫 mtd

#!/bin/bash
#
# FirmwareUpgrade.sh
#



LOG=/tmp/FirmwareUpgrade.log

TMP=/tmp/FirmwareUpgrade.tmp
PROGRESS=/tmp/FirmwareUpgrade.progress.json

CONF_FILES=/tmp/FirmwareUpgrade.conffiles
CONF_TAR=/tmp/FirmwareUpgrade.tgz
UPGRADE_BIN=/tmp/firmware/sysupgrade.bin
UPGRADE_DEV=firmware



# firmware upgrade progress information
STATE="state"
STATE_IDLE="idle"
STATE_BUSY="busy"
STATE_OK="ok"
STATE_FAIL="fail"
STATUS="status"
INIT_TIME="init_time"
CURR_TIME="curr_time"


# ignore SIGINT - Crtl-C
# ignore SIGTSTP - Ctrl-Z
# ignore SITERM - kill
function trap_init {
  trap "echo ignore signal: SIGINT" SIGINT
  trap "echo ignore signal: SIGTSTP" SIGTSTP
  trap "echo ignore signal: SIGTERM" SIGTERM
}






# init the log for the firmware upgrade progress
function log_init {
  echo "firmware upgrade shell script start..." > $LOG
}

# log
# use the -e option to include nonprintable items, such as the tab and newline characters
function log {
 if [ $# -eq 1 ]
 then
    echo -e $1 >> $LOG
 fi
}






# init the information of the firmware upgrade progress
# use json format
function progress_init {
  # use Date.parse() in javascript to get the number of milliseconds since January 1, 1970
  # init_time=$(date "+%a, %d %b %Y %H:%M:%S")
  echo "{\"$STATE\":\"$STATE_BUSY\",\"$STATUS\":\"init\",\"$INIT_TIME\":\"$(cut -f 1 -d ' ' /proc/uptime)\"}" > $PROGRESS
}

# update the status for the firmware upgrade progress
# use jq - command-line JSON processor

function progress_status {
  filter=".status=\"$1\""
  cat $PROGRESS | jq "$filter" > $TMP
  mv $TMP $PROGRESS
}

function progress_fail {
  filter=".state=\"$STATE_FAIL\" | .status=\"$1\""
  cat $PROGRESS | jq "$filter" > $TMP
  mv $TMP $PROGRESS
}

function progress_ok {
  filter=".state=\"$STATE_OK\" | .status=\"$1\""
  cat $PROGRESS | jq "$filter" > $TMP
  mv $TMP $PROGRESS
}






# log_finish
function finish {
  log "\nfirmware upgrade shell script succeeded"
  log "finished"
  progress_ok "firmware upgrade succeeded"
}






# Extract all files from tared firmware
function extract {
  log "\nExtract the tared firmware ..."
  progress_status "Extract the tared firmware ..."
  if [ -n "$1" ]
  then
    log "tar -C /tmp -xf $1"
    tar -C /tmp -xf $1
    if [ $? == 0 ]
    then
      log "Extract the tared firmware succeeded"
      progress_status "Extract all files from tared firmware succeeded"
    else
      log "Error: tar -C /tmp -xf $1 failed"
      progress_fail "Error: extract firmware failed"
      exit 1
    fi
  else
    log "Error: Please offer the firmware"
    progress_fail "Error: Please offer the firmware"
    exit 1
  fi
}



# MCSV check
function mcsv_check {
  log "\nMCSV check ..."
  progress_status "MCSV check ..."
  cd /tmp/firmware

  if [ -e "mcsv.enc" ]
  then
    log "mcsv.enc exist, decrypt it and check with hardware MCSV"
    log "decrypt mcsv.en"
    openssl des3 -d -salt -in mcsv.enc -out mcsv -k 2wsx#EDC
    if [ $? != 0 ]; then
      log "Error: decrypt mcsv.enc failed"
      progress_fail "Error: decrypt mcsv.enc failed"
      exit 1
    fi

    sw_mcsv=$(cat mcsv)
    log "software MCSV: ${sw_mcsv}"

    sw_mcsv_mmmm=$(echo ${sw_mcsv} | cut -c 1-4)
    log "software MCSV-MMMM: ${sw_mcsv_mmmm}"
    sw_mcsv_cccc=$(echo ${sw_mcsv} | cut -c 5-8)
    log "software MCSV-CCCC: ${sw_mcsv_cccc}"

    hw_mcsv=$(cat /tmp/etc/sysinfo.txt | grep HW_MCSV | cut -c 9-24)
    if [ -z "${hw_mcsv}" ]; then
      log "Error: hw_mcsv not exit, MCSV check failed"
      progress_fail "Error: hw_mcsv not exit, MCSV check failed"
      exit 1
    fi

    log "hardware MCSV: ${hw_mcsv}"

    hw_mcsv_mmmm=$(echo ${hw_mcsv} | cut -c 1-4)
    log "hardware MCSV-MMMM: ${hw_mcsv_mmmm}"
    hw_mcsv_cccc=$(echo ${hw_mcsv} | cut -c 5-8)
    log "hardware MCSV-CCCC: ${hw_mcsv_cccc}"



    # Reject if the Upgrade Image with different Model ID with HW MCSV
    if [ "${sw_mcsv_mmmm}" != "${hw_mcsv_mmmm}" ]; then
      log "Error: Model ID not match, MCSV check failed"
      progress_fail "Error: Model ID not match, MCSV check failed"
      exit 1
    fi



    # - If HW MCSV¡¦s and SW MCSV¡¦s custom ID=0000/0001 (i.e. Proscend)
    #   - allow to upgrade the image with all custom ID
    # - If HW MCSV¡¦s custom ID=0000/0001 (i.e. Proscend) and SW MCSV¡¦s custom ID!=0000/0001
    #   - allow upgrade the image with the same CID as SW MCSV¡¦s CID
    #   - reject other mcsv
    # - If HW MCSV¡¦s CID!=0000/0001 (i.e. Proscend)
    #   - reject the upgrade image with different CID
    if [ "${hw_mcsv_cccc}" == "0000" ] || [ "${hw_mcsv_cccc}" == "0001" ]
    then
      log "Hardware customer ID is '0000' or '0001'"
      if [ "${sw_mcsv_cccc}" == "0000" ] || [ "${sw_mcsv_cccc}" == "0001" ]
      then
        log "Software Customer ID is '0000' or '0001'"
        log "Allow to upgrade"
        log "MCSV check succeeded"
        progress_status "MCSV check succeeded"
      else
        log "Software Customer ID is not '0000' or '0001'"
        log "Allow to upgrade"
        log "Reject other Customer ID"
        log "MCSV check succeeded"
        progress_status "MCSV check succeeded"
        fw_setenv hw_mcsv ${sw_mcsv}
      fi
    else
      log "Hardware customer ID is not '0000' or '0001'"
      if [ "${sw_mcsv_cccc}" != "${hw_mcsv_cccc}" ]; then
        log "Error: Customer ID not match, MCSV check failed"
        progress_fail "Error: Customer ID not match, MCSV check failed"
        exit 1
      fi
      log "MCSV check succeeded"
      progress_status "MCSV check succeeded"
    fi
  else
    log "mcsv.enc not exit, bypass MCSV check"
    progress_status "mcsv.enc not exit, bypass MCSV check"
  fi

  cd - > /dev/null
}



# Extract all files from tared firmware
function extract {
  log "\nExtract the tared firmware ..."
  progress_status "Extract the tared firmware ..."
  if [ -n "$1" ]
  then
    log "tar -C /tmp -xf $1"
    tar -C /tmp -xf $1
    if [ $? == 0 ]
    then
      log "Extract the tared firmware succeeded"
      progress_status "Extract all files from tared firmware succeeded"
    else
      log "Error: tar -C /tmp -xf $1 failed"
      progress_fail "Error: extract firmware failed"
      exit 1
    fi
  else
    log "Error: Please offer the firmware"
    progress_fail "Error: Please offer the firmware"
    exit 1
  fi
}



# Add config files and tar them for used latter
function add_config_tar() {
  log "\nAdd config files and tar them ..."

  ( find $(sed -ne '/^[:space:](/silenceuncrio/diary/wiki/:space:)*$/d; /^#/d; p' \
    /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
    -type f 2>/dev/null) | sort -u > "$CONF_FILES"

  tar czf "$CONF_TAR" -T "$CONF_FILES" 2>/dev/null

  log "Add config files and tar them succeeded"
  progress_status "Add config files and tar them succeeded"
  return 0
}



function write_to_mtd {
    log "\nWrite the firmware image to MTD device ..."

    mtd -j "$CONF_TAR" write "$UPGRADE_BIN" "$UPGRADE_DEV"

    log "Write the firmware image to MTD device succeeded"
    progress_status "Write the firmware image to MTD device succeeded"
}



function firmware_upgrde {
    log "\nUpgrading the firmware ..."

    extract $1
    mcsv_check
    add_config_tar
    write_to_mtd

    log "\nUpgrading the firmware succeeded"
}



# main function - entry point
function main {
  firmware_upgrde "$1"
  finish
}



log_init
progress_init

main $1

1820

希望解掉 keep configuration 的問題

commit 0f924127c1708c10a48a074d30646a5f65d38139
Refs: [develop], {origin/develop}
Author: jeffrey <[email protected]>
Date:   Mon Feb 12 18:18:28 2018 +0800

    modify the openwrt's kill_remaining() to skip 'promsg' and 'iweb'

 .../base_fs/default/rootfs/lib/upgrade/common.sh   | 248 +++++++++++++++++++++
 proscend/prosrc/webcgi/firmware.c                  |   2 +-
 2 files changed, 249 insertions(+), 1 deletion(-)