___             ____  _
 / _ \ _ __   ___|  _ \| |_   _ ___
| | | | '_ \ / _ \ |_) | | | | / __|
| |_| | | | |  __/  __/| | |_| \__ \
 \___/|_| |_|\___|_|   |_|\__,_|___/

 _____ _____ ____
|___  |_   _|  _ \ _ __ ___
   / /  | | | |_) | '__/ _ \
  / /   | | |  __/| | | (_) |
 /_/    |_| |_|   |_|  \___/

This tutorial explains how to root, unroot, and update a rooted OnePlus 7T Pro phone.


#-----------------------------------
               SETUP
#-----------------------------------

   - Follow this guide for setting up adb on the PC:

      https://www.androidauthority.com/lineageos-install-guide-893303/

   - Should have this downloaded and unzipped:

      platform-tools_r33.0.1-linux.zip

   - Download these files and link if switching to LineageOS:

      RECOVERY.img -> lineage-18.1-20220412-recovery-hotdog.img
      LINEAGE.zip  -> lineage-18.1-20220412-nightly-hotdog-signed.zip
      GAPPS.zip    -> MindTheGapps-11.0.0-arm64-20220217_100228.zip
      TWRP.img     -> twrp-3.6.1_11-0-hotdog.img         # Probably not needed.

   - Create folder: COPY_TO_DOWNLOADS and add to it:

      app-debug.apk (Magisk) from https://raw.githubusercontent.com/topjohnwu/magisk-files/canary/app-debug.apk
      boot -> ../STOCK/images/latest/boot.img
      safetynet-fix-v2.2.1.zip from https://github.com/kdrag0n/safetynet-fix/releases/tag/v2.2.0

   - Stock Rom:

      - Download:
         https://www.oneplus.com/global/support/softwareupgrade/details?code=PM1574162297465

      - Unzip using:
         payload-dumper-go_1.2.0_linux_amd64/payload-dumper-go

      - Copy extracted folder to:
         STOCK/images/OnePlus7TProOxygen_14.O.32_OTA_0320/

      - Create links to latest stock build:
         A11.0.6.1 -> OnePlus7TProOxygen_14.O.32_OTA_0320
         latest -> A11.0.6.1


#-----------------------------------
             Custom ROM
#-----------------------------------

This will change to a custom ROM (LineageOS):

   https://wiki.lineageos.org/devices/hotdog/#supported-models


#-----------------------------------
            RESTORE/ROOT
#-----------------------------------

This will restore OnePlus 7T Pro to Stock Rom
(Optionally Root):

   - Create boot.img patch file (If rooting):

      https://forum.xda-developers.com/t/working-safetynet-with-pixel-6-pro-android-12.4354149/

         - Use Magisk to create magisk_patched-*.img from boot.img

         - Copy patch file to STOCK/images/latest/

   - Flash the stock ROM (OOS latest version)

         cd STOCK
         adb reboot fastboot
         fastboot devices
         FLASH_STOCK.sh    # --slot=all only works when in recovery->fastboot

   - Do a full factory reset through the TWRP recovery   # Optional?

         fastboot boot TWRP.img
         # Wipe -> All options.

   - Flash stock boot.img

         adb reboot fastboot
         fastboot flash --slot=all boot images/latest/boot.img

   - Flash custom boot (If rooting):

         ROOT.sh

   - Again enter recovery mode in stock recovery. Do a full wipe.

         fastboot reboot recovery
         # Wipe -> All options.

   - Now lock the bootloader (unless rooting):     # Do not lock if rooting!

         fastboot oem lock --skip-reboot 

   - Reboot to enter stock recovery and wipe all again

         fastboot reboot recovery
         # Wipe -> All options.

   - Reboot
         This may require a factory reset if you get put into 'Data may be corrupted'

   - Additional setup (if rooting):

      - Copy 'COPY_TO_DOWNLOADS' to downloads folder on the phone.

      - Install Magisk apk on the phone.

      - Open Magisk -> Go to Install > Install and click on Direct Install.

      - Click button to finish setup (which will also reboot the phone).

      - Pass SafetyNet Checks:

         - Magisk -> Direct Install -> safetynet-fix-v2.2.1.zip 

         - Magisk -> Settings -> enable Zygisk

         - Magisk -> Settings -> Enforce DenyList

            - Show system apps.

            - Select: Google Pay.

            - Select: Google Play Store.

            - Select: (Banking apps).

         - Magisk -> Settings -> Hide the Magisk app.

         - Reboot your device and test SafetyNet with an app like YASNAC

         - Clear app data for "Play Store"


#-----------------------------------
               UPDATE
#-----------------------------------

