From 90be93fd905a48bc74ab4ee470dc0df2bb702860 Mon Sep 17 00:00:00 2001 From: kxtzownsu Date: Wed, 6 Mar 2024 15:13:02 -0500 Subject: [PATCH] hai --- builder/builder.sh | 60 +++++++++++++++++++++++++++++- builder/functions.sh | 21 +++++++++++ builder/scripts/factory_install.sh | 30 +++++++-------- builder/scripts/functions.sh | 0 builder/scripts/tpmutil.sh | 0 5 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 builder/functions.sh mode change 100644 => 100755 builder/scripts/factory_install.sh mode change 100644 => 100755 builder/scripts/functions.sh mode change 100644 => 100755 builder/scripts/tpmutil.sh diff --git a/builder/builder.sh b/builder/builder.sh index c207ed7..df0ebe4 100644 --- a/builder/builder.sh +++ b/builder/builder.sh @@ -1,5 +1,61 @@ #!/bin/bash -VERSION=1 +SCRIPT_DIR=$(dirname "$0") +SCRIPT_DIR=${SCRIPT_DIR:-"."} +. "$SCRIPT_DIR/functions.sh" -echo "KVS Builder $VERSION" \ No newline at end of file +VERSION=1 +echo "KVS Shim Builder v$VERSION" +echo "-=-=-=-=-=-=-=-=-=-" +[ "$EUID" -ne 0 ] && error "Please run KVS builder as root" +[ "$1" == "" ] && error "Shim not specified, remember, usage is $0 " +echo "Requirements: cgpt, e2fsprogs, sgdisk" +echo "-=-=-=-=-=-=-=-=-=-" + +STATE_SIZE=$((4 * 1024 * 1024)) # 4MiB +STATE_MNT=$(mktemp -d) +LOOPDEV=$(losetup -f) + +create_stateful() { + log "Creating KVS/Stateful partition" + local final_sector=$(get_final_sector "$LOOPDEV") + local sector_size=$(get_sector_size "$LOOPDEV") + cgpt add "$LOOPDEV" -i 1 -b $((final_sector + 1)) -s $((STATE_SIZE / sector_size)) -t data -l KVS + partx -u -n 1 "$LOOPDEV" + mkfs.ext4 -F -L KVS "${LOOPDEV}p1" &> /dev/null + + sync + sleep 0.2 + + mount "${LOOPDEV}p1" "$STATE_MNT" + touch "$STATE_MNT/dev_image/etc/lsb-factory" + chmod -R +x "$STATE_MNT" + + umount "$STATE_MNT" + rmdir "$STATE_MNT" +} + +shrink_root() { + log "Shrinking ROOT" + + enable_rw_mount "${LOOPDEV}p3" + suppress e2fsck -fy "${LOOPDEV}p3" + suppress resize2fs -M "${LOOPDEV}p3" + disable_rw_mount "${LOOPDEV}p3" + + local sector_size=$(get_sector_size "$LOOPDEV") + local block_size=$(tune2fs -l "${LOOPDEV}p3" | grep "Block size" | awk '{print $3}') + local block_count=$(tune2fs -l "${LOOPDEV}p3" | grep "Block count" | awk '{print $3}') + + log_debug "sector size: ${sector_size}, block size: ${block_size}, block count: ${block_count}" + + local original_sectors=$("$CGPT" show -i 3 -s -n -q "$LOOPDEV") + local original_bytes=$((original_sectors * sector_size)) + + local resized_bytes=$((block_count * block_size)) + local resized_sectors=$((resized_bytes / sector_size)) + + log_info "Resizing ROOT from $(format_bytes ${original_bytes}) to $(format_bytes ${resized_bytes})" + "$CGPT" add -i 3 -s "$resized_sectors" "$LOOPDEV" + partx -u -n 3 "$LOOPDEV" +} \ No newline at end of file diff --git a/builder/functions.sh b/builder/functions.sh new file mode 100644 index 0000000..ba72fd2 --- /dev/null +++ b/builder/functions.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +COLOR_RESET="\033[0m" +COLOR_BLACK_B="\033[1;30m" +COLOR_RED_B="\033[1;31m" +COLOR_GREEN="\033[0;32m" +COLOR_GREEN_B="\033[1;32m" +COLOR_YELLOW="\033[0;33m" +COLOR_YELLOW_B="\033[1;33m" +COLOR_BLUE_B="\033[1;34m" +COLOR_MAGENTA_B="\033[1;35m" +COLOR_CYAN_B="\033[1;36m" + +log(){ + printf '${COLOR_GREEN}Info: %b${COLOR_RESET}\n' "$*" +} + +error(){ + printf '${COLOR_RED_B}ERR: &b${COLOR_RESET}\n' "$*" + exit 1 +} \ No newline at end of file diff --git a/builder/scripts/factory_install.sh b/builder/scripts/factory_install.sh old mode 100644 new mode 100755 index bc92206..dcf1654 --- a/builder/scripts/factory_install.sh +++ b/builder/scripts/factory_install.sh @@ -9,12 +9,6 @@ if [[ $(/usr/bin/id -u) -ne 0 ]]; then exit fi -# source the file multiple times because it wasn't working :3 -. ./functions.sh -. ./tpmutil.sh -source ./functions.sh -source ./tpmutil.sh - version=1 GITHUB_URL="https://github.com/kxtzownsu/KVS" tpmver=$(tpmc tpmver) @@ -25,8 +19,6 @@ else tpmdaemon="tscd" fi -echo $tpmdaemon - # give me thy kernver NOW case "$(crossystem tpm_kernver)" in "0x00000000") @@ -49,17 +41,24 @@ esac # detect if booted from usb boot or from recovery boot if [ "$(crossystem mainfw_type)" == "recovery" ]; then source tpmutil.sh + source functions mkdir /mnt/state &2> /dev/zero mount /dev/disk/by-label/KVS /mnt/state elif [ "$(crossystem mainfw_type)" == "developer" ]; then # panic "non-reco" - echo "" + clear + . ./functions.sh + . ./tpmutil.sh + source ./functions.sh + source ./tpmutil.sh + style_text "YOU ARE RUNNING A DEBUG VERSION OF KVS, THIS WAS OPTIMIZED TO RUN ON CHROMEOS ONLY! ALL ACTIONS ARE PURELY VISUAL AND NOT FUNCTIONAL IN THIS MODE!!!" + sleep 5 clear fi credits(){ echo "KVS: Kernel Version Switcher" - echo "V$version" + echo "v$version" echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-" echo "kxtzownsu - Writing KVS, Providing kernver 0 & kernver 1 files." echo "??? - Providing kernver 2 files." @@ -74,8 +73,11 @@ endkvs(){ main(){ + if [ $() ] echo "KVS: Kernel Version Switcher v$version" echo "Current kernver: $kernver" + echo "TPM Version: $tpmver" + echo "TPMD: $tpmdaemon" echo "=-=-=-=-=-=-=-=-=-=-" echo "1) Set New kernver" echo "2) Backup kernver (WIP, Kinda Broken)" @@ -87,8 +89,6 @@ main(){ } -#while true; do -# main -#done - -panic "tpmd-not-killed" \ No newline at end of file +while true; do + main +done \ No newline at end of file diff --git a/builder/scripts/functions.sh b/builder/scripts/functions.sh old mode 100644 new mode 100755 diff --git a/builder/scripts/tpmutil.sh b/builder/scripts/tpmutil.sh old mode 100644 new mode 100755