Difference between revisions of "Eject Bug Hack"

From Exploitee.rs
Jump to navigationJump to search
(Add link to Patchloader for developer reference.)
(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 6: Line 6:


<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 12: Line 14:
# USER INSTRUCTIONS
# 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
# 1) Save the full contents of this patchloader file to your computer.  This may
#    involve copying and pasting, or just downloading.
#    involve copying and pasting, or just downloading.
Line 18: Line 23:
# 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.


Line 30: Line 35:
# 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 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="7fc5242685822432b8220b4e760629bf  -" # 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" ]
if [ "$?" != "0" ]
then
then
echo "FATAL: Patchloader truncated.  Please redownload and try again."
echo "FATAL: Error patching.  Please redownload and try again."
exit
exit
fi
fi


# PRE PATCH
# POST PATCH
# -----------------
# -----------------
# Add any commands below which should be run prior to applying the patch.
# Add any commands below which should be run prior to applying the patch.
# Include here any instructions for user to reboot if necessary.
chmod 755 /system/bin/eject_bug_workaround.sh
rm $PATCHLOADER_SCRIPT
echo Please reboot your GTV via the power cord or ctrl-alt-del for this patch
echo to take effect.
echo This patch has a 60 second safety when loaded, so it will not actually run
echo until the GTV has been fully booted for 60 seconds.


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


# PATCH PAYLOAD
# PATCH PAYLOAD
# -----------------
# -----------------
# Add patch content after the "echo" line here. Be sure to escape any
# Add patch content after the "PATCHLOADER PAYLOAD START" line here.
# characters as necessary and adjust dos2unix/unix2dos settings as appropriate.
PATCHLOADER PAYLOAD START
echo '
---
---
  init.eagle.rc                      |    5 +++
  init.eagle.rc                      |    5 +++
Line 126: Line 182:
+LASTDECT=$(date)
+LASTDECT=$(date)
+
+
+logcat -b main DiscPlayerManager:I *:S|busybox awk '"'"'/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {system("echo onStartCommand: com.sony.btv.discplayer.EJECT_DISC")}'"'"'|while busybox awk '"'"'/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {exit 0}'"'"'
+logcat -b main DiscPlayerManager:I *:S|busybox awk '/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {system("echo onStartCommand: com.sony.btv.discplayer.EJECT_DISC")}'|while busybox awk '/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {exit 0}'
+do
+do
+ if [ "$LASTDECT" != "$(date)" ]
+ if [ "$LASTDECT" != "$(date)" ]
Line 141: Line 197:
--  
--  
1.7.6.1
1.7.6.1
' | busybox dos2unix -u > patchloader.patch
PATCHLOADER PAYLOAD END
 
# PATCH MD5
# -----------------
# 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
# 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="023aa9d02d4ef49af781fc65215d4fc7  patchloader.patch"
 
# MD5 VALIDATION
# -----------------
MD5_CALC="`busybox md5sum patchloader.patch`"
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"
fi
rm patchloader.patch
exit
fi
 
# PATCH APPLICATION
# -----------------
busybox patch -p1 < patchloader.patch
if [ "$?" != "0" ]
then
echo "FATAL: Error patching.  Please redownload and try again."
rm patchloader.patch
exit
fi
rm patchloader.patch
 
# 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.
chmod 755 /system/bin/eject_bug_workaround.sh
rm $0
echo Please reboot your GTV via the power cord or ctrl-alt-del for this patch
echo to take effect.
echo This patch has a 60 second safety when loaded, so it will not actually run
echo until the GTV has been fully booted for 60 seconds.
 
# END POST PATCH
# -----------------
 
# PATCHLOADER END FILE SIGNATURE
# -----------------
# Do not remove this PATCHLOADER END FILE SIGNATURE from the end of the file or
# place anything after it.  Pretty much, the text "PATCHLOADER END FILE SIGNATURE"
# needs to be in the last five lines so we can make sure nothing is missing.
</pre>
</pre>

Revision as of 02:58, 17 March 2012

Here is a patchloader that consists of a script to detect and recover from the eject bug and the modification necessary to make that script be run as a service. The user can follow the instructions below to install this on their GTV.

The install procedure and the workaround itself are run as root, so there is of course the risk that the user could brick their system with no recourse for recovery. As with any root-related activity, if you can't afford to lose it, don't do it.

Developers: The patchloader framework used for this can be found here and used for deploying other fixes or updates.

#!/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
# Bluray disc eject workaround, by Catrane
#
# 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.

# 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="7fc5242685822432b8220b4e760629bf  -" # 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.
chmod 755 /system/bin/eject_bug_workaround.sh
rm $PATCHLOADER_SCRIPT
echo Please reboot your GTV via the power cord or ctrl-alt-del for this patch
echo to take effect.
echo This patch has a 60 second safety when loaded, so it will not actually run
echo until the GTV has been fully booted for 60 seconds.

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

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

# PATCH PAYLOAD
# -----------------
# Add patch content after the "PATCHLOADER PAYLOAD START" line here.
PATCHLOADER PAYLOAD START
---
 init.eagle.rc                      |    5 +++
 system/bin/eject_bug_workaround.sh |   53 ++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 0 deletions(-)
 create mode 100644 system/bin/eject_bug_workaround.sh

diff --git a/init.eagle.rc b/init.eagle.rc
index 2004d8a..685ce6f 100644
--- a/init.eagle.rc
+++ b/init.eagle.rc
@@ -329,6 +329,7 @@ on init
     setprop com.sony.btv.discplayer.enable 1
     export DISCPLAYER_KEEP_DMIX_ASIS true
     export DISCPLAYER_LOG_VERBOSE true
+    start ejectworkaround
 
   ## for lighttpd
     mkdir /var/log/lighttpd 0750 system system
@@ -439,6 +440,10 @@ service discplayer /system/bin/discplayer
     user root
     group system
 
+service ejectworkaround /system/bin/eject_bug_workaround.sh 60
+    user root
+    group system
+
 on property:com.sony.btv.discplayer.enable=1
     start discplayer
     start discservice
diff --git a/system/bin/eject_bug_workaround.sh b/system/bin/eject_bug_workaround.sh
new file mode 100644
index 0000000..1f4f282
--- /dev/null
+++ b/system/bin/eject_bug_workaround.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# Reason:
+# There is a known problem that, for an unknown reason, disc eject fails on rooted NSZ-GT1 systems.
+# Side effects of this problem include the possibility that the disc is not ejected and in all cases
+# the problem that discs cannot be played after attempted eject.
+# Rebooting the system fixes the problem, but this solution eliminates the need for reboot.
+
+# Alternative solutions:
+# Restart the device via the power cord or ctrl-alt-del.
+
+# Caveats:
+# This fix does not detect the occurrence of any error, but rather occurrence of a known event which
+# reliably preceeds the error and is reliably followed by the error situation.
+# Error messages and odd behavior may be observed onscreen at the moment of eject, though none of
+# this causes any side-effects.
+
+# Stimulus:
+# From command: logcat -b main
+# Output: I DiscPlayerManager: onStartCommand: com.sony.btv.discplayer.EJECT_DISC
+
+# Response:
+# setprop com.sony.btv.discplayer.enable 0
+# busybox eject /dev/block/sr0
+# setprop com.sony.btv.discplayer.enable 1
+
+# Usage:
+#   eject_bug_workaround.sh &
+#   - Runs workaround in background.
+#   eject_bug_workaround.sh 600 &
+#   - Sleeps for 600 seconds before running, all in background.
+#   - Useful for ensuring a window of recovery in case any side-effects occur.
+
+if [ ! -z "$1" ]
+then
+	sleep $1
+fi
+
+LASTDECT=$(date)
+
+logcat -b main DiscPlayerManager:I *:S|busybox awk '/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {system("echo onStartCommand: com.sony.btv.discplayer.EJECT_DISC")}'|while busybox awk '/onStartCommand: com.sony.btv.discplayer.EJECT_DISC/ {exit 0}'
+do
+	if [ "$LASTDECT" != "$(date)" ]
+	then
+		echo Detected eject failure.
+		setprop com.sony.btv.discplayer.enable 0
+		busybox eject /dev/block/sr0
+		setprop com.sony.btv.discplayer.enable 1
+		echo Eject failure repair complete.
+		LASTDECT=$(date)
+	fi
+done
+
-- 
1.7.6.1
PATCHLOADER PAYLOAD END