Skip to content

Instantly share code, notes, and snippets.

@duggum
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggum/2cb9e72962dff149406b to your computer and use it in GitHub Desktop.
Save duggum/2cb9e72962dff149406b to your computer and use it in GitHub Desktop.
A bash script for installing Cyanogen OS 12 on a OnePlus One phone
#!/usr/bin/env bash
#
# cos12-flash.sh
#
# Run this script from within the extracted CM 12.0 fastboot directory.
#
# This script will scan your extracted Cyanogen OS 12 < FASTBOOT > ROM
# directory for the files required for a successful flash via 'fastboot'.
# If the tests are successful, you can run it in 'flash' mode to install
# your new OS. Enjoy!
#
# NOTE: If you DID NOT download the ROM from the following URL, I cannot
# make any guess at your chance of success running this script.
#
# http://builds.cyngn.com/cyanogen-os/bacon/12.0-YNG1TAS0YL-bacon/2263178b74/cm-12.0-YNG1TAS0YL-bacon-signed-fastboot.zip
#
# :: Run at YOUR OWN RISK!! This worked for ME on MY system. YMMV. ::
#
# <<< TEST FIRST, THEN TEST AGAIN >>>
#
# working variables
declare fb fb_store has_flashed has_tested fb_ok user_fb user_input regex
fb="fastboot" # defaults to command available on $PATH
fb_store=".fb_store"
has_flashed="no"
has_tested="no"
fb_ok="no"
# pretty colors
declare esc bullet e_sub ok_arrows e_arrows
esc=$(printf "\e") # <-- convert escape so it is here doc friendly
bullet="$esc[1;34m::$esc[0m"
e_sub="$esc[1;31m*$esc[0m"
ok_arrows="$esc[1;32m>>$esc[0m"
e_arrows="$esc[1;31m>>$esc[0m"
blue="$esc[34m"
red="$esc[31m"
green="$esc[32m"
yellow="$esc[33m"
mag="$esc[35m"
cyan="$esc[36m"
def="$esc[0m"
# error code tracker - set to 1 to force test on initial run
declare -i err=1
# ---------------------------------------------------------
# sort_array
#
# Sorts an array alphanumerically
# see: man sort(1)
#
# Globals:
# None
# Arguments:
# arr - the array to be sorted
# Returns:
# None
#
# ---------------------------------------------------------
sort_array() {
declare -a arr
arr=( "$@" )
for i in "${arr[@]}" ; do
echo "$i"
done | sort -d -f
unset arr
}
# flashable file arrays
declare -a exts=( 'bin' 'mbn' 'img' )
declare -a bin_files=( $(sort_array "$(basename -s .bin ./*.bin)") )
declare -a bin_expected=( logo NON-HLOS static_nvbk )
declare -a bin_partitions=( LOGO modem oppostanvbk )
declare -a mbn_files=( $(sort_array "$(basename -s .mbn ./*.mbn)") )
declare -a mbn_expected=( emmc_appsboot rpm sbl1 sdi tz )
declare -a mbn_partitions=( aboot rpm sbl1 dbi tz )
declare -a img_files=( $(sort_array "$(basename -s .img ./*.img)") )
declare -a img_expected=( boot cache recovery system userdata )
declare -a img_partitions=( boot cache recovery system userdata )
# ---------------------------------------------------------
# fastboot_test
#
# Verifies the fastboot command and presence of your device
# by running 'fastboot devices'
#
# Globals:
# err, fb_ok, fb_store
# Arguments:
# None
# Returns:
# None
#
# ---------------------------------------------------------
fastboot_test() {
# run 'fastboot devices'
declare out
out="$($fb devices 2> /dev/null)"
err=$?
# if it didn't work, tell the user
if [[ $err -eq 0 && $out = "" ]] ; then
echo -e "\t$e_arrows Error: No fastboot device found!\n"
echo -e "\t $e_sub Verify that your phone is in $yellow'fastboot'$def mode.\n"
echo -e "\t $e_sub Verify that your phone is properly connected or try a different cable."
err=1
elif [[ $err -eq 127 ]] ; then
echo -e "\t$e_arrows Error: The fastboot command was not found!\n"
echo -e "\t $e_sub Verify that the fastboot command is in your \$PATH, or that you"
echo -e "\t entered the correct path when you ran this script.\n"
echo -e "\t $e_sub Verify that you included $mag'fastboot'$def in your entry.\n"
echo -e "\t $e_sub You entered: $yellow$fb$def"
err=1
else
# all is well, so write cmd/path to store
echo -e "\t$ok_arrows $out"
echo "$fb" > "$fb_store"
fb_ok="yes"
fi
}
# ---------------------------------------------------------
# file_test
#
# Verifies the presence of the various files required for a
# successful ROM flash: '.bin', '.mbn', and '.img'
#
# Globals:
# err
# Arguments:
# ext - extension of files to be verified (bin, mbn, img)
# Returns:
# None
#
# ---------------------------------------------------------
file_test() {
# arrays for file names and extensions
declare -a files expected partitions
# arg: ext
declare ext="$1"
# find out what type of file we're testing
# and load up the arrays
case $ext in
bin)
files=( ${bin_files[*]} )
expected=( ${bin_expected[*]} )
partitions=( ${bin_partitions[*]} )
;;
mbn)
files=( ${mbn_files[*]} )
expected=( ${mbn_expected[*]} )
partitions=( ${mbn_partitions[*]} )
;;
img)
files=( ${img_files[*]} )
expected=( ${img_expected[*]} )
partitions=( ${img_partitions[*]} )
;;
*)
;;
esac
if [[ ${files[*]} = "${expected[*]}" ]] ; then
for f in "${files[@]}" ; do
echo -e "\t$ok_arrows $f.$ext"
done
else
echo -e "\t$e_arrows Error: The expected $red$ext$def files are not present!\n"
echo -e "\t $e_sub Expected: $yellow${expected[*]}$def"
echo -e "\t Present : $cyan${files[*]}$def\n"
echo -e "\t $e_sub Does 'Expected' match 'Present' in the output above?\n"
echo -e "\t $e_sub Are you running this script in the extracted ROM directory?\n"
echo -e "\t $e_sub Current dir: $mag$(pwd)$def"
(( err+=2 ))
fi
}
# ---------------------------------------------------------
# flash_rom
#
# Flashes the ROM files to your OnePlus One in the following
# order: bin, mbn, img
#
# Globals:
# None
# Arguments:
# ext - extension of files to be flashed (bin, mbn, img)
# check - used when testing to show user fastboot commands
# Returns:
# None
#
# ---------------------------------------------------------
flash_rom() {
# arrays for filenames and partitions
declare -a files partitions
# args
declare ext="$1"
declare check="$2"
declare -i count=0
declare -i len=0
# find out what type of file we're testing/flashing
# and load up the arrays
case $ext in
img)
files=( ${img_files[*]} )
partitions=( ${img_partitions[*]} )
;;
bin)
files=( ${bin_files[*]} )
partitions=( ${bin_partitions[*]} )
;;
mbn)
files=( ${mbn_files[*]} )
partitions=( ${mbn_partitions[*]} )
;;
*)
;;
esac
# how many files?
(( len+=${#files[@]} ))
# display or execute commands based on test/flash status
while [ $count -lt $len ] ; do
if [[ $check ]] ; then
echo -e "\t$ok_arrows $fb flash" "${partitions[$count]}" "${files[$count]}"".$ext"
else
echo -e "$e_arrows Running: $green$esc[3m$(basename $fb) flash" "${partitions[$count]}" "${files[$count]}"".$ext$esc[0m\n"
#################################### DEBUG ####################################
#
# The actual flash command!
#
# To test full flash functionality without actually flashing, comment this out!
#
"$fb" flash "${partitions[$count]}" "${files[$count]}"."$ext"
#
###############################################################################
echo
fi
count=$((count+1))
done
}
# ---------------------------------------------------------
# run_test
#
# Runs the full test suite in preparation for ROM flashing
#
# Globals:
# has_tested, err
# Arguments:
# None
# Returns:
# None
#
# ---------------------------------------------------------
run_test() {
# tell user we're testing at least once whether
# he/she likes it or not
echo -e "\n$blue-----------------------------------------------------------------------------------------$def\n"
if [[ $1 -eq 1 ]] && [[ $has_tested = "no" ]] ; then
echo -e "$e_arrows You have never tested the ROM package. Running tests ..."
echo -e "\n$blue-----------------------------------------------------------------------------------------$def\n"
fi
# run the file tests
echo -e "$bullet Testing fastboot command using: '$fb devices' ...\n"
fastboot_test
echo -e "\n$bullet Checking for proper binary executables (bin) ...\n"
file_test bin
echo -e "\n$bullet Checking for proper multi-boot binaries (mbn) ...\n"
file_test mbn
echo -e "\n$bullet Checking for proper image files (img) ...\n"
file_test img
echo -e "\n$blue-----------------------------------------------------------------------------------------$def"
# run the flash command test
for e in "${exts[@]}" ; do
if [[ $err -ne 0 ]] ; then
echo -e "\n$e_arrows An error has occured!!\n"
echo -e " $e_sub Check the screen output above for more information.\n"
exit $err
fi
echo -e "\n$bullet Generating flash commands for $yellow$e$def files ...\n"
echo -e "\t$e_arrows Do these look right to you?\n"
flash_rom "$e" true
err="$?"
done
# if no errors, move on; exit otherwise
if [[ $err -eq 0 ]] ; then
echo -e "\n$bullet Everything seems to be in order.\n"
echo -e "\t$ok_arrows If you agree, run the script in 'flash' mode to upgrade your phone!\n"
echo -e "$blue-----------------------------------------------------------------------------------------$def"
has_tested="yes"
else
echo -e "$red::$def Errors were found!\n"
echo -e "\t$e_arrows Correct the errors shown above and run the script"
echo -e "\t again in 'test' mode before continuing.\n"
exit $err
fi
}
# ---------------------------------------------------------
# prompts
#
# Provides various prompts for user input
#
# Globals:
# user_input
# Arguments:
# prompt number - determines which prompt is presented
# fastboot path - the user provided path to the fastboot executable
# Returns:
# None
#
# ---------------------------------------------------------
prompts() {
# be annoying and let user knwo they can quit at any time
if [[ "$has_flashed" = "no" ]] ; then
echo -e "\n$e_arrows Press $yellow'ctrl + c'$def at any time to exit.\n"
fi
# yummy prompt messages with colors!
case $1 in
1)
echo -e "$bullet If not on your \$PATH, enter the full path to the $mag'fastboot'$def command."
echo -en " Example: $yellow/home/user/foo/bar/fastboot$def $mag[fastboot]$def: "
;;
2)
echo -en "$bullet You previously entered $mag$2$def as your fastboot command. Is this still valid? $mag[y/n]$def: "
;;
3)
echo -en " $e_sub Error! Enter $mag'y'$def or $mag'n'$def: "
;;
4)
if [[ "$2" = "second" ]] ; then
echo -e "$bullet Are you certain that you want to $red'flash'$def your ROM?\n"
echo -en "\t$e_arrows Type $red'flash'$def again to continue: "
else
echo -en "$bullet Do you want to $mag'test'$def or $mag'flash'$def your ROM?: "
fi
;;
5)
if [[ "$2" = "second" ]] ; then
echo -en " $e_sub Error! Enter $mag'flash'$def to confirm: "
else
echo -en " $e_sub Error! Enter $mag'test'$def or $mag'flash'$def: "
fi
;;
6)
echo -en "\n$bullet Would you like to reboot your phone? [y|n]: "
;;
*)
;;
esac
read user_input
}
# ---------------------------------------------------------
# welcome
#
# A friendly welcome message!
#
# ---------------------------------------------------------
welcome() {
cat <<WELCOME
$blue---------------------------------------------------------------------------------------------------$def
$e_arrows$esc[1;34m $esc[4mCyanogen OS 12 Fastboot Installation Script$def $esc[1;31m<<$def
This script will test the contents of your extracted Cyanogen OS 12 ROM package.
If the tests are successful, you will then be given the option to flash the new
ROM to your OnePlus One.
If you're nervous about using this script, read through the code comments. Above all else,
$red READ EVERYTHING$def on the screen while you run the script!
Notes:
1. If you did not download the $yellow<< OFFICIAL FASTBOOT >>$def ROM from www.cyngn.com$red STOP$def
now and download the file from:
$ok_arrows$yellow http://builds.cyngn.com/cyanogen-os/bacon/12.0-YNG1TAS0YL-bacon/2263178b74/cm-12.0-YNG1TAS0YL-bacon-signed-fastboot.zip$def
2. Extract the downloaded ZIP archive somewhere on your machine, put your phone in fastboot,
and plug it in to your computer.
$cyan If you're reading this, it is assumed that you have the necessary Android tools installed
to flash via fastboot. If not, Google and forum searchs are your best friend.
$ok_arrows Tip: search for $mag'unlock bootloader'$def or $mag'fastboot'$def
3. Run this script from within the extracted COS 12.0 fastboot directory.
4. Run at$red YOUR OWN RISK!!$def I have done my best to make this thing as foolproof as possible.
Keep in mind though, that this worked for _ME_ on _MY_ system. YMMV.
$red<<< TEST FIRST, THEN TEST AGAIN >>>$def
5. If you have issues, post them in the following thread on the OnePlus forums:
$ok_arrows$yellow https://forums.oneplus.net/threads/cyanogen-os-12-fastboot-download.297136$def
6. Whether you like it or not, the tests will be run no matter how you answer the first prompt
to test or flash $red;-)$def
$esc[1;3;35mENJOY!$def
$blue---------------------------------------------------------------------------------------------------$def
WELCOME
}
# Say hi
welcome
# Keep going until rom is flashed
until [[ $has_flashed = "yes" ]] ; do
# Look for fastboot command in file '.fb_store'.
# If no store file or file is empty, prompt for
# path. Default assumes 'fastboot' is on $PATH
if ! [[ -f $fb_store ]] || [[ $(cat $fb_store) = "" ]] ; then
# no previous fastboot cmd/path found
prompts 1
if ! [[ "$user_input" = "" ]] ; then
fb="$user_input"
fi
elif [[ $fb_ok = "no" ]] ; then
# previous fastboot cmd/path found but not tested yet
user_fb="$(cat $fb_store)"
regex='[ynYN]'
prompts 2 "$user_fb"
until [[ "$user_input" =~ $regex ]] ; do
prompts 3
done
regex='[yY]'
if [[ "$user_input" =~ $regex ]] ; then
fb="$user_fb"
else
prompts 1
if ! [[ "$user_input" = "" ]] ; then
fb="$user_input"
fi
fi
fi
# Flash or test?
prompts 4
regex='(test|flash)'
until [[ "$user_input" =~ $regex ]] ; do
prompts 5
done
if [[ "$user_input" = "flash" ]] ; then
# if test passed and no errors, confirm flash
# otherwise, force test
if [[ "$has_tested" = "yes" ]] && [[ $err -eq 0 ]] ; then
prompts 4 second
regex='(flash)'
until [[ "$user_input" =~ $regex ]] ; do
prompts 5 second
done
else
run_test 1
continue 2
fi
# flash the ROM
echo -e "\n$blue-----------------------------------------------------------------------------------------$def"
for e in "${exts[@]}" ; do
echo -e "\n$bullet Flashing $yellow$e$def files ...\n"
flash_rom "$e"
err=$?
if [[ $err -ne 0 ]] ; then
echo -e "\n$e_arrows An error has occured!!\n"
echo -e " $e_sub Check the screen output from 'fastboot' for more information.\n"
exit $err
fi
done
echo -e "$blue-----------------------------------------------------------------------------------------$def\n"
echo -e "$bullet Flash successful! Enjoy your new ROM!!\n"
echo -e "$blue-----------------------------------------------------------------------------------------$def"
has_flashed="yes"
# ask to reboot
regex='[ynYN]'
prompts 6
echo
until [[ "$user_input" =~ $regex ]] ; do
prompts 3
done
regex='[yY]'
if [[ "$user_input" =~ $regex ]] ; then
$fb reboot 2> /dev/null
fi
# say good-bye
echo -e "$bullet$yellow Good-bye! $def$bullet\n"
else
run_test 2
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment