Patchloader

From Exploitee.rs

Patchloader framework for packaging solutions for end-user install on GTV. Intended to make solutions approachable and installs automatic.

This page is only of use to developers. End-users need not worry about trying to figure out if this will do anything for them. An example of a deployed patchloader is here: Eject Bug Hack

#!/bin/sh
busybox tr -d '\r' <$0 | busybox sed '1,/^PATCHLOADER BEGIN$/ d; s|PATCHLOADER_SCRIPT=.*|PATCHLOADER_SCRIPT='$0'|' | /bin/sh 2>&1 | busybox tr -d '\r' | busybox sed '/^ *$/ d' # Keep comment here.
exit # Do not change or delete these first two lines or their comments, or DOS breaks.

# patchloader, by Catrane
#
# DEVELOPER INSTRUCTIONS
# -----------------
# Search for these sections below:
#  PRE PATCH, PATCH PAYLOAD, PATCH MD5, POST PATCH, and USER INSTRUCTIONS.
# Follow directions in each section.
# Do not modify any other sections.

# NECESSARY LINE
# -----------------
# Don't move or delete this line.  The second line of the file needs it.
PATCHLOADER BEGIN

# PRE PATCH
# -----------------
# Add any commands below which should be run prior to applying the patch.

# END PRE PATCH
# -----------------

# PATCH MD5
# -----------------
# Calculate the MD5 hash of the patch file and set here for validation.
# The easiest way to get the right value is to run the script with "GARBAGE" as
# the MD5 below and let it tell you the right value.
PATCH_MD5="GARBAGE" # PATCH_MD5="f7be3e1337c0d37b2850fabed5469d34  -"

# MD5 VALIDATION
# -----------------
PATCHLOADER_SCRIPT=$0
extractPatch ()
{
	busybox tr -d '\r' <$PATCHLOADER_SCRIPT | busybox sed '1,/^PATCHLOADER PAYLOAD START$/ d; /^PATCHLOADER PAYLOAD END$/,$ d'
}
MD5_CALC="`extractPatch | busybox md5sum`"
if [ "$PATCH_MD5" != "$MD5_CALC" ]
then
	if [ "GARBAGE" != "$PATCH_MD5" ]
	then
		echo "FATAL: Failure to validate patch integrity.  Please redownload and try again."
	else
		echo "patchloader MD5 hash is as follows.  Users should not see this message."
		echo "$MD5_CALC"
		echo "A copy of your extracted patch is located at /tmp/extract.patch for verification."
		extractPatch > /tmp/extract.patch
	fi
	exit
fi

# PATCH APPLICATION
# -----------------
extractPatch | busybox patch -p1
if [ "$?" != "0" ]
then
	echo "FATAL: Error patching.  Please redownload and try again."
	exit
fi

# POST PATCH
# -----------------
# Add any commands below which should be run prior to applying the patch.
# Include here any instructions for user to reboot if necessary.

# END POST PATCH
# -----------------

# USER INSTRUCTIONS
# -----------------
# The patch developer should move these instructions to directly above the
# "DEVELOPER INSTRUCTIONS" line atop this file, deleting these three
# developer instructions lines.
# 1) Save the full contents of this patchloader file to your computer.  This may
#    involve copying and pasting, or just downloading.
# 2) Connect to your device using adb.  adb usage is outside the scope of this
#    document.
# 3) Transfer this patchloader file to your device via adb so that it exists as
#    /patchloader.sh on your device.
#    e.g. adb push patchloader.sh /tmp/patchloader.sh
# 4) Execute the patchloader by running the following command via adb.
#    adb shell /bin/sh /tmp/patchloader.sh
# 5) Follow any instructions printed out by the patchloader.

# END OF SCRIPT.  DO NOT MOVE OR REMOVE
# -----------------
exit

# PATCH PAYLOAD
# -----------------
# Add patch content after the "PATCHLOADER PAYLOAD START" line here.
PATCHLOADER PAYLOAD START
<delete this line and insert payload here>
PATCHLOADER PAYLOAD END