[HD2 Android NAND Development] HypoTurtle:
Annotated code (part 1) ...

Just going to run through the current beta code, devs feel free to pick and mix any code from it. If you find it useful, credit would be appreciated.

#!/sbin/sh
#set -u
set -o verbose
[COLOR="Red"]##### First up is a group of functions #####
[/COLOR]testing() {
[COLOR="red"]### This function checks the arguments passed when running the script and reports back what's going to be run ###
[/COLOR]printf "Checking ${0} with ${@} args. "
[COLOR="red"]### This is the FAT32 partition of the sdcard - need to find a way to autodetect ###
[/COLOR]sdpart=/dev/block/mmcblk0p1
[COLOR="red"]### These are the possible install types (porta-o not working at present) ###
[/COLOR]types="NAND DataOnExt NativeSD DirectSD porta-o "
[COLOR="red"]### This makes the types refer to their description ###
[/COLOR]optslist desc
[COLOR="red"]### check for an argument with the calling of the script, and checking the arguments against the types list ###
[/COLOR]if [ $# -gt 0 ]; then
typecheck=` echo "$types" | grep "$1 " `
if [ -z "${typecheck}" ]; then
echo "$1 is not a valid type - choices are $types"
echo "checking if $1 is a command"
optioncheck=` echo "$options" | grep "$1 " `
if [ -z "${optioncheck}" ]; then
busybox printf "%b\n" "${1} is not a valid option - choices are:"
for i in ${options}
do
opts="$i"
echo "$opts to $(eval echo \$$opts)"
done
printf "script stopping here\n"
exit 0
else
typecheck=` echo "$types" | grep "$2 " `
systemunzip=${5:-n}
if [ -n "${typecheck}" ]; then
if echo ${2} | grep -q "porta"; then
systemunzip=${5:-y}
fi
type=${2}; ROM_NAME=${3}; data_NAME=${4:-$3}; install_type=` busybox expr index "Axvco" "${2}" `; systemunzip=${systemunzip}
printf "Install Type is $type\nRomName is $ROM_NAME\nDataName is $data_NAME\ninstall no is $install_type\nsystem install is $systemunzip\n"
optslist functs
if [ ${1} = "ins" ]; then
systemunzip=y
fi
[COLOR="red"]### Function run with arguments set ###
[/COLOR]	eval "\$"${1} ${type} ${ROM_NAME} ${data_NAME:-$ROM_NAME} ${install_type} ${systemunzip}
else
printf "neither $1 or $2 is a valid install type\n"
exit 0
fi
fi
else
systemunzip=${4:-n}
if echo ${1} | grep -q "porta"; then
systemunzip=${4:-y}
fi
type=${1}; ROM_NAME=${2}; data_NAME=${3:-$2}; install_type=` busybox expr index "Axvro" "${1}" `; systemunzip=${systemunzip}
[COLOR="red"]### runn function run with arguments set ###
[/COLOR]	runn ${type} ${ROM_NAME} ${data_NAME:-$ROM_NAME} ${install_type} ${systemunzip}
fi
else
[COLOR="red"]### Checking for a installtype.prop file at /sdcard if no arguments were passed with the script ###
[/COLOR] printf "no commands sent to script\nchecking /sdcard/installtype.prop\n"
if [ ${checkprop:-false} = "false" ]; then
getpropfile
else
[COLOR="red"]### Asking user for input (works over adb) if no valid installtype.prop ###
[/COLOR]	if [ ${userasked:-no} = "no" -a ${alreadyrandc} = "yes" ]; then
echo "Requesting user input"

printf "Options are:\n"
for i in ${options};
do
opts="$i"
echo "$opts to $(eval echo \$$opts)"
done
printf "Enter an option if you want to do anything other than full script run:"
read optional
printf "Options are ${types}\nEnter install type:"
read type
type=${type:-nil}
ziplist=` ls /sdcard | grep .zip | cut -d "." -f "1" | tr '\n' ' ' `
printf "Possible options are:- ${ziplist}\nEnter ROM-NAME:"
read ROM_NAME
printf "Leave blank to use ${ROM_NAME}\nEnter DATA-NAME:"
read data_NAME
printf "Do you want the script to install system y/n:"
read systemunzip
userasked=yes
testing ${optional:-runn} ${type} ${ROM_NAME} ${data_NAME:-$ROM_NAME} ${systemunzip}
else
echo "No conditions found or given\nNo installtype.prop on sdcard\nNo type or ROM name specified\nusage sh hytmb.sh {options} [type] [ROM_NAME]\ntype can be ${types}\nROM_NAME to be taken from ROM_NAMEadb123.zip"
for i in ${options}
do
opts="$i"
echo "$opts to $(eval echo \$$opts)"
done
exit 0
fi
fi
fi
}
optslist() {
[COLOR="red"]### Lists of functions that can be run and there description ###
[/COLOR]options="m ins mr fb runn "
if [ $1 = "desc" ]; then
m="mount partitions"; ins="install system"; mr="modify the ramdisk"; fb="andthis"; runn="run full script"
else
m="mntstuff"; ins="installsystem"; mr="modifyramdisk"; fb="andthis"; runn="runn"
fi
}
cleanup() {
[COLOR="red"]### List of files to be cleaned ###
[/COLOR]echo "cleaning"
rm -rf /tmp/zI.txt /tmp/bootimg.txt /tmp/umountloop.sh /tmp/6MB /tmp/initrd.gz /tmp/installtype.prop /tmp2 /tmp/mountfs*.sh
}
partnames() {
[COLOR="red"]### List of partition locations - need to automate ###
[/COLOR]firstpart=/dev/block/mmcblk0p2
secondpart=/dev/block/mmcblk0p3
busybox printf "%b\n" "setting partitions with ` echo ${firstpart} | busybox cut -d "/" -f "4" ` format"
}
looppartnames() {
[COLOR="red"]### List of partitions for looped setup (not working at present) ###
[/COLOR]firstpart=/dev/block/loop1
secondpart=/dev/block/loop2
thirdpart=/dev/block/loop3
busybox printf "%b\n" "setting partitions with ` echo ${firstpart} | busybox cut -d "/" -f "4" ` format"
}
mkloopdev() {
[COLOR="red"]### making of filesystem block devices for looped system ###
### size of filesystems can be overwritten be a /sdcard/porta.txt file [type system data cache] ###
[/COLOR]echo "" >> /sdcard/porta.txt
fls=` cat /sdcard/porta.txt | busybox cut -d " " -f "1" `
syssize=` cat /sdcard/porta.txt | busybox cut -d " " -f "2" `
romsize=${romsize:-20000}
datasize=` cat /sdcard/porta.txt | busybox cut -d " " -f "3" `
#datasz=256000
datasz=20000
cachesize=` cat /sdcard/porta.txt | busybox cut -d " " -f "4" `
#cachesz=32000
cachesz=20000
if [ ! -e /sdcard/NativeSD/${ROM_NAME}_${type}/system.${fls:-ext4} ]; then
dd if=/dev/zero of=/sdcard/NativeSD/${ROM_NAME}_${type}/system.${fls:-ext4} bs=4096 count=${syssize:-$romsize}
newsys=yes
fi
if [ ! -e /sdcard/NativeSD/${ROM_NAME}_${type}/data.${fls:-ext4} ]; then
dd if=/dev/zero of=/sdcard/NativeSD/${ROM_NAME}_${type}/data.${fls:-ext4} bs=4096 count=${datasize:-$datasz}
newdata=yes
fi
if [ ! -e /sdcard/NativeSD/${ROM_NAME}_${type}/cache.${fls:-ext4} ]; then
dd if=/dev/zero of=/sdcard/NativeSD/${ROM_NAME}_${type}/cache.${fls:-ext4} bs=4096 count=${cachesize:-$cachesz}
newcache=yes
fi
losetup ${firstpart} /sdcard/NativeSD/${ROM_NAME}_${type}/system.${fls:-ext4}
losetup ${secondpart} /sdcard/NativeSD/${ROM_NAME}_${type}/data.${fls:-ext4}
losetup ${thirdpart} /sdcard/NativeSD/${ROM_NAME}_${type}/cache.${fls:-ext4}
if [ ${newsys:-no} = "yes" ]; then
mke2fs -T ${fls:-ext4} -O has_journal,ext_attr,resize_inode,filetype,extent,sparse_super,large_file,uninit_bg -I 256 ${firstpart}
fi
if [ ${newdata:-no} = "yes" ]; then
mke2fs -T ${fls:-ext4} -O has_journal,ext_attr,resize_inode,filetype,extent,sparse_super,large_file,uninit_bg -I 256 ${secondpart}
tune2fs -o journal_data_writeback ${secondpart}
fi
if [ ${newcache:-no} = "yes" ]; then
mke2fs -T ${fls:-ext4} -O has_journal,ext_attr,resize_inode,filetype,extent,sparse_super,large_file,uninit_bg -I 256 ${thirdpart}
tune2fs -o journal_data_writeback ${thirdpart}
fi
loopdevready=yes
}
getpropfile() {
[COLOR="red"]### propfile check run by testing ###
[/COLOR]checkprop=true
if [ -f "/tmp/installtype.prop" ]; then
echo "installtype.prop found"
testing "$(cat installtype.prop)"
else
echo "no prop file found - will need user input"
testing
fi
}
bootloadercheck() {
[COLOR="red"]### bootloader check - any way to detect haret? ###
[/COLOR]busybox printf "%b\n" "checking bootloader"
bootloader=MAGLDR
if grep -iq "clk" /proc/cmdline; then
bootloader=cLK
fi
if grep -iq "haret" /proc/cmdline; then
bootloader=haret
fi
####For testing
#bootloader=haret
}
bootloader() {
bootloader=MAGLDR
}

Needed to split the code due to 30000 character limit.