fakemurk.sh.pre 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_nvme_namespace() {
  51. # this function doesn't exist if the version is old enough, so we redefine it
  52. local largest size tmp_size dev
  53. size=0
  54. dev=$(basename "$1")
  55. for nvme in /sys/block/"${dev%n*}"*; do
  56. tmp_size=$(cat "${nvme}"/size)
  57. if [ "${tmp_size}" -gt "${size}" ]; then
  58. largest="${nvme##*/}"
  59. size="${tmp_size}"
  60. fi
  61. done
  62. echo "${largest}"
  63. }
  64. verity_enabled_for_n() {
  65. grep -q "root=/dev/dm" <"${DST}p${1}"
  66. }
  67. get_booted_kernnum() {
  68. # for some reason priorities can be like 2 and 1 instead of just 0 and 1???
  69. if (($(cgpt show -n "$DST" -i 2 -P) > $(cgpt show -n "$DST" -i 4 -P))); then
  70. echo -n 2
  71. else
  72. echo -n 4
  73. fi
  74. }
  75. cleanup() {
  76. if [ "$COMPAT" == "1" ]; then
  77. echo "pressure washing..."
  78. yes | mkfs.ext4 "${DST}p1" >/dev/null 2>&1 || : # hope you didn't have anything valuable on there
  79. fi
  80. cvpd -i RW_VPD -s check_enrollment=1 2>/dev/null
  81. cvpd -i RW_VPD -s block_devmode=0 2>/dev/null
  82. csys block_devmode=0 2>/dev/null
  83. }
  84. set_kernel_priority() {
  85. cgpt add "$DST" -i 4 -P 0
  86. cgpt add "$DST" -i 2 -P 0
  87. cgpt add "$DST" -i "$TGT_KERNNUM" -P 1
  88. }
  89. configure_target() {
  90. # remember, the goal here is to end up with one kernel that can be patched, and one kernel for the revert function.
  91. # we prioritize the non booted kernel so a reboot isn't needed
  92. DST=/dev/$(get_largest_nvme_namespace)
  93. if [ "$DST" == "/dev/" ]; then
  94. DST=/dev/mmcblk0
  95. fi
  96. if verity_enabled_for_n 2 && verity_enabled_for_n 4; then
  97. TGT_KERNNUM=
  98. elif verity_enabled_for_n 2; then
  99. TGT_KERNNUM=4
  100. elif verity_enabled_for_n 4; then
  101. TGT_KERNNUM=2
  102. else
  103. TGT_KERNNUM=
  104. if [ "$ROOTFS_BACKUP" == "1" ]; then
  105. 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."
  106. leave
  107. fi
  108. fi
  109. if [ "$TGT_KERNNUM" != "2" ] && [ "$TGT_KERNNUM" != "4" ]; then
  110. if [ "$COMPAT" == "1" ]; then
  111. TGT_KERNNUM=2
  112. else
  113. TGT_KERNNUM=$(opposite_num "$(get_booted_kernnum)")
  114. fi
  115. fi
  116. TGT_ROOTNUM=$((TGT_KERNNUM + 1))
  117. TGT_KERN_DEV="${DST}p$TGT_KERNNUM"
  118. TGT_ROOT_DEV="${DST}p$TGT_ROOTNUM"
  119. ALT_ROOTNUM=$(opposite_num "$TGT_ROOTNUM")
  120. ALT_KERNNUM=$(opposite_num "$TGT_KERNNUM")
  121. ALT_KERN_DEV="${DST}p$ALT_KERNNUM"
  122. ALT_ROOT_DEV="${DST}p$ALT_ROOTNUM"
  123. echo "target kern is $TGT_KERNNUM@$TGT_KERN_DEV"
  124. echo "target root is $TGT_ROOTNUM@$TGT_ROOT_DEV"
  125. echo
  126. echo "backup kern is $ALT_KERNNUM@$ALT_KERN_DEV"
  127. echo "backup root is $ALT_ROOTNUM@$ALT_ROOT_DEV"
  128. }
  129. patch_root() {
  130. echo "disabling autoupdates"
  131. disable_autoupdates
  132. drop_cr50_update
  133. sleep 2
  134. echo "dropping crossystem.sh"
  135. mv "$ROOT/usr/bin/crossystem" "$ROOT/usr/bin/crossystem.old"
  136. drop_crossystem_sh
  137. echo "staging sshd"
  138. sleep 2
  139. echo "dropping pollen"
  140. drop_pollen
  141. sleep 2
  142. echo "preventing stateful bootloop"
  143. drop_startup_patch
  144. if [ "$COMPAT" == "1" ]; then
  145. touch "$ROOT/stateful_unfucked"
  146. fi
  147. echo "installing mush shell"
  148. drop_mush
  149. sleep 2
  150. echo "dropping fakemurk daemon"
  151. drop_daemon
  152. echo "preparing ausystem"
  153. drop_ssd_util
  154. drop_image_patcher
  155. if [ "$DEVBUILD" == "1" ]; then
  156. devbuild_patchroot
  157. fi
  158. }
  159. main() {
  160. traps
  161. fakemurk_info
  162. config
  163. if csys mainfw_type?recovery; then
  164. echo "Entering shim compatability mode"
  165. COMPAT=1
  166. stty sane
  167. sleep 1
  168. fi
  169. echo "----- stage 1: grabbing disk configuration -----"
  170. configure_target
  171. sleep 2
  172. echo "----- stage 2: patching target rootfs -----"
  173. prepare_target_root
  174. patch_root
  175. sync
  176. sleep 2
  177. echo "----- stage 3: cleaning up -----"
  178. cleanup
  179. sleep 1
  180. echo "setting kernel priority"
  181. set_kernel_priority
  182. sleep 1
  183. echo "done! press enter to reboot, and your chromebook should enroll into management when rebooted, but stay hidden in devmode"
  184. swallow_stdin
  185. read -r
  186. reboot
  187. leave
  188. }
  189. if [ "$0" = "$BASH_SOURCE" ]; then
  190. stty sane
  191. # if [ "$SHELL" != "/bin/bash" ]; then
  192. # echo "hey! you ran this with \"sh\" (or some other shell). i would really prefer if you ran it with \"bash\" instead"
  193. # fi
  194. if [ "$EUID" -ne 0 ]; then
  195. echo "Please run as root"
  196. exit
  197. fi
  198. main
  199. fi