[Galaxy S II Original Android Development] Tungstwenty:
After lots of discussion about the famous "SuperBrick" issue on GT-I9100 4.0.4 stock kernels, I wrote a script to allow everyone to check it on their own and hopefully patch it if needed.

Main goal

- Detection

Detect if a STOCK kernel has MMC_CAP_ERASE enabled (unsafe) or not (safe).

I have validated it against XWLPG, XWLPM, XWLPO, XWLPT, XXLP5, XXLP5-CFRoot and all of them were detected correctly: safe on 4.0.3 kernels, unsafe on 4.0.4 ones.

I also checked it against Siyah 3.5.2 (despite knowing from the sources it's safe) and it was also correctly detected.

However, for custom kernels I don't expect the code patterns to be always the same and therefore it's possible that the detection is inconclusive - you will see that in the output.

Secondary goal

- Fixing (instructions provided, not the tools)

When an unsafe kernel is detected, provide instructions on how to patch the code so it's safe.

For that, you'll need:

* an external kernel unpack/repack script (just search the forum as there are several available)

* a Linux box

* a hex editor

* any other requirements for the repack script: CROSS_COMPILE, etc.

Requirements for this script

This is pretty much self contained and can be run on either:

* Linux

* Windows with Cygwin

Running on the device itself would be theoretically possible but it ultimately depends on the installed Busybox version, in particular the parameters accepted by the "grep" command.

On my v1.20.0-cm9 version it's not possible to make it work.

Sample outputs

Here are some executions against existing kernel images:

The latest XWLPT (4.0.4):

###############################################
# #
# GT-I9100 Kernel MMC_CAP_ERASE bug detection #
# By Tungstwenty - xdaforums.com #
# Tungstwenty@gmail.com #
# #
###############################################

Detecting safety of kernel: XWLPT/zImage
Kernel: Linux version 3.0.15-I9100XWLPT-CL941023 (dpi@DELL169) (gcc version 4.4.3 (GCC) ) #3 SMP PREEMPT Fri Jul 27 18:08:15 KST 2012

1 ocurrences of the bad code signature
0 ocurrences of the good code signature

***************
!!! WARNING !!!
***************

[COLOR="Red"]The kernel appears to have MMC_CAP_ERASE *enabled*, which is dangerous on many devices[/COLOR]

Unpacked kernel code stored at: XWLPT/zImage_unpacked
The unsafe instruction can be found at offset 0x00594ec0

==================== Disassembly of the instruction ====================

XWLPT/zImage_instruction: file format binary

Disassembly of section .data:

00000000 <.data>:
0: e3811b01 orr r1, r1, #1024 ; 0x400
========================================================================

*** Instructions for patching ***

- Choose one of the existing unpack/repack scripts
- Unpack the kernel code, initramfs, etc.
- Do a binary edit of the unpacked code
- At offset 0x00594ec0, replace "01 ?b 8? e3" with "00 ?b 8? e3" - change just the first byte to 00
- Repack the kernel, including the changed code and all original contents
- Re-run this script to confirm that the newly generated file no longer has MMC_CAP_ERASE enabled

XWLPG (4.0.3):

###############################################
# #
# GT-I9100 Kernel MMC_CAP_ERASE bug detection #
# By Tungstwenty - xdaforums.com #
# Tungstwenty@gmail.com #
# #
###############################################

Detecting safety of kernel: XWLPG/zImage
Kernel: Linux version 3.0.15-I9100XWLPG-CL619441 (dpi@DELL150) (gcc version 4.4.3 (GCC) ) #3 SMP PREEMPT Thu May 24 18:09:27 KST 2012

0 ocurrences of the bad code signature
1 ocurrences of the good code signature

[COLOR="SeaGreen"]The kernel appears to be good (MMC_CAP_ERASE disabled)[/COLOR]

XXLQ5-CFRoot (4.0.4):

###############################################
# #
# GT-I9100 Kernel MMC_CAP_ERASE bug detection #
# By Tungstwenty - xdaforums.com #
# Tungstwenty@gmail.com #
# #
###############################################

Detecting safety of kernel: XXLQ5_CFRoot/zImage
Kernel: Linux version 3.0.15-I9100XXLQ5-CL753921 (se.infra@SEP-85) (gcc version 4.4.3 (GCC) ) #3 SMP PREEMPT Thu Jun 28 14:16:15 KST 2012

1 ocurrences of the bad code signature
0 ocurrences of the good code signature

***************
!!! WARNING !!!
***************

[COLOR="Red"]The kernel appears to have MMC_CAP_ERASE *enabled*, which is dangerous on many devices[/COLOR]

Unpacked kernel code stored at: XXLQ5_CFRoot/zImage_unpacked
The unsafe instruction can be found at offset 0x00594ef4

==================== Disassembly of the instruction ====================

XXLQ5_CFRoot/zImage_instruction: file format binary

Disassembly of section .data:

00000000 <.data>:
0: e3811b01 orr r1, r1, #1024 ; 0x400
========================================================================

*** Instructions for patching ***

- Choose one of the existing unpack/repack scripts
- Unpack the kernel code, initramfs, etc.
- Do a binary edit of the unpacked code
- At offset 0x00594ef4, replace "01 ?b 8? e3" with "00 ?b 8? e3" - change just the first byte to 00
- Repack the kernel, including the changed code and all original contents
- Re-run this script to confirm that the newly generated file no longer has MMC_CAP_ERASE enabled

Finally, here's the expected output of a kernel after the patch has been applied.

I didn't actually do the entire kernel repack, but I changed the code and compressed the file in a similar way as it will appear in a "complete" zImage file.

Patched XWLPM:

###############################################
# #
# GT-I9100 Kernel MMC_CAP_ERASE bug detection #
# By Tungstwenty - xdaforums.com #
# Tungstwenty@gmail.com #
# #
###############################################

Detecting safety of kernel: XWLPM-patched/zImage
Kernel: Linux version 3.0.15-I9100XWLPM-CL837163 (dpi@DELL145) (gcc version 4.4.3 (GCC) ) #3 SMP PREEMPT Thu Jul 5 11:26:14 KST 2012

0 ocurrences of the bad code signature
1 ocurrences of the good code signature

[COLOR="Blue"]The kernel has been patched by this method to disable MMC_CAP_ERASE and should now be entirely safe[/COLOR]

Disclaimers

My main goal here is to provide

information

, not a one-click solution. I'm personally not worried about this issue since I run a kernel compiled from sources rather than a stock one.

Despite my best effort, I can't promise that:

- The detection will be flawless (although checks exist to make sure there's exactly 1 occurrence of either the "good code snippet" or the "bad code snippet" and an inconclusive result is reported if that's not the case)

- The patch will work or even be a runnable kernel (you might need to reflash another one from download mode). I have not performed the full unpack/repack process to test it out, although it's something already done elsewhere such as the CF-Root kernels and others.

That being said, enjoy ;)