Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 42c2f656 authored by Ahmed Harhash's avatar Ahmed Harhash
Browse files

flash: Add OnePlus Nord (avicii)

parent ff985514
Loading
Loading
Loading
Loading

flash/avicii/config.mk

0 → 100644
+10 −0
Original line number Diff line number Diff line
HLOS_IMAGES_TARGET := boot.img \
    dtbo.img \
    odm.img \
    product.img \
    recovery.img \
    system.img \
    system_ext.img \
    vbmeta.img \
    vbmeta_system.img \
    vendor.img
+185 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

##########
# This script is created and maintained by
#       Bharath(@teamb58).org
# Feel free to connect for any queries or suggestions.
##########

set -e
set -u

CLEAN_FLASH="true" # Control data wipe behavior. Default is "true".

# Target device info
PRODUCT="OnePlus Nord"
PRODUCT_ID="lito"

# Paths/files
ROOT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
IMAGES_DIR="${ROOT_DIR}"

# Abort the script (and wait for a key to be pressed).
abort_now() {
  echo ""
  read -rp "ERROR: Aborting now (press Enter to terminate)." a
  exit 1
}

# Check for connected phone
find_device() {
  echo "INFO: Looking for connected device(s)..."
  DEVICE_FOUND="false"
  while [ ${DEVICE_FOUND} = "false" ]
  do
    serial_numbers=

    for sn in $("${FASTBOOT_BIN}" devices | grep fastboot | grep -oE '^[[:alnum:]]+')
    do
      # Checking the product ID
      PRODUCT_STRING=$("${FASTBOOT_BIN}" -s "${sn}" getvar product 2>&1)
      # Add serial, if product matches
      if [[ ${PRODUCT_STRING} == *"${PRODUCT_ID}"* ]]
      then
        serial_numbers="${serial_numbers} $sn"
      fi
    done

    case $(echo "${serial_numbers}" | wc -w | grep -oE '[0-9]+') in
      0)
        echo ""
        echo "WARNING: No ${PRODUCT} found in fastboot mode."
        echo "WARNING: Make sure that a ${PRODUCT} is connected."
        ;;
      1)
        echo "INFO: One ${PRODUCT} in fastboot mode found)."
        DEVICE_FOUND="true"
        break
        ;;
      *)
        echo ""
        echo "WARNING: Several ${PRODUCT}'s in fastboot mode connected."
        echo "WARNING: Please connect only one ${PRODUCT}."
        ;;
    esac

    echo ""
    while true
    do
      read -rp "Do you want to look for a ${PRODUCT} again? [(Y)es/(n)o]: " a
      if [ -z "${a}" ] || [ "${a}" = 'y' ] || [ "${a}" = 'Y' ]
      then
        break
      elif [ "${a}" = 'n' ] || [ "${a}" = 'N' ]
      then
        exit 0
      fi
    done
  done
}

# Switch to fastbootd
switch_to_fastbootd() {
  echo "INFO: Switching device to fastbootd mode..."
  "$FASTBOOT_BIN" -s "${sn}" reboot fastboot || {
    echo "ERROR: Unable to switch to fastbootd mode."
    abort_now
  }
  sleep 5 # Wait for the device to reboot into fastbootd
  echo "INFO: Device is now in fastbootd mode."
}

# Flash an image to a partition. Abort on failure.
flash_image_or_abort() {
  local retval=0
  "$FASTBOOT_BIN" -s "${1}" flash "${2}" "${IMAGES_DIR}"/"${3}" || retval=$?

  if [ "${retval}" -ne 0 ]
  then
    echo ""
    echo "ERROR: Could not flash the ${2} partition on device ${1}."
    abort_now
  fi
}

# Flash all partitions in fastbootd
flash_device() {
  # Ensure the device is in fastbootd mode
  switch_to_fastbootd

  # Flash partitions
  flash_image_or_abort "${sn}" abl abl.img
  flash_image_or_abort "${sn}" aop aop.img
  flash_image_or_abort "${sn}" bluetooth bluetooth.img
  flash_image_or_abort "${sn}" boot boot.img
  flash_image_or_abort "${sn}" devcfg devcfg.img
  flash_image_or_abort "${sn}" dsp dsp.img
  flash_image_or_abort "${sn}" dtbo dtbo.img
  flash_image_or_abort "${sn}" featenabler featenabler.img
  flash_image_or_abort "${sn}" hyp hyp.img
  flash_image_or_abort "${sn}" imagefv imagefv.img
  flash_image_or_abort "${sn}" keymaster keymaster.img
  flash_image_or_abort "${sn}" logo logo.img
  flash_image_or_abort "${sn}" modem modem.img
  flash_image_or_abort "${sn}" odm odm.img
  flash_image_or_abort "${sn}" product product.img
  flash_image_or_abort "${sn}" qupfw qupfw.img
  flash_image_or_abort "${sn}" recovery recovery.img
  flash_image_or_abort "${sn}" storsec storsec.img
  flash_image_or_abort "${sn}" system system.img
  flash_image_or_abort "${sn}" system_ext system_ext.img
  flash_image_or_abort "${sn}" tz tz.img
  flash_image_or_abort "${sn}" uefisecapp uefisecapp.img
  flash_image_or_abort "${sn}" vbmeta vbmeta.img
  flash_image_or_abort "${sn}" vbmeta_system vbmeta_system.img
  flash_image_or_abort "${sn}" vendor vendor.img
  flash_image_or_abort "${sn}" xbl xbl.img
  flash_image_or_abort "${sn}" xbl_config xbl_config.img

  if [ "${CLEAN_FLASH}" = "true" ]
  then
    "$FASTBOOT_BIN" -s "${sn}" erase userdata || abort_now
  fi
}

# Operating system checks and variable definition
os_checks() {
  case "$(uname -s 2> /dev/null)" in
    Linux|GNU/Linux)
      FASTBOOT_BIN="${ROOT_DIR}/bin-linux-x86/fastboot"
      ;;
    msys|MINGW*)
      FASTBOOT_BIN="${ROOT_DIR}/bin-msys/fastboot.exe"
      ;;
    *)
      echo "ERROR: Unsupported operating system (${OSTYPE})."
      abort_now
      ;;
  esac
}

# Control the reboot sequence
reboot_device() {
  "${FASTBOOT_BIN}" -s "${sn}" reboot
}

# Warn about data wipe, and ask for confirmation
data_wipe_check() {
  if [ "${CLEAN_FLASH}" = "true" ]
  then
    echo ""
    read -rp " Type \"Yes\" (case sensitive) to wipe data and continue. Else, just press Enter: " a
    if [ "_${a:-"No"}" != '_Yes' ]
    then
         CLEAN_FLASH="false"
    fi
  fi
}

echo ""
echo "*** ${PRODUCT} flashing script ***"
os_checks
find_device
data_wipe_check
flash_device
reboot_device