ssd_util.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #!/bin/sh
  2. # this is a modified version of make_dev_ssd from cros. it allows the script to be run on any linux device and adds extra utility
  3. #
  4. # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  5. # Use of this source code is governed by a BSD-style license that can be
  6. # found in the LICENSE file.
  7. #
  8. # This script can change key (usually developer keys) and kernel config
  9. # of kernels on an disk image (usually for SSD but also works for USB).
  10. SCRIPT_BASE="$(dirname "$0")"
  11. . "$SCRIPT_BASE/common_minimal.sh"
  12. load_shflags || exit 1
  13. # Constants used by DEFINE_*
  14. VBOOT_BASE='/usr/share/vboot'
  15. DEFAULT_KEYS_FOLDER="$VBOOT_BASE/devkeys"
  16. DEFAULT_PARTITIONS='2 4'
  17. # Only store backup in stateful partition if available.
  18. if [ -d /mnt/stateful_partition ]; then
  19. DEFAULT_BACKUP_FOLDER="/mnt/stateful_partition"
  20. else
  21. DEFAULT_BACKUP_FOLDER="$(pwd)"
  22. fi
  23. DEFAULT_BACKUP_FOLDER="${DEFAULT_BACKUP_FOLDER}/cros_sign_backups"
  24. # TODO(hungte) The default image selection is no longer a SSD, so the script
  25. # works more like "make_dev_image". We may change the file name in future.
  26. ROOTDEV="$(rootdev -s 2>/dev/null)"
  27. ROOTDEV_PARTITION="$(echo $ROOTDEV | sed -n 's/.*[^0-9]\([0-9][0-9]*\)$/\1/p')"
  28. ROOTDEV_DISK="$(rootdev -s -d 2>/dev/null)"
  29. ROOTDEV_KERNEL="$((ROOTDEV_PARTITION - 1))"
  30. # DEFINE_string name default_value description flag
  31. DEFINE_string image "$ROOTDEV_DISK" "Path to device or image file" "i"
  32. DEFINE_string keys "$DEFAULT_KEYS_FOLDER" "Path to folder of dev keys" "k"
  33. DEFINE_boolean remove_rootfs_verification \
  34. "${FLAGS_FALSE}" "Modify kernel boot config to disable rootfs verification" \
  35. "r"
  36. DEFINE_boolean enable_earlycon "${FLAGS_FALSE}" \
  37. "Enable earlycon from stdout-path (ARM/ARM64) or SPCR (x86)." ""
  38. DEFINE_boolean disable_earlycon "${FLAGS_FALSE}" \
  39. "Disable earlycon." ""
  40. DEFINE_boolean enable_console "${FLAGS_FALSE}" \
  41. "Enable serial console." ""
  42. DEFINE_boolean disable_console "${FLAGS_FALSE}" \
  43. "Disable serial console." ""
  44. DEFINE_string backup_dir \
  45. "$DEFAULT_BACKUP_FOLDER" "Path of directory to store kernel backups" ""
  46. DEFINE_string save_config "" \
  47. "Base filename to store kernel configs to, instead of resigning." ""
  48. DEFINE_string set_config "" \
  49. "Base filename to load kernel configs from" ""
  50. DEFINE_boolean edit_config "${FLAGS_FALSE}" \
  51. "Edit kernel config in-place." ""
  52. DEFINE_string partitions "" \
  53. "List of partitions to examine (default: $DEFAULT_PARTITIONS)" ""
  54. DEFINE_boolean recovery_key "$FLAGS_FALSE" \
  55. "Use recovery key to sign image (to boot from USB)" ""
  56. DEFINE_boolean force "$FLAGS_FALSE" \
  57. "Skip validity checks and make the change" "f"
  58. DEFINE_boolean default_rw_root "${FLAGS_TRUE}" \
  59. "When --remove_rootfs_verification is set, change root mount option to RW." ""
  60. DEFINE_boolean no_resign_kernel "${FLAGS_FALSE}" \
  61. "make changes without resigning the kernel" ""
  62. # Parse command line
  63. FLAGS "$@" || exit 1
  64. ORIGINAL_CMD="$0"
  65. ORIGINAL_PARAMS="$@"
  66. eval set -- "$FLAGS_ARGV"
  67. ORIGINAL_PARTITIONS="$FLAGS_partitions"
  68. : ${FLAGS_partitions:=$DEFAULT_PARTITIONS}
  69. # Globals
  70. # ----------------------------------------------------------------------------
  71. set -e
  72. # a log file to keep the output results of executed command
  73. EXEC_LOG="$(make_temp_file)"
  74. # Functions
  75. # ----------------------------------------------------------------------------
  76. # Removes rootfs verification from kernel boot parameter
  77. # And strip out bootcache args if it exists
  78. remove_rootfs_verification() {
  79. local new_root="PARTUUID=%U/PARTNROFF=1"
  80. # the first line in sed is to strip out bootcache details
  81. local rw_root_opt="s| ro | rw |"
  82. if [ "${FLAGS_default_rw_root}" = "${FLAGS_FALSE}" ]; then
  83. rw_root_opt="s| rw | ro |"
  84. fi
  85. echo "$*" | sed '
  86. s| dm=\"2 [^"]*bootcache[^"]* vroot | dm=\"1 vroot |
  87. s| root=/dev/dm-[0-9] | root='"$new_root"' |
  88. s| dm_verity.dev_wait=1 | dm_verity.dev_wait=0 |
  89. s| payload=PARTUUID=%U/PARTNROFF=1 | payload=ROOT_DEV |
  90. s| hashtree=PARTUUID=%U/PARTNROFF=1 | hashtree=HASH_DEV |
  91. '"${rw_root_opt}"
  92. }
  93. remove_legacy_boot_rootfs_verification() {
  94. # See src/scripts/create_legacy_bootloader_templates
  95. local image="$1"
  96. local mount_point="$(make_temp_dir)"
  97. local config_file
  98. debug_msg "Removing rootfs verification for legacy boot configuration."
  99. mount_image_partition "$image" 12 "$mount_point" || return $FLAGS_FALSE
  100. config_file="$mount_point/efi/boot/grub.cfg"
  101. [ ! -f "${config_file}" ] ||
  102. sudo sed -i -e 's/^ *defaultA=2 *$/defaultA=0/g' \
  103. -e 's/^ *defaultB=3 *$/defaultB=1/g' "${config_file}"
  104. config_file="$mount_point/syslinux/default.cfg"
  105. [ ! -f "$config_file" ] ||
  106. sudo sed -i 's/-vusb/-usb/g; s/-vhd/-hd/g' "$config_file"
  107. sudo umount "$mount_point"
  108. }
  109. # Enable/Disable earlycon or serial console
  110. insert_parameter() {
  111. local cmdline="$1"
  112. local param="$2"
  113. if [ -n "${cmdline##*${param}*}" ]; then
  114. cmdline="${param} ${cmdline}"
  115. fi
  116. echo "${cmdline}"
  117. }
  118. remove_parameter() {
  119. local cmdline="$1"
  120. local param="$2"
  121. cmdline=$(echo "${cmdline}" | sed '
  122. s/'"${param} "'//g')
  123. echo "${cmdline}"
  124. }
  125. # Wrapped version of dd
  126. mydd() {
  127. # oflag=sync is safer, but since we need bs=512, syncing every block would be
  128. # very slow.
  129. dd "$@" >"$EXEC_LOG" 2>&1 ||
  130. die "Failed in [dd $*], Message: $(cat "${EXEC_LOG}")"
  131. }
  132. # Prints a more friendly name from kernel index number
  133. cros_kernel_name() {
  134. case $1 in
  135. 2)
  136. echo "Kernel A"
  137. ;;
  138. 4)
  139. echo "Kernel B"
  140. ;;
  141. 6)
  142. echo "Kernel C"
  143. ;;
  144. *)
  145. echo "Partition $1"
  146. esac
  147. }
  148. find_valid_kernel_partitions() {
  149. local part_id
  150. local valid_partitions=""
  151. for part_id in $*; do
  152. local name="$(cros_kernel_name $part_id)"
  153. local kernel_part="$(make_partition_dev "$FLAGS_image" "$part_id")"
  154. info "futil: $FUTILITY"
  155. if [ -z "$(${FUTILITY} dump_kernel_config "$kernel_part" 2>"$EXEC_LOG")" ]; then
  156. info "${name}: no kernel boot information, ignored." >&2
  157. else
  158. [ -z "$valid_partitions" ] &&
  159. valid_partitions="$part_id" ||
  160. valid_partitions="$valid_partitions $part_id"
  161. continue
  162. fi
  163. done
  164. debug_msg "find_valid_kernel_partitions: [$*] -> [$valid_partitions]"
  165. echo "$valid_partitions"
  166. }
  167. # Resigns a kernel on SSD or image.
  168. resign_ssd_kernel() {
  169. local ssd_device="$1"
  170. local bs="$(blocksize "${ssd_device}")"
  171. # reasonable size for current kernel partition
  172. local min_kernel_size=$((8000 * 1024 / bs))
  173. local resigned_kernels=0
  174. for kernel_index in $FLAGS_partitions; do
  175. local old_blob="$(make_temp_file)"
  176. local new_blob="$(make_temp_file)"
  177. local name="$(cros_kernel_name $kernel_index)"
  178. local rootfs_index="$(($kernel_index + 1))"
  179. debug_msg "Probing $name information"
  180. local offset size
  181. offset="$(partoffset "$ssd_device" "$kernel_index")" ||
  182. die "Failed to get partition ${kernel_index} offset from ${ssd_device}"
  183. size="$(partsize "$ssd_device" "$kernel_index")" ||
  184. die "Failed to get partition ${kernel_index} size from ${ssd_device}"
  185. if [ ! $size -gt $min_kernel_size ]; then
  186. info "${name} seems too small (${size}), ignored."
  187. continue
  188. fi
  189. debug_msg "Reading $name from partition $kernel_index"
  190. mydd if="$ssd_device" of="$old_blob" bs=$bs skip=$offset count=$size
  191. debug_msg "Checking if $name is valid"
  192. local kernel_config
  193. if ! kernel_config="$(${FUTILITY} dump_kernel_config "$old_blob" 2>"$EXEC_LOG")"; then
  194. debug_msg "dump_kernel_config error message: $(cat "$EXEC_LOG")"
  195. info "${name}: no kernel boot information, ignored."
  196. continue
  197. fi
  198. if [ "$FLAGS_no_resign_kernel" = "$FLAGS_TRUE" ]; then
  199. info "Skipping resign for ${FLAGS_image}"
  200. if [ ${FLAGS_remove_rootfs_verification} = $FLAGS_TRUE ]; then
  201. local root_offset_sector=$(partoffset "$ssd_device" $rootfs_index)
  202. local root_offset_bytes=$((root_offset_sector * bs))
  203. info "dev: ${ssd_device} $root_offset_bytes"
  204. if ! is_ext2 "$ssd_device" "$root_offset_bytes"; then
  205. debug_msg "Non-ext2 partition: $ssd_device$rootfs_index, skip."
  206. elif ! rw_mount_disabled "$ssd_device" "$root_offset_bytes"; then
  207. debug_msg "Root file system is writable. No need to modify."
  208. else
  209. # disable the RO ext2 hack
  210. info "Disabling rootfs ext2 RO bit hack"
  211. enable_rw_mount "$ssd_device" "$root_offset_bytes" >"$EXEC_LOG" 2>&1 ||
  212. die "Failed turning off rootfs RO bit. OS may be corrupted. " \
  213. "Message: $(cat "${EXEC_LOG}")"
  214. fi
  215. fi
  216. return
  217. fi
  218. if [ -n "${FLAGS_save_config}" ]; then
  219. # Save current kernel config
  220. local old_config_file
  221. old_config_file="${FLAGS_save_config}.$kernel_index"
  222. info "Saving ${name} config to ${old_config_file}"
  223. echo "$kernel_config" > "$old_config_file"
  224. # Just save; don't resign
  225. continue
  226. fi
  227. if [ -n "${FLAGS_set_config}" ]; then
  228. # Set new kernel config from file
  229. local new_config_file
  230. new_config_file="${FLAGS_set_config}.$kernel_index"
  231. kernel_config="$(cat "$new_config_file")" ||
  232. die "Failed to read new kernel config from ${new_config_file}"
  233. debug_msg "New kernel config: $kernel_config)"
  234. info "${name}: Replaced config from ${new_config_file}"
  235. fi
  236. if [ "${FLAGS_edit_config}" = ${FLAGS_TRUE} ]; then
  237. debug_msg "Editing kernel config file."
  238. local new_config_file="$(make_temp_file)"
  239. echo "${kernel_config}" >"${new_config_file}"
  240. local old_md5sum="$(md5sum "${new_config_file}")"
  241. local editor="${VISUAL:-${EDITOR:-vi}}"
  242. info "${name}: Editing kernel config:"
  243. # On ChromiumOS, some builds may come with broken EDITOR that refers to
  244. # nano so we want to check again if the editor really exists.
  245. if type "${editor}" >/dev/null 2>&1; then
  246. "${editor}" "${new_config_file}"
  247. else
  248. # This script runs under dash but we want readline in bash to support
  249. # editing in in console.
  250. bash -c "read -e -i '${kernel_config}' &&
  251. echo \"\${REPLY}\" >${new_config_file}" ||
  252. die "Failed to run editor. Please specify editor name by VISUAL."
  253. fi
  254. kernel_config="$(cat "${new_config_file}")"
  255. if [ "$(md5sum "${new_config_file}")" = "${old_md5sum}" ]; then
  256. info "${name}: Config not changed."
  257. else
  258. debug_msg "New kernel config: ${kernel_config})"
  259. info "${name}: Config updated"
  260. fi
  261. fi
  262. if [ ${FLAGS_remove_rootfs_verification} = $FLAGS_FALSE ]; then
  263. debug_msg "Bypassing rootfs verification check"
  264. else
  265. debug_msg "Changing boot parameter to remove rootfs verification"
  266. kernel_config="$(remove_rootfs_verification "$kernel_config")"
  267. debug_msg "New kernel config: $kernel_config"
  268. info "${name}: Disabled rootfs verification."
  269. remove_legacy_boot_rootfs_verification "$ssd_device"
  270. fi
  271. if [ "${FLAGS_enable_earlycon}" = "${FLAGS_TRUE}" ]; then
  272. debug_msg "Enabling earlycon"
  273. kernel_config="$(insert_parameter "${kernel_config}" "earlycon")"
  274. debug_msg "New kernel config: ${kernel_config}"
  275. elif [ "${FLAGS_disable_earlycon}" = "${FLAGS_TRUE}" ]; then
  276. debug_msg "Disabling earlycon"
  277. kernel_config="$(remove_parameter "${kernel_config}" "earlycon")"
  278. debug_msg "New kernel config: ${kernel_config}"
  279. fi
  280. if [ "${FLAGS_enable_console}" = "${FLAGS_TRUE}" ]; then
  281. debug_msg "Enabling serial console"
  282. kernel_config="$(remove_parameter "${kernel_config}" "console=")"
  283. debug_msg "New kernel config: ${kernel_config}"
  284. elif [ "${FLAGS_disable_console}" = "${FLAGS_TRUE}" ]; then
  285. debug_msg "Disabling serial console"
  286. kernel_config="$(insert_parameter "${kernel_config}" "console=")"
  287. debug_msg "New kernel config: ${kernel_config}"
  288. fi
  289. local new_kernel_config_file="$(make_temp_file)"
  290. echo -n "$kernel_config" >"$new_kernel_config_file"
  291. debug_msg "Re-signing $name from $old_blob to $new_blob"
  292. debug_msg "Using key: $KERNEL_DATAKEY"
  293. vbutil_kernel \
  294. --repack "$new_blob" \
  295. --keyblock "$KERNEL_KEYBLOCK" \
  296. --config "$new_kernel_config_file" \
  297. --signprivate "$KERNEL_DATAKEY" \
  298. --oldblob "$old_blob" >"$EXEC_LOG" 2>&1 ||
  299. die "Failed to resign ${name}. Message: $(cat "${EXEC_LOG}")"
  300. debug_msg "Creating new kernel image (vboot+code+config)"
  301. local new_kern="$(make_temp_file)"
  302. cp "$old_blob" "$new_kern"
  303. mydd if="$new_blob" of="$new_kern" conv=notrunc
  304. if is_debug_mode; then
  305. debug_msg "for debug purposes, check *.dbgbin"
  306. cp "$old_blob" old_blob.dbgbin
  307. cp "$new_blob" new_blob.dbgbin
  308. cp "$new_kern" new_kern.dbgbin
  309. fi
  310. debug_msg "Verifying new kernel and keys"
  311. vbutil_kernel \
  312. --verify "$new_kern" \
  313. --signpubkey "$KERNEL_PUBKEY" --verbose >"$EXEC_LOG" 2>&1 ||
  314. die "Failed to verify new ${name}. Message: $(cat "${EXEC_LOG}")"
  315. debug_msg "Backup old kernel blob"
  316. local backup_date_time="$(date +'%Y%m%d_%H%M%S')"
  317. local backup_name="$(echo "$name" | sed 's/ /_/g; s/^K/k/')"
  318. local backup_file_name="${backup_name}_${backup_date_time}.bin"
  319. local backup_file_path="$FLAGS_backup_dir/$backup_file_name"
  320. if mkdir -p "$FLAGS_backup_dir" &&
  321. cp -f "$old_blob" "$backup_file_path"; then
  322. info "Backup of ${name} is stored in: ${backup_file_path}"
  323. else
  324. warn "Cannot create file in ${FLAGS_backup_dir} ... Ignore backups."
  325. fi
  326. debug_msg "Writing $name to partition $kernel_index"
  327. mydd \
  328. if="$new_kern" \
  329. of="$ssd_device" \
  330. seek=$offset \
  331. bs=$bs \
  332. count=$size \
  333. conv=notrunc
  334. resigned_kernels=$(($resigned_kernels + 1))
  335. debug_msg "Make the root file system writable if needed."
  336. # TODO(hungte) for safety concern, a more robust way would be to:
  337. # (1) change kernel config to ro
  338. # (2) check if we can enable rw mount
  339. # (3) change kernel config to rw
  340. if [ ${FLAGS_remove_rootfs_verification} = $FLAGS_TRUE ]; then
  341. local root_offset_sector=$(partoffset "$ssd_device" $rootfs_index)
  342. local root_offset_bytes=$((root_offset_sector * bs))
  343. if ! is_ext2 "$ssd_device" "$root_offset_bytes"; then
  344. debug_msg "Non-ext2 partition: $ssd_device$rootfs_index, skip."
  345. elif ! rw_mount_disabled "$ssd_device" "$root_offset_bytes"; then
  346. debug_msg "Root file system is writable. No need to modify."
  347. else
  348. # disable the RO ext2 hack
  349. info "Disabling rootfs ext2 RO bit hack"
  350. enable_rw_mount "$ssd_device" "$root_offset_bytes" >"$EXEC_LOG" 2>&1 ||
  351. die "Failed turning off rootfs RO bit. OS may be corrupted. " \
  352. "Message: $(cat "${EXEC_LOG}")"
  353. fi
  354. fi
  355. # Sometimes doing "dump_kernel_config" or other I/O now (or after return to
  356. # shell) will get the data before modification. Not a problem now, but for
  357. # safety, let's try to sync more.
  358. sync; sync; sync
  359. info "${name}: Re-signed with developer keys successfully."
  360. done
  361. # If we saved the kernel config, exit now so we don't print an error
  362. if [ -n "${FLAGS_save_config}" ]; then
  363. info "(Kernels have not been resigned.)"
  364. exit 0
  365. fi
  366. return $resigned_kernels
  367. }
  368. validity_check_crossystem_flags() {
  369. debug_msg "crossystem validity check"
  370. if [ -n "${FLAGS_save_config}" ]; then
  371. debug_msg "not resigning kernel."
  372. return
  373. fi
  374. if [ "$(crossystem dev_boot_signed_only)" = "0" ]; then
  375. debug_msg "dev_boot_signed_only not set - safe."
  376. return
  377. fi
  378. echo "
  379. ERROR: YOUR FIRMWARE WILL ONLY BOOT SIGNED IMAGES.
  380. Modifying the kernel or root filesystem will result in an unusable system. If
  381. you really want to make this change, allow the firmware to boot self-signed
  382. images by running:
  383. sudo crossystem dev_boot_signed_only=0
  384. before re-executing this command.
  385. "
  386. return $FLAGS_FALSE
  387. }
  388. validity_check_live_partitions() {
  389. debug_msg "Partition validity check"
  390. if [ "$FLAGS_partitions" = "$ROOTDEV_KERNEL" ]; then
  391. debug_msg "only for current active partition - safe."
  392. return
  393. fi
  394. if [ "$ORIGINAL_PARTITIONS" != "" ]; then
  395. debug_msg "user has assigned partitions - provide more info."
  396. info "Making change to ${FLAGS_partitions} on ${FLAGS_image}."
  397. return
  398. fi
  399. echo "
  400. ERROR: YOU ARE TRYING TO MODIFY THE LIVE SYSTEM IMAGE $FLAGS_image.
  401. The system may become unusable after that change, especially when you have
  402. some auto updates in progress. To make it safer, we suggest you to only
  403. change the partition you have booted with. To do that, re-execute this command
  404. as:
  405. sudo $ORIGINAL_CMD $ORIGINAL_PARAMS --partitions $ROOTDEV_KERNEL
  406. If you are sure to modify other partition, please invoke the command again and
  407. explicitly assign only one target partition for each time (--partitions N )
  408. "
  409. return $FLAGS_FALSE
  410. }
  411. validity_check_live_firmware() {
  412. debug_msg "Firmware compatibility validity check"
  413. if [ "$(crossystem mainfw_type)" = "developer" ]; then
  414. debug_msg "developer type firmware in active."
  415. return
  416. fi
  417. debug_msg "Loading firmware to check root key..."
  418. local bios_image="$(make_temp_file)"
  419. local rootkey_file="$(make_temp_file)"
  420. info "checking system firmware..."
  421. sudo flashrom -p host -i GBB -r "$bios_image" >/dev/null 2>&1
  422. futility gbb -g --rootkey="$rootkey_file" "$bios_image" >/dev/null 2>&1
  423. if [ ! -s "$rootkey_file" ]; then
  424. debug_msg "failed to read root key from system firmware..."
  425. else
  426. # The magic 130 is counted by "od dev-rootkey" for the lines until the body
  427. # of key is reached. Trailing bytes (0x00 or 0xFF - both may appear, and
  428. # that's why we need to skip them) are started at line 131.
  429. # TODO(hungte) compare with rootkey in $VBOOT_BASE directly.
  430. local rootkey_hash="$(od "$rootkey_file" |
  431. head -130 | md5sum |
  432. sed 's/ .*$//' )"
  433. if [ "$rootkey_hash" = "a13642246ef93daaf75bd791446fec9b" ]; then
  434. debug_msg "detected DEV root key in firmware."
  435. return
  436. else
  437. debug_msg "non-devkey hash: $rootkey_hash"
  438. fi
  439. fi
  440. echo "
  441. ERROR: YOU ARE NOT USING DEVELOPER FIRMWARE, AND RUNNING THIS COMMAND MAY
  442. THROW YOUR CHROMEOS DEVICE INTO UN-BOOTABLE STATE.
  443. You need to either install developer firmware, or change system root key.
  444. - To install developer firmware: type command
  445. sudo chromeos-firmwareupdate --mode=todev
  446. - To change system rootkey: disable firmware write protection (a hardware
  447. switch) and then type command:
  448. sudo $SCRIPT_BASE/make_dev_firmware.sh
  449. If you are sure that you want to make such image without developer
  450. firmware or you've already changed system root keys, please run this
  451. command again with --force paramemeter:
  452. sudo $ORIGINAL_CMD --force $ORIGINAL_PARAMS
  453. "
  454. return $FLAGS_FALSE
  455. }
  456. validity_check() {
  457. validity_check_live_partitions || return $FLAGS_FALSE
  458. # Remaining checks depend on firmware; skip if device is running in a VM.
  459. if crossystem 'inside_vm?1'; then
  460. debug_msg "Device is a VM, skipping firmware checks"
  461. return $FLAGS_TRUE
  462. fi
  463. validity_check_live_firmware || return $FLAGS_FALSE
  464. validity_check_crossystem_flags || return $FLAGS_FALSE
  465. return $FLAGS_TRUE
  466. }
  467. # Main
  468. # ----------------------------------------------------------------------------
  469. main() {
  470. local num_signed=0
  471. local num_given=$(echo "$FLAGS_partitions" | wc -w)
  472. local kaltdir=$(dirname $0)/keys
  473. if [ -d "$kaltdir" ]; then
  474. FLAGS_keys=$kaltdir
  475. fi
  476. # Check parameters
  477. if [ "$FLAGS_recovery_key" = "$FLAGS_TRUE" ]; then
  478. KERNEL_KEYBLOCK="$FLAGS_keys/recovery_kernel.keyblock"
  479. KERNEL_DATAKEY="$FLAGS_keys/recovery_kernel_data_key.vbprivk"
  480. KERNEL_PUBKEY="$FLAGS_keys/recovery_key.vbpubk"
  481. else
  482. KERNEL_KEYBLOCK="$FLAGS_keys/kernel.keyblock"
  483. KERNEL_DATAKEY="$FLAGS_keys/kernel_data_key.vbprivk"
  484. KERNEL_PUBKEY="$FLAGS_keys/kernel_subkey.vbpubk"
  485. fi
  486. debug_msg "Prerequisite check"
  487. ensure_files_exist \
  488. "$KERNEL_KEYBLOCK" \
  489. "$KERNEL_DATAKEY" \
  490. "$KERNEL_PUBKEY" \
  491. "$FLAGS_image" ||
  492. exit 1
  493. # checks for running on a live system image.
  494. if [ "$FLAGS_image" = "$ROOTDEV_DISK" ]; then
  495. debug_msg "check valid kernel partitions for live system"
  496. local valid_partitions="$(find_valid_kernel_partitions $FLAGS_partitions)"
  497. [ -n "$valid_partitions" ] ||
  498. die "No valid kernel partitions on ${FLAGS_image} (${FLAGS_partitions})."
  499. FLAGS_partitions="$valid_partitions"
  500. # Validity checks
  501. if [ "$FLAGS_force" = "$FLAGS_TRUE" ]; then
  502. echo "
  503. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  504. ! INFO: ALL VALIDITY CHECKS WERE BYPASSED. YOU ARE ON YOUR OWN. !
  505. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  506. " >&2
  507. local i
  508. for i in $(seq 5 -1 1); do
  509. echo -n "\rStart in $i second(s) (^C to abort)... " >&2
  510. sleep 1
  511. done
  512. echo ""
  513. elif ! validity_check; then
  514. die "IMAGE ${FLAGS_image} IS NOT MODIFIED."
  515. fi
  516. fi
  517. resign_ssd_kernel "$FLAGS_image" || num_signed=$?
  518. debug_msg "Complete."
  519. if [ $num_signed -gt 0 -a $num_signed -le $num_given ]; then
  520. # signed something at least
  521. info "Successfully re-signed ${num_signed} of ${num_given} kernel(s)" \
  522. " on device ${FLAGS_image}."
  523. info "Please remember to reboot before updating the kernel on this device."
  524. else
  525. if [ "$FLAGS_no_resign_kernel" = "$FLAGS_FALSE" ]; then
  526. die "Failed re-signing kernels."
  527. fi
  528. fi
  529. }
  530. # People using this to process images may forget to add "-i",
  531. # so adding parameter check is safer.
  532. if [ "$#" -gt 0 ]; then
  533. flags_help
  534. die "Unknown parameters: $*"
  535. fi
  536. main