How to install OTA update and keep root:

   - https://nerdschalk.com/oneplus-7t-pro-root/
   - https://forum.xda-developers.com/t/update-to-latest-firmware-ota-update-without-losing-magisk-root-permissions.4233391/
   - https://topjohnwu.github.io/Magisk/ota.html
   - https://forum.xda-developers.com/conversations/welcome-to-xda.14668127/#convMessage-14988935
   - https://forum.xda-developers.com/t/root-android-11-latest-stock-and-patched-imgs-payload-dumper-magisk_patched-guides.4175033/

   SUMMARY:

      - Disable Automatic system updates:

         Settings
            -> System
            -> Developer Options
            -> Automatic system updates
                (Uncheck it)

      - Disable all Magisk modules.

      - Reboot.

        Settings
            -> System update
            -> Download & Update

      - This is just to have a copy of the update
        (incase future updates fail):

         - Backup the OTA update file which is found
            here on the phone:

            Internal/.Ota

         - Copy back to PC (do not unzip it):

            STOCK/images/OnePlus7TProOxygen_14.O.33_OTA_0330.zip

         - Extract images:

            ../../payload-dumper-go One*.zip

        - Rename extracted folder:

            mv extracted* OnePlus7TProOxygen_14.O.33_OTA_0330 

         - Link new version:

            A11.0.7.1 -> OnePlus7TProOxygen_14.O.33_OTA_0330
            latest -> A11.0.7.1

         - Copy boot.img back to the phone.

         - Patch boot.img:

            Magisk
                -> Install
                -> "Select and Patch a File"

         - Copy patched file back to the PC:

            STOCK/images/latest/

      - Continue with normal update:
        
        Settings
            -> System Update
            -> Click "Install Now"

      - Wait for the update to be installed.

      - !!! DO NOT REBOOT !!!

      - Open Magisk.

      - Run Magisk post-installer:

        Magisk Manager
            -> Install
            -> Install/Inactive Slot (After OTA).
                (Acknowledge warning)

      - Reboot.

      - Enable Magisk modules again.

      - Check SafetyNet checks.

      - Add star to github repo ;)

#-----------------------------------
      Custom ROM - LineageOS
#-----------------------------------

These steps should help to install LineageOS:
(Ended up reverting due to screen flicker).

XDA LineageOS Page:
- https://xdaforums.com/t/rom-official-14-lineageos-21-for-oneplus-7t-pro-hotdog.4648947/

Gapps Download:
- https://wiki.lineageos.org/gapps
- Click LineageOS 21 (Android 14) ARM64 (Download Mind*).

Restore to Stock OS (follow instruction in an above section).

Make sure these commands are already installed:
    adb devices
    fastboot -h
    - https://wiki.lineageos.org/adb_fastboot_guide

Unlock the bootloader.
    adb -d reboot bootloader
    fastboot devices
    fastboot oem unlock

Flash extra partitions:
    fastboot flash dtbo dtbo.img
    fastboot flash vbmeta vbmeta.img

Flash lineage recovery:
    adb -d reboot bootloader
    fastboot devices
    fastboot flash recovery recovery.img

Install LineageOS zip:
    adb -d reboot reovery
    - Do "Factory reset/format data"
    - Enable ADB.
    - Apply Update -> Apply from ADB
    adb -d sideload lineage-*.zip
    - Confirm to reboot.

Install Google Apps:
    - Enable ADB.
    - Apply Update -> Apply from ADB
    adb -d sideload Mind*.zip
    (Signature verification will fail)
    
#-----------------------------------
         Clean Partitions
#-----------------------------------

https://rootmygalaxy.net/fix-remote-not-enough-space-to-resize-partition/
https://xdaforums.com/t/fastboot-invalid-sparse-file-format-at-header-magic-how-to-fix.4281719/

adb reboot fastboot
fastboot boot RECOVERY.img

# Get all fastboot variables:
fastboot getvar all

# Delete product partition
fastboot delete-logical-partition product_a

#-----------------------------------
        QUALCOMM CrashDump Mode
#-----------------------------------

Bricked! - Not really.

Press and hold volume Up and Power buttons for 8 seconds.

Once shutdown:

Press and hold Volume down and power.

Once seeing the logo, release just the power button.


#-----------------------------------
         Errors / Warnings
#-----------------------------------

Warning: "Spare file format at header magic"
Comment: "That warning is normal"

Current image boot/recovery have been destroyed
and can not boot.

"Writing 'odm_b'                                    FAILED (remote: 'No such file or directory'"
fastboot delete-logical-partition odm_b
fastboot create-logical-partition odm_b 1000
fastboot flash --slot=b odm images/latest/odm.img


#-----------------------------------
            Backup
#-----------------------------------

I did system update and would like to backup boot.img file.

adb root
adb shell
su
dd if=/dev/block/bootdevice/by-name/boot of=/sdcard/boot.img

Can patch that file (boot.img) on the phone.