FlashKernelPart.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/sh
  2. # This file is part of PrawnOS (http://www.prawnos.com)
  3. # Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
  4. # Copyright (c) 2020 Fil Bergamo <fil@filberg.eu>
  5. # PrawnOS is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2
  7. # as published by the Free Software Foundation.
  8. # PrawnOS is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
  14. # -----------------------------------------------
  15. # STATIC CONFIGURATION
  16. # -----------------------------------------------
  17. #
  18. # eMMC device name
  19. emmc_devname=mmcblk2
  20. #
  21. # Blank kernel image
  22. blnk=./blank_kernel
  23. #
  24. # Actual kernel image (.kpart)
  25. kimg=./vmlinux.kpart
  26. #
  27. # GPT partition type UUID for "ChromeOS kernel"
  28. ptype_kernel="FE3A2A5D-4F32-41A7-B725-ACCC3285A309"
  29. #
  30. # Kernel partition number
  31. pnum_kernel=1
  32. # -----------------------------------------------
  33. # function die():
  34. # print an error message and exit with the provided code
  35. # $1 = error message
  36. # $2 = exit code
  37. die()
  38. {
  39. redtxt='\033[0;31m'
  40. printf "$redtxt$1\n"
  41. exit $2
  42. }
  43. # function get_partition_type_uuid()
  44. # print the UUID of the gpt partition type
  45. # for the given partition number on the given device
  46. # $1 = device (e.g. /dev/mmcblk2)
  47. # $2 = partition number
  48. get_partition_type_uuid()
  49. {
  50. cgpt show -i "$2" -t -n -q "$1" 2>/dev/null
  51. }
  52. # function get_root_partition()
  53. # print the actual physical device
  54. # currently mounted as "/"
  55. get_root_partition()
  56. {
  57. rootfs=$(findmnt / -n -o SOURCE)
  58. if echo "$rootfs" | grep -q "/dev/mapper" ;then
  59. # root filesystem is on luks volume
  60. # let's find the physical device behind it
  61. rootfs=$(cryptsetup status "$rootfs" | grep "device: " | cut -d ' ' -f 5)
  62. fi
  63. echo "$rootfs"
  64. }
  65. # ------------- BEGIN SCRIPT EXECUTION --------------------
  66. set -e
  67. # Check root or sudo
  68. [ ! $(id -u) = 0 ] &&
  69. die "Please run this script with sudo, or as root" 1
  70. rootfs=$(get_root_partition)
  71. devname=$(lsblk -no pkname $rootfs | head -n 1)
  72. devtype=
  73. kpart=
  74. case "$devname" in
  75. $emmc_devname)
  76. devtype="Internal eMMC"
  77. kpart=/dev/${devname}p$pnum_kernel
  78. ;;
  79. sd*)
  80. devtype="USB Stick"
  81. kpart=/dev/${devname}$pnum_kernel
  82. ;;
  83. mmcblk[0-9])
  84. devtype="External SD card"
  85. kpart=/dev/${devname}p$pnum_kernel
  86. ;;
  87. *)
  88. die "FAILED to recognize booted device type $devname" 127
  89. esac
  90. # Check that the selected partition is effectively of type "kernel"
  91. ptype=$(get_partition_type_uuid /dev/$devname $pnum_kernel)
  92. [ ! "$ptype" = "$ptype_kernel" ] &&
  93. die "FATAL ERROR: unexpected partitioning scheme found. Partition # $pnum_kernel is NOT the kernel partition!" 255
  94. # Check that the needed kernel images exist
  95. [ ! -e "$kimg" ] &&
  96. die "ERROR: cannot find kernel image at $kimg !" 127
  97. [ ! -e "$blnk" ] &&
  98. die "ERROR: cannot find blank image at $blnk !" 127
  99. # Prompt for user's confirmation
  100. echo "
  101. ----------------------------
  102. /!\\ !!! CAUTION !!! /!\\
  103. ----------------------------
  104. This will flash a new kernel image onto the running device's kernel partition: $kpart
  105. The detected running device is: $devtype.
  106. DO NOT shutdown or reboot before completing the process!
  107. "
  108. printf "%s" "Are you REALLY sure you want to continue??? [y/N] "
  109. read ans
  110. [ "$ans" != "y" ] &&
  111. [ "$ans" != "Y" ] &&
  112. die "Aborted by user. Kernel untouched." 1
  113. # put the kernel in the kernel partition
  114. dd if="$blnk" of="$kpart" conv=notrunc ||
  115. die "FAILED to flash blank kernel on $kpart!" 255
  116. dd if="$kimg" of="$kpart" conv=notrunc ||
  117. die "FAILED to flash kernel image on $kpart!" 255
  118. echo "
  119. The new kernel image has been successfully flashed onto $kpart.
  120. It's now safe to reboot."
  121. # TODO: install modules. -----------------------------------
  122. # Right now, there's no easy way to do that on the running machine
  123. # -----------------------------------------------------------------
  124. # make -C build/linux-$KVER ARCH=arm INSTALL_MOD_PATH=$outmnt modules_install