#!/bin/sh -e

source fcns.shinc
# =====================================================================
# $1 - Name of the mtd partition (e.g. "mc boot meta")
# (It's *not* something like "/dev/mtd0")
# $2 - Name of the variable that gets results
# =====================================================================
getMtdStrings()
{
    local mtdName="$1"

    # substitute '_' for invalid characters
    mtdName=${mtdName// /_}
    mtdName=${mtdName//\*/_}

    genAllMtdPropVariables "$(cat /proc/moose/mtd)"

    local mtdfile
    eval mtdfile="/dev/mtd\$mtdnbr_$mtdName"

    if [ ! -b "$mtdfile" ]; then
        err "Requested mtd partition does not exist"
        return 1
    fi

    eval $2=\""$(tail -c $mtd_reservedTail $mtdfile | strings)"\"
}

if [ -n "$1" ]; then
    getMtdStrings "$1" mtdStrings
    echo -e "$mtdStrings"
else
    for mtd in app initramfs kernel loader loader_meta \
        mc_boot_meta spare_meta
    do
        getMtdStrings "$mtd" mtdStrings
        if [ -n "$mtdStrings" ]; then
            echo "[$mtd]"
            echo -e "$mtdStrings\n"
        fi
    done
fi

