You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.4 KiB

  1. #!/bin/bash
  2. ### begin function definitions ###
  3. # Check that we can access the directory that ocntains this script, as well
  4. # as the root directory of the installer USB. Access to both of these
  5. # directories is vital, and Catalina's TCC controls for Terminal are
  6. # capable of blocking both. Therefore we must check access to both
  7. # directories before proceeding.
  8. checkDirAccess() {
  9. # List the two directories, but direct both stdout and stderr to
  10. # /dev/null. We are only interested in the return code.
  11. ls "$VOLUME" . &> /dev/null
  12. }
  13. ### end function definitions ###
  14. # Make sure there isn't already an "EFI" volume mounted.
  15. if [ -d "/Volumes/EFI" ]
  16. then
  17. echo 'An "EFI" volume is already mounted. Please unmount it then try again.'
  18. echo "If you don't know what this means, then restart your Mac and try again."
  19. echo
  20. echo "install-setvars cannot continue."
  21. exit 1
  22. fi
  23. # For this script, root permissions are vital.
  24. [ $UID = 0 ] || exec sudo "$0" "$@"
  25. while [[ $1 = -* ]]
  26. do
  27. case $1 in
  28. -v | --verbose)
  29. VERBOSEBOOT="YES"
  30. #echo 'Verbose boot option enabled.'
  31. ;;
  32. -e | --enable*)
  33. SIPARV="YES"
  34. ;;
  35. -d | --disable*)
  36. SIPARV="NO"
  37. ;;
  38. *)
  39. echo "Unknown command line option: $1"
  40. exit 1
  41. ;;
  42. esac
  43. shift
  44. done
  45. # Allow the user to drag-and-drop the USB stick in Terminal, to specify the
  46. # path to the USB stick in question. (Otherwise it will try hardcoded paths
  47. # for a presumed Big Sur Golden Master/public release, beta 2-or-later,
  48. # and beta 1, in that order.)
  49. if [ -z "$1" ]
  50. then
  51. for x in "Install macOS Big Sur" "Install macOS Big Sur Beta" "Install macOS Beta"
  52. do
  53. if [ -d "/Volumes/$x/$x.app" ]
  54. then
  55. VOLUME="/Volumes/$x"
  56. APPPATH="$VOLUME/$x.app"
  57. break
  58. fi
  59. done
  60. if [ ! -d "$APPPATH" ]
  61. then
  62. echo "Failed to locate Big Sur recovery USB stick."
  63. echo "Remember to create it using createinstallmedia, and do not rename it."
  64. echo "If all else fails, try specifying the path to the USB stick"
  65. echo "as a command line parameter to this script."
  66. echo
  67. echo "install-setvars cannot continue and will now exit."
  68. exit 1
  69. fi
  70. else
  71. VOLUME="$1"
  72. # The use of `echo` here is to force globbing.
  73. APPPATH=`echo -n "$VOLUME"/Install\ macOS*.app`
  74. if [ ! -d "$APPPATH" ]
  75. then
  76. echo "Failed to locate Big Sur recovery USB stick for patching."
  77. echo "Make sure you specified the correct volume. You may also try"
  78. echo "not specifying a volume and allowing the patcher to find"
  79. echo "the volume itself."
  80. echo
  81. echo "install-setvars cannot continue and will now exit."
  82. exit 1
  83. fi
  84. fi
  85. # Check if the payloads directory is inside the current directory. If not,
  86. # it's probably inside the same directory as this script, so find that
  87. # directory.
  88. if [ ! -d payloads ]
  89. then
  90. BASEDIR="`echo $0|sed -E 's@/[^/]*$@@'`"
  91. [ -z "$BASEDIR" ] || cd "$BASEDIR"
  92. fi
  93. # Check again in case we changed directory after the first check
  94. if [ ! -d payloads ]
  95. then
  96. echo '"payloads" folder was not found.'
  97. echo
  98. echo "install-setvars cannot continue and will now exit."
  99. exit 1
  100. fi
  101. # Check to make sure we can access both our own directory and the root
  102. # directory of the USB stick. Terminal's TCC permissions in Catalina can
  103. # prevent access to either of those two directories. However, only do this
  104. # check on Catalina or higher. (I can add an "else" block later to handle
  105. # Mojave and earlier, but Catalina is responsible for every single bug
  106. # report I've received due to this script lacking necessary read permissions.)
  107. if [ `uname -r | sed -e 's@\..*@@'` -ge 19 ]
  108. then
  109. echo 'Checking read access to necessary directories...'
  110. if ! checkDirAccess
  111. then
  112. echo 'Access check failed.'
  113. tccutil reset All com.apple.Terminal
  114. echo 'Retrying access check...'
  115. if ! checkDirAccess
  116. then
  117. echo
  118. echo 'Access check failed again. Giving up.'
  119. echo 'Next time, please give Terminal permission to access removable drives,'
  120. echo 'as well as the location where this patcher is stored (for example, Downloads).'
  121. exit 1
  122. else
  123. echo 'Access check succeeded on second attempt.'
  124. echo
  125. fi
  126. else
  127. echo 'Access check succeeded.'
  128. echo
  129. fi
  130. fi
  131. MOUNTEDPARTITION=`mount | fgrep "$VOLUME" | awk '{print $1}'`
  132. if [ -z "$MOUNTEDPARTITION" ]
  133. then
  134. echo Failed to find the partition that
  135. echo "$VOLUME"
  136. echo is mounted from. install-setvars cannot proceed.
  137. exit 1
  138. fi
  139. DEVICE=`echo -n $MOUNTEDPARTITION | sed -e 's/s[0-9]*$//'`
  140. PARTITION=`echo -n $MOUNTEDPARTITION | sed -e 's/^.*disk[0-9]*s//'`
  141. echo "$VOLUME found on device $MOUNTEDPARTITION"
  142. if [ "x$PARTITION" = "x1" ]
  143. then
  144. echo "The volume $VOLUME"
  145. echo "appears to be on partition 1 of the USB stick, therefore the stick is"
  146. echo "incorrectly partitioned (possibly MBR instead of GPT?)."
  147. echo
  148. echo 'Please use Disk Utility to erase the USB stick as "Mac OS Extended'
  149. echo '(Journaled)" format on "GUID Partition Map" scheme and start over with'
  150. echo '"createinstallmedia". Or for other methods, please refer to the micropatcher'
  151. echo "README for more information."
  152. echo
  153. echo "install-setvars cannot continue."
  154. exit 1
  155. fi
  156. diskutil mount ${DEVICE}s1
  157. if [ ! -d "/Volumes/EFI" ]
  158. then
  159. echo "Partition 1 of the USB stick does not appear to be an EFI partition, or"
  160. echo "mounting of the partition somehow failed."
  161. echo
  162. echo 'Please use Disk Utility to erase the USB stick as "Mac OS Extended'
  163. echo '(Journaled)" format on "GUID Partition Map" scheme and start over with'
  164. echo '"createinstallmedia". Or for other methods, please refer to the micropatcher'
  165. echo "README for more information."
  166. echo
  167. echo "install-setvars cannot continue."
  168. exit 1
  169. fi
  170. # Before proceeding with the actual installation, see if we were provided
  171. # a command line option for SIP/ARV, and if not, make a decision based
  172. # on what Mac model this is.
  173. if [ -z "$SIPARV" ]
  174. then
  175. MACMODEL=`sysctl -n hw.model`
  176. echo "Detected Mac model is:" $MACMODEL
  177. case $MACMODEL in
  178. "iMac14,1" | "iMac14,2" | "iMac14,3")
  179. echo "Late 2013 iMac detected, so enabling SIP/ARV."
  180. echo "(Use -d option to disable SIP/ARV if necessary.)"
  181. SIPARV="YES"
  182. ;;
  183. *)
  184. echo "This Mac is not a Late 2013 iMac, so disabling SIP/ARV."
  185. echo "(Use -e option to enable SIP/ARV if necessary.)"
  186. SIPARV="NO"
  187. ;;
  188. esac
  189. echo
  190. fi
  191. # Now do the actual installation
  192. echo "Installing setvars EFI utility."
  193. rm -rf /Volumes/EFI/EFI
  194. if [ "x$VERBOSEBOOT" = "xYES" ]
  195. then
  196. if [ "x$SIPARV" = "xYES" ]
  197. then
  198. echo 'Verbose boot enabled, SIP/ARV enabled'
  199. cp -r setvars/EFI-enablesiparv-vb /Volumes/EFI/EFI
  200. else
  201. echo 'Verbose boot enabled, SIP/ARV disabled'
  202. cp -r setvars/EFI-verboseboot /Volumes/EFI/EFI
  203. fi
  204. elif [ "x$SIPARV" = "xYES" ]
  205. then
  206. echo 'Verbose boot disabled, SIP/ARV enabled'
  207. cp -r setvars/EFI-enablesiparv /Volumes/EFI/EFI
  208. else
  209. echo 'Verbose boot disabled, SIP/ARV disabled'
  210. cp -r setvars/EFI /Volumes/EFI/EFI
  211. fi
  212. echo "Unmounting EFI volume (if this fails, just eject in Finder afterward)."
  213. umount /Volumes/EFI || diskutil unmount /Volumes/EFI
  214. echo
  215. echo 'install-setvars finished.'