Convert a MAC Address to Motorola S-Records

 in

If you do embedded programming you're probably familiar with Motorola S-Records. This following script takes a MAC Address and outputs S-Record data.

Given a MAC Address the script outputs S-Record data:

  # sh macid.sh 00:11:22:33:44:55
  S00D0000333737382D322E746D703B
  S1090000001122334455F7
  S9030000FC
Optionally, you can specify an address for re-basing the S-Record data and an output file name to write the S-Records to:
  # sh macid.sh --address 0xffff0000 --output ma 00:11:22:33:44:55
  # cat ma
  S00D0000333733302D322E746D7047
  S30BFFFF0000001122334455F7
  S705FFFF0000FC

The script follows:
#!/bin/bash

mac_addr_pattern='^[0-9a-fA-F]{2}[-:]'
mac_addr_pattern+='[0-9a-fA-F]{2}[-:]'
mac_addr_pattern+='[0-9a-fA-F]{2}[-:]'
mac_addr_pattern+='[0-9a-fA-F]{2}[-:]'
mac_addr_pattern+='[0-9a-fA-F]{2}[-:]'
mac_addr_pattern+='[0-9a-fA-F]{2}'

address_pattern='^(0x)?[0-9a-fA-F]{8}$'


######################################################################
function usage()
{
    if [[ "$*" ]]; then echo $* >&2; fi
    cat >&2 <<EOU
Usage: $0 OPTIONS MAC-ADDRESS
  --address ADDRESS              Address in FLASH of serial number or MAC Id.
  --output FILE                  Output file.
EOU
    exit 1
}


######################################################################
trap "rm -f $$-?.tmp" EXIT

output_file=

while [[ "$1" ]]
do
    case "$1" in
    --address)
        shift
        if [[ ! "$1" ]]; then usage "Missing address"; fi
        address=$1
        if ! grep --silent -P "$address_pattern" <<<$address; then
            usage "Invalid address: $address"
        fi
        ;;
    --output)
        shift
        if [[ ! "$1" ]]; then usage "Missing output file name"; fi
        output_file=$1
        ;;
    *)
        mac_addr=$1
        shift
        if [[ "$1" ]]; then usage "Unexpected argument: $1"; fi
        if ! grep --silent -P "$mac_addr_pattern" <<<$mac_addr; then
            usage "Invalid mac address: $mac_addr"
        fi
        ;;
    esac
    shift
done

if [[ ! "$mac_addr" ]]; then usage "Missing mac address"; fi

mac_addr=${mac_addr/:/ }
mac_addr=${mac_addr/-/ }
echo "0000: $mac_addr" | xxd -r >$$-1.tmp
opt=
if [[ "$address" ]]; then opt="--change-addresses $address"; fi
objcopy $opt -I binary -O srec $$-1.tmp $$-2.tmp
if [[ "$output_file" ]]; then
    mv $$-2.tmp $output_file
else
    cat $$-2.tmp
fi

# vim: tabstop=4: shiftwidth=4: noexpandtab:
# kate: tab-width 4; indent-width 4; replace-tabs false;

______________________

Mitch Frazier is an Associate Editor for Linux Journal.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState