[HD2 Android Q&A, Help & Troubleshooting and Genera] HypoTurtle:
Okay first attempt at a universal script.

For magldr and for testing just the script should be needed: (I'll upload a zip with the stuff for clk)

Instructions - have rom zip on sdcard, modify line in script under testing accordingly, install and follow instructions.

Adb push script to /tmp while in recovery and run it.

The script is more geared towards the tradition setup (updater-script without aroma and a boot.img) of ROM.zip's so there might be some bugs to workout.

#!/sbin/sh
testing(){
echo "TESTING MODE"
#echo "[RoM] [type]" > /sdcard/installtype.prop
# change RoM to name of rom - name must be in the name of the rom zip i.e for 567SlimKat-V78.zip, RoM can be 'SlimKat'
# change type to one of NAND DataOnExt NativeSD DirectSD
echo "[RoM] [DirectSD]" > /sdcard/installtype.prop
}
cleanup() {
echo "cleaning"
rm -rf mt1.txt mt2.txt zI.txt bootimg.txt updater-script
}
partnames() {
sdpart=/dev/block/mmcblk0p1
firstpart=/dev/block/mmcblk0p2
secondpart=/dev/block/mmcblk0p3
echo "setting partitions with ${firstpart} format"
}
looppartnames() {
sdpart=/dev/block/platform/msm_sdcc.1/by-name/userdata/media/0
firstpart=/dev/block/loop1
secondpart=/dev/block/loop2
echo "setting partitions with ${firstpart} format"
}
getpropfile() {
if [ ! -d /tmp/sdcard ]; then
mkdir /tmp/sdcard
fi
mount ${sdpart} /tmp/sdcard
if [ -f /tmp/sdcard/installtype.prop ]; then
type=` cat /tmp/sdcard/installtype.prop | awk '{print $(NF)}' | sed 's/\(\[\|\]\)//g' `
ROM_NAME=` cat /tmp/sdcard/installtype.prop | awk '{print $(NF-1)}' | sed 's/\(\[\|\]\)//g' `
# local=` cat /tmp/sdcard/installtype.prop | awk '{print $(NF-2)}' | sed 's/\(\[\|\]\)//g' `
else
echo "no installtype.prop on sdcard - exitting"
exit 0
fi
umount /tmp/sdcard
}
bootloadercheck() {
echo "checking bootloader"
bootloader=MAGLDR
output=`grep -i "clk" /proc/cmdline`
if [ -n "${output}" ]; then
bootloader=cLK
fi
}
bootloader() {
bootloader=cLK
}
exportvar() {
echo "allocating numbered value"
## Changing install_type to numbered value
if [ ${type} = 'NAND' ]; then
install_type='1'
fi
if [ ${type} = 'DataOnExt' ]; then
install_type='2'
fi
if [ ${type} = 'NativeSD' ]; then
install_type='3'
fi
if [ ${type} = 'DirectSD' ]; then
install_type='4'
fi
}
mkdirs(){
echo "creating directories"
cd /
if [ ! -d "NativeSD" ]; then
mkdir NativeSD
fi
if [ ! -d "boot_dir" ]; then
mkdir boot_dir
fi
if [ ! -d "boot" ]; then
mkdir boot
fi
if [ ! -d "sdcard/NativeSD/${ROM_NAME}" ]; then
mkdir -p sdcard/NativeSD/${ROM_NAME}/system
fi
}
mountpart(){
echo "mounting files"
mount -t auto ${firstpart} /NativeSD
mount --bind NativeSD/${ROM_NAME}/system /system
mount ${sdpart} /sdcard
mount --bind /sdcard/NativeSD /boot_dir
mount --bind sdcard/NativeSD/${ROM_NAME} /boot
}
bootorig(){
echo "saving original boot.img to boot-orig folder"
mkdir /boot_dir/boot-orig/
/tmp/dump_image boot /boot/boot-orig/boot.img
}
systeminstall(){
echo "installing system"
# rm -rf /system/*
unzip /sdcard/*${ROM_NAME}*.zip system/*
# /tmp/unzip /sdcard/*${ROM_NAME}*.zip data/*
unzip /sdcard/*${ROM_NAME}*.zip META-INF/*
unzip /sdcard/*${ROM_NAME}*.zip boot.img -d /tmp/
cat '/META-INF/com/google/android/updater-script' | sed -e '/assert(/,/);/!d' > /tmp/updater-script
cat '/META-INF/com/google/android/updater-script' | sed -e '/symlink(/,/);/!d' >> /tmp/updater-script
cat '/META-INF/com/google/android/updater-script' | sed -e '/setmetadata/,/);/!d' >> /tmp/updater-script
cp /tmp/updater-script /META-INF/com/google/android/updater-script
zip -r /META-INF
cp /META-INF.zip /sdcard/symlink-${ROM_NAME}.zip
}
bootingred(){
echo "obtaining an initrd.gz and zImage to work with"
###### Copying zImage and initrd.gz from /boot_dir when one wasn't in the zip
if [ ! -a "/tmp/boot.img" ]; then
mount ${sdpart} /sdcard
cp /sdcard/zImage /tmp/zImage
cp /sdcard/initrd.gz /tmp/initrd.gz
else
/tmp/unpackbootimg /tmp/boot.img /tmp > /tmp/bootimg.txt
/tmp/unpackbootimg /tmp/boot.img /tmp
cp /tmp/boot.img-ramdisk.gz /tmp/initrd.gz
fi
}
usecurrentboot(){
/tmp/dump_image boot /tmp/boot.img
/tmp/unpackbootimg /tmp/boot.img /tmp > /tmp/bootimg.txt
cp /tmp/boot.img-ramdisk.gz /tmp/initrd.gz
cp /tmp/boot.img-zImage /tmp/zImage
}
mkmountfs(){
echo "creating mountfs.sh"
if [ "${install_type}" -gt "1" ]; then
echo "#!/bin/busybox sh
# wait for 2nd partition ready
wait ${firstpart}
wait ${firstpart}
wait ${firstpart}
wait ${firstpart}" > /tmp/mountfs-${type}.sh
if [ ${type} = 'DirectSD' ]; then
echo "# wait for 3rd partition ready
wait ${secondpart}
wait ${secondpart}
wait ${secondpart}
wait ${secondpart}" >> /tmp/mountfs-${type}.sh
fi
hexdump -C ${firstpart} | head -30 | grep 'video' > /tmp/mt1.txt
if grep -q 00000940 "/tmp/mt1.txt"; then
mt1='f2fs'
else
mt1='ext4'
fi
if [ ${mt1} = 'ext4' ]; then
echo "# check the ext4 fs
/bin/e2fsck -p ${firstpart} /e2fsck.log" >> /tmp/mountfs-${type}.sh
else
echo "/bin/fsck.f2fs -p ${firstpart} /fsckf2fs.log" >> /tmp/mountfs-${type}.sh
fi
if [ ${type} = 'DirectSD' ]; then
echo "Modifying for ${type}"
if [ -b ${secondpart} ]; then
hexdump -C ${secondpart} | head -30 | grep 'video' > /tmp/mt2.txt
else
echo "No 2nd ext partition present, format one and run script again"
fi
if grep -q 00000940 "/tmp/mt2.txt"; then
mt2='f2fs'
else
mt2='ext4'
fi
if [ ${mt2} = 'ext4' ]; then
echo "# check the ext4 fs
/bin/e2fsck -p ${secondpart} /e2fsck.log" >> /tmp/mountfs-${type}.sh
else
echo "/bin/fsck.f2fs -p ${secondpart} /fsckf2fs.log" >> /tmp/mountfs-${type}.sh
fi
fi
echo "/bin/busybox mkdir -p /NativeSD
/bin/busybox mount -t ${mt1} -o ro,noatime,nodiratime,barrier=0 ${firstpart} /NativeSD
# mount the system data app dir" >> /tmp/mountfs-${type}.sh
echo "Modifying for ${type}"
if [ ${type} = 'DataOnExt' ]; then
echo "/bin/busybox mount --bind /NativeSD/${ROM_NAME}/data /data" >> /tmp/mountfs-${type}.sh
fi
if [ "${install_type}" -gt "2" ]; then
echo "/bin/busybox mount --bind /NativeSD/${ROM_NAME}/system /system" >> /tmp/mountfs-${type}.sh
if [ ${type} = 'NativeSD' ]; then
echo "/bin/busybox mount --bind /NativeSD/${ROM_NAME}/data /data" >> /tmp/mountfs-${type}.sh
else
echo "/bin/busybox mkdir -p /DirectData
/bin/busybox mount -t ${mt2} -o noatime,nodiratime,nosuid,nodev,discard ${secondpart} /DirectData
/bin/busybox mount --bind /DirectData/${ROM_NAME}/data /data" >> /tmp/mountfs-${type}.sh
fi
fi
echo "# cleanup
# delete /bin to free ram
/bin/busybox rm -rf /bin" >> /tmp/mountfs-${type}.sh
fi
}
mkinitrd(){
echo "modifying ramdisk"
###### Setting up working directory
cp /tmp/initrd.gz /tmp/initrd-${ROM_NAME}.gz
mkdir -p /tmp/work
cd /tmp/work

###### Unzipping nand kernel
gzip -d -c ../initrd-${ROM_NAME}.gz | cpio -i -d
if [ ! -d bin ]; then
mkdir bin
fi
cd /
cp /sbin/busybox /tmp/work/bin/busybox
cp /sbin/e2fsck /tmp/work/bin/e2fsck
cp /tmp/mountfs-${type}.sh /tmp/work/mountfs-${type}.sh

if [[ ${mt1} = 'f2fs' || ${mt2} = 'f2fs' ]]; then
echo "copying fsck for f2fs"
cp /sbin/fsck.f2fs /tmp/work/bin/fsck.f2fs
chmod 0777 /tmp/work/bin/fsck.f2fs
fi
chmod 0777 /tmp/work/mountfs-${type}.sh
chmod 0777 /tmp/work/bin/busybox
chmod 0777 /tmp/work/bin/e2fsck

###### Modifying ramdisk to not use yaffs, and use sd partitions instead.
if [ "${install_type}" -gt "2" ]; then
sed -i '/mtd@misc/s/^/#/' /tmp/work/ueventd.*.rc
sed -i '/mount yaffs2/s/^/#/
s/# #/# /g' /tmp/work/*.rc
sed -i "s/rOm/$ROM_NAME/g" /tmp/work/mountfs-${type}.sh
fi

###### Checking for anything other than nand is to be installed
if [ "${install_type}" -gt "1" ]; then
sed -i '/on fs/ a exec /mountfs-'${type}'.sh' /tmp/work/*.rc
chmod 0750 /tmp/work/init*.rc
chmod 0644 /tmp/work/ueventd.htcleo.rc
fi
cd /tmp/work
###### Building new ramdisk
find . | cpio -o -H newc | gzip -9 > /tmp/initrd-done.gz
}
initrdcp(){
echo "copying ramdisk"
###### Checking bootloader
cd /tmp
if [ ${bootloader} != "cLK" ]; then
###### Checking zImage type
if [ -f "zImage" ]; then
hexdump -C zImage | grep '1f 8b 08' > zI.txt
if grep -q 00003f80 "zI.txt"; then
dd if=/dev/zero of=/tmp/6MB bs=1M count=6
fi
fi
cat /tmp/initrd-done.gz >> 6MB
cp 6MB /boot_dir/${ROM_NAME}-new/initrd.gz
cp zImage /boot_dir/${ROM_NAME}-new/zImage
else
cp /tmp/initrd-done.gz /tmp/initrd.gz
if [ ${install_type} -lt "3" ]; then
chmod 0755 mkbootimg
/tmp/mkbootimg --base "`cat /tmp/bootimg.txt | grep 'base' | cut -d ' ' -f '9'`" --cmdline "`cat /tmp/tools/boot.img-cmdline`" --kernel /tmp/zImage --ramdisk /tmp/initrd.gz -o /tmp/boot.img
/tmp/flash_image boot /tmp/boot.img
else
cp /tmp/initrd.gz /boot_dir/${ROM_NAME}-new/initrd.gz
cp /tmp/zImage /boot_dir/${ROM_NAME}-new/zImage
fi
fi
}
bootflash(){
echo "flashing boot"
cd /tmp
chmod 0755 mkbootimg
/tmp/mkbootimg --base "`cat /tmp/bootimg.txt | grep 'base' | cut -d ' ' -f '9'`" --cmdline "`cat /tmp/boot.img-cmdline`" --kernel /boot_dir/${ROM_NAME}-new/zImage --ramdisk /boot_dir/${ROM_NAME}-new/initrd.gz -o /tmp/boot.img
/tmp/flash_image boot /tmp/boot.img
}
revertinitrd(){
echo "reverting ramdisk in /tmp"
cd /
cp /tmp/initrd-${ROM_NAME}.gz /tmp/initrd.gz
}
showresult(){
echo "Ramdisk successfully modified for ${ROM_NAME} running as ${type} on ${mt1}"
if [ -f tmp/mt2.txt ]; then
echo "with data on ${mt2}"
fi
echo "The install folder is ${ROM_NAME}-new"
if [ -f tmp/zI.txt ]; then
echo "kernel used is"
if grep -q 00003f80 "tmp/zI.txt"; then
echo "a 3.0.x kernel"
else
echo "a 2.x kernel"
fi
fi
if [ -f /sdcard/symlink-${ROM_NAME}.zip ]; then
echo "Please flash symlink-${ROM_NAME}.zip in recovery"
fi
}

testrun(){
testing
cleanup
echo "0 0 0 0 0 0 0 0 0x11800000" > /tmp/bootimg.txt
partnames
getpropfile
bootloadercheck
exportvar
mkdirs
if [ "${install_type}" -gt "1" ]; then
mountpart
fi
if	[ "${bootloader}" = "cLK" ]; then
bootorig
fi
if [ `find '/sdcard' -name '*.zip' | grep ${ROM_NAME}` != '' ]; then
systeminstall
fi
if [ ! -f /tmp/initrd.gz ]; then
bootingred
fi
if [ ! -f /tmp/mountfs-${type}.sh ]; then
mkmountfs
fi
mkinitrd
initrdcp
if [[ "${install_type}" -lt "3" && "${bootloader}" = "cLK" ]]; then
bootflash
fi
revertinitrd
showresult
}

testrun

exit 0