Introducing z4mod – Android mod, RFS free (lagfree, nolag, you name it)

[UPDATE:]
Don’t use this method. It will wear out the moviNAND device on the SGS. I will post soon an update.zip method which should address this issue – ie, formatting each RFS partition on the SGS and writing back (NOT dumping) it’s contents. (Thanks to Unhelpful for all his valuable information)

[UPDATE 2:]
Fixed and updated. See here: https://z4ziggy.wordpress.com/2010/10/02/z4mod-updated/

I’m introducing z4mod – a new Android mod to break free from Samsungs’ RFS death grip. Eventually this should create a complete mod without RFS usage. For now, it breaks because the data contained in the RFS image doesn’t fit a same-sized ext2 image. I will fix this later on. For now, you can see where I’m aiming at:

WARNING: USE THIS ON YOUR OWN RISK.


#!/bin/bash
###############################################################################
#
# z4build by Elia Yehuda, aka z4ziggy
#
# script to automate z4mod creation - a ROM mod without RFS.
# Released under the GPLv2
#
# many thanks goto various coders & Android hackers out there who made this
# possible: supercurio, Unhelpful, dkcldark, XDA & modaco forums.
#
# WARNING:
# FOR YOUR OWN SAFETY, IF YOU CAN'T FOLLOW THE SCRIPT, AVOID USING IT.
# USE AT YOUR OWN RISK! NO WARRANTIES WHAT SO EVER!
#
# zImage extraction script copied from here:
# http://forum.xda-developers.com/wiki/index.php?title=Extract_initramfs_from_zImage
#
# kernel_repacker taken from here:
# http://forum.xda-developers.com/showthread.php?t=789712
#
# TODO:
# - tar the result files - would do so for version 1.0
# - Add /data & /dbdata support (and maybe /efs) for complete RFS removal
# - Add a simpler kernel_repacker
# - Rewrite all this to an initramfs startup script which does all this - in 
#   real-time, to any ROM - will simplify the process (no kernel unpack/repack)
#   and speed things up but might be too dangerous
#
# Changelog:
# - 0.1 initial release
#
###############################################################################
#
# SETTINGS: You must provide valid paths to adb & kernel_repacker below
#
###############################################################################

# where to find kernel_repack/editor.sh
KERNEL_REPACKER=/home/ziggy/code/android/kernel_repacker/editor.sh
# where to find adb
ADB=/home/ziggy/code/android/android-sdk-linux_x86/tools/adb
# rebuild zImage
REBUILD_ZIMAGE=yes
# which format would you like to change your filesystem to:
# only ext2 is supported atm - we're still using the built-in kernel
FORMAT=ext2

# No need to change anything below this

C_H1="\033[1;37m"        # highlight text 1
C_ERR="\033[1;31m"
C_CLEAR="\033[1;0m"

# helper functions:

printhl() {
	printf "${C_H1}${1}${C_CLEAR} \n"
}

printerr() {
	printf "${C_ERR}${1}${C_CLEAR} \n"
}

exit_error() {
	printerr "$1"
	rm -r /tmp/z4mod
	if [ -d "$1" ]; then
		popd
	fi
	exit 1
}

###############################################################################
#
# make sure we have everything we need to run
#
###############################################################################

if [ -d "$1" ]; then
        pushd $1
fi

# Making sure we're root
if [ "$(id -u)" != "0" ]; then
	exit_error "This script must be run as root"
fi

printhl "\n[I] z4build begins, going to try and build a z4mod"

# Making sure we have everything
for check in zImage cache.rfs factoryfs.rfs; do
	if [ ! -f $check ]; then
		exit_error "[E] Can't find $check"
	fi
done
if [ ! -f "${ADB}" ]; then
	exit_error "[E] Can't find adb"
fi
if [ ! -f "${KERNEL_REPACKER}" ]; then
	exit_error "[E] Can't find kernel_repacker"
fi

# We can start working
rm -r /tmp/z4mod
mkdir -p /tmp/z4mod/{rfs,${FORMAT},initramfs}

if [ "${REBUILD_ZIMAGE}" != "yes" ]; then
	printerr "[W] Skipping zImage build (REBUILD_ZIMAGE disabled)"
else
	###############################################################################
	#
	# extract the initramfs.img from zImage
	#
	###############################################################################

	zImage=zImage
	#=======================================================
	# find start of gziped kernel object in the zImage file:
	#=======================================================
	pos=`grep -P -a -b --only-matching $'\x1F\x8B\x08' $zImage | cut -f 1 -d :`
	printhl "[I] Extracting kernel image from $zImage (start = $pos)"
	dd if=$zImage bs=1 skip=$pos | gunzip > /tmp/kernel.img
	#===========================================================================
	# find start and end of the "cpio" initramfs image inside the kernel object:
	# ASCII cpio header starts with '070701'
	# The end of the cpio archive is marked with an empty file named TRAILER!!!
	#===========================================================================
	start=`grep -a -b --only-matching '070701' /tmp/kernel.img | head -1 | cut -f 1 -d :`
	end=`grep -a -b --only-matching 'TRAILER!!!' /tmp/kernel.img | head -1 | cut -f 1 -d :`
	end=$((end + 10))
	count=$((end - start))
	if (($count < 0)); then
		exit_error "[E] Couldn't match start/end of the initramfs image."
	fi
	printhl "[I] Extracting initramfs image from /tmp/kernel.img (start = $start, end = $end)"
	dd if=/tmp/kernel.img bs=1 skip=$start count=$count > /tmp/z4mod/initramfs.img

	###############################################################################
	#
	# extract initramfs.img, patch, repack initramfs, and repack a new zImage
	#
	###############################################################################

	(cd /tmp/z4mod/initramfs/; cpio -i --no-absolute-filenames < /tmp/z4mod/initramfs.img)

	# Making sure we are not re-patching the initramfs scripts
	if [ "`grep "mount ${FORMAT} /dev/block/stl9 /system" initramfs/*.rc`" ]; then
		printhl "[I] Target initramfs already supports ${FORMAT}, skipping patch"
	else
		printhl "[I] Patching startup scripts to add ${FORMAT} support"
		sed -i 's|mount rfs /dev/block/stl9 /system*|mount '${FORMAT}' /dev/block/stl9 /system check=no \|\| mount rfs /dev/block/stl9 /system|g' /tmp/z4mod/initramfs/*.rc
		sed -i 's|mount rfs /dev/block/stl11 /cache*|mount '${FORMAT}' /dev/block/stl11 /cache nosuid nodev check=no \|\| mount rfs /dev/block/stl11 /cache|g' /tmp/z4mod/initramfs/*.rc
		printhl "[I] Saving patched initramfs.img"
	cp /bin/bash /tmp/z4mod/initramfs/sbin/bash
		(cd /tmp/z4mod/initramfs/; find . | cpio -H newc -o > /tmp/z4mod/initramfs.img)
		printhl "[I] Repacking zImage"
		OLD_PATH=`pwd`
		pushd `dirname ${KERNEL_REPACKER}`
		rm new_zImage >/dev/null 2>&1
		${KERNEL_REPACKER} ${OLD_PATH}/zImage /tmp/z4mod/initramfs.img
		popd
		if [ ! -f `dirname ${KERNEL_REPACKER}`/new_zImage ]; then
			exit_error "[E] Could not build new zImage"
		fi
		printhl "[I] Moving new kernel to current dir"
		mv `dirname ${KERNEL_REPACKER}`/new_zImage zImage
	fi

fi

###############################################################################
#
# convert the cache.rfs and factoryfs.rfs to ${FORMAT}
#
###############################################################################

echo
for PARTITION in cache factoryfs; do
	if [ -f ${PARTITION}.${FORMAT} ]; then
		printerr "[W] Using existing ${PARTITION}.${FORMAT}, skipping build"
	else
		if [ -f ${PARTITION}.tar.bz2 ]; then
			printerr "[W] Using existing ${PARTITION}.tar.bz2, skipping build"
		else
			printhl "[I] Uploading ${PARTITION}.rfs"
			${ADB} push ${PARTITION}.rfs /sdcard/${PARTITION}.rfs >/dev/null 2>&1
	        	printhl "[I] Compressing ${PARTITION}.rfs contents on the phone"
			# must be a better way to wake up the phone...
			printhl "[I] Phone must be ACTIVE - not just turned on, so you can apply root permissions"
			printhl "[I] Press [ENTER] when ready."
			read key
			${ADB} shell su -c "busybox mknod /dev/block/z4mod b 7 200; busybox losetup /dev/block/z4mod /sdcard/${PARTITION}.rfs; busybox mkdir -p /sdcard/z4mod; busybox mount /dev/block/z4mod /sdcard/z4mod; cd /sdcard/z4mod; busybox tar zcvf /sdcard/${PARTITION}.tar.bz2 *; cd /; busybox umount /sdcard/z4mod; busybox losetup -d /dev/block/z4mod"
			printhl "[I] Pulling ${PARTITION}.tar.bz2"
			${ADB} pull /sdcard/${PARTITION}.tar.bz2 ${PARTITION}.tar.bz2 > /dev/null 2>&1
			printhl "[I] Cleanning up after ${PARTITION}.rfs"
			# must be a better way to wake up the phone...
		        printhl "[I] Phone must be ACTIVE - not just turned on, so you can apply root permissions"
		        printhl "[I] Press [ENTER] when ready."
		        read key
			${ADB} shell su -c "busybox rm /sdcard/${PARTITION}.rfs; busybox rm /sdcard/${PARTITION}.tar.bz2; busybox rm -r /sdcard/z4mod; busybox rm -r /dev/block/z4mod"
		        if [ ! -f ${PARTITION}.tar.bz2 ]; then
				exit_error "[E] Failed compressing ${PARTITION} partition"
			fi
		fi
		SIZE=`ls -lk ${PARTITION}.rfs | awk '{print $5}'`
	        printhl "[I] Creating ${PARTITION}.${FORMAT}, size=${SIZE} kb"
	        dd if=/dev/zero of=${PARTITION}.${FORMAT} bs=1024 count=${SIZE} >/dev/null 2>&1
	        printhl "[I] Formatting ${PARTITION}.${FORMAT}"
	        mkfs -t $FORMAT -m 0 -F ${PARTITION}.${FORMAT} >/dev/null 2>&1
		printhl "[I] Extracting ${PARTITION} partition"
		mount -o loop  ${PARTITION}.${FORMAT} /tmp/z4mod/${FORMAT}
		tar zxvf ${PARTITION}.tar.bz2 -C /tmp/z4mod/${FORMAT}/
		umount /tmp/z4mod/${FORMAT}
	fi
done

rm -r /tmp/z4mod
if [ -d "$1" ]; then
	popd
fi
printhl "[I] Done."

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s