Skip to content

Commit

Permalink
Initial check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
trickapm committed Jan 14, 2017
1 parent 9642d18 commit 519b9ae
Show file tree
Hide file tree
Showing 222 changed files with 694 additions and 7 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ The bootstrap folder contains CGI scripts for the embedded Boa webserver. The ``

Both must be copied to the vfat partition.

### 3. Copy hacks folder
The hacks folder must be copied to the ext2 partition.
### 3. Copy data folder
The data folder must be copied to the ext2 partition.

### 4. Place sd-card in device
The device will automatically run ```snx_autorun.sh``` when the sd-card is inserted.
Expand All @@ -38,9 +38,6 @@ Both are based on the original files and don't affect the original behaviour in

## Hacks
When the status page shows the hacks have been applied successfully, the following features are available:
- You can place any binaries, scripts etc. you need on the ext2 partition. The device only has limited space available on internal flash so you don't risk running out of space.
- You can place any binaries, scripts etc. you need in the data folder on ext2 partition. The device only has limited space available on internal flash so you don't risk running out of space.
- A busybox build is provided with many applets available such as telnetd, ftpd, netcat.
- Scripts placed in hacks/etc/scripts will be automatically executed after the device boots.

Scripts can be enabled and disabled by visiting ```http://device-ip/cgi-bin/scripts```

- Scripts placed in data/etc/scripts will be automatically executed after the device boots.
80 changes: 80 additions & 0 deletions bootstrap/fang_hacks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/sh

HACKS_ENABLED=1
HACKS_HOME=/media/mmcblk0p2/data

#####################################################

logmsg()
{
echo "$(date) - $0: $1" >> /tmp/hacks.log
echo "$1"
}

do_patch()
{
if [ ! -d "$1" ]; then
logmsg "do_patch: \"$1\" must be a directory!"
return 1
fi

if [ ! -f "$2" ]; then
logmsg "do_patch: \"$2\" must be a patch file!"
return 1
fi

old_cwd=$(pwd)
cd "$1"
patch < "$2"
rc=$?
cd "$old_cwd"
return $rc
}

# Remove stale log
if [ -f /tmp/hacks.log ]; then
rm /tmp/hacks.log
logmsg "Removed stale logfile"
fi

logmsg "Executing script (enabled: $HACKS_ENABLED)"

# Link cgi files again if available (/tmp is volatile)
CGI_FILES="/media/mmcblk0p1/bootstrap/www"
if [ -d "$CGI_FILES" ]; then
for i in $CGI_FILES/*; do
if [ ! -e "/tmp/www/cgi-bin/$(basename $i)" ]; then
logmsg "Linking $i -> /tmp/www/cgi-bin/$(basename $i)"
ln -sf "$i" "/tmp/www/cgi-bin/$(basename $i)"
else
logmsg "Not linking $i: already exists"
fi
done
fi

if [ $HACKS_ENABLED -ne 1 ]; then
return 0
fi

if [ ! -d "$HACKS_HOME" -o ! -f "$HACKS_HOME/etc/profile" ]; then
logmsg "Failed to find hacks in $HACKS_HOME!"
mount /dev/mmcblk0p2 /media/mmcblk0p2 >> /tmp/hacks.log 2>&1
if [ ! -d "$HACKS_HOME" -o ! -f "$HACKS_HOME/etc/profile" ]; then
logmsg "Failed to find $HACKS_HOME!"
return 1
fi
else
source "$HACKS_HOME/etc/profile" >/dev/null
fi

if ! type patch >/dev/null; then
logmsg "Patch command not found! Patches will not be applied."
else
# todo: apply patches?
true
fi

logmsg "Running startup scripts"
run-parts "$HACKS_HOME/etc/scripts"
logmsg "Finished"

19 changes: 19 additions & 0 deletions bootstrap/rc.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# Set serial number (MAC address) as default hostname
echo "Set hostname ..."
if [ ! -f /etc/hostname ]; then
echo "iSmartAlarm" > /etc/hostname
fi
/bin/hostname -F /etc/hostname

ifconfig lo up

echo 45 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 3 > /proc/sys/net/ipv4/tcp_keepalive_probes
echo 5 > /proc/sys/net/ipv4/tcp_keepalive_intvl

if [ -x "/etc/fang_hacks.sh" ]; then
sleep 30
/etc/fang_hacks.sh
fi
110 changes: 110 additions & 0 deletions bootstrap/sdcard
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/sh

if [ $ACTION = "add" ]; then
mkdir -p /media/$MDEV

grep -w "3" /proc/mmc/pwroff
$status = $?

if [ ! -x /tmp/mmc.all.log ] || [ $status == 0 ]; then
# sleep 1

ps > /tmp/mmc.ps.log
grep -w "lsblk" /tmp/mmc.ps.log
if [ $? == 0 ]; then
echo "1" > /tmp/mmc.ps2.log
exit 0
fi

grep -w "1" /proc/mmc/removal
if [ $? == 0 ]; then
exit 0
fi

grep -w "1" /proc/mmc/pwroff
if [ $? == 0 ]; then
killall -9 lsblk
exit 0
fi

sleep 1
# ps > /tmp/mmc.ps.log
# grep -w "lsblk" /tmp/mmc.ps.log
# if [ $? == 0 ]; then
# echo "1" > /tmp/mmc.ps2.log
# exit 0
# fi

# echo "2" > /tmp/mmc.ps2.log

echo "0x1" >/proc/mmc/pwroff
lsblk -o KNAME,FSTYPE /dev/mmc* > /tmp/lsblk.log
echo "0x0" >/proc/mmc/pwroff

grep "mmc" /tmp/lsblk.log > /tmp/mmc.all.log

grep "vfat" /tmp/mmc.all.log > /tmp/mmc.vfat.log
grep "exfat" /tmp/mmc.all.log > /tmp/mmc.exfat.log
grep "ntfs" /tmp/mmc.all.log > /tmp/mmc.ntfs.log
grep "ext2" /tmp/mmc.all.log > /tmp/mmc.ext2.log
fi

grep -w $MDEV /tmp/mmc.vfat.log
if [ $? == 0 ]; then
# sleep 1
grep -w "1" /proc/mmc/removal
if [ $? == 0 ]; then
exit 0
fi

mount -t vfat -o rw /dev/$MDEV /media/$MDEV
echo "1" > /tmp/$MDEV
mount -o remount,rw /dev/$MDEV
echo "1" > /tmp/guozhixin
if [ -f /media/$MDEV/snx_autorun.sh ]; then
echo "find snx_autorun.sh" > /dev/console
/media/$MDEV/snx_autorun.sh
fi
exit 0
fi

grep -w $MDEV /tmp/mmc.exfat.log
if [ $? == 0 ]; then
mount /dev/$MDEV /media/$MDEV
#mount.exfat-fuse /dev/$MDEV /media/$MDEV
echo "1" > /tmp/$MDEV
mount -o remount,rw /dev/$MDEV
echo "2" > /tmp/guozhixin
if [ -f /media/$MDEV/snx_autorun.sh ]; then
echo "find snx_autorun.sh" > /dev/console
/media/$MDEV/snx_autorun.sh
fi
exit 0
fi

grep -w $MDEV /tmp/mmc.ntfs.log
if [ $? == 0 ]; then
ntfs-3g /dev/$MDEV /media/$MDEV
echo "1" > /tmp/$MDEV
#mount -o remount,rw /dev/$MDEV
echo "3" > /tmp/guozhixin
if [ -f /media/$MDEV/snx_autorun.sh ]; then
echo "find snx_autorun.sh" > /dev/console
/media/$MDEV/snx_autorun.sh
fi
exit 0
fi

grep -w $MDEV /tmp/mmc.ext2.log
if [ $? == 0 ]; then
mount /dev/$MDEV /media/$MDEV
echo "1" > /tmp/$MDEV
exit 0
fi
else
umount /dev/$MDEV
rm -rf /media/$MDEV
rm -f /tmp/$MDEV
rm -f /tmp/guozhixin
echo "0x3" >/proc/mmc/pwroff
fi
Binary file added bootstrap/www/action
Binary file not shown.
45 changes: 45 additions & 0 deletions bootstrap/www/func.cgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
# Source: http://isquared.nl/blog/2008/11/01/Bourne-Bash-Shell-CGI-Scripts/

_DEBUG_=

if [ "${REQUEST_METHOD}" = "POST" ]
then
POST_QUERY_STRING=`dd bs=1 count=${CONTENT_LENGTH} 2>/dev/null`
if [ "${QUERY_STRING}" != "" ]
then
QUERY_STRING=${POST_QUERY_STRING}"&"${QUERY_STRING}
else
QUERY_STRING=${POST_QUERY_STRING}"&"
fi
fi

#echo "Content-type: text/plain"; echo

_IFS=${IFS}; IFS=\&
i=0
for _VAR in ${QUERY_STRING}
do
if [ ${_DEBUG_} ]
then
i=`expr $i + 1`; echo "--- ENTER LOOP $i ---"
echo _VAR: ${_VAR}
echo -n variable: `echo ${_VAR} | cut -d= -f1`" "
echo value: `echo ${_VAR} | cut -d= -f2`
fi

eval "`echo F_${_VAR} | cut -d= -f1`=`echo ${_VAR} | cut -d= -f2`"

if [ ${_DEBUG_} ]
then
echo "--- EXIT LOOP $i ---"
fi
done
IFS=${_IFS}
unset i _IFS _VAR

if [ ${_DEBUG_} ]
then
echo query string: ${QUERY_STRING}
echo post-part of query string: ${POST_QUERY_STRING}
fi
94 changes: 94 additions & 0 deletions bootstrap/www/status
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/sh

echo "Content-type: text/html"
echo ""

cat << EOF
<html>
<head>
<title>Fang Hacks</title>
<head>
<style type="text/css">
body { background-color: #B0E0E6; }
.err { color: red; }
.tbl { border-collapse: collapse; border-spacing: 0;}
.tbl th { text-align: left; vertical-align: top; font-weight: bold; padding: 10px 5px; border-style: solid; border-width: 1px; overflow: hidden; word-break: normal; }
.tbl td { padding: 10px 5px; border-style: solid; border-width: 1px; overflow: hidden; word-break: normal; }
</style>
<body>
<pre>
____ ___.__ ___________
\ \/ /|__|____ ____\_ _____/____ ____ ____
\ / | \__ \ / _ \| __) \__ \ / \ / ___\
/ \ | |/ __ \( <_> ) \ / __ \| | \/ /_/ >
/___/\ \|__(____ /\____/\___ / (____ /___| /\___ /
\_/ \/ \/ \/ \//_____/
___ ___ __
/ | \_____ ____ | | __ ______
/ ~ \__ \ _/ ___\| |/ / / ___/
\ Y // __ \\ \___| < \___ \
\___|_ /(____ /\___ >__|_ \/____ >
\/ \/ \/ \/ \/
Version 1.0
</pre>
<div>
<p>
EOF
HACKS_HOME="/media/mmcblk0p2/data"
if [ -x "/etc/fang_hacks.sh" ]; then
echo "Script is already installed!<br/>"
if [ ! $(mount |grep /media/mmcblk0p2) ]; then
echo "<span class='err'>Error: Nothing is mounted on /media/mmcblk0p2!</span>"
echo "<button title='Try to mount the hacks partition manually' type='button' onClick=\"window.location.href='action?cmd=manual_mount'\">Manual Mount</button>"
echo "<br/>"
elif [ ! -d "$HACKS_HOME" ]; then
echo "<span class='err'>Error: No 'hacks' directory found in /media/mmcblk0p2!</span>"
echo "<br/>"
fi
echo "Apply updates from sd-card?"
echo "<button title='Copy updates from sd-card to device' type='button' onClick=\"window.location.href='action?cmd=update'\">Update</button>"
else
echo "Do you want to apply Xiao Fang hacks?<br/>"
echo "<button title='Copy the fang_hacks.sh script to the device' type='button' onClick=\"window.location.href='action?cmd=apply'\">Apply</button>"
fi

cat << EOF
</p>
<hr/>
<button title='Start default telnetd on port 23' type='button' onClick="window.location.href='action?cmd=telnetd'">Start rescue telnetd</button>
<button title='Reboot the device' type='button' onClick="window.location.href='action?cmd=reboot'">Reboot</button>
<button title='Manage scripts' type='button' onClick="window.location.href='action?cmd=scripts'">Manage scripts</button>
<button title='Network' type='button' onClick="window.location.href='action?cmd=network'">Network</button>
<button title='View /tmp/hacks.log' type='button' onClick="window.location.href='action?cmd=showlog'">View log</button>
<hr/>
<table class='tbl'>
<tr>
<th>Date:</th>
<td>$(date)</td>
</tr>
<tr>
<th>System uptime:</th>
<td>$(uptime)</td>
</tr>
<tr>
<th>Version:</th>
<td>$(cat /etc/os-release | cut -d'=' -f2)</td>
</tr>
<tr>
<th>Kernel:</th>
<td>$(uname -a)</td>
</tr>
<tr>
<th>Process list:</th>
<td><pre>$(ps)</td>
</tr>
<tr>
<th>Mounts:</th>
<td><pre>$(mount)</td>
</tr>
</table>
</div>
</body>
</html>
EOF
1 change: 1 addition & 0 deletions data/bin/ash
Binary file added data/bin/busybox
Binary file not shown.
1 change: 1 addition & 0 deletions data/bin/cat
1 change: 1 addition & 0 deletions data/bin/chgrp
1 change: 1 addition & 0 deletions data/bin/chmod
1 change: 1 addition & 0 deletions data/bin/chown
1 change: 1 addition & 0 deletions data/bin/cp
1 change: 1 addition & 0 deletions data/bin/date
1 change: 1 addition & 0 deletions data/bin/dd
Loading

0 comments on commit 519b9ae

Please sign in to comment.