backup_one(){
  [ ! -f "$backuppath/$1.gz" ] || return 0
  echo "backup $1"
  dd "if=/dev/$1" bs=128K | gzip > "$backuppath/$1.gz"
}

backup_nandb(){
  [ -b /dev/mapper/root-crypt ] || return 1
  mkdir -p "$backuppath" || return 1
  cd "$mountpoint/etc/clover" || return 1
  local nandb_filename="$(cat boardtype)-$(cat VERSION)-$(cat REGION).hsqs"
  nandb_filename="$(echo "$nandb_filename" | tr '[:upper:]' '[:lower:]')"
  cd "$backuppath" || return 1
  if [ ! -f "$nandb_filename" ]; then
    echo "backup $nandb_filename"
    dd if=/dev/mapper/root-crypt bs=4k \
    count="$((($(hexdump -e '1/4 "%u"' -s $((0x28)) -n 4 /dev/mapper/root-crypt)+0xfff)/0x1000))" \
    > "$nandb_filename" && \
    md5sum "$nandb_filename" > "$nandb_filename.md5"
  fi
}

backup_nandc(){
  [ "$cf_backup_nandc" == "y" ] || return 0
  local backuppath="$installpath/backup"
  [ ! -f "$backuppath/nandc.gz" ] || return 0
  backuppath="$modpath"
  backup_one nandc
}

backup(){
  local backuppath="$installpath/backup"
  if [ -f "$modpath/nandc.gz" ]; then
    echo "move nandc"
    mkdir -p "$backuppath"
    mv "$modpath/nandc.gz" "$backuppath/"
  fi
  [ "$cf_backup" == "y" ] || return 0
  backup_nandb
  cd /
  [ "$cf_backup_dummy" == "y" ] || return 0
  backup_one nanda
  backup_one nandd
  backup_one nande
}

dumpFirmware(){
  if [ -z "$1" ]; then
    echo "specify path"
    return 1
  fi
  local backuppath="$1"
  if backup_nandb; then
    cd /
    return 0
  fi
  echo "whoopsie"
  cd /
  return 1
}
