Skip to content

Commit

Permalink
repartition
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKing062 committed Feb 25, 2024
0 parents commit 389724e
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/repartition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: patch-recovery-repartition

on:
workflow_dispatch:
inputs:
IMG_URL:
description: 'URL of recovery.img'
required: true
default: ''
PART_NAME:
description: 'shrink a partition(keep empty if the partition is not ext4)'
required: false
default: ''
NEW_PART_SIZE_MB:
description: 'new size for partition(unit is MB)'
required: false
default: ''

jobs:
build:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v4
with:
ref: main
path: main

- name: patch
run: |
sudo apt update
sudo apt -y install android-sdk-libsparse-utils
curl -o recovery.img -L "${{ github.event.inputs.IMG_URL }}"
curl -o magisk.apk -L $(curl -s https://api.github.com/repos/topjohnwu/Magisk/releases/latest | grep -Po '(?<=download_url\"\: \").*Magisk.*apk')
wget https://raw.githubusercontent.com/magojohnji/magiskboot-linux/main/x86_64/magiskboot
mkdir zzz
busybox unzip -oq magisk.apk -d zzz
mv main/boot_patch.sh ./
chmod +x boot_patch.sh
./boot_patch.sh
dd if=/dev/zero of=ramdisk.img bs=1M count=32
mkfs.ext4 ramdisk.img
mkdir ramdisk
sudo mount -t ext4 -o rw ramdisk.img ramdisk
cd ramdisk
sudo cpio -idv < ../ramdisk.cpio
cd ..
sudo umount ramdisk
- name: generate partition
if: |
github.event.inputs.PART_NAME != null
&& github.event.inputs.NEW_PART_SIZE_MB != null
run: |
dd if=/dev/zero of=temp.img bs=1M count=${{ github.event.inputs.NEW_PART_SIZE_MB }}
mkfs.ext4 temp.img
img2simg temp.img ${{ github.event.inputs.PART_NAME }}.img
- name: Upload to Release
uses: softprops/action-gh-release@v1
with:
files: |
ramdisk.img
${{ github.event.inputs.PART_NAME }}.img
name: ${{ github.run_id }}
tag_name: ${{ github.run_id }}
body: |
URL: ${{ github.event.inputs.IMG_URL }}
PART: ${{ github.event.inputs.PART_NAME }}
PART_SIZE: ${{ github.event.inputs.NEW_PART_SIZE_MB }}M
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
109 changes: 109 additions & 0 deletions boot_patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Flags
export KEEPVERITY=false
export KEEPFORCEENCRYPT=false
export RECOVERYMODE=false
export PREINITDEVICE=cache

#########
# Unpack
#########

chmod -R 755 .

CHROMEOS=false

echo "Unpacking boot image"
./magiskboot unpack recovery.img

case $? in
0 ) ;;
1 )
echo "Unsupported/Unknown image format"
;;
2 )
echo "ChromeOS boot image detected"
;;
* )
echo "Unable to unpack boot image"
;;
esac

###################
# Ramdisk Restores
###################

# Test patch status and do restore
echo "Checking ramdisk status"
if [ -e ramdisk.cpio ]; then
./magiskboot cpio ramdisk.cpio test
STATUS=$?
else
# Stock A only legacy SAR, or some Android 13 GKIs
STATUS=0
fi
case $((STATUS & 3)) in
0 ) # Stock boot
echo "Stock boot image detected"
SHA1=$(./magiskboot sha1 recovery.img)
cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null
;;
1 ) # Magisk patched
echo "Magisk patched boot image detected"
# Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=$(./magiskboot cpio ramdisk.cpio sha1)
./magiskboot cpio ramdisk.cpio restore
cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null
;;
2 ) # Unsupported
echo "Boot image patched by unsupported programs"
echo "Please restore back to stock boot image"
;;
esac

# Work around custom legacy Sony /init -> /(s)bin/init_sony : /init.real setup
INIT=init
if [ $((STATUS & 4)) -ne 0 ]; then
INIT=init.real
fi

##################
# Ramdisk Patches
##################

echo "- Patching ramdisk"
export SKIP64=""
mkdir cpiotmp
cd cpiotmp
sudo cpio -idv < ../ramdisk.cpio
cd ..
export cpu_abi=$(grep -o 'ro.product.cpu.abi=[^ ]*' cpiotmp/prop.default | cut -d '=' -f 2)
if [ "$cpu_abi" != "arm64-v8a" ]; then
cpu_abi=armeabi-v7a
SKIP64="#"
fi

echo -n "RANDOMSEED=" > config
tr -dc A-Za-z0-9 </dev/urandom | head -c 8 >> config
echo -ne "\n" >> config
echo "KEEPVERITY=$KEEPVERITY" >> config
echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "RECOVERYMODE=$RECOVERYMODE" >> config
echo "PREINITDEVICE=$PREINITDEVICE" >> config
[ ! -z $SHA1 ] && echo "SHA1=$SHA1" >> config

# Compress to save precious ramdisk space
./magiskboot compress=xz zzz/lib/armeabi-v7a/libmagisk32.so magisk32.xz
$SKIP64 ./magiskboot compress=xz zzz/lib/$cpu_abi/libmagisk64.so magisk64.xz
./magiskboot compress=xz zzz/assets/stub.apk stub.xz

./magiskboot cpio ramdisk.cpio \
"add 0750 $INIT zzz/lib/$cpu_abi/libmagiskinit.so" \
"mkdir 0750 overlay.d" \
"mkdir 0750 overlay.d/sbin" \
"add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \
"$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
"patch" \
"backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \
"add 000 .backup/.magisk config"

0 comments on commit 389724e

Please sign in to comment.