FlashKernelPart.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/sh
  2. # This file is part of PrawnOS (https://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. # Actual kernel image (.kpart)
  22. kimg=./vmlinux.kpart
  23. #
  24. # GPT partition type UUID for "ChromeOS kernel"
  25. ptype_kernel="FE3A2A5D-4F32-41A7-B725-ACCC3285A309"
  26. #
  27. # Kernel partition number
  28. pnum_kernel=1
  29. # -----------------------------------------------
  30. # function die():
  31. # print an error message and exit with the provided code
  32. # $1 = error message
  33. # $2 = exit code
  34. die()
  35. {
  36. redtxt='\033[0;31m'
  37. printf "$redtxt$1\n"
  38. exit $2
  39. }
  40. # function get_partition_type_uuid()
  41. # print the UUID of the gpt partition type
  42. # for the given partition number on the given device
  43. # $1 = device (e.g. /dev/mmcblk2)
  44. # $2 = partition number
  45. get_partition_type_uuid()
  46. {
  47. cgpt show -i "$2" -t -n -q "$1" 2>/dev/null
  48. }
  49. # function get_root_partition()
  50. # print the actual physical device
  51. # currently mounted as "/"
  52. get_root_partition()
  53. {
  54. rootfs=$(findmnt / -n -o SOURCE)
  55. if echo "$rootfs" | grep -q "/dev/mapper" ;then
  56. # root filesystem is on luks volume
  57. # let's find the physical device behind it
  58. rootfs=$(cryptsetup status "$rootfs" | grep "device: " | cut -d ' ' -f 5)
  59. fi
  60. echo "$rootfs"
  61. }
  62. # ------------- BEGIN SCRIPT EXECUTION --------------------
  63. set -e
  64. # Check root or sudo
  65. [ ! $(id -u) = 0 ] &&
  66. die "Please run this script with sudo, or as root" 1
  67. rootfs=$(get_root_partition)
  68. devname=$(lsblk -no pkname $rootfs | head -n 1)
  69. devtype=
  70. kpart=
  71. case "$devname" in
  72. $emmc_devname)
  73. devtype="Internal eMMC"
  74. kpart=/dev/${devname}p$pnum_kernel
  75. ;;
  76. sd*)
  77. devtype="USB Stick"
  78. kpart=/dev/${devname}$pnum_kernel
  79. ;;
  80. mmcblk[0-9])
  81. devtype="External SD card"
  82. kpart=/dev/${devname}p$pnum_kernel
  83. ;;
  84. *)
  85. die "FAILED to recognize booted device type $devname" 127
  86. esac
  87. # Check that the selected partition is effectively of type "kernel"
  88. ptype=$(get_partition_type_uuid /dev/$devname $pnum_kernel)
  89. [ ! "$ptype" = "$ptype_kernel" ] &&
  90. die "FATAL ERROR: unexpected partitioning scheme found. Partition # $pnum_kernel is NOT the kernel partition!" 255
  91. # Check that the needed kernel images exist
  92. [ ! -e "$kimg" ] &&
  93. die "ERROR: cannot find kernel image at $kimg !" 127
  94. [ ! -e "$blnk" ] &&
  95. die "ERROR: cannot find blank image at $blnk !" 127
  96. # Prompt for user's confirmation
  97. echo "
  98. ----------------------------
  99. /!\\ !!! CAUTION !!! /!\\
  100. ----------------------------
  101. This will flash a new kernel image onto the running device's kernel partition: $kpart
  102. The detected running device is: $devtype.
  103. DO NOT shutdown or reboot before completing the process!
  104. "
  105. printf "%s" "Are you REALLY sure you want to continue??? [y/N] "
  106. read ans
  107. [ "$ans" != "y" ] &&
  108. [ "$ans" != "Y" ] &&
  109. die "Aborted by user. Kernel untouched." 1
  110. # put the kernel in the kernel partition
  111. #blank the kernel partition first, with 32MiB of zeros
  112. kernel_size=65536
  113. block_size=512
  114. dd if=/dev/zero of="$kpart" conv=notrunc bs=512 count=$kernel_size ||
  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 INSTALL_HEADERS_PATH=$outmnt headers_install modules_install