Any way to implement the "nvram export --set" in ARM builds?

Discussion in 'Tomato Firmware' started by rs232, Feb 7, 2019.

  1. rs232

    rs232 Network Guru Member

    As per title I noticed that the export --set works out of the box in MIPS but it returns an error on ARM.

    So lazy question (without documenting myself first) is there any known issue why this is not working and/or was removed/never implemented?

    Thanks

    MIPS:
    root@mips-router:/tmp/home/root# nvram
    NVRAM Utility
    Copyright (C) 2006-2009 Jonathan Zarate

    Usage: nvram set <key=value> | get <key> | unset <key> |
    ren <key> <key> | commit | erase | show [--nosort|--nostat] |
    find <text> | defaults <--yes|--initcheck> | backup <filename> |
    restore <filename> [--test] [--force] [--forceall] [--nocommit] |
    export <--quote|--c|--set|--tab> [--nodefaults] |
    export <--dump|--dump0> | import [--forceall] <filename> |
    setfb64 <key> <filename> | getfb64 <key> <filename> |
    setfile <key> <filename> | getfile <key> <filename> | setfile2nvram <filename>
    Code:
    root@3500L:/tmp/home/root# nvram
    NVRAM Utility
    Copyright (C) 2006-2009 Jonathan Zarate
    
    Usage: nvram set <key=value> | get <key> | unset <key> |
    ren <key> <key> | commit | erase | show [--nosort|--nostat] |
    find <text> | defaults <--yes|--initcheck> | backup <filename> |
    restore <filename> [--test] [--force] [--forceall] [--nocommit] |
    export <--quote|--c|--set|--tab> [--nodefaults] |
    export <--dump|--dump0> | import [--forceall] <filename> |
    setfb64 <key> <filename> | getfb64 <key> <filename> |
    setfile <key> <filename> | getfile <key> <filename> | setfile2nvram <filename>
    
    ARM:
    Code:
    root@arm-router:/tmp/home/root# nvram
    usage: nvram [get name] [set name=value] [unset name] [show] [commit] [save] [restore] [erase] ...
    
     
    rgnldo and Mercjoe like this.
  2. Twincam

    Twincam Networkin' Nut Member

    @rs232 I run the scripts below to execute a daily nvram "Backup". The code copes with both ARM & MIPS architectures.

    upload_2019-2-7_22-30-42.png

    01) Backup-NVRAM.sh

    Code:
    # ------------------------------------------------------------------------------ #
    # NB - Added 13/12/17 - Daily NVRAM Backup
    # ------------------------------------------------------------------------------ #
    # 20171216 NB - Added "tomato_backup.sh"
    #             - Handle non-"standard" FS
    # ------------------------------------------------------------------------------ #
    # Idea @[http://tomatousb.org/forum/t-561198/backup-and-restore-to-new-router]
    #
    #  NB: Set the following variables as required
    #
    DIR_NAME="/tmp/mnt/NTFS-RW/Backups-NVRAM" # <- "Standardised" default [Ext2]
    #
    # -> NB Change
    #  Fudge for "Tomato" & "Tomato-AL" ....
    if [ "`nvram get router_name`" = "Tomato" ];then DIR_NAME="/tmp/mnt/Tomato-NTFS/Backups-NVRAM"; fi
    if [ "`nvram get router_name`" = "Tomato-AL" ];then DIR_NAME="/opt/Backups-NVRAM"; fi
    # <- NB Change
    FILENAME="`date +"%Y%m%d-%H%M%S"`-`nvram get wan_hostname`"
    #
    #  NB: WILL work via PuTTY session [WebUI can also be used for testing]
    #       String: [$DIR_NAME/Backup-NVRAM.sh]
    #
    #  For interactive output
    echo ""&&echo " [`nvram get wan_hostname`] Executing Backup-NVRAM script ...."
    echo ""&&echo " [$DIR_NAME/Backup-NVRAM.sh]"&&echo ""
    #
    cd $DIR_NAME
    #  Are we ARM or MIPS? [use "Hardware Type" switch]
    uname -m | grep armv7l > /dev/null && (
        nvram save $FILENAME.cfg
    ) || (
        nvram export --set > $FILENAME.cfg
        # For text "comparison" tools ....
        sort $FILENAME.cfg -o $FILENAME-Sorted.txt
    )
    if [ "$1" != "" ]; then
        echo " [`nvram get wan_hostname`] Submitting BGD job [tomato_backup] ...." && echo ""
        /opt/tomato_backup.sh > $DIR_NAME/`date +%Y%m%d-%H%M%S`-`nvram get wan_hostname`.nvram &
    fi
    logger "[`nvram get wan_hostname`] Executed Backup-NVRAM script [`echo $?`]"
    02) tomato_backup.sh

    Code:
    #--------------------------------------------------------------------------------#
    # NB - Found @[https://gist.github.com/ilium007/c35ef98a3d92352d2ba525e2fe4d722e]
    #--------------------------------------------------------------------------------#
    #
    #  Blanked "items" list [ALL variables] & amended EXPORT "Header"
    #
    
    #!/bin/sh
    
    #USE AT YOUR OWN RISK.
    #THIS SCRIPT DOES NOT COME WITH ANY WARRANTY WHATSOEVER.
    #
    #Backs up selected nvram variables in "nvram export --set" format.
    #
    #Correctly handles multi-line entries.
    #
    #Thanks to ryzhov_al for basic approach.
    #
    #Should work equally well with both MIPS and ARM builds.
    #
    #Looks for a list of items to export in $etc/scriptname.ini
    #OR enter items to grep for below.
    #
    #The items list is a list of regular expressions to match against the
    #nvram variable names.
    #
    #Script assumes all entries are at beginning of line(prefixed with ^).
    #
    #Leave items list blank to backup up all of nvram.  Resulting in essentially
    #the same output as MIPS "nvram export --set"
    #
    #The items list below is only intended as example and is not complete or
    #comprehensive. Customize for your own use.
    #
    
    config_dir=${2-/mnt/EXTUSB/scripts}
    backup_path=${3-/mnt/EXTUSB/backups}
     
    #file to output - default to stdout
    backupfile=${1-/proc/$$/fd/1}
    if [ $1 ]; then
      backupfile=$backup_path/$backupfile`date +%Y%m%d`.nvram
    fi
    
    #Edit list below if not using .ini file, it is ignored if .ini file is found
    # -> NB Change
    #items='
    #  dhcpd_
    #  dns_
    #  wl[0-9]_security_mode
    #  wl[0-9]_ssid
    #  wl[0-9]_wpa_psk
    #'
    items=''
    # <- NB Change
    
    base=${0##*/}; base=${base%.*}
    config=$config_dir/$base.ini
    
    #file to output - default to stdout
    if [ ! "$1" ] ; then
      backupfile=/proc/$$/fd/1
    fi
    
    grepstr=$( { [ -r $config ] && cat $config || echo "$items" ; } | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\\\|\^/g')
    
    {
    # -> NB Change
    #echo "#Exporting $grepstr"
    echo "#Exporting [`nvram get os_version`] `date +"%d-%m-%Y @%H:%M:%S"` $grepstr"
    # <- NB Change
    for item in $(nvram show 2>/dev/null | grep "^.*=" | grep -v "hwaddr" | grep "$grepstr"  | awk -F= "{print \$1}" | sort -u)
    do
      item_value="$(nvram get $item | sed 's!\([\$\"\`]\)!\\\1!g'; echo nvgetwasnull)"
      case $item_value in
      nvgetwasnull) ;;
      *) echo "nvram set ${item}=\"${item_value%
    nvgetwasnull}\"" ;;
      esac
    done
    }>"$backupfile"
    
    /bin/chmod 600 "$backupfile"
    Note that I'm not that clever! The links that inspired me [ie. where I blagged the code from] are within the two code blocks above. Apart from playing around with the path names to suit your environment, it should be possible to use derivatives of these files. They appear to work reliably as I have just deleted >9 months of old files! HTH.
     
  3. rs232

    rs232 Network Guru Member

    Thanks for this I was aware of back up scripts for NVRAM but my point is more general:
    why do we have 2 version of nvram?
    The MIPS version as far as I can see it's just great why don't we have the same under ARM?
    It seems like rewritten to me.

    Is there a technical issue with the implementation? was the old nvram not "portable" into arm?

    Thanks
     
  4. Twincam

    Twincam Networkin' Nut Member

    @rs232 I think you're correct. As your original post confirms, nvram is different between MIPS & ARM. At a guess it is a situation caused by the different architectures [ie. the MIPS version won't work on ARM hardware].

    What I find remarkable is just how similar, for an end User, the two versions actually are. I think the developers have done a fantastic job; anyone familiar with the original "Tomato" [MIPS] WebUI should have very little difficulty in managing a more recent version of FreshTomato [eg. running on ARM] or, for that matter, Advanced Tomato [in fact, any version of "Tomato"-derived FW I have seen]. The developers have succeeded in hiding the hardware differences from the User.
     
  5. rs232

    rs232 Network Guru Member

    I agree on all the good work done, and yes i can't really complain: it works!
    I just feel though like we run a marathon and we stopped at the 41th Km.

    I guess there was some rush to go live with ARM thinking to go back and fix the nvram command later on but that never happened.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice