Remember wifi settings after random reset?

mr-b

Member
Hi

I've just managed (after 1.5h) to remotely restore my mother's Hummy after she was confronted with the "Select Language" option, and she was sure she hadn't pressed any random buttons!
After navigating the installation wizard (which has several UI pitfalls) it turned out that the wifi dongle settings had gone too and so she had to enter those from scratch. Finally I managed to get the ip address from the router and logged in to restore her scheduled recordings and everything was Ok. But it was very painful for her!

The crash.log seems to have no obvs pattern. After some odd crashes last year I upgraded her aerial/masthead amp to ensure the signal was optimal but it seems some persist. No idea what the uptimes are.

19 Humax crashed at Thu May 7 16:21:38 UTC 2020 - Uptime: 30
18 Humax crashed at Thu Apr 9 16:31:05 UTC 2020 - Uptime: 33
17 Humax crashed at Tue Mar 24 12:36:44 UTC 2020 - Uptime: 29
16 Humax crashed at Mon Mar 23 14:55:34 UTC 2020 - Uptime: 33
15 Humax crashed at Fri Mar 20 17:26:11 UTC 2020 - Uptime: 34
14 Humax crashed at Mon Mar 16 14:40:28 UTC 2020 - Uptime: 12
13 Humax crashed at Wed Mar 11 10:18:28 UTC 2020 - Uptime: 36
12 Humax crashed at Fri Feb 28 10:03:29 UTC 2020 - Uptime: 30
11 Humax crashed at Tue Jan 28 14:51:11 UTC 2020 - Uptime: 30
10 Humax crashed at Sun Jan 26 19:42:19 UTC 2020 - Uptime: 12
9 Humax crashed at Thu Jan 23 16:06:23 UTC 2020 - Uptime: 32
8 Humax crashed at Tue Jan 21 16:17:02 UTC 2020 - Uptime: 37

She is no technophobe and can usually handle tech devices fine but sometimes unexpected events throw her and the restoration story turned into an episode of Child's Play ... Normal terminology of on-screen events is forgotten and I was left wondering what "it keeps clicking off" etc. meant ....

So, is there any way that wifi settings could be saved or easily restored?

Failing that, is this going to be a regular event i.e. do boxes go flaky due to caps etc. and are there any suggestions for reliable PVR soln's that can be remotely managed, for quiet lives all round?
 

Black Hole

May contain traces of nut
boot-settings, auto-schedule-restore, and tunefix between them can re-establish pretty much everything... except networking. A wired connection with DHCP will re-establish itself, a wireless link won't.

https://hummy.tv/forum/threads/is-there-a-good-time-to-retune.7624/post-103888

Failing that, is this going to be a regular event
Check out System Flush. We believe the Humax goes into the wizard if the databases get corrupted, but it happens and does not appear to be sinister.

are there any suggestions for reliable PVR soln's that can be remotely managed, for quiet lives all round?
No.
 
OP
M

mr-b

Member
Ok tx - in the end I had to restore the schedule again. (I've never seen auto restore work for some reason after I had issues with the box kept losing channels last year - due to tunefix barfing on an invalid config value, now fixed apparently). I'll post that in a separate thread though.

I suppose I could use a homeplug instead of a wifi usb stick if it starts to be an issue, but I think I'd need to rethink the whole setup in that case.

Unfortunately System Flush looks to be a no go since it involves flashing firmware and it's such a long time since I did it that it sounds too risky to do remotely.
AllI can recall is lots of issues with usb sticks!

Any ideas if I can diagnose the crashes? Mine virtually never does.
 

Black Hole

May contain traces of nut
AllI can recall is lots of issues with usb sticks!
If it was flashing the firmware, that would be the case. But I don't think it is - I think it is actually having an instruction on USB available at boot time which tells the CF to flush the databases. If somebody can confirm/correct that I would be grateful (for my own knowledge).
 

/df

Active Member
I've always assumed that System Flush is a sort of automatic fix-disk for the flash filesystems.

boot-settings, auto-schedule-restore, and tunefix between them can re-establish pretty much everything... except networking. A wired connection with DHCP will re-establish itself, a wireless link won't.
...
The WiFi settings are stored in a way that boot-settings can't handle. However it would be a good thing to make these settings recoverable. Might it be a reasonable solution for current connection settings to be written to the /mod filesystem such that, if no WiFi settings are found after reboot, the saved settings can be reloaded and applied?

Actually this seems to be easy to do in the wifi-up script, something like this, which I'm running as a test (the script is a flash file that I'm overriding with a bind mount at xinit time). Somewhat trickier is to make the settings persist into the Humax settings database, but the updated patch below also does that (see post #13 below):
Code:
--- /sbin/wifi-up.313
+++ /sbin/wifi-up.new
@@ -3,44 +3,200 @@
 export PATH=/sbin:/bin
 unset LD_LIBRARY_PATH
 
-wid="`iwgetid`"
-if [ $? -ne 0 ]; then
-       echo "No wireless dongle detected."
-       exit 0
-fi
+# set this to log details (including keys!)
+WLANDBG=1
 
-eth_check()
-{
+# some useful functions
+
+# save/restore functionality
+WLANCONF=/mod/etc/wlan.conf
+save_settings() {
+       local conf
+       [ -w "${WLANCONF%/*}" ] &&
+                 # don't clobber it if it is being read as well as updated!
+                 conf=$(swifi) &&
+                 { echo "# these settings are used if no settings are configured"
+                   echo "$conf"; } > "$WLANCONF"
+}
+
+ip2x() { # a.b.c.d
+       set -- $(if [ -z "$1" ]; then cat; else echo "$*"; fi | sed 's/\./ /g')
+       printf "%02.2X%02.2X%02.2X%02.2X" "${1:-0}" "${2:-0}" "${3:-0}" "${4:-0}"
+}
+
+put_setting() { # parm value [type=Value [tag=ETHERNET_CONF_2ND]]
+       local type
+       local parm
+       
+       # dbupdate command format
+       # dbname:tbl:parm:type:val -->
+       # attach '/var/lib/humaxtv/dbname.db/' as db; 
+       # 'update or insert' db.TBL_tbl set itemName=parm,item<type>=val where itemName=parm; /* roughly */
+       
+       # parm is prefixed with tag if not empty
+       printf "setup:MENUCONFIG:%s:%s:%s\n" "${parm:="${4:-ETHERNET_CONF_2ND}${1:+_$1}"}" "${type:=${3:-Value}}" "$2"
+       # with certain Blob parms, the Value must be set to 1 as well
+       case "${type}:${parm}" in
+       Blob:ETHERNET_CONF_*)
+               printf "setup:MENUCONFIG:%s:%s:%s\n" "$parm" Value 1
+               ;;
+       esac
+}
+
+# print a string of NULs to stdout
+nulls() { # len
+       if [ "$1" -gt 0 ]; then
+               printf "%*.*s\n" "$1" "$1" "" | sed 's/ /\d0/g'
+       fi
+}
+
+# hex-encode (len bytes or all) bytes of the input stream
+xxcode() { # [len] <stdin
+       # convert to hex-encoded bytes with no repeat elision
+       # allow for final \n in byte count, then strip its encoding
+       hexdump -v ${1:+-n "$(( $1 + 1 ))"} -e '/1 "%02X"' | sed -r 's/[[:xdigit:]]{2}$//'
+}
+
+# hex-encode a string right-padded with NULs
+xxpacks() { # sval len
+       local vlen
+       local plen
+       
+       vlen=${#1}
+       [ "$vlen" -gt "$2" ] && vlen=$2
+       plen=$(( $2 - $vlen ))
+       # add a string of plen NULs
+       { printf "%*s" "$vlen" "$1"; nulls $plen; } | xxcode "$2"
+}
+
+auth_encode() { # triple
+       set -- $*
+       printf "%b\0\0\0%b\0\0\0%b\0\0\0\n" "\0$1" "\0$2" "\0$3" | xxcode
+}
+
+# save the current WLAN settings to the setup database
+# uses the dbupdate procedure, so a reboot is needed
+# undocumented hacks copied from webif/html/settings/modules/network/save.jim
+save_settings_db() {
+       # dbupdate script saved here will be executed on next reboot
+       local dbupdate="/mod/boot/dbupdate"
+
+       [ -w "$dbupdate" ] || return
+       
+       local auth
+       local val
+       {       case $(swifi manual) in 
+               1)      # set manual and all parms
+                       put_setting "" 1
+                       put_setting IP "$(swifi ip | ip2x)" Blob
+                       put_setting NETMASK "$(swifi mask | ip2x)" Blob
+                       put_setting GATEWAY "$(swifi gw | ip2x)" Blob
+                       put_setting DNS "$(swifi dns | ip2x)" Blob
+                       ;;
+               0)      # set DHCP
+                       put_setting "" 0
+                       ;;
+               esac
+
+               put_setting CONNECTED_AP 0 Value WLAN
+               put_setting IS_CONNECTED 1 Value WLAN
+               put_setting AUTH_TYPE "${auth:=$(swifi auth)}" Value WLAN
+
+               case $auth in
+               8) auth="3 4 4" ;; # WPA2 PSK (AES)
+               7) auth="3 4 2" ;; # WPA2 PSK (TKIP+AES)
+               6) auth="2 3 4" ;; # WPA PSK (AES)
+               5) auth="2 3 2" ;; # WPA PSK (TKIP)
+               # unconfirmed guesses? humaxtv always seems to say 'WEP 64-bit Hex'
+               4) auth="1 1 2" ;; # WEP 128-bit ASCII  # Guess
+               3) auth="1 2 2" ;; # WEP 128-bit Hex    # Guess
+               2) auth="1 1 1" ;; # WEP 64-bit ASCII   # Guess
+               1) auth="1 0 1" ;; # WEP 64-bit Hex
+               *) auth="0 0 0" ;; # None
+               esac
+               
+               # print the null padded ssid (max 104), 4 NULs, auth (12), key (max 132) as a hex-encoded 252 byte string
+               val=$(printf "%s%s%s%s" \
+                               "$(xxpacks "$(swifi ssid)" 104)" "$(auth_encode $auth)" "$(xxpacks "" 4)" "$(xxpacks "$(swifi key)" 132)")
+               put_setting CONNECTED_AP "$val" Blob WLAN
+       } > "${dbupdate}/wifi_up"
+}
+
+eth_check() {
+       local eth=eth0
        # If the Ethernet interface has no link, disable it.
-       if ifconfig eth0 | grep -q RUNNING; then
-               echo "Link found on eth0."
+       if ifconfig "$eth" | grep -q RUNNING; then
+               echo "Link found on $eth."
        else
-               echo "No link found on eth0, disabling."
-               ifconfig eth0 down
+               echo "No link found on $eth, disabling."
+               ifconfig "$eth" down
+               ip route flush dev "$eth"
                # Refresh hosts file
                /sbin/modinit setup_hosts
        fi
 }
 
-wif="`echo $wid | sed 's/ .*//'`"
-essid=`echo $wid | cut -d\" -f2`
+# define a function to confirm active connection
+isConnected() { # ifname
+       [ -n "$(iwgetid --raw --ap "$1")" ]
+}
 
+# now start trying to connect 
+wid=$(iwgetid) ||
+       { echo "No wireless dongle detected."; exit 0; }
+
+wif=${wid%% *}
+ssid=$(iwgetid --raw "$wif")
+
 echo "Wireless interface: $wif"
 
-if [ -n "$essid" ]; then
-       echo "Already connected to '$essid'"
+if [ -n "$ssid" ] && isConnected "$wif"; then
+       echo "Already connected to '$ssid'"
        eth_check
+       save_settings
        exit 0
 fi
 
-wset()
-{
+# if no settings and saved settings exist, restore ...
+[ -z "$(swifi ssid)" ] && [ -r "$WLANCONF" ] &&
+       # ... by defining a replacement swifi
+       swifi() { # parm ...
+               local parm ptn
+               for parm in "${@:-..*}"; do
+                       ptn="${ptn}${ptn:+|}${parm}"
+               done
+               if [ $# -eq 0 ]; then
+                       grep -E "(${ptn})=" "$WLANCONF"
+               else
+                       sed -nr "/(${ptn})=/ {s/^..*=//;p}" <"$WLANCONF"
+               fi
+       }
+
+if [ -n "$WLANDBG" ]; then 
+       echo "== swifi"
+       swifi
+fi
+
+ssid=$(swifi ssid)
+if [ -z "$ssid" ]; then
+       echo "No SSID configured for $wif"
+       exit 1
+fi
+
+wset() { # param value
        echo "Set $1=$2"
-       iwpriv $wif set $1=$2
+       iwpriv "$wif" set "$1=$2"
 }
 
-ifconfig $wif up
-wset NetworkType Infra
+ifconfig "$wif" up
+# enable all allowed channels
+#   from rt2870 README_STA.txt:
+#   region 31: use ch1-11:active scan, ch12-14 passive scan
+#   otherwise defaults to region 7: use 5 ~ 13 Channel
+#   alternatively try CountryRegion 0 (1-11) or (not in UK) 5 (1-14)
+wset CountryRegion 31
+# same as default
+# wset NetworkType Infra
 
 # 0 None
 # 1 WEP 64-bit Hex
@@ -54,7 +210,7 @@
 # AuthMode                {OPEN,SHARED,WEPAUTO,WPAPSK,WPA2PSK,WPANONE}
 # EncrypType              {NONE,WEP,TKIP,AES}
 
-authmode="`swifi auth`"
+authmode=$(swifi auth)
 case $authmode in
        0)
                wset AuthMode OPEN
@@ -63,6 +219,7 @@
        1|2|3|4)
                wset AuthMode SHARED
                wset EncrypType WEP
+               wset DefaultKeyID 1
                ;;
        5)      wset AuthMode WPAPSK
                wset EncrypType TKIP
@@ -78,24 +235,80 @@
                ;;
 esac
 
-ssid=`swifi ssid`
-psk=`swifi key`
-wpakey=`wpa_passphrase "$ssid" "$psk" | sed -n '4 { s/.*=//; p }'`
+psk=$(swifi key)
 
-wset SSID $ssid
-wset WPAPSK $wpakey
-wset SSID $ssid
+# add processing to handle WEP
+# also work around Humax failure to distinguish WEP key types
+isHex() {
+       [ -z "$(echo "${1:-Z}" | sed 's/[[:xdigit:]]*//')" ]
+}
 
-if [ "`swifi manual`" = 1  ]; then
+case $authmode in
+       0)      # no key
+               ;;
+
+       1|2|3|4)        # WEP, but may be confused wrt key type (see list above)
+               if [ ${#psk} -eq 10 ] && isHex "$psk"; then
+                       authmode=1
+               elif [ ${#psk} -eq 26 ] && isHex "$psk"; then
+                       authmode=3
+               # these will likely actually have length 5 or 13
+               elif [ ${#psk} -le 5 ]; then
+                       authmode=2
+               elif [ ${#psk} -le 13 ]; then
+                       authmode=4
+               fi
+               
+               wset Key1 "$psk"
+               ;;
+
+       *)      # apparently necessary to set SSID twice for WPA-PSK?
+               wset SSID "$ssid"
+               wset WPAPSK "$(wpa_passphrase "$ssid" "$psk" | sed -n '4 { s/.*=//; p }')"
+               ;;
+esac
+
+wset SSID "$ssid"
+
+if [ -n "$WLANDBG" ]; then 
+       echo "== iwpriv show"
+       for p in NetworkType AuthMode EncrypType Key1 WPAPSK SSID; do 
+               iwpriv "$wif" show "$p"
+       done
+fi
+
+if [ "$(swifi manual)" = 1  ]; then
        echo "Manual config:"
        set -x
-       ifconfig $wif "`swifi ip`" netmask "`swifi mask`" up
-       route add -net default gw "`swifi gw`"
-       echo "nameserver `swifi dns`" > /etc/resolv.conf
+       # fix lost route - must eth_check before ifconfig
+       # see https://hummy.tv/forum/threads/wifi-static-manual-gateway-ip-occasionally-incorrect-on-startup.9657/post-139964
        eth_check
+       ifconfig "$wif" "$(swifi ip)" netmask "$(swifi mask)" up
+       route add -net default gw "$(swifi gw)"
+       echo "nameserver $(swifi dns)" > /etc/resolv.conf
 else
+       # wait for PHY connection 
+       while ! isConnected "$wif"; do
+               sleep 2
+       done
        eth_check
-       echo "DHCP:"
-       udhcpc -t 5 -T 10 -p /var/lib/humaxtv/udhcpc.$wif.pid -i $wif &
+       echo "$(date -Iseconds -u) DHCP:"
+       # file holds PID of $wif's udhcpc
+       pidf="/var/lib/humaxtv/udhcpc.$wif.pid"
+       # if udhcpc is running for $wif, renew the lease - or launch a new one
+       [ -s "$pidf" ] && 
+               pid=$(cat "$pidf") && 
+               kill -USR1 "$pid" 2>/dev/null ||
+               udhcpc -t 5 -T 10 -p "$pidf" -i "$wif" &
+       # give some time for above to work before next init routine?
        sleep 7
 fi
+
+# if successful connection, save settings
+if isConnected "$wif"; then
+       # save to DB if DB was unpopulated
+       type swifi | grep -qF function && save_settings_db
+       # save safety copy to file
+       save_settings
+fi
+
This /sbin/wifi-up script is run by a startup script installed by the wifi-helper package, so you'd need to install wifi-helper to ensure that the new version has the desired effect (the script is supposed to operate in the old way if the configuration file or directory should be unavailable). Also, in case anyone cares, the password will be readable from the /mod/etc/wlan.conf file.

As with certain other improvements, it might be possible to get this baked into the next CF release.
 
Last edited:
OP
M

mr-b

Member
No, it never came up in any forum or wiki searches and it wasn't in a sticky. I guess it's a vintage post! Will take a look.

Re: Sytem Flush, it seems to be a file and the wiki says:
"Install as standard firmware update to perform a full system flush and reset. "
... so I'm guessing it'll be prone to the old usb compat issues.


The wifi backup script sounds interesting. Could it be part of a package, vs firmware?
 

Black Hole

May contain traces of nut
Could it be part of a package, vs firmware?
Not sure about your nomenclature. "Firmware" is only what's baked into the flash, the rest of the CFiverse is packages or personal scripts stored on HDD and therefore software. If you mean whether the network settings can be made to backup and restore through a new or modified published package - yes, maybe, but things don't happen very fast these days (because of changed priorities in the life of the prime mover) so incorporating your own tweaks shouldn't be ruled out.

... so I'm guessing it'll be prone to the old usb compat issues.
Not sure. I inquired about this recently but don't recall a reply - it is not a literal firmware update, I believe it is a mechanism for triggering the CF to do something at boot time so the limited UPD support during firmware update might not be relevant. Some time ago I asked for the wording in the wiki to be tweaked to clarify that a System Flush is not an actual firmware update.

Another thread you might not have found (but all linked through my post signature): Glossary (click). Why aren't these "vintage" threads all pinned? Well, it would seem very self-serving to pin all the summaries I have written. I prefer to pin indexes.
 
OP
M

mr-b

Member
My POV was for remote installation/administration, so a firmware update is definitely going to be in the too-hard pile, whereas a package/script can be installed/configured/removed/recovered from etc. remotely.

RE: the flush, if it's a file that needs to be read off a USB stick (irrespective of what happen afterwards), then I'd hazard that it would be prone to the old compat issues.

RE: vintage thread, I don't see how pinning something useful is self-serving, no matter how old it is! :) I did search extensively for crash info but couldn't find it in any index posts either ...
 

/df

Active Member
...
Re: Sytem Flush, it seems to be a file and the wiki says:
"Install as standard firmware update to perform a full system flush and reset. "
... so I'm guessing it'll be prone to the old usb compat issues.
Hmmm, I extracted the update from the System Flush download, so as to establish in my own mind what it does, but the extracted (by humidify) image files don't appear to be a valid squashfs filesystem, unlike the actual updates. However, unlike eg the IPTABLES flush update it doesn't seem to be a package update, so would depend on the USB support in the bootloader as you suggest.
The wifi backup script sounds interesting. Could it be part of a package, vs firmware?
The script is used for Maintenance Mode, so it's part of the CF itself. Ideally the existing script would be modified for the next CF version; alternatively the wifi-helper package could be modified to patch it, similar to my test setup.

In this setup, I have a copy of the script in /var/lib/humaxtv_backup/mod/wifi-up and a script named /var/lib/humaxtv/mod/xinit.d/wifi, that mounts the modified script over the original, as below (both scripts need to be marked executable):
Code:
#!/bin/sh

mount --bind \
        /var/lib/humaxtv_backup/mod/wifi-up \
        /sbin/wifi-up
This modifies /sbin/wifi-up just before the settop binary, or Maintenance Mode, starts. Note that this setup is on the flash storage that gets reinitialised by a System Flush, so you need a backup on the hard disk.
 
OP
M

mr-b

Member
Tx for all the spadework. I look forward to my mother being a guinea-pig (!), though if it's only in a firmware flash vehicle it may have to wait until a CV19 vaccine is out! Altenatively I guess I could send a pre-tested USB drive first and hope the compat issue doesn't vary between boxes ...
 

/df

Active Member
After running a System Flush on the test machine, I have some observations.
  • There's a small bug in the patch from post #6, where if the settings are being restored from /mod/etc/wlan.conf the file is trashed. Because of the next point this breaks subsequent usages of the script.
  • The persistent network settings that you get with Menu>Settings>System>Internet Setting aren't automatically updated if the system has managed to connect in another way (eg using /sbin/wifi-up). It's necessary to save the settings in /var/lib/humaxtv/setup.db.
Fortunately I have a version of the Jim implementation of the swifi command modified to support saving the parameters to the DB as well as fetching them, but that is for later. Meanwhile this version of the save_settings function from the patched script will be more successful:
Code:
save_settings() {
        local conf
        [ -w "${WLANCONF%/*}" ] &&
                  # don't clobber it if it is being read as well as updated!
                  conf=$(swifi) &&
                  { echo "# these settings are used if no settings are configure
                    echo "$conf"; } > "$WLANCONF"
}
 

/df

Active Member
I wrote
...Fortunately I have a version of the Jim implementation of the swifi command modified to support saving the parameters to the DB as well as fetching them...
In the end I adapted the technique that the WebIf>Settings>Network page uses, whereby the settings are written to a database update script that's run on the next reboot before the Humax settop binary runs.

Now, if the wifi settings are missing from the settings database and successfully restored from the configuration file /mod/etc/wlan.conf, on the next reboot, the settings will have been updated into the settings database and the Humax settop binary can successfully make the wifi connection, as if you had entered the values using the Humax user interface or WebIf>Settings>Network.

Regardless of the values entered, any WEP security appears in the Humax UI as "WEP 64-bit Hex"; this is a product bug; but as the UI can't reveal the password it isn't significant; you just have to change the value if you need to change the password.

I've updated the now more extensive patch in post 6.
 
Last edited:

Black Hole

May contain traces of nut
I've arrived at the Supported User only to find the installation wizard has kicked in and she hasn't been able to watch any telly since yesterday! Loads of phone messages, not one mentioning the TV!

I had to run the wizard, then re-authenticate my phone hotspot on the WiFi to get access to WebIF, reboot through diagnostics to run boot-settings and tunefix, then run a manual schedule restore because the last saved backup was empty (and reboot again).
 

/df

Active Member
If you have a spare USB stick, why not save the resulting good settings using the hidden menu function?

If there's any hope of SU being able to follow remote instructions for the procedure of running the wizard and then restoring the settings without having lost or destroyed the stick beforehand, you could leave it there, or at least having it with you would simplify your work when you arrive next time.

You might even be able to generate a usable stick from your own machine if both are in the same reception area, so you could take the latest tuning data with you, and then let bootset restore the other local settings (but then you need to be able to restore the local wifi settings to, per this thread).
 
Top