v1ral_ITS

Legacy IOS Part2

Jun 10th, 2026
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 119.72 KB | None | 0 0
  1.         return
  2.     fi
  3.     rm -f *.shsh*
  4.  
  5.     ExtraArgs="-d $device_type -i $version -e $device_ecid -m $buildmanifest -o -s -B ${device_model}ap -b "
  6.     if [[ -n $apnonce ]]; then
  7.         ExtraArgs+="--apnonce $apnonce"
  8.     else
  9.         ExtraArgs+="-g 0x1111111111111111"
  10.     fi
  11.     log "Running tsschecker with command: $tsschecker $ExtraArgs"
  12.     $tsschecker $ExtraArgs
  13.     shsh_path="$(ls $shsh_check)"
  14.     if [[ -z "$shsh_path" ]]; then
  15.         error "Saving $version blobs failed. Please run the script again"
  16.     fi
  17.     if [[ -z $apnonce ]]; then
  18.         cp "$shsh_path" ../saved/shsh/
  19.     fi
  20.     log "Successfully saved $version blobs: $shsh_path"
  21. }
  22.  
  23. ipsw_download() {
  24.     local version="$device_target_vers"
  25.     local build_id="$device_target_build"
  26.     local type="$device_type"
  27.     local special
  28.     if [[ -n $2 && -n $3 ]]; then
  29.         version="$2"
  30.         build_id="$3"
  31.     elif [[ $2 == "latest" ]]; then
  32.         version="$device_latest_vers"
  33.         build_id="$device_latest_build"
  34.     elif [[ $2 == "special" ]]; then
  35.         type="$device_type_special"
  36.         special="special"
  37.     fi
  38.     local ipsw_dl="$1"
  39.     ipsw_get_url $build_id $type $version
  40.     if [[ -z $ipsw_dl ]]; then
  41.         ipsw_dl="../${ipsw_url##*/}"
  42.         ipsw_dl="${ipsw_dl%?????}"
  43.     fi
  44.     if [[ ! -e "$ipsw_dl.ipsw" ]]; then
  45.         if [[ -n $version ]]; then
  46.             print "* The script will now proceed to download iOS $version IPSW."
  47.         fi
  48.         print "* If you want to download it yourself, here is the link: $ipsw_url"
  49.         log "Downloading IPSW... (Press Ctrl+C to cancel)"
  50.         download_from_url "$ipsw_url" temp.ipsw
  51.         mv temp.ipsw "$ipsw_dl.ipsw"
  52.     fi
  53.     ipsw_verify "$ipsw_dl" "$build_id" $special
  54. }
  55.  
  56. ipsw_verify() {
  57.     local ipsw_dl="$1"
  58.     local build_id="$2"
  59.     local IPSWSHA1
  60.     local IPSWSHA1E=$(cat "$device_fw_dir/$build_id/sha1sum" 2>/dev/null)
  61.     local IPSWSHA1L
  62.     local type="$device_type"
  63.     [[ $3 == "special" ]] && type="$device_type_special"
  64.  
  65.     log "Getting SHA1 hash for $ipsw_dl.ipsw..."
  66.     IPSWSHA1L=$($sha1sum "${ipsw_dl//\\//}.ipsw" | awk '{print $1}')
  67.     case $build_id in
  68.         *[bcdefgkmpquv] )
  69.             log "Beta IPSW detected, skip verification"
  70.             if [[ $build_id == "$device_base_build" ]]; then
  71.                 device_base_sha1="$IPSWSHA1L"
  72.             else
  73.                 ipsw_isbeta=1
  74.                 device_target_sha1="$IPSWSHA1L"
  75.             fi
  76.             return
  77.         ;;
  78.     esac
  79.  
  80.     if [[ $(echo "$IPSWSHA1E" | grep -c '<') != 0 ]]; then
  81.         rm "$device_fw_dir/$build_id/sha1sum"
  82.     fi
  83.  
  84.     log "Getting SHA1 hash from AppleDB..."
  85.     download_appledb ios $build_id
  86.     IPSWSHA1="$(cat tmp.json | $jq -r ".sources[] | select(.type == \"ipsw\" and any(.deviceMap[]; . == \"$type\")) | .hashes.sha1")"
  87.    mkdir -p $device_fw_dir/$build_id
  88.  
  89.    if [[ -n $IPSWSHA1 && -n $IPSWSHA1E && $IPSWSHA1 == "$IPSWSHA1E" ]]; then
  90.        log "Using saved SHA1 hash for this IPSW: $IPSWSHA1"
  91.    elif [[ -z $IPSWSHA1 && -n $IPSWSHA1E ]]; then
  92.        warn "No SHA1 hash from AppleDB, using local hash"
  93.        IPSWSHA1="$IPSWSHA1E"
  94.    elif [[ -z $IPSWSHA1 && -z $IPSWSHA1E ]]; then
  95.        warn "No SHA1 hash from either AppleDB or local hash, cannot verify IPSW."
  96.        pause
  97.        if [[ $build_id == "$device_base_build" ]]; then
  98.            device_base_sha1="$IPSWSHA1L"
  99.        else
  100.            device_target_sha1="$IPSWSHA1L"
  101.        fi
  102.        return
  103.    elif [[ -n $IPSWSHA1E ]]; then
  104.        warn "Local SHA1 hash mismatch. Overwriting local hash."
  105.        echo "$IPSWSHA1" > $device_fw_dir/$build_id/sha1sum
  106.    elif [[ -z $IPSWSHA1E ]]; then
  107.        log "Local SHA1 hash does not exist. Saving local hash."
  108.        echo "$IPSWSHA1" > $device_fw_dir/$build_id/sha1sum
  109.    fi
  110.  
  111.    if [[ $IPSWSHA1L != "$IPSWSHA1" ]]; then
  112.        rm "$device_fw_dir/$build_id/sha1sum"
  113.        if [[ $3 != "nopause" ]]; then
  114.            log "SHA1sum mismatch. Expected $IPSWSHA1, got $IPSWSHA1L"
  115.            warn "Verifying IPSW failed. Your IPSW may be corrupted or incomplete. Make sure to download and select the correct IPSW."
  116.            pause
  117.        fi
  118.        if [[ $build_id == "$device_base_build" ]]; then
  119.            ipsw_base_validate=1
  120.        else
  121.            ipsw_validate=1
  122.        fi
  123.        return 1
  124.    fi
  125.    log "IPSW SHA1sum matches"
  126.    if [[ $build_id == "$device_base_build" ]]; then
  127.        device_base_sha1="$IPSWSHA1"
  128.        ipsw_base_validate=0
  129.    else
  130.        ipsw_isbeta=
  131.        device_target_sha1="$IPSWSHA1"
  132.        ipsw_validate=0
  133.    fi
  134. }
  135.  
  136. ipsw_prepare_1033() {
  137.    # patch iBSS, iBEC, iBSSb, iBECb and set variables
  138.    iBSS="ipad4"
  139.    if [[ $device_type == "iPhone6"* ]]; then
  140.        iBSS="iphone6"
  141.    fi
  142.    iBEC="iBEC.${iBSS}.RELEASE"
  143.    iBSSb="iBSS.${iBSS}b.RELEASE"
  144.    iBECb="iBEC.${iBSS}b.RELEASE"
  145.    iBSS="iBSS.$iBSS.RELEASE"
  146.  
  147.    log "Patching iBSS and iBEC..."
  148.    file_extract_from_archive "$ipsw_path.ipsw" Firmware/dfu/$iBSS.im4p
  149.    file_extract_from_archive "$ipsw_path.ipsw" Firmware/dfu/$iBEC.im4p
  150.    mv $iBSS.im4p $iBSS.orig
  151.    mv $iBEC.im4p $iBEC.orig
  152.    $bspatch $iBSS.orig $iBSS.im4p ../resources/patch/1033/$iBSS.patch
  153.    $bspatch $iBEC.orig $iBEC.im4p ../resources/patch/1033/$iBEC.patch
  154.    if [[ $device_type == "iPad4,"* ]]; then
  155.        file_extract_from_archive "$ipsw_path.ipsw" Firmware/dfu/$iBSSb.im4p
  156.        file_extract_from_archive "$ipsw_path.ipsw" Firmware/dfu/$iBECb.im4p
  157.        mv $iBSSb.im4p $iBSSb.orig
  158.        mv $iBECb.im4p $iBECb.orig
  159.        $bspatch $iBSSb.orig $iBSSb.im4p ../resources/patch/1033/$iBSSb.patch
  160.        $bspatch $iBECb.orig $iBECb.im4p ../resources/patch/1033/$iBECb.patch
  161.    fi
  162.    case $device_type in
  163.        iPad4,[45] ) cp $iBSSb.im4p $iBECb.im4p ../saved/$device_type;;
  164.        * ) cp $iBSS.im4p $iBEC.im4p ../saved/$device_type;;
  165.    esac
  166.    log "Pwned iBSS and iBEC saved at: saved/$device_type"
  167. }
  168.  
  169. ipsw_prepare_openssh_plist() {
  170.    [[ $ipsw_openssh != 1 ]] && return
  171.    echo "echo '<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  172. <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  173. <plist version=\"1.0\">
  174.  
  175. <dict>
  176.    <key>Label</key>
  177.    <string>com.openssh.sshd</string>
  178.  
  179.    <key>Program</key>
  180.    <string>/usr/libexec/sshd-keygen-wrapper</string>
  181.  
  182.    <key>ProgramArguments</key>
  183.    <array>
  184.        <string>/usr/sbin/sshd</string>
  185.        <string>-i</string>
  186.    </array>
  187.  
  188.    <key>SessionCreate</key>
  189.    <true/>
  190.  
  191.    <key>Sockets</key>
  192.    <dict>
  193.        <key>Listeners</key>
  194.        <dict>
  195.            <key>SockServiceName</key>
  196.            <string>ssh</string>
  197.        </dict>
  198.    </dict>
  199.  
  200.    <key>StandardErrorPath</key>
  201.    <string>/dev/null</string>
  202.  
  203.    <key>inetdCompatibility</key>
  204.    <dict>
  205.        <key>Wait</key>
  206.        <false/>
  207.    </dict>
  208. </dict>
  209.  
  210. </plist>' > /mnt1/Library/LaunchDaemons/com.openssh.sshd.plist" | tee -a reboot.sh
  211. }
  212.  
  213. ipsw_prepare_rebootsh() {
  214.    log "Generating reboot.sh"
  215.    echo '#!/bin/bash' | tee reboot.sh
  216.    echo "mount_hfs /dev/disk0s1s1 /mnt1; mount_hfs /dev/disk0s1s2 /mnt2" | tee -a reboot.sh
  217.    [[ $1 != "aquila2" ]] && echo "nvram -d boot-partition; nvram -d boot-ramdisk" | tee -a reboot.sh
  218.    if [[ $1 == "aquila"* ]]; then
  219.        echo "mv /mnt1/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist_ /mnt1/Library/LaunchDaemons/com.apple.mDNSResponder.plist" | tee -a reboot.sh
  220.        echo "mv /mnt1/Library/LaunchDaemons/com.apple.sandboxd.plist /mnt1/System/Library/LaunchDaemons/" | tee -a reboot.sh
  221.        echo "mv /mnt1/Library/LaunchDaemons/com.saurik.Cydia.Startup.plist /mnt1/System/Library/LaunchDaemons/" | tee -a reboot.sh
  222.        local crash
  223.        [[ $1 == "aquila" ]] && crash+="_o"
  224.        echo "mv /mnt1/usr/libexec/CrashHousekeeping$crash /mnt1/usr/libexec/CrashHousekeeping.backup" | tee -a reboot.sh
  225.        echo "ln -sf /aquila /mnt1/usr/libexec/CrashHousekeeping" | tee -a reboot.sh
  226.        ipsw_prepare_openssh_plist
  227.        echo "/sbin/reboot_" | tee -a reboot.sh
  228.    else
  229.        echo "/usr/bin/haxx_overwrite --${device_type}_${device_target_build}" | tee -a reboot.sh
  230.    fi
  231. }
  232.  
  233. ipsw_prepare_logos_convert() {
  234.    local fourcc="logo"
  235.    if [[ -n $ipsw_customlogo ]]; then
  236.        log "Converting custom logo"
  237.        logoname=$(echo "$device_fw_key" | $jq -j '.keys[] | select(.image == "AppleLogo") | .filename')
  238.        if [[ $device_target_powder == 1 ]]; then
  239.            fourcc="logb"
  240.            case $target_vers_maj in
  241.                [34] ) fourcc="log4";;
  242.            esac
  243.        fi
  244.        "$dir/ibootim" "$ipsw_customlogo" logo.raw
  245.        "$dir/img3maker" -f logo.raw -o logo.img3 -t $fourcc
  246.        if [[ ! -s logo.img3 ]]; then
  247.            error "Converting custom logo failed. Check your image"
  248.        fi
  249.        mkdir -p $all_flash
  250.        mv logo.img3 $all_flash/$logoname
  251.    fi
  252.    if [[ -n $ipsw_customrecovery ]]; then
  253.        log "Converting custom recovery"
  254.        recmname=$(echo "$device_fw_key" | $jq -j '.keys[] | select(.image == "RecoveryMode") | .filename')
  255.        "$dir/ibootim" "$ipsw_customlogo" recovery.raw
  256.        "$dir/img3maker" -f recovery.raw -o recovery.img3 -t recm
  257.        if [[ ! -s recovery.img3 ]]; then
  258.            error "Converting custom recovery failed. Check your image"
  259.        fi
  260.        mkdir -p $all_flash
  261.        mv recovery.img3 $all_flash/$recmname
  262.    fi
  263. }
  264.  
  265. ipsw_prepare_logos_add() {
  266.    if [[ -n $ipsw_customlogo ]]; then
  267.        log "Adding custom logo to IPSW"
  268.        zip -r0 temp.ipsw $all_flash/$logoname
  269.    fi
  270.    if [[ -n $ipsw_customrecovery ]]; then
  271.        log "Adding custom recovery to IPSW"
  272.        zip -r0 temp.ipsw $all_flash/$recmname
  273.    fi
  274. }
  275.  
  276. ipsw_prepare_jailbreak() {
  277.    if [[ -s "$ipsw_custom.ipsw" ]]; then
  278.        log "Found existing Custom IPSW. Skipping IPSW creation."
  279.        return
  280.    fi
  281.    local ExtraArgs=
  282.    local JBFiles=()
  283.    local daibutsu=$1
  284.  
  285.    if [[ $ipsw_jailbreak == 1 ]]; then
  286.        case $device_target_vers in
  287.            6.*  ) JBFiles=("aquila_6.tar");;
  288.            5.*  ) JBFiles=("aquila_5.tar");;
  289.            4.3* ) JBFiles=("aquila_4.tar");;
  290.            4.[10]* | 3.2* ) JBFiles=("greenpois0n/${device_type}_${device_target_build}.tar");;
  291.        esac
  292.        if [[ -n ${JBFiles[0]} ]]; then
  293.            JBFiles[0]=$jelbrek/${JBFiles[0]}
  294.        fi
  295.        case $device_target_vers in
  296.            [43].* ) JBFiles+=("$jelbrek/fstab_old.tar");;
  297.            *      ) JBFiles+=("$jelbrek/fstab_rw.tar");;
  298.        esac
  299.        JBFiles+=("freeze.tar")
  300.        case $device_target_vers in
  301.            4.2.[8761] )
  302.                if [[ $device_type != "iPhone1,2" ]]; then
  303.                    ExtraArgs+=" -punchd"
  304.                    JBFiles+=("$jelbrek/greenpois0n/${device_type}_${device_target_build}.tar")
  305.                fi
  306.            ;;
  307.            3.1* )
  308.                if [[ $device_type != "iPhone1,2" && $device_type != "iPhone2,1" && $ipsw_24o != 1 ]]; then
  309.                    JBFiles+=("$jelbrek/greenpois0n/${device_type}_${device_target_build}.tar")
  310.                fi
  311.            ;;
  312.        esac
  313.        case $device_target_vers in
  314.            [543].* ) JBFiles+=("$jelbrek/cydiasubstrate.tar");;
  315.        esac
  316.        if [[ $device_target_vers == "3."* ]]; then
  317.            JBFiles+=("$jelbrek/cydiahttpatch.tar")
  318.        fi
  319.        ExtraArgs+=" -S 30" # system partition add
  320.        if [[ $ipsw_openssh == 1 ]]; then
  321.            JBFiles+=("$jelbrek/sshdeb.tar")
  322.            if (( target_vers_maj >= 4 )); then
  323.                cp $jelbrek/openssh.tar.gz $jelbrek/openssl.tar.gz .
  324.                gzip -d openssh.tar.gz
  325.                gzip -d openssl.tar.gz
  326.                JBFiles+=("openssh.tar" "openssl.tar")
  327.            fi
  328.        fi
  329.        case $device_target_vers in
  330.            [43]* ) :;;
  331.            * ) JBFiles+=("$jelbrek/LukeZGD.tar");;
  332.        esac
  333.        cp $jelbrek/freeze.tar.gz .
  334.        gzip -d freeze.tar.gz
  335.    fi
  336.  
  337.    ipsw_prepare_bundle $daibutsu
  338.    ipsw_prepare_logos_convert
  339.  
  340.    if [[ $ipsw_memory == 1 ]]; then
  341.        ExtraArgs+=" -memory"
  342.    fi
  343.    ExtraArgs+=" -ramdiskgrow 10"
  344.    if [[ $device_use_bb != 0 && $device_type != "$device_disable_bbupdate" ]]; then
  345.        ExtraArgs+=" -bbupdate"
  346.    elif [[ $device_type == "$device_disable_bbupdate" && $device_deadbb != 1 ]]; then
  347.        ExtraArgs+=" ../saved/$device_type/baseband-$device_ecid.tar"
  348.    fi
  349.    if [[ $device_actrec == 1 ]]; then
  350.        ExtraArgs+=" ../saved/$device_type/activation-$device_ecid.tar"
  351.    fi
  352.    if [[ $1 == "iboot" ]]; then
  353.        ExtraArgs+=" iBoot.tar"
  354.    fi
  355.    if [[ $ipsw_isbeta == 1 ]]; then
  356.        ipsw_prepare_systemversion
  357.        ExtraArgs+=" systemversion.tar"
  358.    fi
  359.  
  360.    log "Preparing custom IPSW: $dir/ipsw $ipsw_path.ipsw temp.ipsw $ExtraArgs ${JBFiles[*]}"
  361.    "$dir/ipsw" "$ipsw_path.ipsw" temp.ipsw $ExtraArgs ${JBFiles[@]}
  362.  
  363.    if [[ ! -e temp.ipsw ]]; then
  364.        error "Failed to find custom IPSW. Please run the script again" \
  365.              "* You may try selecting N for memory option"
  366.    fi
  367.  
  368.    ipsw_prepare_logos_add
  369.    ipsw_bbreplace
  370.    if [[ $device_type == "iPhone2,1" ]] && (( target_vers_maj >= 5 )); then
  371.        ipsw_prepare_ios4patches
  372.        log "Add all to custom IPSW"
  373.        zip -r0 temp.ipsw Firmware/dfu/*
  374.    fi
  375.  
  376.    mv temp.ipsw "$ipsw_custom.ipsw"
  377. }
  378.  
  379. ipsw_prepare_fourthree() {
  380.    local comps=("AppleLogo" "DeviceTree" "iBoot" "RecoveryMode")
  381.    local saved_path="../saved/$device_type/8L1"
  382.    local bpatch="../resources/patch/fourthree/$device_type/6.1.3"
  383.    local name
  384.    local iv
  385.    local key
  386.    if [[ $ipsw_fourthree != 1 ]]; then
  387.        return
  388.    fi
  389.    log "Preparing IPSW for FourThree"
  390.    ipsw_get_url 8L1
  391.    url="$ipsw_url"
  392.    device_fw_key_check
  393.    device_fw_key_check temp 8L1
  394.    mkdir -p $all_flash Downgrade $saved_path
  395.    log "Extracting files"
  396.    file_extract_from_archive "$ipsw_path.ipsw" $all_flash/manifest
  397.    mv manifest $all_flash
  398.    file_extract_from_archive temp.ipsw Downgrade/RestoreDeviceTree
  399.    log "RestoreDeviceTree"
  400.    iv=$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "DeviceTree") | .iv')
  401.    key=$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "DeviceTree") | .key')
  402.    "$dir/xpwntool" RestoreDeviceTree RestoreDeviceTree.dec -iv $iv -k $key -decrypt
  403.    $bspatch RestoreDeviceTree.dec Downgrade/RestoreDeviceTree $bpatch/RestoreDeviceTree.patch
  404.    for getcomp in "${comps[@]}"; do
  405.        name=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  406.        iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  407.        key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  408.        path="$all_flash/"
  409.        log "$getcomp"
  410.        if [[ $vers == "$device_base_vers" ]]; then
  411.            file_extract_from_archive "$ipsw_base_path.ipsw" ${path}$name
  412.        elif [[ -e $saved_path/$name ]]; then
  413.            cp $saved_path/$name .
  414.        else
  415.            "$dir/pzb" -g "${path}$name" -o "$name" "$url"
  416.            cp $name $saved_path/
  417.        fi
  418.        "$dir/xpwntool" $name $getcomp.dec -iv $iv -k $key -decrypt
  419.        case $getcomp in
  420.            "AppleLogo" )
  421.                getcomp="applelogo"
  422.                mv AppleLogo.dec applelogo.dec
  423.                echo "0000010: 6267" | xxd -r - applelogo.dec
  424.                echo "0000020: 6267" | xxd -r - applelogo.dec
  425.            ;;
  426.            "DeviceTree" )
  427.                echo "0000010: 6272" | xxd -r - DeviceTree.dec
  428.                echo "0000020: 6272" | xxd -r - DeviceTree.dec
  429.            ;;
  430.            "RecoveryMode" )
  431.                getcomp="recoverymode"
  432.                mv RecoveryMode.dec recoverymode.dec
  433.                echo "0000010: 6263" | xxd -r - recoverymode.dec
  434.                echo "0000020: 6263" | xxd -r - recoverymode.dec
  435.            ;;
  436.            "iBoot" )
  437.                mv iBoot.dec iBoot.dec0
  438.                $bspatch iBoot.dec0 iBoot.dec $bpatch/iBoot.${device_model}ap.RELEASE.patch
  439.                #"$dir/xpwntool" iBoot.dec0 iBoot.dec2
  440.                #"$dir/iBoot32Patcher" iBoot.dec2 iBoot.patched --rsa -b "rd=disk0s3 -v amfi=0xff cs_enforcement_disable=1 pio-error=0"
  441.                #"$dir/xpwntool" iBoot.patched iBoot.dec -t iBoot.dec0
  442.                #echo "0000010: 626F" | xxd -r - iBoot.dec
  443.                #echo "0000020: 626F" | xxd -r - iBoot.dec
  444.            ;;
  445.        esac
  446.        mv $getcomp.dec $path/${getcomp}B.img3
  447.        echo "${getcomp}B.img3" >> $path/manifest
  448.    done
  449.    log "Add files to IPSW"
  450.    zip -r0 temp.ipsw $all_flash/* Downgrade/*
  451. }
  452.  
  453. ipsw_prepare_fourthree_part2() {
  454.    device_fw_key_check base
  455.    local saved_path="../saved/$device_type/$device_base_build"
  456.    local bpatch="../resources/patch/fourthree/$device_type/$device_base_vers"
  457.    local iv
  458.    local key
  459.    mkdir -p $saved_path
  460.    log "Preparing components for FourThree dualboot"
  461.    if [[ ! -s $saved_path/Kernelcache ]]; then
  462.        log "Kernelcache"
  463.        iv=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "Kernelcache") | .iv')
  464.        key=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "Kernelcache") | .key')
  465.        file_extract_from_archive "$ipsw_base_path.ipsw" kernelcache.release.$device_model
  466.        "$dir/xpwntool" kernelcache.release.$device_model kernelcache.dec -iv $iv -k $key
  467.        $bspatch kernelcache.dec kernelcache.patched $bpatch/kernelcache.release.patch
  468.        "$dir/xpwntool" kernelcache.patched kernelcachb -t kernelcache.release.$device_model -iv $iv -k $key
  469.        "$dir/xpwntool" kernelcachb $saved_path/Kernelcache -iv $iv -k $key -decrypt
  470.    fi
  471.    if [[ ! -s $saved_path/LLB ]]; then
  472.        log "LLB"
  473.        iv=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "LLB") | .iv')
  474.        key=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "LLB") | .key')
  475.        file_extract_from_archive "$ipsw_base_path.ipsw" $all_flash/LLB.${device_model}ap.RELEASE.img3
  476.        "$dir/xpwntool" LLB.${device_model}ap.RELEASE.img3 llb.dec -iv $iv -k $key
  477.        $bspatch llb.dec $saved_path/LLB $bpatch/LLB.${device_model}ap.RELEASE.patch
  478.    fi
  479.    if [[ ! -s $saved_path/RootFS.dmg ]]; then
  480.        log "RootFS"
  481.        name=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "RootFS") | .filename')
  482.        key=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "RootFS") | .key')
  483.        file_extract_from_archive "$ipsw_base_path.ipsw" $name
  484.        "$dir/dmg" extract $name rootfs.dec -k $key
  485.        rm $name
  486.        "$dir/dmg" build rootfs.dec $saved_path/RootFS.dmg
  487.    fi
  488.    echo "device_base_vers=$device_base_vers" > ../saved/$device_type/fourthree_$device_ecid
  489.    echo "device_base_build=$device_base_build" >> ../saved/$device_type/fourthree_$device_ecid
  490. }
  491.  
  492. ipsw_prepare_keys() {
  493.    local comp="$1"
  494.    local getcomp="$1"
  495.    case $comp in
  496.        "RestoreLogo" ) getcomp="AppleLogo";;
  497.        *"KernelCache" ) getcomp="Kernelcache";;
  498.        "RestoreDeviceTree" ) getcomp="DeviceTree";;
  499.    esac
  500.    local fw_key="$device_fw_key"
  501.    if [[ $2 == "base" ]]; then
  502.        fw_key="$device_fw_key_base"
  503.    fi
  504.    local name=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  505.    [[ $name == *".dmg" ]] && name="${name%%.dmg*}.dmg"
  506.    local iv=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  507.    local key=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  508.    if [[ -z $name && $device_proc != 1 ]]; then
  509.        error "Issue with firmware keys: Failed getting $getcomp. Check The Apple Wiki or your wikiproxy"
  510.    fi
  511.  
  512.    case $comp in
  513.        "iBSS" | "iBEC" )
  514.            if [[ -z $name ]]; then
  515.                name="$getcomp.${device_model}ap.RELEASE.dfu"
  516.            fi
  517.            echo "<key>$comp</key><dict><key>File</key><string>Firmware/dfu/$name</string>" >> $NewPlist
  518.            if [[ -n $iv && -n $key ]]; then
  519.                echo "<key>IV</key><string>$iv</string><key>Key</key><string>$key</string>" >> $NewPlist
  520.            fi
  521.            if [[ $ipsw_prepare_usepowder == 1 ]]; then
  522.                echo "<key>Patch</key><true/>" >> $NewPlist
  523.            elif [[ -s $FirmwareBundle/$comp.${device_model}ap.RELEASE.patch ]]; then
  524.                echo "<key>Patch</key><string>$comp.${device_model}ap.RELEASE.patch</string>" >> $NewPlist
  525.            elif [[ -s $FirmwareBundle/$comp.${device_model}.RELEASE.patch ]]; then
  526.                echo "<key>Patch</key><string>$comp.${device_model}.RELEASE.patch</string>" >> $NewPlist
  527.            fi
  528.        ;;
  529.  
  530.        "iBoot" )
  531.            echo "<key>$comp</key><dict><key>File</key><string>$all_flash/$name</string><key>IV</key><string>$iv</string><key>Key</key><string>$key</string>" >> $NewPlist
  532.            echo "<key>Patch</key><string>$comp.${device_model}ap.RELEASE.patch</string>" >> $NewPlist
  533.        ;;
  534.  
  535.        "RestoreRamdisk" )
  536.            echo "<key>Restore Ramdisk</key><dict><key>File</key><string>$name</string><key>IV</key><string>$iv</string><key>Key</key><string>$key</string>" >> $NewPlist
  537.        ;;
  538.  
  539.        "RestoreDeviceTree" | "RestoreLogo" )
  540.            echo "<key>$comp</key><dict><key>File</key><string>$all_flash/$name</string><key>IV</key><string>$iv</string><key>Key</key><string>$key</string><key>DecryptPath</key><string>Downgrade/$comp</string>" >> $NewPlist
  541.        ;;
  542.  
  543.        "RestoreKernelCache" )
  544.            echo "<key>$comp</key><dict><key>File</key><string>$name</string><key>IV</key><string>$iv</string><key>Key</key><string>$key</string><key>DecryptPath</key><string>Downgrade/$comp</string>" >> $NewPlist
  545.        ;;
  546.  
  547.        "KernelCache" )
  548.            echo "<key>$comp</key><dict><key>File</key><string>$name</string><key>IV</key><string>$iv</string><key>Key</key><string>$key</string>" >> $NewPlist
  549.            if [[ $ipsw_prepare_usepowder == 1 ]]; then
  550.                echo "<key>Patch</key><true/>" >> $NewPlist
  551.            elif [[ -e $FirmwareBundle/kernelcache.release.patch ]]; then
  552.                echo "<key>Patch</key><string>kernelcache.release.patch</string>" >> $NewPlist
  553.            fi
  554.        ;;
  555.  
  556.        "WTF2" )
  557.            echo "<key>WTF 2</key><dict><key>File</key><string>Firmware/dfu/WTF.s5l8900xall.RELEASE.dfu</string><key>Patch</key><string>WTF.s5l8900xall.RELEASE.patch</string>" >> $NewPlist
  558.        ;;
  559.    esac
  560.    if [[ $2 != "old" ]]; then
  561.        echo "<key>Decrypt</key><true/>" >> $NewPlist
  562.    fi
  563.    echo "</dict>" >> $NewPlist
  564. }
  565.  
  566. ipsw_prepare_paths() {
  567.    local comp="$1"
  568.    local getcomp="$1"
  569.    case $comp in
  570.        "BatteryPlugin" ) getcomp="GlyphPlugin";;
  571.        "NewAppleLogo" | "APTicket" ) getcomp="AppleLogo";;
  572.        "NewRecoveryMode" ) getcomp="RecoveryMode";;
  573.        "NewiBoot" ) getcomp="iBoot";;
  574.    esac
  575.    local fw_key="$device_fw_key"
  576.    if [[ $2 == "base" ]]; then
  577.        fw_key="$device_fw_key_base"
  578.    fi
  579.    local name=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  580.    if [[ -z $name && $getcomp != "manifest" ]]; then
  581.        error "Issue with firmware keys: Failed getting $getcomp. Check The Apple Wiki or your wikiproxy"
  582.    fi
  583.    local str="<key>$comp</key><dict><key>File</key><string>$all_flash/"
  584.    local str2
  585.    local logostuff
  586.    if [[ $2 == "target" ]]; then
  587.        case $comp in
  588.            *"AppleLogo" )
  589.                if [[ $device_latest_vers == "5"* ]]; then
  590.                    logostuff=1
  591.                else
  592.                    case $device_target_vers in
  593.                        [789]* ) logostuff=1;;
  594.                    esac
  595.                fi
  596.            ;;
  597.        esac
  598.        case $comp in
  599.            "AppleLogo" ) str2="${name/applelogo/applelogo7}";;
  600.            "APTicket" ) str2="${name/applelogo/applelogoT}";;
  601.            "RecoveryMode" ) str2="${name/recoverymode/recoverymode7}";;
  602.            "NewiBoot" ) str2="${name/iBoot/iBoot2}";;
  603.        esac
  604.        case $comp in
  605.            "AppleLogo" )
  606.                str+="$str2"
  607.                if [[ $logostuff == 1 ]]; then
  608.                    echo "$str2" >> $FirmwareBundle/manifest
  609.                fi
  610.            ;;
  611.            "APTicket" | "RecoveryMode" )
  612.                str+="$str2"
  613.                echo "$str2" >> $FirmwareBundle/manifest
  614.            ;;
  615.            "NewiBoot" )
  616.                if [[ $device_type != "iPad1,1" ]]; then
  617.                    str+="$str2"
  618.                    echo "$str2" >> $FirmwareBundle/manifest
  619.                fi
  620.            ;;
  621.            "manifest" ) str+="manifest";;
  622.            * ) str+="$name";;
  623.        esac
  624.    else
  625.        str+="$name"
  626.    fi
  627.    str+="</string>"
  628.  
  629.    if [[ $comp == "NewiBoot" ]]; then
  630.        local iv=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  631.        local key=$(echo $fw_key | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  632.        str+="<key>IV</key><string>$iv</string><key>Key</key><string>$key</string>"
  633.    elif [[ $comp == "manifest" ]]; then
  634.        str+="<key>manifest</key><string>manifest</string>"
  635.    fi
  636.  
  637.    echo "$str</dict>" >> $NewPlist
  638. }
  639.  
  640. ipsw_prepare_config() {
  641.    # usage: ipsw_prepare_config [jailbreak (true/false)] [needpref (true/false)]
  642.    # creates config file to FirmwareBundles/config.plist
  643.    local bootargs_enabled="false"
  644.    local bootargs="$device_bootargs_default"
  645.    if [[ $ipsw_verbose == 1 ]]; then
  646.        bootargs_enabled="true"
  647.        bootargs="pio-error=0 -v"
  648.    fi
  649.    if [[ -n $device_bootargs ]]; then
  650.        bootargs_enabled="true"
  651.        bootargs+=" $device_bootargs"
  652.    fi
  653.    log "Preparing config file"
  654.    echo "<plist>
  655. <dict>
  656.     <key>FilesystemJailbreak</key>
  657.     <$1/>
  658.     <key>needPref</key>
  659.     <$2/>
  660.     <key>iBootPatches</key>
  661.     <dict>
  662.         <key>debugEnabled</key>
  663.         <false/>
  664.         <key>bootArgsInjection</key>
  665.         <$bootargs_enabled/>
  666.         <key>bootArgsString</key>
  667.         <string>$bootargs</string>
  668.     </dict>
  669. </dict>
  670. </plist>" | tee FirmwareBundles/config.plist
  671. }
  672.  
  673. ipsw_prepare_systemversion() {
  674.    local sysplist="SystemVersion.plist"
  675.    log "Beta iOS detected, preparing modified $sysplist"
  676.    echo '<plist><dict>' > $sysplist
  677.    echo "<key>ProductBuildVersion</key><string>$device_target_build</string>" >> $sysplist
  678.    local copyright="<key>ProductCopyright</key><string>1983-201"
  679.    case $device_target_vers in
  680.        3* ) copyright+="0";;
  681.        4* ) copyright+="1";;
  682.        5* ) copyright+="2";;
  683.        6* ) copyright+="3";;
  684.        7* ) copyright+="4";;
  685.        8* ) copyright+="5";;
  686.        9* ) copyright+="6";;
  687.    esac
  688.    copyright+=" Apple Inc.</string>"
  689.    echo "$copyright" >> $sysplist # idk if the copyright key is actually needed but whatever
  690.    echo "<key>ProductName</key><string>iPhone OS</string>" >> $sysplist
  691.    echo "<key>ProductVersion</key><string>$device_target_vers</string>" >> $sysplist
  692.    echo "</dict></plist>" >> $sysplist
  693.    cat $sysplist
  694.    mkdir -p System/Library/CoreServices
  695.    mv SystemVersion.plist System/Library/CoreServices
  696.    tar -cvf systemversion.tar System
  697. }
  698.  
  699. ipsw_prepare_bundle() {
  700.    device_fw_key_check $1
  701.    local ipsw_p="$ipsw_path"
  702.    local key="$device_fw_key"
  703.    local vers="$device_target_vers"
  704.    local build="$device_target_build"
  705.    local hw="$device_model"
  706.    local base_build="11D257"
  707.    local RootSize
  708.    local daibutsu
  709.    FirmwareBundle="FirmwareBundles/"
  710.    if [[ $1 == "daibutsu" ]]; then
  711.        daibutsu=1
  712.    fi
  713.  
  714.    mkdir -p FirmwareBundles
  715.    if [[ $1 == "base" ]]; then
  716.        ipsw_p="$ipsw_base_path"
  717.        key="$device_fw_key_base"
  718.        vers="$device_base_vers"
  719.        build="$device_base_build"
  720.        FirmwareBundle+="BASE_"
  721.    elif [[ $1 == "target" ]]; then
  722.        if [[ $ipsw_jailbreak == 1 ]]; then
  723.            case $vers in
  724.                [689]* ) ipsw_prepare_config true true;;
  725.                * ) ipsw_prepare_config false true;;
  726.            esac
  727.        else
  728.            ipsw_prepare_config false true
  729.        fi
  730.    elif [[ $ipsw_jailbreak == 1 ]]; then
  731.        ipsw_prepare_config false true
  732.    else
  733.        ipsw_prepare_config false false
  734.    fi
  735.    local FirmwareBundle2="../resources/firmware/FirmwareBundles/Down_${device_type}_${vers}_${build}.bundle"
  736.    if [[ $ipsw_prepare_usepowder == 1 ]]; then
  737.        FirmwareBundle2=
  738.    elif [[ -d $FirmwareBundle2 ]]; then
  739.        FirmwareBundle+="Down_"
  740.    fi
  741.    FirmwareBundle+="${device_type}_${vers}_${build}.bundle"
  742.    local NewPlist=$FirmwareBundle/Info.plist
  743.    mkdir -p $FirmwareBundle
  744.  
  745.    log "Generating firmware bundle for $device_type-$vers ($build) $1..."
  746.    file_extract_from_archive "$ipsw_p.ipsw" $all_flash/manifest
  747.    mv manifest $FirmwareBundle/
  748.    local ramdisk_name=$(echo "$key" | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .filename')
  749.    ramdisk_name="${ramdisk_name%%.dmg*}.dmg"
  750.    local RamdiskIV=$(echo "$key" | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .iv')
  751.    local RamdiskKey=$(echo "$key" | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .key')
  752.    if [[ -z $ramdisk_name ]]; then
  753.        error "Issue with firmware keys: Failed getting RestoreRamdisk. Check The Apple Wiki or your wikiproxy"
  754.    fi
  755.    file_extract_from_archive "$ipsw_p.ipsw" $ramdisk_name
  756.    "$dir/xpwntool" $ramdisk_name Ramdisk.raw -iv $RamdiskIV -k $RamdiskKey
  757.    "$dir/hfsplus" Ramdisk.raw extract usr/local/share/restore/options.$device_model.plist
  758.    if [[ ! -s options.$device_model.plist ]]; then
  759.        rm options.$device_model.plist
  760.        "$dir/hfsplus" Ramdisk.raw extract usr/local/share/restore/options.plist
  761.        mv options.plist options.$device_model.plist
  762.    fi
  763.    local ver2="${device_target_vers:0:1}"
  764.    if [[ ! -s options.$device_model.plist ]] && (( ver2 >= 4 )); then
  765.        error "Failed to extract options plist from restore ramdisk. Probably an issue with firmware keys."
  766.    fi
  767.    if [[ $device_target_vers == "3.2"* ]]; then
  768.        RootSize=1000
  769.    elif [[ $ver2 == 3 ]]; then
  770.        case $device_type in
  771.            iPhone1,* | iPod1,1 ) RootSize=420;;
  772.            iPod2,1 ) RootSize=450;;
  773.            *       ) RootSize=750;;
  774.        esac
  775.    elif [[ $platform == "macos" ]]; then
  776.        plutil -extract 'SystemPartitionSize' xml1 options.$device_model.plist -o size
  777.        RootSize=$(cat size | sed -ne '/<integer>/,/<\/integer>/p' | sed -e "s/<integer>//" | sed "s/<\/integer>//" | sed '2d')
  778.    else
  779.        RootSize=$(cat options.$device_model.plist | grep -i SystemPartitionSize -A 1 | grep -oPm1 "(?<=<integer>)[^<]+")
  780.     fi
  781.     RootSize=$((RootSize+30))
  782.     local rootfs_name="$(echo "$key" | $jq -j '.keys[] | select(.image == "RootFS") | .filename')"
  783.     rootfs_name="${rootfs_name%%.dmg*}.dmg"
  784.     local rootfs_key="$(echo "$key" | $jq -j '.keys[] | select(.image == "RootFS") | .key')"
  785.     if [[ -z $rootfs_name ]]; then
  786.         error "Issue with firmware keys: Failed getting RootFS. Check The Apple Wiki or your wikiproxy"
  787.     fi
  788.     echo '<plist><dict>' > $NewPlist
  789.     echo "<key>Filename</key><string>$ipsw_p.ipsw</string>" >> $NewPlist
  790.     echo "<key>RootFilesystem</key><string>$rootfs_name</string>" >> $NewPlist
  791.     echo "<key>RootFilesystemKey</key><string>$rootfs_key</string>" >> $NewPlist
  792.     echo "<key>RootFilesystemSize</key><integer>$RootSize</integer>" >> $NewPlist
  793.     printf "<key>RamdiskOptionsPath</key><string>/usr/local/share/restore/options" >> $NewPlist
  794.     if [[ $device_target_vers != "3"* && $device_target_vers != "4"* ]] ||
  795.        [[ $device_type == "iPad1,1" && $device_target_vers == "4"* ]]; then
  796.         printf ".%s" "$device_model" >> $NewPlist
  797.     fi
  798.     echo ".plist</string>" >> $NewPlist
  799.     if [[ $1 == "base" ]]; then
  800.         echo "<key>SHA1</key><string>$device_base_sha1</string>" >> $NewPlist
  801.     else
  802.         echo "<key>SHA1</key><string>$device_target_sha1</string>" >> $NewPlist
  803.     fi
  804.  
  805.     if [[ $1 == "base" ]]; then
  806.         case $device_type in
  807.             iPhone5,[12] ) hw="iphone5";;
  808.             iPhone5,[34] ) hw="iphone5b";;
  809.             iPad2,[123]  ) hw="ipad2";;
  810.             iPad2,[567]  ) hw="ipad2b";;
  811.             iPad3,[123]  ) hw="ipad3";;
  812.             iPad3,[456]  ) hw="ipad3b";;
  813.         esac
  814.         case $device_base_build in
  815.             11[AB]* ) base_build="11B554a";;
  816.             9B*     ) base_build="9B206";;
  817.             9A*     ) base_build="9A405";;
  818.         esac
  819.         echo "<key>RamdiskExploit</key><dict>" >> $NewPlist
  820.         echo "<key>exploit</key><string>src/target/$hw/$base_build/exploit</string>" >> $NewPlist
  821.         echo "<key>inject</key><string>partition</string></dict>" >> $NewPlist
  822.     elif [[ $1 == "target" ]]; then
  823.         echo "<key>FilesystemPackage</key><dict><key>bootstrap</key><string>freeze.tar</string>" >> $NewPlist
  824.         case $vers in
  825.             8* | 9* ) echo "<key>package</key><string>src/ios9.tar</string>" >> $NewPlist;;
  826.         esac
  827.         printf "</dict><key>RamdiskPackage</key><dict><key>package</key><string>src/bin.tar</string><key>ios</key><string>ios" >> $NewPlist
  828.         [[ $ipsw_jailbreak == 1 ]] && printf "%s" "${vers:0:1}" >> "$NewPlist"
  829.         echo "</string></dict>" >> $NewPlist
  830.     elif [[ $ipsw_prepare_usepowder == 1 ]]; then
  831.         echo "<key>FilesystemPackage</key><dict/><key>RamdiskPackage</key><dict/>" >> $NewPlist
  832.     elif [[ $ipsw_isbeta == 1 && $ipsw_prepare_usepowder != 1 ]]; then
  833.         warn "iOS 4.1 beta or older detected. Attempting workarounds"
  834.         cp $FirmwareBundle2/* $FirmwareBundle
  835.         echo "<key>RamdiskPatches</key><dict/>" >> $NewPlist
  836.         echo "<key>FilesystemPatches</key><dict/>" >> $NewPlist
  837.         ipsw_isbeta_needspatch=1
  838.     elif [[ -d $FirmwareBundle2 ]]; then
  839.         cp $FirmwareBundle2/* $FirmwareBundle
  840.         echo "<key>RamdiskPatches</key><dict>" >> $NewPlist
  841.         echo "<key>asr</key><dict>" >> $NewPlist
  842.         echo "<key>File</key><string>usr/sbin/asr</string><key>Patch</key><string>asr.patch</string></dict>" >> $NewPlist
  843.         if [[ -s $FirmwareBundle/restoredexternal.patch ]]; then
  844.             echo "<key>restoredexternal</key><dict>" >> $NewPlist
  845.             echo "<key>File</key><string>usr/local/bin/restored_external</string><key>Patch</key><string>restoredexternal.patch</string></dict>" >> $NewPlist
  846.         fi
  847.         echo "</dict>" >> $NewPlist
  848.         if [[ $ipsw_hacktivate == 1 ]]; then
  849.             echo "<key>FilesystemPatches</key><dict>" >> $NewPlist
  850.             echo "<key>Hacktivation</key><array><dict>" >> $NewPlist
  851.             echo "<key>Action</key><string>Patch</string><key>File</key><string>usr/libexec/lockdownd</string>" >> $NewPlist
  852.             echo "<key>Patch</key><string>lockdownd.patch</string></dict></array></dict>" >> $NewPlist
  853.         else
  854.             echo "<key>FilesystemPatches</key><dict/>" >> $NewPlist # ipsw segfaults if this is missing lol
  855.         fi
  856.     fi
  857.  
  858.     if [[ $1 == "base" ]]; then
  859.         echo "<key>Firmware</key><dict/>" >> $NewPlist
  860.     elif [[ $1 == "target" && $vers == "4"* ]]; then
  861.         echo "<key>Firmware</key><dict>" >> $NewPlist
  862.         ipsw_prepare_keys iBSS $1
  863.         ipsw_prepare_keys RestoreRamdisk $1
  864.         echo "</dict>" >> $NewPlist
  865.     elif [[ $ipsw_isbeta_needspatch == 1 ]]; then
  866.         echo "<key>FirmwarePatches</key><dict>" >> $NewPlist
  867.         ipsw_prepare_keys RestoreDeviceTree $1
  868.         ipsw_prepare_keys RestoreLogo $1
  869.         ipsw_prepare_keys RestoreKernelCache $1
  870.         ipsw_prepare_keys RestoreRamdisk $1
  871.         echo "</dict>" >> $NewPlist
  872.     elif [[ $device_target_build == "14"* ]]; then
  873.         echo "<key>Firmware</key><dict>" >> $NewPlist
  874.         ipsw_prepare_keys iBSS
  875.         ipsw_prepare_keys iBEC
  876.         ipsw_prepare_keys RestoreRamdisk
  877.     else
  878.         if [[ $ipsw_prepare_usepowder == 1 ]]; then
  879.             echo "<key>Firmware</key><dict>" >> $NewPlist
  880.         else
  881.             echo "<key>FirmwarePatches</key><dict>" >> $NewPlist
  882.         fi
  883.         ipsw_prepare_keys iBSS $1
  884.         # ios 4 and lower do not need ibec patches. the exception is the ipad lineup
  885.         if [[ $vers != "3"* && $vers != "4"* ]] || [[ $device_type == "iPad1,1" || $device_type == "iPad2"* ]]; then
  886.             ipsw_prepare_keys iBEC $1
  887.         fi
  888.         if [[ $device_proc == 1 && $device_target_vers != "4.2.1" ]]; then
  889.             :
  890.         else
  891.             ipsw_prepare_keys RestoreDeviceTree $1
  892.             ipsw_prepare_keys RestoreLogo $1
  893.         fi
  894.         if [[ $1 == "target" ]]; then
  895.             case $vers in
  896.                 [457]* ) ipsw_prepare_keys RestoreKernelCache $1;;
  897.                 * ) ipsw_prepare_keys KernelCache $1;;
  898.             esac
  899.         elif [[ $device_proc == 1 && $device_target_vers == "4.2.1" ]]; then
  900.             ipsw_prepare_keys RestoreKernelCache $1
  901.         elif [[ $device_proc != 1 && $device_target_vers != "3.0"* ]]; then
  902.             ipsw_prepare_keys RestoreKernelCache $1
  903.         fi
  904.         ipsw_prepare_keys RestoreRamdisk $1
  905.         if [[ $1 == "old" ]]; then
  906.             if [[ $ipsw_24o == 1 ]]; then # old bootrom ipod2,1 3.1.3
  907.                 ipsw_prepare_keys iBoot $1
  908.                 ipsw_prepare_keys KernelCache $1
  909.             elif [[ $device_type == "iPod2,1" && $device_target_vers == "3.1.3" ]]; then
  910.                 : # dont patch iboot/kcache for new bootrom ipod2,1 3.1.3
  911.             elif [[ $device_proc == 1 ]]; then
  912.                 ipsw_prepare_keys KernelCache $1
  913.                 ipsw_prepare_keys WTF2 $1
  914.             else
  915.                 case $device_target_vers in
  916.                     $device_latest_vers | 4.1 ) :;;
  917.                     3.0* ) ipsw_prepare_keys iBoot $1;;
  918.                     * )
  919.                         ipsw_prepare_keys iBoot $1
  920.                         ipsw_prepare_keys KernelCache $1
  921.                     ;;
  922.                 esac
  923.             fi
  924.         fi
  925.         echo "</dict>" >> $NewPlist
  926.     fi
  927.  
  928.     if [[ $1 == "base" ]]; then
  929.         echo "<key>FirmwarePath</key><dict>" >> $NewPlist
  930.         ipsw_prepare_paths AppleLogo $1
  931.         ipsw_prepare_paths BatteryCharging0 $1
  932.         ipsw_prepare_paths BatteryCharging1 $1
  933.         ipsw_prepare_paths BatteryFull $1
  934.         ipsw_prepare_paths BatteryLow0 $1
  935.         ipsw_prepare_paths BatteryLow1 $1
  936.         ipsw_prepare_paths BatteryPlugin $1
  937.         ipsw_prepare_paths RecoveryMode $1
  938.         ipsw_prepare_paths LLB $1
  939.         ipsw_prepare_paths iBoot $1
  940.         echo "</dict>" >> $NewPlist
  941.     elif [[ $1 == "target" ]]; then
  942.         echo "<key>FirmwareReplace</key><dict>" >> $NewPlist
  943.         if [[ $vers == "4"* ]]; then
  944.             ipsw_prepare_paths APTicket $1
  945.         fi
  946.         ipsw_prepare_paths AppleLogo $1
  947.         ipsw_prepare_paths NewAppleLogo $1
  948.         ipsw_prepare_paths BatteryCharging0 $1
  949.         ipsw_prepare_paths BatteryCharging1 $1
  950.         ipsw_prepare_paths BatteryFull $1
  951.         ipsw_prepare_paths BatteryLow0 $1
  952.         ipsw_prepare_paths BatteryLow1 $1
  953.         ipsw_prepare_paths BatteryPlugin $1
  954.         ipsw_prepare_paths RecoveryMode $1
  955.         ipsw_prepare_paths NewRecoveryMode $1
  956.         ipsw_prepare_paths LLB $1
  957.         ipsw_prepare_paths iBoot $1
  958.         ipsw_prepare_paths NewiBoot $1
  959.         ipsw_prepare_paths manifest $1
  960.         echo "</dict>" >> $NewPlist
  961.     fi
  962.  
  963.     if [[ $daibutsu == 1 ]]; then
  964.         if [[ $ipsw_prepare_usepowder == 1 ]]; then
  965.             echo "<key>RamdiskPackage2</key>" >> $NewPlist
  966.         else
  967.             echo "<key>PackagePath</key><string>./freeze.tar</string>" >> $NewPlist
  968.             echo "<key>RamdiskPackage</key>" >> $NewPlist
  969.         fi
  970.         echo "<string>./bin.tar</string><key>RamdiskReboot</key><string>./reboot.sh</string><key>UntetherPath</key><string>./untether.tar</string>" >> $NewPlist
  971.         local hwmodel="$(tr '[:lower:]' '[:upper:]' <<< ${device_model:0:1})${device_model:1}"
  972.         echo "<key>hwmodel</key><string>$hwmodel</string>" >> $NewPlist
  973.     fi
  974.  
  975.     echo "</dict></plist>" >> $NewPlist
  976.     cat $NewPlist
  977. }
  978.  
  979. ipsw_prepare_32bit() {
  980.     local ExtraArgs
  981.     local daibutsu
  982.     local JBFiles=()
  983.     # redirect to ipsw_prepare_jailbreak for 4.1 and lower
  984.     case $device_target_vers in
  985.         [23]* | 4.[01]* ) ipsw_prepare_jailbreak $1; return;;
  986.         10* )
  987.             if [[ ! -s "$ipsw_custom.ipsw" && $ipsw_gasgauge_patch == 1 ]]; then
  988.                 log "Copying custom IPSW..."
  989.                 cp "$ipsw_path.ipsw" "$ipsw_custom.ipsw"
  990.             fi
  991.             return
  992.         ;;
  993.     esac
  994.     # use everuntether+jsc_untether instead of everuntether+dsc haxx for a5(x) 8.0-8.2
  995.     if [[ $device_proc == 5 && $ipsw_jailbreak == 1 ]]; then
  996.         case $device_target_vers in
  997.             8.[012]* )
  998.                 ipsw_everuntether=1
  999.                 JBFiles=("everuntether.tar")
  1000.             ;;
  1001.         esac
  1002.     fi
  1003.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  1004.         log "Found existing Custom IPSW. Skipping IPSW creation."
  1005.         return
  1006.     elif [[ $ipsw_jailbreak == 1 && $ipsw_everuntether != 1 ]]; then
  1007.         if [[ $device_target_vers == "8."* ]]; then
  1008.             daibutsu="daibutsu"
  1009.             ExtraArgs+=" -daibutsu"
  1010.             cp $jelbrek/daibutsu/bin.tar $jelbrek/daibutsu/untether.tar .
  1011.             ipsw_prepare_rebootsh
  1012.         elif [[ $device_target_vers == "7."* ]]; then
  1013.             daibutsu="daibutsu"
  1014.             ExtraArgs+=" -daibutsu"
  1015.             cp $jelbrek/daibutsu/bin.tar .
  1016.             cp $jelbrek/aquila_7.tar untether.tar
  1017.             ipsw_prepare_rebootsh aquila
  1018.         fi
  1019.     elif [[ $ipsw_nskip == 1 ]]; then
  1020.         :
  1021.     elif [[ $ipsw_jailbreak != 1 && $device_target_build != "9A406" && # 9a406 needs custom ipsw
  1022.             $device_proc != 4 && $device_actrec != 1 && $device_target_tethered != 1 && $ipsw_isbeta != 1 ]]; then
  1023.         log "No need to create custom IPSW for non-jailbroken restores on $device_type-$device_target_build"
  1024.         return
  1025.     fi
  1026.     ipsw_prepare_usepowder=1
  1027.  
  1028.     ipsw_prepare_bundle $daibutsu
  1029.  
  1030.     if [[ $ipsw_memory == 1 ]]; then
  1031.         ExtraArgs+=" -memory"
  1032.     fi
  1033.     ExtraArgs+=" -ramdiskgrow 10"
  1034.     if [[ $device_use_bb != 0 && $device_type != "$device_disable_bbupdate" ]]; then
  1035.         ExtraArgs+=" -bbupdate"
  1036.     fi
  1037.  
  1038.     if [[ $ipsw_jailbreak == 1 ]]; then
  1039.         case $device_target_vers in
  1040.             9.3.[56] ) :;;
  1041.             9.*  ) JBFiles=("everuntether.tar");;
  1042.             6.*  ) JBFiles=("aquila_6.tar");;
  1043.             5.*  ) JBFiles=("aquila_5.tar");;
  1044.             4.3* ) JBFiles=("aquila_4.tar");;
  1045.             4.2.[8761] )
  1046.                 ExtraArgs+=" -punchd"
  1047.                 JBFiles=("greenpois0n/${device_type}_${device_target_build}.tar")
  1048.             ;;
  1049.         esac
  1050.  
  1051.         # temporary measure for a6 ios 6
  1052.         if [[ $device_proc == 6 ]]; then
  1053.             case $device_target_vers in
  1054.                 6.1.[34] ) JBFiles=("p0sixspwn.tar");;
  1055.                 6.*      ) JBFiles=("evasi0n6-untether.tar");;
  1056.             esac
  1057.         fi
  1058.         # temporary measure for a5 ios 5
  1059.         if [[ $device_proc == 5 && $device_target_vers == "5."* ]]; then
  1060.             JBFiles=("g1lbertJB/${device_type}_${device_target_build}.tar")
  1061.         fi
  1062.  
  1063.         if [[ -n ${JBFiles[0]} ]]; then
  1064.             JBFiles[0]=$jelbrek/${JBFiles[0]}
  1065.         fi
  1066.         case $device_target_vers in
  1067.             [98].* ) JBFiles+=("$jelbrek/fstab8.tar");;
  1068.             7.*    ) JBFiles+=("$jelbrek/fstab7.tar");;
  1069.             4.*    ) JBFiles+=("$jelbrek/fstab_old.tar");;
  1070.             *      ) JBFiles+=("$jelbrek/fstab_rw.tar");;
  1071.         esac
  1072.         JBFiles+=("freeze.tar")
  1073.         case $device_target_vers in
  1074.             9.* ) JBFiles+=("$jelbrek/launchctl.tar" "$jelbrek/zebra.tar");;
  1075.             5.* ) JBFiles+=("$jelbrek/cydiasubstrate.tar");;
  1076.         esac
  1077.         if [[ $ipsw_openssh == 1 ]]; then
  1078.             cp $jelbrek/openssh.tar.gz $jelbrek/openssl.tar.gz .
  1079.             gzip -d openssh.tar.gz
  1080.             gzip -d openssl.tar.gz
  1081.             JBFiles+=("$jelbrek/sshdeb.tar" "openssh.tar" "openssl.tar")
  1082.         fi
  1083.  
  1084.         # temporary measure for a5 ios 5
  1085.         if [[ $device_proc == 5 && $device_target_vers == "5."* && $device_target_tethered == 1 ]]; then
  1086.             JBFiles+=("$jelbrek/g1lbertJB/install.tar")
  1087.         fi
  1088.  
  1089.         case $device_target_vers in
  1090.             [43]* ) :;;
  1091.             * ) JBFiles+=("$jelbrek/LukeZGD.tar");;
  1092.         esac
  1093.         cp $jelbrek/freeze.tar.gz .
  1094.         gzip -d freeze.tar.gz
  1095.     fi
  1096.  
  1097.     if [[ $ipsw_isbeta == 1 ]]; then
  1098.         ipsw_prepare_systemversion
  1099.         ExtraArgs+=" systemversion.tar"
  1100.     fi
  1101.     if [[ $1 == "iboot" ]]; then
  1102.         ExtraArgs+=" iBoot.tar"
  1103.     fi
  1104.     if [[ $device_type == "$device_disable_bbupdate" && $device_deadbb != 1 ]]; then
  1105.         ExtraArgs+=" ../saved/$device_type/baseband-$device_ecid.tar"
  1106.     fi
  1107.     if [[ $device_actrec == 1 ]]; then
  1108.         ExtraArgs+=" ../saved/$device_type/activation-$device_ecid.tar"
  1109.     fi
  1110.  
  1111.     log "Preparing custom IPSW: $dir/powdersn0w $ipsw_path.ipsw temp.ipsw $ExtraArgs ${JBFiles[*]}"
  1112.     "$dir/powdersn0w" "$ipsw_path.ipsw" temp.ipsw $ExtraArgs ${JBFiles[@]}
  1113.  
  1114.     if [[ ! -e temp.ipsw ]]; then
  1115.         if [[ $platform == "macos" && $platform_arch == "arm64" ]]; then
  1116.             warn "Updating to macOS 12.6 or newer is recommended for Apple Silicon Macs to resolve issues."
  1117.         fi
  1118.         error "Failed to find custom IPSW. Please run the script again" \
  1119.               "* You may try selecting N for memory option"
  1120.     fi
  1121.  
  1122.     ipsw_prepare_fourthree
  1123.     ipsw_bbreplace
  1124.     if [[ $target_vers_maj == 4 ]]; then
  1125.         ipsw_prepare_ios4patches
  1126.         log "Add all to custom IPSW"
  1127.         zip -r0 temp.ipsw Firmware/dfu/*
  1128.     fi
  1129.  
  1130.     mv temp.ipsw "$ipsw_custom.ipsw"
  1131. }
  1132.  
  1133. ipsw_bbdigest() {
  1134.     local loc="BuildIdentities:0:"
  1135.     if [[ $2 != "UniqueBuildID" ]]; then
  1136.         loc+="Manifest:BasebandFirmware:"
  1137.     fi
  1138.     loc+="$2"
  1139.     local out="$1"
  1140.     log "Replacing $2"
  1141.     if [[ $platform == "macos" ]]; then
  1142.         echo $out | base64 --decode > t
  1143.         $PlistBuddy -c "Import $loc t" BuildManifest.plist
  1144.         rm t
  1145.         return
  1146.     fi
  1147.     in=$($PlistBuddy -c "Print $loc" BuildManifest.plist | tr -d "<>" | xxd -r -p | base64)
  1148.     in="${in}<"
  1149.     in="$(echo "$in" | sed -e 's,AAAAAAAAAAAAAAAAAAAAAAA<,==,' \
  1150.                           -e 's,AAAAAAAAAAAAA<,=,' \
  1151.                           -e 's,AAAAAAAAA<,=,')"
  1152.     case $2 in
  1153.         *"PartialDigest" )
  1154.             in="${in%????????????}"
  1155.             in=$(grep -m1 "$in" BuildManifest.plist)
  1156.             sed "s,$in,replace," BuildManifest.plist | \
  1157.             awk 'f{f=0; next} /replace/{f=1} 1' | \
  1158.             awk '/replace$/{printf "%s", $0; next} 1' > tmp.plist
  1159.             in="replace"
  1160.         ;;
  1161.         * ) mv BuildManifest.plist tmp.plist;;
  1162.     esac
  1163.     sed "s,$in,$out," tmp.plist > BuildManifest.plist
  1164.     rm tmp.plist
  1165. }
  1166.  
  1167. ipsw_bbreplace() {
  1168.     local rsb1
  1169.     local sbl1
  1170.     local path
  1171.     local rsb_latest
  1172.     local sbl_latest
  1173.     local bbfw="Print BuildIdentities:0:Manifest:BasebandFirmware"
  1174.     local ubid
  1175.     if [[ $device_use_bb == 0 || $device_target_vers == "$device_latest_vers" ||
  1176.           $device_type == "$device_disable_bbupdate" ]] || (( device_proc < 5 )); then
  1177.         return
  1178.     fi
  1179.  
  1180.     if [[ $1 != "exist" ]]; then
  1181.         log "Extracting BuildManifest from IPSW"
  1182.         file_extract_from_archive temp.ipsw BuildManifest.plist
  1183.     fi
  1184.     mkdir -p Firmware
  1185.     restore_download_bbsep
  1186.     cp $restore_baseband Firmware/$device_use_bb
  1187.  
  1188.     case $device_type in
  1189.         iPhone4,1 ) ubid="d9Xbp0xyiFOxDvUcKMsoNjIvhwQ=";;
  1190.         iPhone5,1 ) ubid="IcrFKRzWDvccKDfkfMNPOPYHEV0=";;
  1191.         iPhone5,2 ) ubid="lnU0rtBUK6gCyXhEtHuwbEz/IKY=";;
  1192.         iPhone5,3 ) ubid="dwrol4czV3ijtNHh3w1lWIdsNdA=";;
  1193.         iPhone5,4 ) ubid="Z4ST0TczwAhpfluQFQNBg7Y3BVE=";;
  1194.         iPad2,6 ) ubid="L73HfN42pH7qAzlWmsEuIZZg2oE=";;
  1195.         iPad2,7 ) ubid="z/vJsvnUovZ+RGyXKSFB6DOjt1k=";;
  1196.         iPad3,5 ) ubid="849RPGQ9kNXGMztIQBhVoU/l5lM=";;
  1197.         iPad3,6 ) ubid="cO+N+Eo8ynFf+0rnsIWIQHTo6rg=";;
  1198.     esac
  1199.     ipsw_bbdigest $ubid UniqueBuildID
  1200.  
  1201.     case $device_type in
  1202.         iPhone4,1 )
  1203.             rsb1=$($PlistBuddy -c "$bbfw:eDBL-Version" BuildManifest.plist)
  1204.             sbl1=$($PlistBuddy -c "$bbfw:RestoreDBL-Version" BuildManifest.plist)
  1205.             path=$($PlistBuddy -c "$bbfw:Info:Path" BuildManifest.plist | tr -d '"')
  1206.             rsb_latest="-1577031936"
  1207.             sbl_latest="-1575983360"
  1208.             ipsw_bbdigest XAAAAADHAQCqerR8d+PvcfusucizfQ4ECBI0TA== RestoreDBL-PartialDigest
  1209.             ipsw_bbdigest Q1TLjk+/PjayCzSJJo68FTtdhyE= AMSS-HashTableDigest
  1210.             ipsw_bbdigest KkJI7ufv5tfNoqHcrU7gqoycmXA= OSBL-DownloadDigest
  1211.             ipsw_bbdigest eAAAAADIAQDxcjzF1q5t+nvLBbvewn/arYVkLw== eDBL-PartialDigest
  1212.             ipsw_bbdigest 3CHVk7EmtGjL14ApDND81cqFqhM= AMSS-DownloadDigest
  1213.         ;;
  1214.         iPhone5,[12] | iPad2,[67] | iPad3,[56] )
  1215.             rsb1=$($PlistBuddy -c "$bbfw:RestoreSBL1-Version" BuildManifest.plist)
  1216.             sbl1=$($PlistBuddy -c "$bbfw:SBL1-Version" BuildManifest.plist)
  1217.             path=$($PlistBuddy -c "$bbfw:Info:Path" BuildManifest.plist | tr -d '"')
  1218.             rsb_latest="-1559114512"
  1219.             sbl_latest="-1560163088"
  1220.             ipsw_bbdigest 2bmJ7Vd+WAmogV+hjq1a86UlBvA= APPS-DownloadDigest
  1221.             ipsw_bbdigest oNmIZf39zd94CPiiKOpKvhGJbyg= APPS-HashTableDigest
  1222.             ipsw_bbdigest dFi5J+pSSqOfz31fIvmah2GJO+E= DSP1-DownloadDigest
  1223.             ipsw_bbdigest HXUnmGmwIHbVLxkT1rHLm5V6iDM= DSP1-HashTableDigest
  1224.             ipsw_bbdigest oA5eQ8OurrWrFpkUOhD/3sGR3y8= DSP2-DownloadDigest
  1225.             ipsw_bbdigest L7v8ulq1z1Pr7STR47RsNbxmjf0= DSP2-HashTableDigest
  1226.             ipsw_bbdigest MZ1ERfoeFcbe79pFAl/hbWUSYKc= DSP3-DownloadDigest
  1227.             ipsw_bbdigest sKmLhQcjfaOliydm+iwxucr9DGw= DSP3-HashTableDigest
  1228.             ipsw_bbdigest oiW/8qZhN0r9OaLdUHCT+MMGknY= RPM-DownloadDigest
  1229.             ipsw_bbdigest fAAAAEAQAgAH58t5X9KETIPrycULi8dg7b2rSw== RestoreSBL1-PartialDigest
  1230.             ipsw_bbdigest ZAAAAIC9AQAfgUcPMN/lMt+U8s6bxipdy6td6w== SBL1-PartialDigest
  1231.             ipsw_bbdigest kHLoJsT9APu4Xwu/aRjNK10Hx84= SBL2-DownloadDigest
  1232.         ;;
  1233.         iPhone5,[34] )
  1234.             rsb1=$($PlistBuddy -c "$bbfw:RestoreSBL1-Version" BuildManifest.plist)
  1235.             sbl1=$($PlistBuddy -c "$bbfw:SBL1-Version" BuildManifest.plist)
  1236.             path=$($PlistBuddy -c "$bbfw:Info:Path" BuildManifest.plist | tr -d '"')
  1237.             rsb_latest="-1542379296"
  1238.             sbl_latest="-1543427872"
  1239.             ipsw_bbdigest TSVi7eYY4FiAzXynDVik6TY2S1c= APPS-DownloadDigest
  1240.             ipsw_bbdigest xd/JBOTxYJWmLkTWqLWl8GeINgU= APPS-HashTableDigest
  1241.             ipsw_bbdigest RigCEz69gUymh2UdyJdwZVx74Ic= DSP1-DownloadDigest
  1242.             ipsw_bbdigest a3XhREtzynTWtyQGqi/RXorXSVE= DSP1-HashTableDigest
  1243.             ipsw_bbdigest 3JTgHWvC+XZYWa5U5MPvle+imj4= DSP2-DownloadDigest
  1244.             ipsw_bbdigest Hvppb92/1o/cWQbl8ftoiW5jOLg= DSP2-HashTableDigest
  1245.             ipsw_bbdigest R60ZfsOqZX+Pd/UnEaEhWfNvVlY= DSP3-DownloadDigest
  1246.             ipsw_bbdigest DFQWkktFWNh90G2hOfwO14oEbrI= DSP3-HashTableDigest
  1247.             ipsw_bbdigest Rsn+u2mOpYEmdrw98yA8EDT5LiE= RPM-DownloadDigest
  1248.             ipsw_bbdigest cAAAAIC9AQBLeCHzsjHo8Q7+IzELZTV/ri/Vow== RestoreSBL1-PartialDigest
  1249.             ipsw_bbdigest eAAAAEBsAQB9b44LqXjR3izAYl5gB4j3Iqegkg== SBL1-PartialDigest
  1250.             ipsw_bbdigest iog3IVe+8VqgQzP2QspgFRUNwn8= SBL2-DownloadDigest
  1251.         ;;
  1252.     esac
  1253.  
  1254.     log "Replacing $rsb1 with $rsb_latest"
  1255.     log "Replacing $sbl1 with $sbl_latest"
  1256.     log "Replacing $path with Firmware/$device_use_bb"
  1257.     sed -e "s,$rsb1,$rsb_latest," \
  1258.         -e "s,$sbl1,$sbl_latest," \
  1259.         -e "s,$path,Firmware/$device_use_bb," BuildManifest.plist > tmp.plist
  1260.     mv tmp.plist BuildManifest.plist
  1261.  
  1262.     zip -r0 temp.ipsw Firmware/$device_use_bb BuildManifest.plist
  1263. }
  1264.  
  1265. patch_iboot() {
  1266.     device_fw_key_check
  1267.     local iboot_name=$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "iBoot") | .filename')
  1268.     local iboot_iv=$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "iBoot") | .iv')
  1269.     local iboot_key=$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "iBoot") | .key')
  1270.     if [[ -z $iboot_name ]]; then
  1271.         error "Issue with firmware keys: Failed getting iBoot. Check The Apple Wiki or your wikiproxy"
  1272.     fi
  1273.     local rsa="--rsa"
  1274.     log "Patch iBoot: $*"
  1275.     if [[ $1 == "--logo" ]]; then
  1276.         iboot_name="${iboot_name/iBoot/iBoot2}"
  1277.         rsa=
  1278.         file_extract_from_archive temp.ipsw $all_flash/$iboot_name
  1279.     else
  1280.         file_extract_from_archive "$ipsw_path.ipsw" $all_flash/$iboot_name
  1281.     fi
  1282.     mv $iboot_name iBoot.orig
  1283.     "$dir/xpwntool" iBoot.orig iBoot.dec -iv $iboot_iv -k $iboot_key
  1284.     "$dir/iBoot32Patcher" iBoot.dec iBoot.pwned $rsa "$@"
  1285.     "$dir/xpwntool" iBoot.pwned iBoot -t iBoot.orig
  1286.     if [[ $device_type == "iPad1,1" || $device_type == "iPhone5,"* ]]; then
  1287.         echo "0000010: 6365" | xxd -r - iBoot
  1288.         echo "0000020: 6365" | xxd -r - iBoot
  1289.         return
  1290.     elif [[ $device_type != "iPhone2,1" ]]; then
  1291.         echo "0000010: 626F" | xxd -r - iBoot
  1292.         echo "0000020: 626F" | xxd -r - iBoot
  1293.     fi
  1294.     "$dir/xpwntool" iBoot.pwned $iboot_name -t iBoot -iv $iboot_iv -k $iboot_key
  1295. }
  1296.  
  1297. ipsw_patch_file() {
  1298.     # usage: ipsw_patch_file <ramdisk/fs> <location> <filename> <patchfile>
  1299.     "$dir/hfsplus" "$1" extract "$2"/"$3"
  1300.     "$dir/hfsplus" "$1" rm "$2"/"$3"
  1301.     $bspatch "$3" "$3".patched "$4"
  1302.     "$dir/hfsplus" "$1" add "$3".patched "$2"/"$3"
  1303.     "$dir/hfsplus" "$1" chmod 755 "$2"/"$3"
  1304.     "$dir/hfsplus" "$1" chown 0:0 "$2"/"$3"
  1305. }
  1306.  
  1307. ipsw_prepare_ios4multipart() {
  1308.     local JBFiles=()
  1309.     ipsw_custom_part1="../${device_type}_${device_target_vers}_${device_target_build}_CustomNP-${device_ecid}"
  1310.     local all_flash2="$ipsw_custom_part1/$all_flash"
  1311.     local iboot
  1312.  
  1313.     if [[ -e "$ipsw_custom_part1.ipsw" && -e "$ipsw_custom.ipsw" ]]; then
  1314.         log "Found existing Custom IPSWs. Skipping IPSW creation."
  1315.         return
  1316.     elif [[ -e "$ipsw_custom_part1.ipsw" ]]; then
  1317.         rm -f "$ipsw_custom_part1.ipsw"
  1318.     fi
  1319.  
  1320.     log "Preparing NOR flash IPSW..."
  1321.     mkdir -p $ipsw_custom_part1/Firmware/dfu $ipsw_custom_part1/Downgrade $all_flash2
  1322.  
  1323.     local comps=("iBSS" "iBEC" "DeviceTree" "Kernelcache" "RestoreRamdisk")
  1324.     local name
  1325.     local iv
  1326.     local key
  1327.     local path
  1328.     local vers="5.1.1"
  1329.     local build="9B206"
  1330.     local saved_path="../saved/$device_type/$build"
  1331.     ipsw_get_url $build
  1332.     local url="$ipsw_url"
  1333.     device_fw_key_check temp $build
  1334.  
  1335.     mkdir -p $saved_path
  1336.     log "Getting $vers restore components"
  1337.     for getcomp in "${comps[@]}"; do
  1338.         name=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  1339.         iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  1340.         key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  1341.         case $getcomp in
  1342.             "iBSS" | "iBEC" ) path="Firmware/dfu/";;
  1343.             "DeviceTree" ) path="$all_flash/";;
  1344.             * ) path="";;
  1345.         esac
  1346.         log "$getcomp"
  1347.         if [[ $vers == "$device_base_vers" ]]; then
  1348.             file_extract_from_archive "$ipsw_base_path.ipsw" ${path}$name
  1349.         elif [[ -e $saved_path/$name ]]; then
  1350.             cp $saved_path/$name .
  1351.         else
  1352.             "$dir/pzb" -g "${path}$name" -o "$name" "$url"
  1353.             cp $name $saved_path/
  1354.         fi
  1355.         case $getcomp in
  1356.             "DeviceTree" )
  1357.                 "$dir/xpwntool" $name $ipsw_custom_part1/Downgrade/RestoreDeviceTree -iv $iv -k $key -decrypt
  1358.             ;;
  1359.             "Kernelcache" )
  1360.                 "$dir/xpwntool" $name $ipsw_custom_part1/Downgrade/RestoreKernelCache -iv $iv -k $key -decrypt
  1361.             ;;
  1362.             * )
  1363.                 mv $name $getcomp.orig
  1364.                 "$dir/xpwntool" $getcomp.orig $getcomp.dec -iv $iv -k $key
  1365.             ;;
  1366.         esac
  1367.     done
  1368.  
  1369.     log "Patch iBSS"
  1370.     "$dir/iBoot32Patcher" iBSS.dec iBSS.patched --rsa
  1371.     "$dir/xpwntool" iBSS.patched $ipsw_custom_part1/Firmware/dfu/iBSS.${device_model}ap.RELEASE.dfu -t iBSS.orig
  1372.  
  1373.     log "Patch iBEC"
  1374.     "$dir/iBoot32Patcher" iBEC.dec iBEC.patched --rsa --ticket -b "rd=md0 -v nand-enable-reformat=1 amfi=0xff cs_enforcement_disable=1"
  1375.     "$dir/xpwntool" iBEC.patched $ipsw_custom_part1/Firmware/dfu/iBEC.${device_model}ap.RELEASE.dfu -t iBEC.orig
  1376.  
  1377.     log "Manifest plist"
  1378.     if [[ $vers == "$device_base_vers" ]]; then
  1379.         file_extract_from_archive "$ipsw_base_path.ipsw" BuildManifest.plist
  1380.     elif [[ -e $saved_path/BuildManifest.plist ]]; then
  1381.         cp $saved_path/BuildManifest.plist .
  1382.     else
  1383.         "$dir/pzb" -g "${path}BuildManifest.plist" -o "BuildManifest.plist" "$url"
  1384.         cp BuildManifest.plist $saved_path/
  1385.     fi
  1386.     $PlistBuddy -c "Set BuildIdentities:0:Manifest:RestoreDeviceTree:Info:Path Downgrade/RestoreDeviceTree" BuildManifest.plist
  1387.     $PlistBuddy -c "Set BuildIdentities:0:Manifest:RestoreKernelCache:Info:Path Downgrade/RestoreKernelCache" BuildManifest.plist
  1388.     cp BuildManifest.plist $ipsw_custom_part1/
  1389.  
  1390.     log "Restore Ramdisk"
  1391.     local ramdisk_name=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .filename')
  1392.     mv RestoreRamdisk.dec ramdisk.dec
  1393.     "$dir/hfsplus" ramdisk.dec grow 18000000
  1394.  
  1395.     local rootfs_name=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "RootFS") | .filename')
  1396.     touch $ipsw_custom_part1/$rootfs_name
  1397.     log "Dummy RootFS: $rootfs_name"
  1398.  
  1399.     log "Modify options.plist"
  1400.     local options_plist="options.$device_model.plist"
  1401.     echo '<plist>
  1402. <dict>
  1403.    <key>CreateFilesystemPartitions</key>
  1404.    <false/>
  1405.    <key>UpdateBaseband</key>
  1406.    <false/>
  1407.    <key>SystemImage</key>
  1408.    <false/>
  1409. </dict>
  1410. </plist>' | tee $options_plist
  1411.     "$dir/hfsplus" ramdisk.dec rm usr/local/share/restore/$options_plist
  1412.     "$dir/hfsplus" ramdisk.dec add $options_plist usr/local/share/restore/$options_plist
  1413.  
  1414.     log "Patch ASR"
  1415.     cp ../resources/patch/old/$device_type/$vers/* .
  1416.     ipsw_patch_file ramdisk.dec usr/sbin asr asr.patch
  1417.  
  1418.     log "Repack Restore Ramdisk"
  1419.     "$dir/xpwntool" ramdisk.dec $ipsw_custom_part1/$ramdisk_name -t RestoreRamdisk.orig
  1420.  
  1421.     log "Extract all_flash from $device_base_vers base"
  1422.     file_extract_from_archive "$ipsw_base_path.ipsw" Firmware/all_flash/\* $all_flash2
  1423.  
  1424.     log "Add $device_target_vers DeviceTree to all_flash"
  1425.     rm -f $all_flash2/DeviceTree.${device_model}ap.img3
  1426.     file_extract_from_archive "$ipsw_path.ipsw" $all_flash/DeviceTree.${device_model}ap.img3 $all_flash2
  1427.  
  1428.     local ExtraArr=("--boot-partition" "--boot-ramdisk" "--logo4")
  1429.     case $device_target_vers in
  1430.         4.2.9 | 4.2.10 ) :;;
  1431.         * ) ExtraArr+=("--433");;
  1432.     esac
  1433.     local bootargs="$device_bootargs_default"
  1434.     if [[ $ipsw_verbose == 1 ]]; then
  1435.         bootargs="pio-error=0 -v"
  1436.     fi
  1437.     [[ -n $device_bootargs ]] && bootargs+=" $device_bootargs"
  1438.     ExtraArr+=("-b" "$bootargs")
  1439.     patch_iboot "${ExtraArr[@]}"
  1440.  
  1441.     if [[ $device_type == "iPad1,1" && $device_target_vers == "3"* ]]; then
  1442.         cp iBoot ../saved/iPad1,1/iBoot3_$device_ecid
  1443.     elif [[ $device_type == "iPad1,1" ]]; then
  1444.         cp iBoot iBEC
  1445.         tar -cvf iBoot.tar iBEC
  1446.         iboot="iboot"
  1447.     else
  1448.         log "Add $device_target_vers iBoot to all_flash"
  1449.         cp iBoot $all_flash2/iBoot2.img3
  1450.         echo "iBoot2.img3" >> $all_flash2/manifest
  1451.     fi
  1452.  
  1453.     log "Add APTicket to all_flash"
  1454.     cat "$shsh_path" | sed '64,$d' | sed -ne '/<data>/,/<\/data>/p' | sed -e "s/<data>//" | sed "s/<\/data>//" | tr -d '[:space:]' | base64 --decode > apticket.der
  1455.     "$dir/xpwntool" apticket.der $all_flash2/applelogoT.img3 -t ../resources/firmware/src/scab_template.img3
  1456.     echo "applelogoT.img3" >> $all_flash2/manifest
  1457.  
  1458.     log "AppleLogo"
  1459.     local logo_name="$(echo $device_fw_key | $jq -j '.keys[] | select(.image == "AppleLogo") | .filename')"
  1460.     if [[ -n $ipsw_customlogo ]]; then
  1461.         ipsw_prepare_logos_convert
  1462.         mv $all_flash/$logoname $logo_name
  1463.     else
  1464.         file_extract_from_archive "$ipsw_path.ipsw" $all_flash/$logo_name
  1465.         echo "0000010: 3467" | xxd -r - $logo_name
  1466.         echo "0000020: 3467" | xxd -r - $logo_name
  1467.     fi
  1468.     log "Add AppleLogo to all_flash"
  1469.     if [[ $device_latest_vers == "5"* ]]; then
  1470.         mv $logo_name $all_flash2/applelogo4.img3
  1471.         echo "applelogo4.img3" >> $all_flash2/manifest
  1472.     else
  1473.         sed '/applelogo/d' $all_flash2/manifest > manifest
  1474.         rm $all_flash2/manifest
  1475.         echo "$logo_name" >> manifest
  1476.         mv $logo_name manifest $all_flash2/
  1477.     fi
  1478.  
  1479.     log "Creating $ipsw_custom_part1.ipsw..."
  1480.     pushd $ipsw_custom_part1 >/dev/null
  1481.     zip -r0 $ipsw_custom_part1.ipsw *
  1482.     popd >/dev/null
  1483.  
  1484.     if [[ $ipsw_skip_first == 1 ]]; then
  1485.         return
  1486.     fi
  1487.  
  1488.     # ------ part 1 (nor flash) ends here. start creating part 2 ipsw ------
  1489.     ipsw_prepare_32bit $iboot
  1490.  
  1491.     ipsw_prepare_ios4multipart_patch=1
  1492.     ipsw_prepare_multipatch
  1493. }
  1494.  
  1495. download_with_pzb() {
  1496.     local url="$1"
  1497.     local file="$2"
  1498.     local file_out="$3"
  1499.     local sha="$4"
  1500.     log "Downloading $file from: $url"
  1501.     "$dir/pzb" -g "$file" -o "$file_out" "$url"
  1502.     if [[ -n $sha ]]; then
  1503.         local sha_local="$($sha1sum "$file_out" | awk '{print $1}')"
  1504.         if [[ $sha_local != "$sha" ]]; then
  1505.             error "SHA1sum mismatch. Expected $sha, got $sha_local. Please run the script again"
  1506.         fi
  1507.     fi
  1508. }
  1509.  
  1510. ipsw_prepare_specialios7() {
  1511.     local all_flash2="$ipsw_custom/$all_flash"
  1512.     local patches="../resources/patch/touch4-ios7"
  1513.     local saves="../saved/touch4-ios7"
  1514.     local ipad1ios7="../saved/ipad1-ios7/repo"
  1515.     local kc="../saved/ipad1-ios7/kernelcache.release.n90" # iPhone3,1 7.1.2
  1516.     local ramdisk6="../saved/ipad1-ios7/048-2516-005.dmg" # iPad2,1 6.1.3
  1517.  
  1518.     if [[ -e "$ipsw_custom.ipsw" ]]; then
  1519.         log "Found existing Custom IPSW. Skipping IPSW creation."
  1520.         return
  1521.     fi
  1522.  
  1523.     if [[ $device_type == "iPad1,1" ]]; then
  1524.         log "Preparing files"
  1525.         mkdir -p $ipad1ios7
  1526.         if [[ ! -s $kc ]]; then
  1527.             local sha="b7b8771c6b54f472b1342f5cb92e3cf730ac9ef7"
  1528.             log "Downloading iPhone3,1 7.1.2 kernelcache"
  1529.             download_with_pzb "https://secure-appldnld.apple.com/iOS7.1/031-4812.20140627.cq6y8/iPhone3,1_7.1.2_11D257_Restore.ipsw" $(basename $kc) kc $sha
  1530.             mv kc $kc
  1531.         fi
  1532.         if [[ ! -s $ramdisk6 ]]; then
  1533.             local sha="dcb5760e02a2abc4cfec610cca8c359179c3eebc"
  1534.             log "Downloading iPad2,1 6.1.3 ramdisk"
  1535.             download_with_pzb "https://secure-appldnld.apple.com/iOS6.1/091-2397.20130319.EEae9/iPad2,1_6.1.3_10B329_Restore.ipsw" $(basename $ramdisk6) ramdisk6 $sha
  1536.             mv ramdisk6 $ramdisk6
  1537.         fi
  1538.  
  1539.         log "Preparing ipad-1-ios-7"
  1540.         if [[ ! -s $ipad1ios7/run.tool ]]; then
  1541.             local repo="https://github.com/amy-and-nicole/ipad-1-ios-7"
  1542.             rm -rf $ipad1ios7
  1543.             log "git clone: $repo"
  1544.             git clone $repo $ipad1ios7
  1545.         fi
  1546.         download_sundancerepo
  1547.  
  1548.     else # iPod4,1
  1549.         mkdir -p $saves/$device_target_build
  1550.     fi
  1551.  
  1552.     log "Preparing custom IPSW..."
  1553.     mkdir -p $ipsw_custom/Firmware/dfu $ipsw_custom/Downgrade $all_flash2
  1554.  
  1555.     local comps=("iBSS" "iBEC" "DeviceTree" "Kernelcache" "RestoreRamdisk"
  1556.         "AppleLogo" "BatteryCharging0" "BatteryCharging1" "BatteryFull" "BatteryLow0" "BatteryLow1"
  1557.         "GlyphCharging" "GlyphPlugin" "iBoot" "LLB" "RecoveryMode")
  1558.     local name
  1559.     local iv
  1560.     local key
  1561.     local path
  1562.     device_fw_key_check base
  1563.  
  1564.     log "Getting base ($device_base_vers) restore components"
  1565.     for getcomp in "${comps[@]}"; do
  1566.         name=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  1567.         iv=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  1568.         key=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  1569.         case $getcomp in
  1570.             "iBSS" | "iBEC" ) path="Firmware/dfu/";;
  1571.             "Kernelcache" | "RestoreRamdisk" ) path="";;
  1572.             * ) path="$all_flash/";;
  1573.         esac
  1574.         log "$getcomp"
  1575.         file_extract_from_archive "$ipsw_base_path.ipsw" ${path}$name
  1576.         case $getcomp in
  1577.             "DeviceTree" )
  1578.                 cp $name $all_flash2/
  1579.                 "$dir/xpwntool" $name $ipsw_custom/Downgrade/RestoreDeviceTree -iv $iv -k $key -decrypt
  1580.             ;;
  1581.             "Kernelcache" )
  1582.                 "$dir/xpwntool" $name $ipsw_custom/Downgrade/RestoreKernelCache -iv $iv -k $key -decrypt
  1583.             ;;
  1584.             "AppleLogo" )
  1585.                 cp $name $all_flash2/
  1586.                 "$dir/xpwntool" $name $ipsw_custom/Downgrade/RestoreLogo -iv $iv -k $key -decrypt
  1587.             ;;
  1588.             "iBSS" | "iBEC" | "RestoreRamdisk" )
  1589.                 mv $name $getcomp.orig
  1590.                 "$dir/xpwntool" $getcomp.orig $getcomp.dec -iv $iv -k $key
  1591.             ;;
  1592.             * ) mv $name $all_flash2/;;
  1593.         esac
  1594.     done
  1595.  
  1596.     log "Patch iBSS"
  1597.     "$dir/iBoot32Patcher" iBSS.dec iBSS.patched --rsa
  1598.     "$dir/xpwntool" iBSS.patched $ipsw_custom/Firmware/dfu/iBSS.${device_model}ap.RELEASE.dfu -t iBSS.orig
  1599.  
  1600.     log "Patch iBEC"
  1601.     if [[ $device_type == "iPad1,1" ]]; then
  1602.         $bspatch iBEC.dec iBEC.patched $patches/iBEC.k48ap.RELEASE.patch
  1603.         "$dir/xpwntool" iBEC.patched $ipsw_custom/Firmware/dfu/iBEC.${device_model}ap.RELEASE.dfu -t iBEC.orig
  1604.     else # iPod4,1
  1605.         "$dir/iBoot32Patcher" iBEC.dec iBEC.patched --rsa --debug --ticket -b "rd=md0 -v amfi=0xff cs_enforcement_disable=1"
  1606.         "$dir/xpwntool" iBEC.patched $ipsw_custom/Firmware/dfu/iBEC.${device_model}ap.RELEASE.dfu -t iBEC.orig # restore ibec
  1607.         "$dir/iBoot32Patcher" iBEC.dec iBEC.patched --rsa --debug --ticket -b "-v amfi=0xff cs_enforcement_disable=1"
  1608.         "$dir/xpwntool" iBEC.patched $saves/pwnediBEC.dfu -t iBEC.orig # boot ibec
  1609.     fi
  1610.  
  1611.     log "Base manifest plist"
  1612.     file_extract_from_archive "$ipsw_base_path.ipsw" BuildManifest.plist
  1613.     $PlistBuddy -c "Set BuildIdentities:0:Manifest:RestoreDeviceTree:Info:Path Downgrade/RestoreDeviceTree" BuildManifest.plist
  1614.     $PlistBuddy -c "Set BuildIdentities:0:Manifest:RestoreKernelCache:Info:Path Downgrade/RestoreKernelCache" BuildManifest.plist
  1615.     $PlistBuddy -c "Set BuildIdentities:0:Manifest:RestoreLogo:Info:Path Downgrade/RestoreLogo" BuildManifest.plist
  1616.     $PlistBuddy -c "Set ProductBuildVersion $device_target_build" BuildManifest.plist
  1617.     $PlistBuddy -c "Set ProductVersion $device_target_vers" BuildManifest.plist
  1618.     cp BuildManifest.plist $ipsw_custom/
  1619.  
  1620.     local ramdisk_name=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .filename')
  1621.     log "Restore Ramdisk: $ramdisk_name"
  1622.     if [[ $device_type == "iPad1,1" ]]; then
  1623.         # iPad2,1 6.1.3
  1624.         "$dir/xpwntool" $ramdisk6 ramdisk.dec -iv 8775b711d2e09e332f8ebfbebe63cce7 -k d406dc4343eedf9d6567e8303ba39a21f81f99bf701840c888963af58a84fb8f
  1625.     else # iPod4,1
  1626.         mv RestoreRamdisk.dec ramdisk.dec
  1627.     fi
  1628.     "$dir/hfsplus" ramdisk.dec grow 13000000
  1629.  
  1630.     log "Patch ASR"
  1631.     ipsw_patch_file ramdisk.dec usr/sbin asr $patches/asr.${device_model}.patch
  1632.  
  1633.     log "Modify options.plist"
  1634.     [[ $device_type == "iPod4,1" ]] && "$dir/hfsplus" ramdisk.dec rm usr/local/share/restore/options.${device_model}.plist
  1635.     "$dir/hfsplus" ramdisk.dec add $patches/options.plist usr/local/share/restore/options.${device_model}.plist
  1636.  
  1637.     if [[ $device_type == "iPad1,1" ]]; then # NyanSatan rc.boot and exploit
  1638.         log "Add exploit stuff from SundanceInH2A"
  1639.         "$dir/hfsplus" ramdisk.dec rm private/etc/rc.boot
  1640.         "$dir/hfsplus" ramdisk.dec add $sundance/rc_boot/rc.boot private/etc/rc.boot
  1641.         "$dir/hfsplus" ramdisk.dec chmod 755 private/etc/rc.boot
  1642.         "$dir/hfsplus" ramdisk.dec chown 0:0 private/etc/rc.boot
  1643.         "$dir/hfsplus" ramdisk.dec add $sundance/exploit/exploit-k48.dmg exploit.dmg
  1644.     elif [[ $ipsw_jailbreak == 1 ]]; then # touch 4 only
  1645.         ipsw_prepare_rebootsh aquila2
  1646.         log "Jailbreak stuff in ramdisk"
  1647.         "$dir/hfsplus" ramdisk.dec untar $jelbrek/daibutsu/bin.tar
  1648.         "$dir/hfsplus" ramdisk.dec mv sbin/reboot sbin/reboot_
  1649.         "$dir/hfsplus" ramdisk.dec add reboot.sh sbin/reboot
  1650.         "$dir/hfsplus" ramdisk.dec chmod 755 sbin/reboot
  1651.         "$dir/hfsplus" ramdisk.dec chown 0:0 sbin/reboot
  1652.     fi
  1653.  
  1654.     log "Repack Restore Ramdisk"
  1655.     "$dir/xpwntool" ramdisk.dec $ipsw_custom/$ramdisk_name -t RestoreRamdisk.orig
  1656.  
  1657.     local rootfs_name=$(echo $device_fw_key_base | $jq -j '.keys[] | select(.image == "RootFS") | .filename')
  1658.     log "Base RootFS: $rootfs_name"
  1659.  
  1660.     log "Target manifest plist"
  1661.     rm BuildManifest.plist
  1662.     file_extract_from_archive "$ipsw_path.ipsw" BuildManifest.plist
  1663.     local rootfs_target_name=$($PlistBuddy -c "Print BuildIdentities:0:Manifest:OS:Info:Path" BuildManifest.plist | tr -d '"')
  1664.     local rootfs_target_key
  1665.     local kc_iv
  1666.     local kc_key
  1667.     case $device_type_special in
  1668.         iPad2,1 )
  1669.             # iPad2,1 7.1.2
  1670.             rootfs_target_key="2ce48d3e6cbd6fd68c775f2f0261e205f27c78280035bb6bffadccfbec44f4d890bd34b9"
  1671.             # iPad1,1 5.1.1
  1672.             kc_iv="140984858e5c7fe245a43596f5370a0e"
  1673.             kc_key="fc6733108c9bd4b2179257102f81998089437c2c58cbdb8752fd40a442bd9434"
  1674.         ;;
  1675.         iPhone3,3 )
  1676.             # iPhone3,3 7.1.2
  1677.             rootfs_target_key="423b3503689b7058d1398d1b5d56a7b1ccf4d79e1c3e6ba853122b4f86820a9e3bc911f6"
  1678.             kc_iv="b84212f017d5ffd962db0bbe050581dc"
  1679.             kc_key="92e5720cadf724cdf428d44119b634ab3346aef1ab4e3e20abc8ecb73f7f8642"
  1680.         ;;
  1681.     esac
  1682.     local rootfs_target_size=1589
  1683.     rootfs_target_size=$((rootfs_target_size*1024*1024))
  1684.  
  1685.     if [[ $device_type == "iPad1,1" ]]; then
  1686.         log "Target kernelcache"
  1687.         cp $kc $ipsw_custom/kernelcache.release.k48
  1688.         log "Restore kernelcache"
  1689.         file_extract_from_archive "$ipsw_base_path.ipsw" kernelcache.release.k48
  1690.         "$dir/xpwntool" $sundance/artifacts/kernelcache.k48ap.bin $ipsw_custom/Downgrade/RestoreKernelCache -t kernelcache.release.k48 -iv $kc_iv -k $kc_key
  1691.         log "Target devicetree"
  1692.         cp $ipad1ios7/artifacts/DeviceTree.k48ap.img3 $all_flash2/
  1693.         log "Restore devicetree"
  1694.         "$dir/img3maker" -f $sundance/artifacts/DeviceTree.k48ap.bin -o $ipsw_custom/Downgrade/RestoreDeviceTree -t dtre
  1695.  
  1696.     else # iPod4,1
  1697.         log "Target kernelcache"
  1698.         file_extract_from_archive "$ipsw_path.ipsw" kernelcache.release.$device_model_special
  1699.         mv kernelcache.release.$device_model_special kc
  1700.         "$dir/xpwntool" kc kc.new -iv $kc_iv -k $kc_key -decrypt
  1701.         cp kc.new $saves/$device_target_build/kernelcache
  1702.         cp kc.new $ipsw_custom/kernelcache.release.$device_model # wont be used, but needed for restore
  1703.     fi
  1704.  
  1705.     log "Target RootFS: extracting dmg from ipsw"
  1706.     file_extract_from_archive "$ipsw_path.ipsw" $rootfs_target_name
  1707.     log "Target RootFS: extracting dmg with key $rootfs_target_key"
  1708.     "$dir/dmg" extract $rootfs_target_name rootfs.dec -k $rootfs_target_key
  1709.     if [[ $? != 0 || ! -s rootfs.dec ]]; then
  1710.         error "Failed to extract dmg. Please run the script again"
  1711.     fi
  1712.     rm $rootfs_target_name
  1713.     log "Target RootFS: growing $rootfs_target_size"
  1714.     "$dir/hfsplus" rootfs.dec grow $rootfs_target_size
  1715.  
  1716.     log "Target RootFS: untar firmwares"
  1717.     "$dir/hfsplus" rootfs.dec untar $patches/wifi.$device_model.tar
  1718.  
  1719.     local gestalt="private/var/mobile/Library/Caches/com.apple.MobileGestalt.plist"
  1720.     log "Target RootFS: adding gestalt"
  1721.     "$dir/hfsplus" rootfs.dec add $patches/gestalt.$device_model.plist $gestalt
  1722.  
  1723.     if [[ $device_type == "iPad1,1" ]]; then
  1724.         local compass="System/Library/HIDPlugins/CompassPlugIn.plugin"
  1725.         log "Target RootFS: remove CompassPlugIn"
  1726.         "$dir/hfsplus" rootfs.dec rm $compass/CompassPlugIn
  1727.  
  1728.         local dsc="System/Library/Caches/com.apple.dyld/dyld_shared_cache_armv7"
  1729.         log "Target RootFS: extracting dsc, this will take a while"
  1730.         "$dir/hfsplus" rootfs.dec extract $dsc
  1731.         log "Patching dsc, this will take a while"
  1732.         xxd dyld_shared_cache_armv7 > dyld_shared_cache_armv7.hex
  1733.         if [[ $(head -n1 dyld_shared_cache_armv7.hex | grep 00000000) ]]; then
  1734.             git apply $patches/dyld_shared_cache_armv7.patch
  1735.         else
  1736.             git apply $patches/dyld_shared_cache_armv7-oldxxd.patch
  1737.         fi
  1738.         xxd -r dyld_shared_cache_armv7.hex > dyld_shared_cache_armv7.patched
  1739.         log "Target RootFS: replacing dsc, this will take a while"
  1740.         "$dir/hfsplus" rootfs.dec rm $dsc
  1741.         "$dir/hfsplus" rootfs.dec add dyld_shared_cache_armv7.patched $dsc
  1742.         "$dir/hfsplus" rootfs.dec chmod 755 $dsc
  1743.         "$dir/hfsplus" rootfs.dec chown 0:0 $dsc
  1744.         rm dyld_shared_cache_armv7*
  1745.  
  1746.         local db="System/Library/Extensions/IMGSGX535GLDriver.bundle"
  1747.         log "Target RootFS: adding iphone graphics drivers"
  1748.         "$dir/hfsplus" rootfs.dec mkdir $db
  1749.         "$dir/hfsplus" rootfs.dec add $ipad1ios7/artifacts/IMGSGX535GLDriver $db/IMGSGX535GLDriver
  1750.         "$dir/hfsplus" rootfs.dec add $ipad1ios7/artifacts/H264H2.videodecoder System/Library/VideoDecoders/H264H2.videodecoder
  1751.         "$dir/hfsplus" rootfs.dec add $ipad1ios7/artifacts/MP4VH2.videodecoder System/Library/VideoDecoders/MP4VH2.videodecoder
  1752.     fi
  1753.  
  1754.     if [[ $ipsw_jailbreak == 1 ]]; then
  1755.         log "Target RootFS: untar jailbreak bootstrap"
  1756.         cp $jelbrek/freeze.tar.gz .
  1757.         gzip -d freeze.tar.gz
  1758.         "$dir/hfsplus" rootfs.dec untar freeze.tar
  1759.         "$dir/hfsplus" rootfs.dec untar $jelbrek/LukeZGD.tar
  1760.         touch .cydia_no_stash
  1761.         "$dir/hfsplus" rootfs.dec add .cydia_no_stash .cydia_no_stash
  1762.  
  1763.         log "Target RootFS: untar jailbreak untether"
  1764.         "$dir/hfsplus" rootfs.dec untar $jelbrek/aquila_7.tar
  1765.         if [[ $device_type == "iPad1,1" ]]; then
  1766.             "$dir/hfsplus" rootfs.dec rm usr/lib/libmis.dylib
  1767.             "$dir/hfsplus" rootfs.dec mv aquila usr/libexec/dirhelper
  1768.         fi
  1769.  
  1770.         if [[ $ipsw_openssh == 1 ]]; then
  1771.             log "Target RootFS: untar jailbreak openssh"
  1772.             cp $jelbrek/openssh.tar.gz $jelbrek/openssl.tar.gz .
  1773.             gzip -d openssh.tar.gz
  1774.             gzip -d openssl.tar.gz
  1775.             "$dir/hfsplus" rootfs.dec untar $jelbrek/sshdeb.tar
  1776.             "$dir/hfsplus" rootfs.dec untar openssh.tar
  1777.             "$dir/hfsplus" rootfs.dec untar openssl.tar
  1778.         fi
  1779.     fi
  1780.  
  1781.     log "Target RootFS: building dmg as $rootfs_name"
  1782.     "$dir/dmg" build rootfs.dec $ipsw_custom/$rootfs_name
  1783.     if [[ $? != 0 || ! -s $ipsw_custom/$rootfs_name ]]; then
  1784.         error "Failed to build dmg. Please run the script again"
  1785.     fi
  1786.  
  1787.     log "Creating $ipsw_custom.ipsw..."
  1788.     pushd $ipsw_custom >/dev/null
  1789.     zip -r0 $ipsw_custom.ipsw *
  1790.     popd >/dev/null
  1791.  
  1792.     if [[ $device_type == "iPod4,1" ]]; then
  1793.         echo "device_target_build=$device_target_build" > $saves/$device_ecid
  1794.     fi
  1795. }
  1796.  
  1797. download_sundancerepo() {
  1798.     local sr="../saved/SundanceResources.b64"
  1799.     local sr_sha1="ebf508aff198fa80204bf3d6df0114e9b645f1c0"
  1800.     local sr_url="https://gist.githubusercontent.com/NyanSatan/1cf6921821484a2f8f788e567b654999/raw/54c6ad7554710af454c87ec2d99f869e6e669c99/SundanceResources.b64"
  1801.  
  1802.     log "Preparing SundanceInH2A"
  1803.  
  1804.     # handle older sundance folders/repos
  1805.     [[ -d ${sundance}_macos ]] && mv ${sundance}_macos/ ${sundance}/
  1806.     [[ -d ${sundance}_linux ]] && rm -rf ${sundance}_linux/
  1807.  
  1808.     if [[ -s $sundance/Sundancer ]]; then
  1809.         pushd $sundance >/dev/null
  1810.         git reset --hard
  1811.         git checkout master
  1812.         git pull
  1813.         popd >/dev/null
  1814.     else
  1815.         local repo="https://github.com/NyanSatan/SundanceInH2A"
  1816.         rm -rf $sundance
  1817.         log "git clone: $repo"
  1818.         git clone $repo $sundance
  1819.     fi
  1820.  
  1821.     if [[ -s $sr ]]; then
  1822.         if [[ $($sha1sum $sr 2>/dev/null | awk '{print $1}') != "$sr_sha1" ]]; then
  1823.             rm $sr
  1824.         fi
  1825.     fi
  1826.  
  1827.     if [[ ! -s $sr ]]; then
  1828.         log "Downloading resources: $(basename $sr)"
  1829.         download_from_url "$sr_url" "$sr"
  1830.     fi
  1831.  
  1832.     if [[ $($sha1sum $sr 2>/dev/null | awk '{print $1}') != "$sr_sha1" ]]; then
  1833.         rm $sr
  1834.         error "Downloading/verifying resources failed. Please run the script again"
  1835.     fi
  1836.  
  1837.     log "Preparing resources"
  1838.     if [[ $platform == "macos" ]]; then
  1839.         cat $sr | base64 --decode | tar -xvf -
  1840.     else
  1841.         base64 --decode $sr | xz -d | tar -xvf -
  1842.     fi
  1843.     mv artifacts/* $sundance/artifacts/
  1844.     mv resources/* $sundance/resources/
  1845. }
  1846.  
  1847. ipsw_prepare_sundanceinh2a() {
  1848.     local ipsw_path2="${device_type_special}_${device_target_vers}_${device_target_build}_Restore"
  1849.     local ipsw_base_path2="${device_type}_${device_base_vers}_${device_base_build}_Restore"
  1850.     local ipsw_custom2="${device_type}_${device_target_vers}_${device_target_build}_Custom"
  1851.     local jb
  1852.     [[ $ipsw_jailbreak == 1 ]] && jb="-j"
  1853.  
  1854.     if [[ -e "$ipsw_custom.ipsw" ]]; then
  1855.         log "Found existing Custom IPSW. Skipping IPSW creation."
  1856.         return
  1857.     fi
  1858.  
  1859.     download_sundancerepo
  1860.  
  1861.     log "Copying freeze.tar to Cydia.tar"
  1862.     cp $jelbrek/freeze.tar.gz .
  1863.     gzip -d freeze.tar.gz
  1864.     mv freeze.tar $sundance/resources/Cydia.tar
  1865.  
  1866.     log "Copying IPSWs..."
  1867.     cp "$ipsw_path.ipsw" "$sundance/$ipsw_path2.ipsw"
  1868.     cp "$ipsw_base_path.ipsw" "$sundance/$ipsw_base_path2.ipsw"
  1869.     log "Preparing custom IPSW..."
  1870.     pushd $sundance >/dev/null
  1871.     rm -rf "$ipsw_custom2"
  1872.     ./Sundancer $jb "$ipsw_base_path2.ipsw" "$ipsw_path2.ipsw" "$ipsw_custom2"
  1873.     rm "$ipsw_path2.ipsw" "$ipsw_base_path2.ipsw"
  1874.     if [[ ! -d "$ipsw_custom2" ]]; then
  1875.         error "Custom IPSW creation seems to have failed. Please run the script again" \
  1876.               "* If you do not have Python 3 installed, install it since SundanceInH2A requires it."
  1877.     fi
  1878.     pushd "$ipsw_custom2" >/dev/null
  1879.     zip -r0 ../../$ipsw_custom.ipsw *
  1880.     popd >/dev/null
  1881.     rm -rf "$ipsw_custom2"
  1882.     popd >/dev/null
  1883.  
  1884.     # ibec ticket patch
  1885.     log "Starting iBEC ticket patch"
  1886.     mv "$ipsw_custom.ipsw" temp.ipsw
  1887.     device_fw_key_check temp $device_base_build
  1888.     mkdir -p Firmware/dfu
  1889.     local iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "iBEC") | .iv')
  1890.     local key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "iBEC") | .key')
  1891.     local name="iBEC.${device_model}ap.RELEASE.dfu"
  1892.     file_extract_from_archive temp.ipsw Firmware/dfu/$name
  1893.     "$dir/xpwntool" $name ibec -iv $iv -k $key
  1894.     "$dir/iBoot32Patcher" ibec ibec.patched --ticket
  1895.     "$dir/img3maker" -f ibec.patched -o Firmware/dfu/$name -t ibec
  1896.     log "Add all to custom IPSW"
  1897.     zip -r0 temp.ipsw Firmware/dfu/*
  1898.     mv temp.ipsw "$ipsw_custom.ipsw"
  1899. }
  1900.  
  1901. ipsw_prepare_partition_script() {
  1902.     local file="partition"
  1903.     log "Preparing partition script"
  1904.     if [[ $ipsw_powder_ramdiskH == 1 ]]; then
  1905.         log "Using ramdiskH partition script"
  1906.         cp ../resources/firmware/src/target/iphone5/partition .
  1907.         return
  1908.     fi
  1909.     cp ../resources/firmware/src/partition .
  1910.  
  1911.     if [[ $device_base_vers == "5."* ]]; then
  1912.         local new_bs=64
  1913.         local new_value=$((new_bs*1024))
  1914.         new_bs="${new_bs}k"
  1915.         log "Changing exploit size to $new_bs for iOS 5"
  1916.         sed -i.bak 's|^Exploit_LastSector=.*|Exploit_LastSector="$(('"$new_value"'/$LogicalSector))"|' "$file"
  1917.         sed -i.bak 's|^dd of=\$exploitDisk if=/exploit bs=.* count=1$|dd of=\$exploitDisk if=/exploit bs='"$new_bs"' count=1|' "$file"
  1918.     fi
  1919.  
  1920.     if [[ $device_base_vers == "5."* || $device_type == "iPhone3,1" ]]; then
  1921.         log "Removing nvram boot-ramdisk"
  1922.         sed -i.bak '/^nvram boot-ramdisk/d' "$file"
  1923.         rm "$file.bak"
  1924.     fi
  1925. }
  1926.  
  1927. ipsw_prepare_multipatch() {
  1928.     local vers
  1929.     local build
  1930.     local options_plist
  1931.     local saved_path
  1932.     local url
  1933.     local ramdisk_name
  1934.     local rootfs_name
  1935.     local name
  1936.     local iv
  1937.     local key
  1938.     local comps=("iBSS" "iBEC" "DeviceTree" "Kernelcache" "RestoreRamdisk")
  1939.     local use_ticket=1
  1940.  
  1941.     log "Starting multipatch"
  1942.     mv "$ipsw_custom.ipsw" temp.ipsw
  1943.     rm -f asr* iBSS* iBEC* ramdisk* *.dmg
  1944.     options_plist="options.$device_model.plist"
  1945.     if [[ $device_type == "iPad1,1" && $device_target_vers == "4"* ]]; then
  1946.         use_ticket=
  1947.     elif [[ $device_target_vers == "3"* || $device_target_vers == "4"* ]]; then
  1948.         options_plist="options.plist"
  1949.         use_ticket=
  1950.     fi
  1951.  
  1952.     # for 4.2.1 and lower powdersn0w multipatch
  1953.     vers="$device_target_vers"
  1954.     build="$device_target_build"
  1955.     if [[ $ipsw_isbeta == 1 ]]; then
  1956.         vers="4.2.1"
  1957.         build="8C148"
  1958.     fi
  1959.     # for betas multipatch
  1960.     case $device_target_vers in
  1961.         4.3* ) vers="4.3.5"; build="8L1";;
  1962.         5* ) vers="5.1.1"; build="9B206";;
  1963.         6* ) vers="6.1.3"; build="10B329";;
  1964.         7* ) vers="7.1.2"; build="11D257";;
  1965.         8* ) vers="8.4.1"; build="12H321";;
  1966.         9* ) vers="9.3.5"; build="13G36";;
  1967.     esac
  1968.     # for gasgauge multipatch
  1969.     if [[ $ipsw_gasgauge_patch == 1 ]]; then
  1970.         vers="6.1.3"
  1971.         build="10B329"
  1972.         if [[ $device_type == "iPhone5,3" || $device_type == "iPhone5,4" ]]; then
  1973.             vers="7.1.2"
  1974.             build="11D257"
  1975.         fi
  1976.     fi
  1977.  
  1978.     saved_path="../saved/$device_type/$build"
  1979.     ipsw_get_url $build
  1980.     url="$ipsw_url"
  1981.     log "Extracting BuildManifest..."
  1982.     file_extract_from_archive temp.ipsw BuildManifest.plist
  1983.     ramdisk_name=$($PlistBuddy -c "Print BuildIdentities:0:Manifest:RestoreRamDisk:Info:Path" BuildManifest.plist | tr -d '"')
  1984.     rootfs_name=$($PlistBuddy -c "Print BuildIdentities:0:Manifest:OS:Info:Path" BuildManifest.plist | tr -d '"')
  1985.     log "ramdisk_name: $ramdisk_name"
  1986.     log "rootfs_name: $rootfs_name"
  1987.  
  1988.     log "Extracting ramdisk from IPSW"
  1989.     file_extract_from_archive temp.ipsw $ramdisk_name
  1990.     mv $ramdisk_name ramdisk2.orig
  1991.     "$dir/xpwntool" ramdisk2.orig ramdisk2.dec
  1992.  
  1993.     log "Checking multipatch"
  1994.     "$dir/hfsplus" ramdisk2.dec extract multipatched
  1995.     if [[ -s multipatched ]]; then
  1996.         log "Already multipatched"
  1997.         mv temp.ipsw "$ipsw_custom.ipsw"
  1998.         return
  1999.     fi
  2000.  
  2001.     if [[ $(cat BuildManifest.plist | grep -c "Downgrade") == 0 ]]; then
  2002.         local ind=(0)
  2003.         if [[ $device_proc == 6 && $target_vers_maj == 10 ]]; then
  2004.             ind+=(2 4)
  2005.             [[ $device_type == "iPhone5,"* ]] && ind+=(6)
  2006.         fi
  2007.         log "Modifying BuildManifest..."
  2008.         if [[ $platform == "macos" ]]; then
  2009.             for i in "${ind[@]}"; do
  2010.                 $PlistBuddy -c "Set BuildIdentities:$i:Manifest:RestoreDeviceTree:Info:Path Downgrade/RestoreDeviceTree" BuildManifest.plist
  2011.                 $PlistBuddy -c "Set BuildIdentities:$i:Manifest:RestoreKernelCache:Info:Path Downgrade/RestoreKernelCache" BuildManifest.plist
  2012.             done
  2013.         else
  2014.             awk -i inplace '
  2015.                /^[[:space:]]*<key>RestoreDeviceTree<\/key>/ { mode = "rdt" }
  2016.                /^[[:space:]]*<key>RestoreKernelCache<\/key>/ { mode = "rkc" }
  2017.                /^[[:space:]]*<key>Path<\/key>/ && mode {
  2018.                    print
  2019.                    getline
  2020.                    match($0, /^[[:space:]]*/)
  2021.                    indent = substr($0, RSTART, RLENGTH)
  2022.                    if (mode == "rdt") {
  2023.                        sub(/^[[:space:]]*.*/, indent "<string>Downgrade/RestoreDeviceTree</string>")
  2024.                    } else if (mode == "rkc" && $0 ~ /<string>.*kernelcache.*<\/string>/) {
  2025.                        sub(/^[[:space:]]*.*/, indent "<string>Downgrade/RestoreKernelCache</string>")
  2026.                    }
  2027.                    mode = ""
  2028.                }
  2029.                { print }
  2030.            ' BuildManifest.plist
  2031.         fi
  2032.         if [[ $device_proc == 6 && $target_vers_maj == 10 && $device_target_vers != "$device_latest_vers" ]]; then
  2033.             ipsw_bbreplace exist
  2034.         else
  2035.             zip -r0 temp.ipsw BuildManifest.plist
  2036.         fi
  2037.     fi
  2038.  
  2039.     mkdir -p $saved_path Downgrade Firmware/dfu
  2040.     device_fw_key_check temp $build
  2041.     log "Getting $vers restore components"
  2042.     for getcomp in "${comps[@]}"; do
  2043.         name=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .filename')
  2044.         iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  2045.         key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  2046.         case $getcomp in
  2047.             "iBSS" | "iBEC" ) path="Firmware/dfu/";;
  2048.             "DeviceTree" ) path="$all_flash/";;
  2049.             * ) path="";;
  2050.         esac
  2051.         log "$getcomp"
  2052.         if [[ $vers == "$device_target_vers" ]]; then
  2053.             file_extract_from_archive "$ipsw_path.ipsw" ${path}$name
  2054.         elif [[ -e $saved_path/$name ]]; then
  2055.             cp $saved_path/$name .
  2056.         else
  2057.             "$dir/pzb" -g "${path}$name" -o "$name" "$url"
  2058.             cp $name $saved_path/
  2059.         fi
  2060.         case $getcomp in
  2061.             "DeviceTree" )
  2062.                 "$dir/xpwntool" $name Downgrade/RestoreDeviceTree -iv $iv -k $key -decrypt
  2063.                 zip -r0 temp.ipsw Downgrade/RestoreDeviceTree
  2064.             ;;
  2065.             "Kernelcache" )
  2066.                 "$dir/xpwntool" $name Downgrade/RestoreKernelCache -iv $iv -k $key -decrypt
  2067.                 zip -r0 temp.ipsw Downgrade/RestoreKernelCache
  2068.             ;;
  2069.             * )
  2070.                 mv $name $getcomp.orig
  2071.                 "$dir/xpwntool" $getcomp.orig $getcomp.dec -iv $iv -k $key
  2072.             ;;
  2073.         esac
  2074.         if [[ $getcomp == "iB"* ]]; then
  2075.             local ticket=
  2076.             if [[ $getcomp == "iBEC" && $use_ticket == 1 ]]; then
  2077.                 ticket="--ticket"
  2078.             fi
  2079.             log "Patch $getcomp"
  2080.             "$dir/iBoot32Patcher" $getcomp.dec $getcomp.patched --rsa --debug $ticket -b "rd=md0 -v nand-enable-reformat=1 amfi=0xff amfi_get_out_of_my_way=1 cs_enforcement_disable=1 pio-error=0"
  2081.             "$dir/xpwntool" $getcomp.patched ${path}$name -t $getcomp.orig
  2082.             if [[ $target_vers_maj == 10 ]]; then
  2083.                 cp ${path}$name ${path}$getcomp.iphone5.RELEASE.dfu
  2084.                 cp ${path}$name ${path}$getcomp.iphone5b.RELEASE.dfu
  2085.                 cp ${path}$name ${path}$getcomp.ipad3b.RELEASE.dfu
  2086.             fi
  2087.             if (( target_vers_maj >= 8 )); then
  2088.                 cp ${path}$name ${path}$getcomp.$device_model.RELEASE.dfu
  2089.             fi
  2090.             zip -r0 temp.ipsw ${path}$getcomp*
  2091.         fi
  2092.     done
  2093.  
  2094.     log "Grow ramdisk"
  2095.     "$dir/hfsplus" RestoreRamdisk.dec grow 30000000
  2096.  
  2097.     log "Patch ASR"
  2098.     local asrpatch="../resources/firmware/FirmwareBundles/Down_${device_type}_${vers}_${build}.bundle/asr.patch"
  2099.     if [[ -s "$asrpatch" ]]; then
  2100.         cp "$asrpatch" .
  2101.         ipsw_patch_file RestoreRamdisk.dec usr/sbin asr asr.patch
  2102.     elif [[ $ipsw_gasgauge_patch == 1 ]]; then
  2103.         "$dir/hfsplus" RestoreRamdisk.dec rm usr/sbin/asr
  2104.         "$dir/hfsplus" RestoreRamdisk.dec add ../resources/patch/asr usr/sbin/asr
  2105.         "$dir/hfsplus" RestoreRamdisk.dec chmod 755 usr/sbin/asr
  2106.         if [[ $vers == "6.1.3" ]]; then
  2107.             log "Patch restored_external"
  2108.             "$dir/hfsplus" RestoreRamdisk.dec rm usr/local/bin/restored_external
  2109.             "$dir/hfsplus" RestoreRamdisk.dec add ../resources/patch/re usr/local/bin/restored_external
  2110.             "$dir/hfsplus" RestoreRamdisk.dec chmod 755 usr/local/bin/restored_external
  2111.         fi
  2112.     else
  2113.         "$dir/hfsplus" ramdisk2.dec extract usr/sbin/asr
  2114.         "$dir/hfsplus" RestoreRamdisk.dec add asr usr/sbin/asr
  2115.         "$dir/hfsplus" RestoreRamdisk.dec chmod 755 usr/sbin/asr
  2116.     fi
  2117.  
  2118.     if [[ $device_target_vers == "3."* ]]; then
  2119.         log "3.x options.plist"
  2120.         cp ../resources/firmware/src/target/${device_model}/options.plist $options_plist
  2121.     else
  2122.         log "Extract options.plist from $device_target_vers IPSW"
  2123.         "$dir/hfsplus" ramdisk2.dec extract usr/local/share/restore/$options_plist
  2124.     fi
  2125.  
  2126.     log "Modify options.plist"
  2127.     cat $options_plist | sed '$d' | sed '$d' > options2.plist # remove </dict> and </plist>
  2128.     if [[ $ipsw_prepare_ios4multipart_patch == 1 || $device_target_tethered == 1 ]]; then
  2129.         printf "<key>FlashNOR</key><false/>\n" >> options2.plist
  2130.     fi
  2131.     if [[ $device_type == "$device_disable_bbupdate" && $(cat options2.plist | grep -c "UpdateBaseband") == 0 ]]; then
  2132.         printf "<key>UpdateBaseband</key><false/>\n" >> options2.plist
  2133.     fi
  2134.     printf "</dict>\n</plist>\n" >> options2.plist
  2135.     cat options2.plist # print result
  2136.     "$dir/hfsplus" RestoreRamdisk.dec rm usr/local/share/restore/$options_plist
  2137.     "$dir/hfsplus" RestoreRamdisk.dec add options2.plist usr/local/share/restore/$options_plist
  2138.  
  2139.     if [[ $device_target_vers == "3"* ]]; then
  2140.         :
  2141.     elif [[ $device_target_powder == 1 && $device_target_vers == "4"* ]]; then
  2142.         log "Adding exploit and partition stuff"
  2143.         cp -R ../resources/firmware/src .
  2144.         "$dir/hfsplus" RestoreRamdisk.dec untar src/bin4.tar
  2145.         "$dir/hfsplus" RestoreRamdisk.dec mv sbin/reboot sbin/reboot_
  2146.         "$dir/hfsplus" RestoreRamdisk.dec add src/target/$device_model/reboot4 sbin/reboot
  2147.         "$dir/hfsplus" RestoreRamdisk.dec chmod 755 sbin/reboot
  2148.         "$dir/hfsplus" RestoreRamdisk.dec chown 0:0 sbin/reboot
  2149.     elif [[ $device_target_powder == 1 ]]; then
  2150.         local hw="$device_model"
  2151.         local base_build="11D257"
  2152.         case $device_type in
  2153.             iPhone5,[12] ) hw="iphone5";;
  2154.             iPhone5,[34] ) hw="iphone5b";;
  2155.             iPad2,[123]  ) hw="ipad2";;
  2156.             iPad2,[567]  ) hw="ipad2b";;
  2157.             iPad3,[123]  ) hw="ipad3";;
  2158.             iPad3,[456]  ) hw="ipad3b";;
  2159.         esac
  2160.         case $device_base_build in
  2161.             "11A"* | "11B"* ) base_build="11B554a";;
  2162.             "9"* ) base_build="9B206";;
  2163.         esac
  2164.         local exploit="src/target/$hw/$base_build/exploit"
  2165.         ipsw_prepare_partition_script
  2166.         log "Adding exploit and partition stuff"
  2167.         "$dir/hfsplus" RestoreRamdisk.dec untar src/bin.tar
  2168.         "$dir/hfsplus" RestoreRamdisk.dec mv sbin/reboot sbin/reboot_
  2169.         "$dir/hfsplus" RestoreRamdisk.dec add partition sbin/reboot
  2170.         "$dir/hfsplus" RestoreRamdisk.dec chmod 755 sbin/reboot
  2171.         "$dir/hfsplus" RestoreRamdisk.dec chown 0:0 sbin/reboot
  2172.         "$dir/hfsplus" RestoreRamdisk.dec add $exploit exploit
  2173.     elif [[ $ipsw_jailbreak == 1 && $device_target_vers == "8."* && $ipsw_everuntether != 1 ]] ||
  2174.          [[ $ipsw_jailbreak == 1 && $device_target_vers == "7."* ]]; then
  2175.         # daibutsu/everpwnage haxx overwrite and aquila reboot.sh
  2176.         "$dir/hfsplus" RestoreRamdisk.dec untar bin.tar
  2177.         "$dir/hfsplus" RestoreRamdisk.dec mv sbin/reboot sbin/reboot_
  2178.         "$dir/hfsplus" RestoreRamdisk.dec add reboot.sh sbin/reboot
  2179.         "$dir/hfsplus" RestoreRamdisk.dec chmod 755 sbin/reboot
  2180.         "$dir/hfsplus" RestoreRamdisk.dec chown 0:0 sbin/reboot
  2181.     fi
  2182.  
  2183.     echo "multipatched" > multipatched
  2184.     "$dir/hfsplus" RestoreRamdisk.dec add multipatched multipatched
  2185.  
  2186.     log "Repack Restore Ramdisk"
  2187.     "$dir/xpwntool" RestoreRamdisk.dec $ramdisk_name -t RestoreRamdisk.orig
  2188.     log "Add Restore Ramdisk to IPSW"
  2189.     zip -r0 temp.ipsw $ramdisk_name
  2190.  
  2191.     # 3.2.x ipad/4.2.x cdma fs workaround
  2192.     # removed back in fcdc9ec, added back for now
  2193.     case $device_target_vers in
  2194.     4.2.10 | 4.2.9 | 4.2.[876] | 3.2* | 3.1.3 )
  2195.         local ipsw_name="../${device_type}_${device_target_vers}_${device_target_build}_FS"
  2196.         local type="iPad1.1"
  2197.         [[ $device_type == "iPhone3,3" ]] && type="iPhone3.3"
  2198.         [[ $device_type == "iPod3,1" ]] && type="iPod3.1"
  2199.         local build="$device_target_build"
  2200.         local vers="$device_target_vers"
  2201.         local rootfs_name_fs="$rootfs_name"
  2202.         case $device_target_vers in
  2203.         4.2.10 | 4.2.9 )
  2204.             build="8E401"
  2205.             vers="4.2.8"
  2206.             device_fw_key_check temp $build
  2207.             rootfs_name_fs=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "RootFS") | .filename')
  2208.         esac
  2209.         local ipsw_url="https://github.com/LukeZGD/Legacy-iOS-Kit-Keys/releases/download/jailbreak/${type}_${vers}_${build}_FS2.ipsw"
  2210.         local sha1E="f4660666ce9d7bd9312d761c850fa3a1615899e9" # 3.2.2
  2211.         local sha1L="none"
  2212.         case $vers in
  2213.             4.2.10 | 4.2.[98] ) sha1E="b78fc4aba52bbf652c71cc633eccfba6d659698f";;
  2214.             4.2.7 ) sha1E="d07c841bbedae42f9ff98fa9160fc1298e6fffb2";;
  2215.             4.2.6 ) sha1E="671cbbb3964e5e5c38078577f5c2844bbe16699c";;
  2216.             3.2.1 ) sha1E="896c0344435615aee7f52fc75739241022e38fe7";;
  2217.             3.2   ) sha1E="47fdfe04ad9b65da009c834902eda3f141feac28";;
  2218.             3.1.3 ) sha1E="5500f63ff36ddf3379c66fcff26f0a6837ad522d";;
  2219.         esac
  2220.         if [[ -s "$ipsw_name.ipsw" ]]; then
  2221.             log "Verifying FS IPSW..."
  2222.             sha1L=$($sha1sum "$ipsw_name.ipsw" | awk '{print $1}')
  2223.             if [[ $sha1L != "$sha1E" ]]; then
  2224.                 log "Verifying IPSW failed. Expected $sha1E, got $sha1L"
  2225.                 log "Deleting existing custom IPSW"
  2226.                 rm "$ipsw_name.ipsw"
  2227.             fi
  2228.         fi
  2229.         if [[ ! -s "$ipsw_name.ipsw" ]]; then
  2230.             log "Downloading FS IPSW..."
  2231.             download_from_url "$ipsw_url" temp2.ipsw
  2232.             log "Getting SHA1 hash for FS IPSW..."
  2233.             sha1L=$($sha1sum temp2.ipsw | awk '{print $1}')
  2234.             if [[ $sha1L != "$sha1E" ]]; then
  2235.                 error "Verifying IPSW failed. The IPSW may be corrupted or incomplete. Please run the script again" \
  2236.                 "* SHA1sum mismatch. Expected $sha1E, got $sha1L"
  2237.             fi
  2238.             mv temp2.ipsw "$ipsw_name.ipsw"
  2239.         fi
  2240.         log "Extract RootFS from FS IPSW"
  2241.         file_extract_from_archive "$ipsw_name.ipsw" $rootfs_name_fs
  2242.         [[ $rootfs_name_fs != "$rootfs_name" ]] && mv $rootfs_name_fs $rootfs_name
  2243.         log "Add RootFS to IPSW"
  2244.         zip -r0 temp.ipsw $rootfs_name
  2245.     ;;
  2246.     esac
  2247.  
  2248.     mv temp.ipsw "$ipsw_custom.ipsw"
  2249. }
  2250.  
  2251. ipsw_prepare_tethered() {
  2252.     local name
  2253.     local iv
  2254.     local key
  2255.     local options_plist="options.$device_model.plist"
  2256.     if [[ $device_type == "iPad1,1" && $device_target_vers == "4"* ]]; then
  2257.         :
  2258.     elif [[ $device_target_vers == "3"* || $device_target_vers == "4"* ]]; then
  2259.         options_plist="options.plist"
  2260.     fi
  2261.  
  2262.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  2263.         log "Found existing Custom IPSW. Skipping IPSW creation."
  2264.         return
  2265.     fi
  2266.  
  2267.     ipsw_prepare_32bit
  2268.  
  2269.     log "Extract RestoreRamdisk and options.plist"
  2270.     mv "$ipsw_custom.ipsw" temp.ipsw
  2271.     file_extract_from_archive temp.ipsw BuildManifest.plist
  2272.     name=$($PlistBuddy -c "Print BuildIdentities:0:Manifest:RestoreRamDisk:Info:Path" BuildManifest.plist | tr -d '"')
  2273.     file_extract_from_archive temp.ipsw $name
  2274.     device_fw_key_check temp $device_target_build
  2275.     iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .iv')
  2276.     key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "RestoreRamdisk") | .key')
  2277.     mv $name ramdisk.orig
  2278.     "$dir/xpwntool" ramdisk.orig ramdisk.dec -iv $iv -k $key
  2279.     "$dir/hfsplus" ramdisk.dec extract usr/local/share/restore/$options_plist
  2280.  
  2281.     log "Modify options.plist"
  2282.     "$dir/hfsplus" ramdisk.dec rm usr/local/share/restore/$options_plist
  2283.     cat $options_plist | sed '$d' | sed '$d' > options2.plist
  2284.     printf "<key>FlashNOR</key><false/></dict>\n</plist>\n" >> options2.plist
  2285.     cat options2.plist
  2286.     "$dir/hfsplus" ramdisk.dec add options2.plist usr/local/share/restore/$options_plist
  2287.  
  2288.     log "Repack Restore Ramdisk"
  2289.     "$dir/xpwntool" ramdisk.dec $name -t ramdisk.orig
  2290.     log "Add Restore Ramdisk to IPSW"
  2291.     zip -r0 temp.ipsw $name
  2292.     mv temp.ipsw "$ipsw_custom.ipsw"
  2293. }
  2294.  
  2295. ipsw_prepare_ios4patches() {
  2296.     local comps=("iBSS" "iBEC")
  2297.     local iv
  2298.     local key
  2299.     local name
  2300.     local path="Firmware/dfu/"
  2301.     local build_id="$device_target_build"
  2302.     local ipsw_path="$ipsw_path"
  2303.     local ticket
  2304.     if [[ -n $device_type_special ]]; then
  2305.         build_id="$device_base_build"
  2306.         ipsw_path="$ipsw_base_path"
  2307.     fi
  2308.     device_fw_key_check temp $build_id
  2309.     log "Applying iBSS/iBEC patches"
  2310.     mkdir -p $all_flash $path
  2311.     for getcomp in "${comps[@]}"; do
  2312.         iv=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .iv')
  2313.         key=$(echo $device_fw_key_temp | $jq -j '.keys[] | select(.image == "'$getcomp'") | .key')
  2314.         name="$getcomp.${device_model}ap.RELEASE.dfu"
  2315.         log "Patch $getcomp"
  2316.         file_extract_from_archive "$ipsw_path.ipsw" ${path}$name
  2317.         mv $name $getcomp.orig
  2318.         "$dir/xpwntool" $getcomp.orig $getcomp.dec -iv $iv -k $key
  2319.         ticket=
  2320.         if [[ $getcomp == "iBEC" ]] && (( target_vers_maj >= 5 )); then
  2321.             ticket="--ticket"
  2322.         fi
  2323.         "$dir/iBoot32Patcher" $getcomp.dec $getcomp.patched --rsa --debug $ticket -b "rd=md0 -v amfi=0xff cs_enforcement_disable=1 pio-error=0"
  2324.         "$dir/xpwntool" $getcomp.patched ${path}$name -t $getcomp.orig
  2325.     done
  2326. }
  2327.  
  2328. ipsw_prepare_ios4powder() {
  2329.     local ExtraArgs="-apticket $shsh_path"
  2330.     local JBFiles=()
  2331.     ipsw_prepare_usepowder=1
  2332.  
  2333.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  2334.         log "Found existing Custom IPSW. Skipping IPSW creation."
  2335.         return
  2336.     fi
  2337.  
  2338.     if [[ $ipsw_jailbreak == 1 ]]; then
  2339.         JBFiles=("aquila_4.tar" "fstab_old.tar" "cydiasubstrate.tar" "freeze.tar")
  2340.         for i in {0..2}; do
  2341.             JBFiles[i]=$jelbrek/${JBFiles[$i]}
  2342.         done
  2343.         if [[ $ipsw_openssh == 1 ]]; then
  2344.             cp $jelbrek/openssh.tar.gz $jelbrek/openssl.tar.gz .
  2345.             gzip -d openssh.tar.gz
  2346.             gzip -d openssl.tar.gz
  2347.             JBFiles+=("$jelbrek/sshdeb.tar" "openssh.tar" "openssl.tar")
  2348.         fi
  2349.         cp $jelbrek/freeze.tar.gz .
  2350.         gzip -d freeze.tar.gz
  2351.     fi
  2352.  
  2353.     ipsw_prepare_bundle target
  2354.     ipsw_prepare_bundle base
  2355.     ipsw_prepare_logos_convert
  2356.     cp -R ../resources/firmware/src .
  2357.     mv src/target/$device_model/reboot4 partition
  2358.     rm src/bin.tar
  2359.     mv src/bin4.tar src/bin.tar
  2360.     ipsw_prepare_config false true
  2361.     if [[ $ipsw_memory == 1 ]]; then
  2362.         ExtraArgs+=" -memory"
  2363.     fi
  2364.     if [[ $device_actrec == 1 ]]; then
  2365.         ExtraArgs+=" ../saved/$device_type/activation-$device_ecid.tar"
  2366.     fi
  2367.  
  2368.     local ExtraArr=("--boot-partition" "--boot-ramdisk" "--logo4")
  2369.     case $device_target_vers in
  2370.         4.3.[45] ) :;;
  2371.         * ) ExtraArr+=("--433");;
  2372.     esac
  2373.     local bootargs="$device_bootargs_default"
  2374.     if [[ $ipsw_verbose == 1 ]]; then
  2375.         bootargs="pio-error=0 -v"
  2376.     fi
  2377.     [[ -n $device_bootargs ]] && bootargs+=" $device_bootargs"
  2378.     ExtraArr+=("-b" "$bootargs")
  2379.     patch_iboot "${ExtraArr[@]}"
  2380.  
  2381.     tar -rvf src/bin.tar iBoot
  2382.     if [[ $device_type == "iPad1,1" ]]; then
  2383.         cp iBoot iBEC
  2384.         tar -cvf iBoot.tar iBEC
  2385.         ExtraArgs+=" iBoot.tar"
  2386.     fi
  2387.     if [[ $ipsw_isbeta == 1 ]]; then
  2388.         ipsw_prepare_systemversion
  2389.         ExtraArgs+=" systemversion.tar"
  2390.     fi
  2391.  
  2392.     log "Preparing custom IPSW: $dir/powdersn0w $ipsw_path.ipsw temp.ipsw -base $ipsw_base_path.ipsw $ExtraArgs ${JBFiles[*]}"
  2393.     "$dir/powdersn0w" "$ipsw_path.ipsw" temp.ipsw -base "$ipsw_base_path.ipsw" $ExtraArgs ${JBFiles[@]}
  2394.  
  2395.     if [[ ! -e temp.ipsw ]]; then
  2396.         if [[ $platform == "macos" && $platform_arch == "arm64" ]]; then
  2397.             warn "Updating to macOS 12.6 or newer is recommended for Apple Silicon Macs to resolve issues."
  2398.         fi
  2399.         error "Failed to find custom IPSW. Please run the script again" \
  2400.               "* You may try selecting N for memory option"
  2401.     fi
  2402.  
  2403.     ipsw_prepare_ios4patches
  2404.     if [[ -n $ipsw_customlogo ]]; then
  2405.         ipsw_prepare_logos_add
  2406.     else
  2407.         log "Patch AppleLogo"
  2408.         local applelogo_name=$(echo "$device_fw_key" | $jq -j '.keys[] | select(.image == "AppleLogo") | .filename')
  2409.         file_extract_from_archive temp.ipsw $all_flash/$applelogo_name
  2410.         echo "0000010: 3467" | xxd -r - $applelogo_name
  2411.         echo "0000020: 3467" | xxd -r - $applelogo_name
  2412.         mv $applelogo_name $all_flash/$applelogo_name
  2413.     fi
  2414.  
  2415.     log "Add all to custom IPSW"
  2416.     if [[ $device_type != "iPad1,1" ]]; then
  2417.         cp iBoot $all_flash/iBoot2.${device_model}ap.RELEASE.img3
  2418.     fi
  2419.     zip -r0 temp.ipsw $all_flash/* Firmware/dfu/* $ramdisk_name
  2420.  
  2421.     mv temp.ipsw "$ipsw_custom.ipsw"
  2422. }
  2423.  
  2424. ipsw_prepare_powder() {
  2425.     local ExtraArgs
  2426.     local JBFiles=()
  2427.  
  2428.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  2429.         log "Found existing Custom IPSW. Skipping IPSW creation."
  2430.         return
  2431.     fi
  2432.     ipsw_prepare_usepowder=1
  2433.  
  2434.     ipsw_prepare_bundle target
  2435.     ipsw_prepare_bundle base
  2436.     ipsw_prepare_logos_convert
  2437.     cp -R ../resources/firmware/src .
  2438.     if [[ $ipsw_memory == 1 ]]; then
  2439.         ExtraArgs+=" -memory"
  2440.     fi
  2441.     if [[ $device_use_bb != 0 && $device_type != "$device_disable_bbupdate" ]]; then
  2442.         ExtraArgs+=" -bbupdate"
  2443.     fi
  2444.  
  2445.     if [[ $ipsw_jailbreak == 1 ]]; then
  2446.         case $device_target_vers in
  2447.             7.* ) JBFiles=("aquila_7.tar");;
  2448.             5.* ) JBFiles=("aquila_5.tar");;
  2449.         esac
  2450.  
  2451.         # temporary measure for a5 ios 5
  2452.         if [[ $device_proc == 5 && $device_target_vers == "5."* ]]; then
  2453.             JBFiles=("g1lbertJB/${device_type}_${device_target_build}.tar")
  2454.         fi
  2455.  
  2456.         if [[ -n ${JBFiles[0]} ]]; then
  2457.             JBFiles[0]=$jelbrek/${JBFiles[0]}
  2458.         fi
  2459.         case $device_target_vers in
  2460.             9.* ) JBFiles+=("$jelbrek/zebra.tar");;
  2461.             5.* ) JBFiles+=("$jelbrek/cydiasubstrate.tar");;
  2462.         esac
  2463.         case $device_target_vers in
  2464.             [689].* ) :;;
  2465.             * ) JBFiles+=("freeze.tar");;
  2466.         esac
  2467.         if [[ $ipsw_openssh == 1 ]]; then
  2468.             cp $jelbrek/openssh.tar.gz $jelbrek/openssl.tar.gz .
  2469.             gzip -d openssh.tar.gz
  2470.             gzip -d openssl.tar.gz
  2471.             JBFiles+=("$jelbrek/sshdeb.tar" "openssh.tar" "openssl.tar")
  2472.         fi
  2473.         JBFiles+=("$jelbrek/LukeZGD.tar")
  2474.         cp $jelbrek/freeze.tar.gz .
  2475.         gzip -d freeze.tar.gz
  2476.     fi
  2477.  
  2478.     local ExtraArr=("--boot-partition" "--boot-ramdisk")
  2479.     local bootargs="$device_bootargs_default"
  2480.     if [[ $ipsw_verbose == 1 ]]; then
  2481.         bootargs="pio-error=0 -v"
  2482.     fi
  2483.     case $device_target_vers in
  2484.         [789]* ) :;;
  2485.         * ) ExtraArr+=("--logo");;
  2486.     esac
  2487.     if [[ $device_type == "iPhone5,3" || $device_type == "iPhone5,4" ]] && [[ $device_base_vers == "7.0"* ]]; then
  2488.         ipsw_powder_5c70=1
  2489.     fi
  2490.     if [[ $device_type == "iPhone5"* && $ipsw_powder_5c70 != 1 ]]; then
  2491.         # do this stuff because these use ramdiskH (jump to /boot/iBEC) instead of ramdiskI (jump ibot to ibob)
  2492.         ipsw_powder_ramdiskH=1
  2493.         if [[ $device_target_vers == "9"* ]]; then
  2494.             ExtraArr[0]+="9"
  2495.         fi
  2496.         if [[ $ipsw_jailbreak == 1 && $device_target_vers != "7"* ]]; then
  2497.             bootargs+=" cs_enforcement_disable=1 amfi_get_out_of_my_way=1 amfi=0xff"
  2498.         fi
  2499.         ExtraArr+=("-b" "$bootargs")
  2500.         patch_iboot "${ExtraArr[@]}"
  2501.         tar -cvf iBoot.tar iBoot
  2502.         ExtraArgs+=" iBoot.tar"
  2503.     elif [[ $device_type == "iPad1,1" ]]; then
  2504.         # ipad 1 ramdiskH jumps to /iBEC instead
  2505.         ExtraArr+=("-b" "$bootargs")
  2506.         patch_iboot "${ExtraArr[@]}"
  2507.         mv iBoot iBEC
  2508.         tar -cvf iBoot.tar iBEC
  2509.         ExtraArgs+=" iBoot.tar"
  2510.     fi
  2511.  
  2512.     if [[ $ipsw_isbeta == 1 ]]; then
  2513.         ipsw_prepare_systemversion
  2514.         ExtraArgs+=" systemversion.tar"
  2515.     fi
  2516.     if [[ $device_type == "$device_disable_bbupdate" && $device_deadbb != 1 ]]; then
  2517.         ExtraArgs+=" ../saved/$device_type/baseband-$device_ecid.tar"
  2518.     fi
  2519.     if [[ $device_actrec == 1 ]]; then
  2520.         ExtraArgs+=" ../saved/$device_type/activation-$device_ecid.tar"
  2521.     fi
  2522.     ipsw_prepare_partition_script
  2523.  
  2524.     log "Preparing custom IPSW: $dir/powdersn0w $ipsw_path.ipsw temp.ipsw -base $ipsw_base_path.ipsw $ExtraArgs ${JBFiles[*]}"
  2525.     "$dir/powdersn0w" "$ipsw_path.ipsw" temp.ipsw -base "$ipsw_base_path.ipsw" $ExtraArgs ${JBFiles[@]}
  2526.  
  2527.     if [[ ! -e temp.ipsw ]]; then
  2528.         if [[ $platform == "macos" && $platform_arch == "arm64" ]]; then
  2529.             warn "Updating to macOS 12.6 or newer is recommended for Apple Silicon Macs to resolve issues."
  2530.         fi
  2531.         error "Failed to find custom IPSW. Please run the script again" \
  2532.               "* You may try selecting N for memory option"
  2533.     fi
  2534.  
  2535.     if [[ $device_type != "iPhone5"* && $device_type != "iPad1,1" ]] || [[ $ipsw_powder_5c70 == 1 ]]; then
  2536.         case $device_target_vers in
  2537.             [789]* ) :;;
  2538.             * )
  2539.                 patch_iboot --logo
  2540.                 mkdir -p $all_flash
  2541.                 mv iBoot*.img3 $all_flash
  2542.                 zip -r0 temp.ipsw $all_flash/iBoot*.img3
  2543.             ;;
  2544.         esac
  2545.     fi
  2546.     ipsw_prepare_logos_add
  2547.     ipsw_bbreplace
  2548.  
  2549.     mv temp.ipsw "$ipsw_custom.ipsw"
  2550. }
  2551.  
  2552. ipsw_prepare_patchcomp() {
  2553.     local path="$all_flash/"
  2554.     local name="LLB.${device_model}ap.RELEASE"
  2555.     local name41
  2556.     local ext="img3"
  2557.     local patch
  2558.     local iv
  2559.     local key
  2560.  
  2561.     if [[ $1 == "WTF2" ]]; then
  2562.         path="Firmware/dfu/"
  2563.         name="WTF.s5l8900xall.RELEASE"
  2564.         ext="dfu"
  2565.     elif [[ $1 == "iBoot" ]]; then
  2566.         name="iBoot.${device_model}ap.RELEASE"
  2567.     elif [[ $1 == "iB"* ]]; then
  2568.         path="Firmware/dfu/"
  2569.         name="$1.${device_model}ap.RELEASE"
  2570.         ext="dfu"
  2571.     elif [[ $1 == "RestoreRamdisk" ]]; then
  2572.         path=
  2573.         name="018-6494-014"
  2574.         ext="dmg"
  2575.         iv=25e713dd5663badebe046d0ffa164fee
  2576.         key=7029389c2dadaaa1d1e51bf579493824
  2577.         if [[ $device_target_vers == "4"* ]]; then
  2578.             name="018-7079-079"
  2579.             iv=a0fc6ca4ef7ef305d975e7f881ddcc7f
  2580.             key=18eab1ba646ae018b013bc959001fbde
  2581.             if [[ $device_target_vers == "4.2.1" ]]; then
  2582.                 name41="$name"
  2583.                 name="038-0029-002"
  2584.             fi
  2585.         fi
  2586.     elif [[ $1 == "RestoreDeviceTree" ]]; then
  2587.         name="DeviceTree.${device_model}ap"
  2588.     elif [[ $1 == "RestoreKernelCache" ]]; then
  2589.         path=
  2590.         name="kernelcache.release"
  2591.         ext="$device_model"
  2592.     fi
  2593.     patch="../resources/firmware/FirmwareBundles/Down_${device_type}_${device_target_vers}_${device_target_build}.bundle/$name.patch"
  2594.     local saved_path="../saved/$device_type/8B117"
  2595.     if [[ $1 == "RestoreRamdisk" ]]; then
  2596.         local ivkey
  2597.         if [[ $device_target_vers == "4"* || $device_type == *"1,1" ]]; then
  2598.             ivkey="-iv $iv -k $key"
  2599.         fi
  2600.         log "Patch $1"
  2601.         if [[ $device_target_vers == "4.2.1" ]]; then
  2602.             mkdir -p $saved_path
  2603.             if [[ -s $saved_path/$name41.$ext ]]; then
  2604.                 cp $saved_path/$name41.$ext $name.$ext
  2605.             else
  2606.                 ipsw_get_url 8B117
  2607.                 "$dir/pzb" -g $name41.$ext -o $name.$ext "$ipsw_url"
  2608.                 cp $name.$ext $saved_path/$name41.$ext
  2609.             fi
  2610.         else
  2611.             file_extract_from_archive "$ipsw_path.ipsw" $name.$ext
  2612.         fi
  2613.         mv $name.$ext rd.orig
  2614.         "$dir/xpwntool" rd.orig rd.dec -iv $iv -k $key
  2615.         $bspatch rd.dec rd.patched "$patch"
  2616.         "$dir/xpwntool" rd.patched $name.$ext -t rd.orig $ivkey
  2617.         zip -r0 temp.ipsw $name.$ext
  2618.         return
  2619.     fi
  2620.     log "Patch $1"
  2621.     if [[ $device_target_vers == "4.2.1" ]] && [[ $1 == "RestoreDeviceTree" || $1 == "RestoreKernelCache" ]]; then
  2622.         mkdir -p $saved_path
  2623.         if [[ -s $saved_path/$name.$ext ]]; then
  2624.             cp $saved_path/$name.$ext $name.$ext
  2625.         else
  2626.             ipsw_get_url 8B117
  2627.             "$dir/pzb" -g ${path}$name.$ext -o $name.$ext "$ipsw_url"
  2628.             cp $name.$ext $saved_path/$name.$ext
  2629.         fi
  2630.         mkdir -p Downgrade
  2631.         if [[ $1 == "RestoreKernelCache" ]]; then
  2632.             local ivkey="-iv 7238dcea75bf213eff209825a03add51 -k 0295d4ef87b9db687b44f54c8585d2b6"
  2633.             "$dir/xpwntool" $name.$ext kernelcache $ivkey
  2634.             $bspatch kernelcache kc.patched ../resources/firmware/FirmwareBundles/Down_iPhone1,2_4.1_8B117.bundle/kernelcache.release.patch
  2635.             "$dir/xpwntool" kc.patched Downgrade/$1 -t $name.$ext $ivkey
  2636.         else
  2637.             mv $name.$ext Downgrade/$1
  2638.         fi
  2639.         zip -r0 temp.ipsw Downgrade/$1
  2640.         return
  2641.     else
  2642.         file_extract_from_archive "$ipsw_path.ipsw" ${path}$name.$ext
  2643.     fi
  2644.     $bspatch $name.$ext $name.patched $patch
  2645.     mkdir -p $path
  2646.     mv $name.patched ${path}$name.$ext
  2647.     zip -r0 temp.ipsw ${path}$name.$ext
  2648. }
  2649.  
  2650. ipsw_prepare_s5l8900() {
  2651.     local rname="018-6494-014.dmg"
  2652.     local sha1E="4f6539d2032a1c7e1a068c667e393e62d8912700"
  2653.     local sha1L="none"
  2654.     ipsw_url="https://github.com/LukeZGD/Legacy-iOS-Kit-Keys/releases/download/jailbreak/"
  2655.     if [[ $device_target_vers == "4.1" ]]; then
  2656.         rname="018-7079-079.dmg"
  2657.         sha1E="9a64eea9949b720f1033d41adc85254e6dbf9525"
  2658.     elif [[ $device_target_vers == "4.2.1" ]]; then
  2659.         rname="038-0029-002.dmg"
  2660.         sha1E="9a64eea9949b720f1033d41adc85254e6dbf9525"
  2661.     elif [[ $device_type == "iPhone1,1" && $ipsw_hacktivate == 1 ]]; then
  2662.         ipsw_url+="iPhone1.1_3.1.3_7E18_CustomHJ.ipsw"
  2663.         sha1E="8140ed162c6712a6e8d1608d3a36257998253d82"
  2664.     elif [[ $device_type == "iPhone1,1" ]]; then
  2665.         ipsw_url+="iPhone1.1_3.1.3_7E18_CustomJ.ipsw"
  2666.         sha1E="4aa139672835d95bebdd2945f713321dcc4965b5"
  2667.     elif [[ $device_type == "iPod1,1" ]]; then
  2668.         ipsw_url+="iPod1.1_3.1.3_7E18_CustomJ.ipsw"
  2669.         sha1E="39d0e16536c281c3f98db91923e3d53b6fad6c6c"
  2670.     fi
  2671.  
  2672.     if [[ $device_type == "iPhone1,2" && -e "$ipsw_custom.ipsw" ]]; then
  2673.         log "Checking RestoreRamdisk hash of custom IPSW"
  2674.         file_extract_from_archive "$ipsw_custom.ipsw" $rname
  2675.         sha1L="$($sha1sum $rname | awk '{print $1}')"
  2676.     elif [[ -e "$ipsw_custom2.ipsw" ]]; then
  2677.         log "Getting SHA1 hash for $ipsw_custom2.ipsw..."
  2678.         sha1L=$($sha1sum "$ipsw_custom2.ipsw" | awk '{print $1}')
  2679.     fi
  2680.     if [[ $sha1L == "$sha1E" && $ipsw_customlogo2 == 1 ]]; then
  2681.         log "Verified existing Custom IPSW. Preparing custom logo images and IPSW"
  2682.         rm -f "$ipsw_custom.ipsw"
  2683.         cp "$ipsw_custom2.ipsw" temp.ipsw
  2684.         device_fw_key_check
  2685.         ipsw_prepare_logos_convert
  2686.         ipsw_prepare_logos_add
  2687.         mv temp.ipsw "$ipsw_custom.ipsw"
  2688.         return
  2689.     elif [[ $sha1L == "$sha1E" ]]; then
  2690.         log "Verified existing Custom IPSW. Skipping IPSW creation."
  2691.         return
  2692.     else
  2693.         log "Verifying IPSW failed. Expected $sha1E, got $sha1L"
  2694.     fi
  2695.  
  2696.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  2697.         log "Deleting existing custom IPSW"
  2698.         rm "$ipsw_custom.ipsw"
  2699.     fi
  2700.  
  2701.     if [[ $device_type != "iPhone1,2" ]]; then
  2702.         log "Downloading IPSW: $ipsw_url"
  2703.         download_from_url "$ipsw_url" temp.ipsw
  2704.         log "Getting SHA1 hash for IPSW..."
  2705.         sha1L=$($sha1sum temp.ipsw | awk '{print $1}')
  2706.         if [[ $sha1L != "$sha1E" ]]; then
  2707.             error "Verifying IPSW failed. The IPSW may be corrupted or incomplete. Please run the script again" \
  2708.                   "* SHA1sum mismatch. Expected $sha1E, got $sha1L"
  2709.         fi
  2710.         if [[ $ipsw_customlogo2 == 1 ]]; then
  2711.             cp temp.ipsw "$ipsw_custom2.ipsw"
  2712.             device_fw_key_check
  2713.             ipsw_prepare_logos_convert
  2714.             ipsw_prepare_logos_add
  2715.         fi
  2716.         mv temp.ipsw "$ipsw_custom.ipsw"
  2717.         return
  2718.     fi
  2719.  
  2720.     ipsw_prepare_jailbreak old
  2721.  
  2722.     mv "$ipsw_custom.ipsw" temp.ipsw
  2723.     ipsw_prepare_patchcomp LLB
  2724.     ipsw_prepare_patchcomp iBoot
  2725.     ipsw_prepare_patchcomp RestoreRamdisk
  2726.     if [[ $device_target_vers == "4"* ]]; then
  2727.         ipsw_prepare_patchcomp WTF2
  2728.         ipsw_prepare_patchcomp iBEC
  2729.     fi
  2730.     if [[ $device_target_vers == "4.2.1" ]]; then
  2731.         ipsw_prepare_patchcomp iBSS
  2732.         ipsw_prepare_patchcomp RestoreDeviceTree
  2733.         ipsw_prepare_patchcomp RestoreKernelCache
  2734.     fi
  2735.     mv temp.ipsw "$ipsw_custom.ipsw"
  2736. }
  2737.  
  2738. ipsw_prepare_custom() {
  2739.     if [[ -s "$ipsw_custom.ipsw" ]]; then
  2740.         log "Found existing Custom IPSW. Skipping IPSW creation."
  2741.         return
  2742.     elif [[ $device_target_vers == "4.1" && $ipsw_jailbreak != 1 ]]; then
  2743.         log "No need to create custom IPSW for non-jailbroken restores on $device_type-$device_target_build"
  2744.         return
  2745.     fi
  2746.  
  2747.     ipsw_prepare_jailbreak old
  2748.  
  2749.     mv "$ipsw_custom.ipsw" temp.ipsw
  2750.     if [[ $ipsw_24o == 1 ]]; then # old bootrom ipod2,1
  2751.         ipsw_prepare_patchcomp LLB
  2752.         mv temp.ipsw "$ipsw_custom.ipsw"
  2753.         return
  2754.     elif [[ $device_type == "iPod2,1" && $device_target_vers == "3.1.3" ]]; then # new bootrom ipod2,1 3.1.3
  2755.         mv temp.ipsw "$ipsw_custom.ipsw"
  2756.         return
  2757.     fi
  2758.  
  2759.     case $device_target_vers in
  2760.         $device_latest_vers | 4.1 ) :;;
  2761.         3.0* )
  2762.             ipsw_prepare_patchcomp LLB
  2763.             log "Patch Kernelcache"
  2764.             file_extract_from_archive "$ipsw_path.ipsw" kernelcache.release.s5l8920x
  2765.             mv kernelcache.release.s5l8920x kernelcache.orig
  2766.             $bspatch kernelcache.orig kernelcache.release.s5l8920x ../resources/firmware/FirmwareBundles/Down_iPhone2,1_${device_target_vers}_${device_target_build}.bundle/kernelcache.release.patch
  2767.             zip -r0 temp.ipsw kernelcache.release.s5l8920x
  2768.         ;;
  2769.         * )
  2770.             ipsw_prepare_patchcomp LLB
  2771.             local bootargs="$device_bootargs_default"
  2772.             if [[ $ipsw_verbose == 1 ]]; then
  2773.                 bootargs="pio-error=0 -v"
  2774.             fi
  2775.             if [[ $device_target_vers == "3"* ]]; then
  2776.                 bootargs+=" amfi=0xff cs_enforcement_disable=1"
  2777.             fi
  2778.             local path="Firmware/all_flash/all_flash.${device_model}ap.production"
  2779.             local name="iBoot.${device_model}ap.RELEASE.img3"
  2780.             patch_iboot -b "$bootargs"
  2781.             mkdir -p $path
  2782.             mv $name $path/$name
  2783.             zip -r0 temp.ipsw $path/$name
  2784.         ;;
  2785.     esac
  2786.     mv temp.ipsw "$ipsw_custom.ipsw"
  2787. }
  2788.  
  2789. ipsw_extract() {
  2790.     local ipsw="$ipsw_path"
  2791.     if [[ $1 == "custom" ]]; then
  2792.         ipsw="$ipsw_custom"
  2793.     fi
  2794.     if [[ ! -d "$ipsw" ]]; then
  2795.         mkdir -p "$ipsw"
  2796.         log "Extracting IPSW: $ipsw.ipsw"
  2797.         file_extract "$ipsw.ipsw" "$ipsw/"
  2798.     fi
  2799. }
  2800.  
  2801. restore_download_bbsep() {
  2802.     # download and check manifest, baseband, and sep to be used for restoring
  2803.     # sets variables: restore_manifest, restore_baseband, restore_sep
  2804.     local build_id
  2805.     local baseband_sha1
  2806.     local restore_baseband_check
  2807.     if [[ $device_proc == 8 || $device_latest_vers == "15"* || $device_latest_vers == "16"* || $device_checkm8ipad == 1 ]]; then
  2808.         return
  2809.     elif [[ $device_latest_vers == "$device_use_vers" || $device_target_vers == "10"* ]]; then
  2810.         build_id="$device_use_build"
  2811.         restore_baseband="$device_use_bb"
  2812.         baseband_sha1="$device_use_bb_sha1"
  2813.     else
  2814.         build_id="$device_latest_build"
  2815.         restore_baseband="$device_latest_bb"
  2816.         baseband_sha1="$device_latest_bb_sha1"
  2817.     fi
  2818.     ipsw_get_url $build_id
  2819.  
  2820.     mkdir -p tmp
  2821.     # BuildManifest
  2822.     if [[ ! -e ../saved/$device_type/$build_id.plist ]]; then
  2823.         if [[ $device_proc == 7 && $device_target_vers == "10"* ]]; then
  2824.             cp ../resources/manifest/BuildManifest_${device_type}_10.3.3.plist $build_id.plist
  2825.         else
  2826.             log "Downloading $build_id BuildManifest"
  2827.             "$dir/pzb" -g BuildManifest.plist -o $build_id.plist "$ipsw_url"
  2828.         fi
  2829.         mv $build_id.plist ../saved/$device_type
  2830.     fi
  2831.     cp ../saved/$device_type/$build_id.plist tmp/BuildManifest.plist
  2832.     if [[ $? != 0 ]]; then
  2833.         rm ../saved/$device_type/$build_id.plist
  2834.         error "An error occurred copying manifest. Please run the script again"
  2835.     fi
  2836.     log "Manifest: ../saved/$device_type/$build_id.plist"
  2837.     restore_manifest="tmp/BuildManifest.plist"
  2838.  
  2839.     # Baseband
  2840.     if [[ $restore_baseband != 0 ]]; then
  2841.         restore_baseband_check="../saved/baseband/$restore_baseband"
  2842.         if [[ -e $restore_baseband_check ]]; then
  2843.             if [[ $baseband_sha1 != "$($sha1sum $restore_baseband_check | awk '{print $1}')" ]]; then
  2844.                 rm $restore_baseband_check
  2845.             fi
  2846.         fi
  2847.         if [[ ! -e $restore_baseband_check ]]; then
  2848.             log "Downloading $build_id Baseband"
  2849.             "$dir/pzb" -g Firmware/$restore_baseband -o $restore_baseband "$ipsw_url"
  2850.             if [[ $baseband_sha1 != "$($sha1sum $restore_baseband | awk '{print $1}')" ]]; then
  2851.                 error "Downloading/verifying baseband failed. Please run the script again"
  2852.             fi
  2853.             mv $restore_baseband $restore_baseband_check
  2854.         fi
  2855.         cp $restore_baseband_check tmp/bbfw.tmp
  2856.         if [[ $? != 0 ]]; then
  2857.             rm $restore_baseband_check
  2858.             error "An error occurred copying baseband. Please run the script again"
  2859.         fi
  2860.         log "Baseband: $restore_baseband_check"
  2861.         restore_baseband="tmp/bbfw.tmp"
  2862.     fi
  2863.  
  2864.     # SEP
  2865.     if (( device_proc >= 7 )); then
  2866.         restore_sep="sep-firmware.$device_model.RELEASE"
  2867.         if [[ ! -e ../saved/$device_type/$restore_sep-$build_id.im4p ]]; then
  2868.             log "Downloading $build_id SEP"
  2869.             "$dir/pzb" -g Firmware/all_flash/$restore_sep.im4p -o $restore_sep.im4p "$ipsw_url"
  2870.             mv $restore_sep.im4p ../saved/$device_type/$restore_sep-$build_id.im4p
  2871.         fi
  2872.         restore_sep="$restore_sep-$build_id.im4p"
  2873.         cp ../saved/$device_type/$restore_sep .
  2874.         if [[ $? != 0 ]]; then
  2875.             rm ../saved/$device_type/$restore_sep
  2876.             error "An error occurred copying SEP. Please run the script again"
  2877.         fi
  2878.         log "SEP: ../saved/$device_type/$restore_sep"
  2879.     fi
  2880. }
  2881.  
  2882. restore_idevicerestore() {
  2883.     local ExtraArgs="-ew"
  2884.     local idevicerestore2="$idevicerestore"
  2885.  
  2886.     mkdir -p shsh
  2887.     cp "$shsh_path" shsh/$device_ecid-$device_type-$device_target_vers.shsh
  2888.     if [[ $device_use_bb == 0 || $device_type == "$device_disable_bbupdate" ]]; then
  2889.         log "Device $device_type has no baseband/disabled baseband update"
  2890.     fi
  2891.     ipsw_extract custom
  2892.     case $1 in
  2893.         first   ) cp "$shsh_path" shsh/$device_ecid-$device_type-5.1.1.shsh;;
  2894.         special ) cp "$shsh_path" shsh/$device_ecid-$device_type-$device_base_vers.shsh;;
  2895.     esac
  2896.     device_rd_build=
  2897.     if [[ $device_type == "iPad"* && $device_pwnrec != 1 ]] &&
  2898.        [[ $device_target_vers == "3"* || $device_target_vers == "4"* ]]; then
  2899.         if [[ $device_type == "iPad1,1" ]]; then
  2900.             patch_ibss
  2901.             log "Sending iBSS..."
  2902.             $irecovery -f pwnediBSS.dfu
  2903.             sleep 1
  2904.         fi
  2905.         log "Sending iBEC..."
  2906.         $irecovery -f "$ipsw_custom/Firmware/dfu/iBEC.${device_model}ap.RELEASE.dfu"
  2907.         device_find_mode Recovery
  2908.     fi
  2909.     if [[ $debug_mode == 1 ]]; then
  2910.         ExtraArgs+="d"
  2911.     fi
  2912.  
  2913.     log "Running idevicerestore with command: $idevicerestore2 $ExtraArgs \"$ipsw_custom.ipsw\""
  2914.     $idevicerestore2 $ExtraArgs "$ipsw_custom.ipsw"
  2915.     opt=$?
  2916.     if [[ $1 == "first" ]]; then
  2917.         return $opt
  2918.     fi
  2919.     echo
  2920.     log "Restoring done! Read the message below if any error has occurred:"
  2921.     print "* For device activation, go to: Main Menu -> Attempt Activation"
  2922.     if [[ $platform == "macos" && $platform_arch == "arm64" ]]; then
  2923.         print "* Make sure that you are using a USB dock or hub to connect your device. Do not use USB dongles."
  2924.         echo
  2925.     fi
  2926.     if [[ $device_target_vers == "3.2"* && $device_target_powder != 1 ]]; then
  2927.         log "Note: Make sure to do the following below to fix the jailbreak."
  2928.         print "* To fix this, go to: Useful Utilities -> Disable/Enable Exploit -> Disable Exploit"
  2929.     fi
  2930.     print "* Please read the \"Troubleshooting\" wiki page in GitHub before opening any issue!"
  2931.     print "* Your problem may have already been addressed within the wiki page."
  2932.     print "* If opening an issue in GitHub, please provide a FULL log/output. Otherwise, your issue may be dismissed."
  2933. }
  2934.  
  2935. restore_futurerestore() {
  2936.     local ExtraArr=()
  2937.     local futurerestore2="$futurerestore"
  2938.     local port=8888
  2939.     local opt
  2940.  
  2941.     if [[ $1 == "--use-pwndfu" ]]; then
  2942.         device_fw_key_check
  2943.         pushd ../saved >/dev/null
  2944.         log "Starting local server for firmware keys"
  2945.         "$dir/darkhttpd" . --port $port &
  2946.         httpserver_pid=$!
  2947.         log "httpserver PID: $httpserver_pid"
  2948.         popd >/dev/null
  2949.         log "Waiting for local server"
  2950.         until [[ $($curl http://127.0.0.1:$port 2>/dev/null) ]]; do
  2951.             sleep 1
  2952.         done
  2953.     fi
  2954.  
  2955.     restore_download_bbsep
  2956.     # baseband args
  2957.     if [[ $restore_baseband == 0 ]]; then
  2958.         ExtraArr+=("--no-baseband")
  2959.     else
  2960.         ExtraArr+=("-b" "$restore_baseband" "-p" "$restore_manifest")
  2961.     fi
  2962.     # sep args for 64bit
  2963.     if [[ -n $restore_sep ]]; then
  2964.         ExtraArr+=("-s" "$restore_sep" "-m" "$restore_manifest")
  2965.     fi
  2966.     if (( device_proc < 7 )); then
  2967.         futurerestore2+="_old"
  2968.     elif [[ $device_proc == 7 && $device_target_other != 1 && $device_target_vers == "10.3.3" &&
  2969.             $restore_usepwndfu64 != 1 && $platform == "linux" && $platform_arch == "arm64" ]]; then
  2970.         futurerestore2+="_new" # no futurerestore nightly build for linux arm64
  2971.     else
  2972.         futurerestore2="../saved/futurerestore_$platform"
  2973.         if [[ $target_vers_maj == 10 ]]; then
  2974.             export FUTURERESTORE_I_SOLEMNLY_SWEAR_THAT_I_AM_UP_TO_NO_GOOD=1 # required since custom-latest-ota is broken
  2975.         else
  2976.             ExtraArr=("--latest-sep")
  2977.             case $device_type in
  2978.                 iPhone* | iPad4,[235689] | iPad5,[24] | iPad6,[48] | iPad[67],12 | iPad7,[46] ) ExtraArr+=("--latest-baseband");;
  2979.                 * ) ExtraArr+=("--no-baseband");;
  2980.             esac
  2981.         fi
  2982.         if (( target_vers_maj < 16 )) && [[ $ipsw_ipx != 1 ]]; then
  2983.             ExtraArr+=("--no-rsep")
  2984.         fi
  2985.         if [[ $device_target_setnonce == 1 ]]; then
  2986.             ExtraArr+=("--set-nonce")
  2987.         elif [[ $ipsw_ipx == 1 ]]; then
  2988.             ExtraArr+=("--rdsk" "rdsk.im4p" "--rkrn" "kcache.im4p")
  2989.         fi
  2990.         log "futurerestore nightly will be used for this restore: https://github.com/futurerestore/futurerestore"
  2991.         print "* Builds from here: https://github.com/LukeeGD/futurerestore"
  2992.         if [[ $platform == "linux" && $platform_arch != "x86_64" ]]; then
  2993.             warn "futurerestore nightly is not supported on Linux $platform_arch, cannot continue. x86_64 only."
  2994.             return
  2995.         fi
  2996.         log "Checking for futurerestore updates..."
  2997.         #download_from_url "https://api.github.com/repos/futurerestore/futurerestore/commits" commits
  2998.         #local fr_latest="$(cat commits | $jq -r '.[0].sha')"
  2999.         local fr_latest="45d0267ee24854d8bb9f5dbef29c3226af0d48db"
  3000.         local fr_branch="main"
Add Comment
Please, Sign In to add comment