Difference between revisions of "Patchloader"

From Exploitee.rs
(Add link to example.)
(So the sh in busybox is extremely incompatible with DOS CRLF. That sucks. Rework the script a bunch to not use temp files and actually work with DOS CRLF. Tested in both dos and unix formats.)
 
Line 5: Line 5:
  
 
<pre>#!/bin/sh
 
<pre>#!/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
 
# patchloader, by Catrane
Line 15: Line 17:
 
# Do not modify any other sections.
 
# Do not modify any other sections.
  
# PATCHLOADER TRUNCATION CHECK
+
# NECESSARY LINE
 
# -----------------
 
# -----------------
busybox tail -5 $0 | grep -q "[P]ATCHLOADER END FILE SIGNATURE"
+
# Don't move or delete this lineThe second line of the file needs it.
if [ "$?" != "0" ]
+
PATCHLOADER BEGIN
then
 
echo "FATAL: Patchloader truncatedPlease redownload and try again."
 
exit
 
fi
 
  
 
# PRE PATCH
 
# PRE PATCH
Line 30: Line 28:
 
# END PRE PATCH
 
# END PRE PATCH
 
# -----------------
 
# -----------------
 
# PATCH PAYLOAD
 
# -----------------
 
# Add patch content after the "echo" line here.  Be sure to escape any
 
# characters as necessary and adjust dos2unix/unix2dos settings as appropriate.
 
echo '
 
<delete this line and insert payload here>
 
' | busybox dos2unix -u > patchloader.patch
 
  
 
# PATCH MD5
 
# PATCH MD5
 
# -----------------
 
# -----------------
 
# Calculate the MD5 hash of the patch file and set here for validation.
 
# Calculate the MD5 hash of the patch file and set here for validation.
# Note that the script above starts and ends with a linefeed, so the best way to
+
# The easiest way to get the right value is to run the script with "GARBAGE" as
# get the right value is to run the script with "GARBAGE" as the MD5 below and
+
# the MD5 below and let it tell you the right value.
# let it tell you the right value.
+
PATCH_MD5="GARBAGE" # PATCH_MD5="f7be3e1337c0d37b2850fabed5469d34  -"
PATCH_MD5="GARBAGE" # PATCH_MD5="f7be3e1337c0d37b2850fabed5469d34  patchloader.patch"
 
  
 
# MD5 VALIDATION
 
# MD5 VALIDATION
 
# -----------------
 
# -----------------
MD5_CALC="`busybox md5sum patchloader.patch`"
+
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" ]
 
if [ "$PATCH_MD5" != "$MD5_CALC" ]
 
then
 
then
Line 58: Line 52:
 
echo "patchloader MD5 hash is as follows.  Users should not see this message."
 
echo "patchloader MD5 hash is as follows.  Users should not see this message."
 
echo "$MD5_CALC"
 
echo "$MD5_CALC"
 +
echo "A copy of your extracted patch is located at /tmp/extract.patch for verification."
 +
extractPatch > /tmp/extract.patch
 
fi
 
fi
rm patchloader.patch
 
 
exit
 
exit
 
fi
 
fi
Line 65: Line 60:
 
# PATCH APPLICATION
 
# PATCH APPLICATION
 
# -----------------
 
# -----------------
busybox patch -p1 < patchloader.patch
+
extractPatch | busybox patch -p1
 
if [ "$?" != "0" ]
 
if [ "$?" != "0" ]
 
then
 
then
 
echo "FATAL: Error patching.  Please redownload and try again."
 
echo "FATAL: Error patching.  Please redownload and try again."
rm patchloader.patch
 
 
exit
 
exit
 
fi
 
fi
rm patchloader.patch
 
  
 
# POST PATCH
 
# POST PATCH
Line 93: Line 86:
 
# 3) Transfer this patchloader file to your device via adb so that it exists as
 
# 3) Transfer this patchloader file to your device via adb so that it exists as
 
#    /patchloader.sh on your device.
 
#    /patchloader.sh on your device.
#    e.g. adb push patchloader.sh /patchloader.sh
+
#    e.g. adb push patchloader.sh /tmp/patchloader.sh
 
# 4) Execute the patchloader by running the following command via adb.
 
# 4) Execute the patchloader by running the following command via adb.
#    adb shell /bin/sh /patchloader.sh
+
#    adb shell /bin/sh /tmp/patchloader.sh
 
# 5) Follow any instructions printed out by the patchloader.
 
# 5) Follow any instructions printed out by the patchloader.
  
# PATCHLOADER END FILE SIGNATURE
+
# END OF SCRIPT.  DO NOT MOVE OR REMOVE
 +
# -----------------
 +
exit
 +
 
 +
# PATCH PAYLOAD
 
# -----------------
 
# -----------------
# Do not remove this PATCHLOADER END FILE SIGNATURE from the end of the file or
+
# Add patch content after the "PATCHLOADER PAYLOAD START" line here.
# place anything after it.  Pretty much, the text "PATCHLOADER END FILE SIGNATURE"
+
PATCHLOADER PAYLOAD START
# needs to be in the last five lines so we can make sure nothing is missing.
+
<delete this line and insert payload here>
 +
PATCHLOADER PAYLOAD END
 
</pre>
 
</pre>

Latest revision as of 03:00, 17 March 2012

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