#!/bin/bash
set -eu

#--------------------------------------------Input checks-----------------------------------
binary_file=$1
mac=$2

# check if the binary file exists
if [ ! -f "$binary_file" ]; then
    echo "Error: File $binary_file does not exist"
    exit 1
fi

# Check if the correct number of arguments have been passed
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <binary_file> <mac_address>"
    exit 1
fi

#--------------------------------------------Mac calculations--------------------------------
# Read and convert MAC address to uppercase
input_mac_address=$(echo "$mac" | tr '[:lower:]' '[:upper:]')

# Set IFS(Internal Field Separator) to ":" to use ":" as the delimiter
IFS=":"

# Use a for loop to iterate over the numbers in the MAC address
summa=0x0
for num in $input_mac_address; do
  summa=$(( summa + 0x$num ))
done

# Reset IFS to its default value
IFS=" "

# s/pattern/replacement/g s=substitution command, g=globally
string_mac_address=$(echo "$input_mac_address" | sed 's/://g')

# convert to hexadecimal number form string
hex_mac_address=$(echo "ibase=16; $string_mac_address" | bc)

#-------------------------------------------- Checksum calculations--------------------------------
# Checksum value excluding the part number
# version   + Sect data size  +  Reserved byte
# (01 + 00) + (0c + 00)       + (00 + 00 + ff + ff +ff + ff)
constant_checksum_hex=0x409

# Checksum value including mac address summa
checksum_hex=$(( constant_checksum_hex + summa ))

# Checksum value LSB and MSB
checksum_hex_lsb=$(( checksum_hex & 0xFF ))

checksum_hex_msb=$((( checksum_hex & 0xFF00 ) >> 8))

# constant parts of the write data
constant_hex_part_1=0x01000c00
constant_hex_part_2=0x0000
constant_hex_part_3=0xffffffff

# Address Offset | 0x00              | 0x01              | 0x02                  | 0x03                  |
#------------------------------------------------------------------------------------------------------------------------
# 0x000000       | Version LSB(0x01) | Version MSB(0x00) | Sector Size LSB(0x0c) | Sector Size MSB(0x00) |  $write_part_1
# 0x000004       | CheckSum[0]       | CheckSum[1]       | CheckSum[2](0x00)     | CheckSum[3](0x00)     |  $write_part_1
# 0x000008       | MAC[0]            | MAC[1]            | MAC[2]                | MAC[3]                |  $write_part_2
# 0x00000C       | MAC[4]            | MAC[5]            | Reserved(0x00)        | Reserved(0x00)        |  $write_part_2
# 0x000010       | Reserved(0xff)    | Reserved(0xff)    | Reserved(0xff)        | Reserved(0xff)        |  $write_part_3

# write data in 3 separate hexa numbers
write_part_1=$(( constant_hex_part_1 << 32 | checksum_hex_lsb << 24 | checksum_hex_msb << 16 | 0x0000 ))
write_part_2=$(( hex_mac_address << 16 | constant_hex_part_2 ))
write_part_3=$(( constant_hex_part_3 ))

# write the binary numbers to the file
#specified width here is in hexadecimal numbers(4bit)
(printf '%016x' "$write_part_1") | xxd -r -p | dd of="$binary_file" bs=8 seek=0 conv=notrunc
(printf '%16x' "$write_part_2") | xxd -r -p | dd of="$binary_file" bs=8 seek=1 conv=notrunc
(printf '%8x' "$write_part_3") | xxd -r -p | dd of="$binary_file" bs=4 seek=4 conv=notrunc

