fakemurk.sh.pre 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "fakemurk_lib.sh.pre"
  2. drop_image_patcher(){
  3. base64 -d <<-EOF | bunzip2 -dc >"$ROOT/sbin/image_patcher.sh"
  4. #include "image_patcher.sh.b64"
  5. EOF
  6. chmod 777 "$ROOT/sbin/image_patcher.sh"
  7. }
  8. is_target_booted() {
  9. [ -z "$COMPAT" ] && [ "$(get_booted_kernnum)" == "$TGT_KERNNUM" ]
  10. }
  11. opposite_num() {
  12. if [ "$1" == "2" ]; then
  13. echo -n 4
  14. elif [ "$1" == "4" ]; then
  15. echo -n 2
  16. elif [ "$1" == "3" ]; then
  17. echo -n 5
  18. elif [ "$1" == "5" ]; then
  19. echo -n 3
  20. else
  21. return 1
  22. fi
  23. }
  24. prepare_target_root() {
  25. sleep 2
  26. if verity_enabled_for_n "$TGT_KERNNUM"; then
  27. echo "removing rootfs verification on target kernel $TGT_KERN_DEV"
  28. /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions "$TGT_KERNNUM" -i "$DST" 2>/dev/null
  29. if is_target_booted; then
  30. # if we're booted from the target kernel, we need to reboot. this is a pretty rare circumstance
  31. cat <<-EOF
  32. ROOTFS VERIFICATION SUCCESSFULLY REMOVED
  33. IN ORDER TO PROCCEED, THE CHROMEBOOK MUST BE REBOOTED
  34. PRESS ENTER TO REBOOT, THEN ONCE BOOTED RUN THIS SCRIPT AGAIN
  35. EOF
  36. swallow_stdin
  37. read -r
  38. reboot
  39. leave
  40. fi
  41. fi
  42. if ! is_target_booted; then
  43. mkdir /tmp/rootmnt
  44. mount "$TGT_ROOT_DEV" /tmp/rootmnt
  45. ROOT=/tmp/rootmnt
  46. else
  47. ROOT=
  48. fi
  49. }
  50. get_largest_cros_blockdev() {
  51. local largest size dev_name tmp_size remo
  52. size=0
  53. for blockdev in /sys/block/"${dev%n*}"*; do
  54. dev_name="${blockdev##*/}"
  55. echo "$dev_name" | grep -q '^\(loop\|ram\)' && continue
  56. tmp_size=$(cat "$blockdev"/size)
  57. remo=$(cat "$blockdev"/removable)
  58. if [ "$tmp_size" -gt "$size" ] && [ "${remo:-0}" -eq 0 ]; then
  59. case "$(sfdisk -l -o name "/dev/$dev_name" 2>/dev/null)" in
  60. *STATE*KERN-A*ROOT-A*KERN-B*ROOT-B*)
  61. largest="/dev/$dev_name"
  62. size="$tmp_size"
  63. ;;
  64. esac
  65. fi
  66. done
  67. echo "$largest"
  68. }
  69. verity_enabled_for_n() {
  70. grep -q "root=/dev/dm" <"${DST}p${1}"
  71. }
  72. get_booted_kernnum() {
  73. # for some reason priorities can be like 2 and 1 instead of just 0 and 1???
  74. if (($(cgpt show -n "$DST" -i 2 -P) > $(cgpt show -n "$DST" -i 4 -P))); then
  75. echo -n 2
  76. else
  77. echo -n 4
  78. fi
  79. }
  80. cleanup() {
  81. if [ "$COMPAT" == "1" ]; then
  82. echo "pressure washing..."
  83. yes | mkfs.ext4 "${DST}p1" >/dev/null 2>&1 || : # hope you didn't have anything valuable on there
  84. fi
  85. cvpd -i RW_VPD -s check_enrollment=1 2>/dev/null
  86. cvpd -i RW_VPD -s block_devmode=0 2>/dev/null
  87. csys block_devmode=0 2>/dev/null
  88. }
  89. set_kernel_priority() {
  90. cgpt add "$DST" -i 4 -P 0
  91. cgpt add "$DST" -i 2 -P 0
  92. cgpt add "$DST" -i "$TGT_KERNNUM" -P 1
  93. }
  94. configure_target() {
  95. # remember, the goal here is to end up with one kernel that can be patched, and one kernel for the revert function.
  96. # we prioritize the non booted kernel so a reboot isn't needed
  97. DST="$(get_largest_cros_blockdev)"
  98. if [ "$DST" == "" ]; then
  99. echo "No CrOS SSD found on device!"
  100. leave
  101. fi
  102. if verity_enabled_for_n 2 && verity_enabled_for_n 4; then
  103. TGT_KERNNUM=
  104. elif verity_enabled_for_n 2; then
  105. TGT_KERNNUM=4
  106. elif verity_enabled_for_n 4; then
  107. TGT_KERNNUM=2
  108. else
  109. TGT_KERNNUM=
  110. if [ "$ROOTFS_BACKUP" == "1" ]; then
  111. echo "Rootfs restore is requested to be enabled, but both partitions have rootfs verification disabled. Please go through the recovery process to enable rootfs verification or run again and do not choose to enable rootfs restore."
  112. leave
  113. fi
  114. fi
  115. if [ "$TGT_KERNNUM" != "2" ] && [ "$TGT_KERNNUM" != "4" ]; then
  116. if [ "$COMPAT" == "1" ]; then
  117. TGT_KERNNUM=2
  118. else
  119. TGT_KERNNUM=$(opposite_num "$(get_booted_kernnum)")
  120. fi
  121. fi
  122. TGT_ROOTNUM=$((TGT_KERNNUM + 1))
  123. TGT_KERN_DEV="${DST}p$TGT_KERNNUM"
  124. TGT_ROOT_DEV="${DST}p$TGT_ROOTNUM"
  125. ALT_ROOTNUM=$(opposite_num "$TGT_ROOTNUM")
  126. ALT_KERNNUM=$(opposite_num "$TGT_KERNNUM")
  127. ALT_KERN_DEV="${DST}p$ALT_KERNNUM"
  128. ALT_ROOT_DEV="${DST}p$ALT_ROOTNUM"
  129. echo "target kern is $TGT_KERNNUM@$TGT_KERN_DEV"
  130. echo "target root is $TGT_ROOTNUM@$TGT_ROOT_DEV"
  131. echo
  132. echo "backup kern is $ALT_KERNNUM@$ALT_KERN_DEV"
  133. echo "backup root is $ALT_ROOTNUM@$ALT_ROOT_DEV"
  134. }
  135. patch_root() {
  136. echo "disabling autoupdates"
  137. disable_autoupdates
  138. drop_cr50_update
  139. sleep 2
  140. echo "dropping crossystem.sh"
  141. mv "$ROOT/usr/bin/crossystem" "$ROOT/usr/bin/crossystem.old"
  142. drop_crossystem_sh
  143. echo "staging sshd"
  144. sleep 2
  145. echo "dropping pollen"
  146. drop_pollen
  147. sleep 2
  148. echo "preventing stateful bootloop"
  149. drop_startup_patch
  150. if [ "$COMPAT" == "1" ]; then
  151. touch "$ROOT/stateful_unfucked"
  152. fi
  153. echo "installing mush shell"
  154. drop_mush
  155. sleep 2
  156. echo "dropping fakemurk daemon"
  157. drop_daemon
  158. echo "preparing ausystem"
  159. drop_ssd_util
  160. drop_image_patcher
  161. if [ "$DEVBUILD" == "1" ]; then
  162. devbuild_patchroot
  163. fi
  164. }
  165. main() {
  166. traps
  167. fakemurk_info
  168. config
  169. if csys mainfw_type?recovery; then
  170. echo "Entering shim compatability mode"
  171. COMPAT=1
  172. stty sane
  173. sleep 1
  174. fi
  175. # make sure it doesn't accidentally brick
  176. crossystem dev_boot_signed_only=0
  177. echo "----- stage 1: grabbing disk configuration -----"
  178. configure_target
  179. sleep 2
  180. echo "----- stage 2: patching target rootfs -----"
  181. prepare_target_root
  182. patch_root
  183. sync
  184. sleep 2
  185. echo "----- stage 3: cleaning up -----"
  186. cleanup
  187. sleep 1
  188. echo "setting kernel priority"
  189. set_kernel_priority
  190. sleep 1
  191. echo "done! press enter to reboot, and your chromebook should enroll into management when rebooted, but stay hidden in devmode"
  192. swallow_stdin
  193. read -r
  194. reboot
  195. leave
  196. }
  197. if [ "$0" = "$BASH_SOURCE" ]; then
  198. stty sane
  199. # if [ "$SHELL" != "/bin/bash" ]; then
  200. # echo "hey! you ran this with \"sh\" (or some other shell). i would really prefer if you ran it with \"bash\" instead"
  201. # fi
  202. if [ "$EUID" -ne 0 ]; then
  203. echo "Please run as root"
  204. exit
  205. fi
  206. main
  207. fi