Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flash_swagbadge.sh only generate keys when not found. #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 58 additions & 39 deletions scripts/flash_swagbadge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,39 @@ KEYS_PATHNAME=z_keys
KEYS_INDEX=$KEYS_PATHNAME/index
KEYS_LOG_PATHNAME=$KEYS_PATHNAME/allocated_keys.log

# If not already saved, then save default "configuration/keys.db"
#
if [ ! -f "${CONFIGURATION_KEY_FILENAME}_default" ]; then
mv $CONFIGURATION_KEY_FILENAME ${CONFIGURATION_KEY_FILENAME}_default 2>/dev/null
# If key is non default don't generate a new one
ORIGINAL_KEY=$(cat $CONFIGURATION_KEY_FILENAME | cut -d "=" -f2)
if [ $ORIGINAL_KEY != "0000000000000000000000000000000000000000000000000000000000000000" ]; then
SKIP_KEY_GEN=1
echo "Key found, Skipping new key generation"
fi

# Determine next "keys_$INDEX.db" file to use
#
INDEX=0000`cat $KEYS_INDEX`
INDEX=${INDEX: -4}
KEYS_FILENAME=$KEYS_PATHNAME/keys_$INDEX.db
if [ -z "$SKIP_KEY_GEN" ]; then
# If not already saved, then save default "configuration/keys.db"
#
if [ ! -f "${CONFIGURATION_KEY_FILENAME}_default" ]; then
mv $CONFIGURATION_KEY_FILENAME ${CONFIGURATION_KEY_FILENAME}_default 2>/dev/null
fi

# Check if "keys_$INDEX.db" file exists
#
if [ ! -f "$KEYS_FILENAME" ]; then
echo "$KEYS_FILENAME does not exist, you've run out of unique keys !"
exit
# Determine next "keys_$INDEX.db" file to use
#
INDEX=0000`cat $KEYS_INDEX`
INDEX=${INDEX: -4}
KEYS_FILENAME=$KEYS_PATHNAME/keys_$INDEX.db

# Check if "keys_$INDEX.db" file exists
#
if [ ! -f "$KEYS_FILENAME" ]; then
echo "$KEYS_FILENAME does not exist, you've run out of unique keys !"
exit
fi

# Use unique "keys_$INDEX.db" when flashing ESP32
#
cp $KEYS_FILENAME $CONFIGURATION_KEY_FILENAME
echo "Index is $INDEX and using $KEYS_FILENAME"
fi

# Use unique "keys_$INDEX.db" when flashing ESP32
#
cp $KEYS_FILENAME $CONFIGURATION_KEY_FILENAME
echo "Index is $INDEX and using $KEYS_FILENAME"
echo "###########################################"

# Erase, flash microPython and flash Aiko Engine to ESP32
Expand All @@ -52,32 +62,41 @@ echo "###########################################"
# Display the unique key INDEX (up-one integer) and ESP32 serial id
#
DEVICE_SERIAL_ID=`./scripts/device_info.py $AMPY_PORT 2>&1 | grep passed | cut -d\' -f2`
echo "Index is $INDEX for ESP32 serial id: $DEVICE_SERIAL_ID"
if [ -z "$DEVICE_SERIAL_ID" ]; then
echo "Testing failed, see ./scripts/device_info.py for more details"
exit
else
echo "$DEVICE_SERIAL_ID succesfully flashed"
fi

# Increment unique key index value
#
INDEX=`cat $KEYS_INDEX`
echo `expr $INDEX + 1` >$KEYS_INDEX
if [ -z "$SKIP_KEY_GEN" ]; then
echo "Index is $INDEX for ESP32 serial id: $DEVICE_SERIAL_ID"

# Update allocated keys log file "INDEX:DEVICE_SERIAL_ID"
# Record count should increment by one, each time
#
touch $KEYS_LOG_PATHNAME
KEYS_LOG_OLD_LENGTH=`wc -l $KEYS_LOG_PATHNAME | column -t | cut -d" " -f1`
KEYS_LOG_OUTPUT="$INDEX:$DEVICE_SERIAL_ID"
echo $KEYS_LOG_OUTPUT >>$KEYS_LOG_PATHNAME
KEYS_LOG_NEW_LENGTH=`wc -l $KEYS_LOG_PATHNAME | column -t | cut -d" " -f1`
echo "$KEYS_LOG_PATHNAME record count: $KEYS_LOG_OLD_LENGTH -> $KEYS_LOG_NEW_LENGTH"
# Increment unique key index value
#
INDEX=`cat $KEYS_INDEX`
echo `expr $INDEX + 1` >$KEYS_INDEX

# Update allocated keys log file "INDEX:DEVICE_SERIAL_ID"
# Record count should increment by one, each time
#
touch $KEYS_LOG_PATHNAME
KEYS_LOG_OLD_LENGTH=`wc -l $KEYS_LOG_PATHNAME | column -t | cut -d" " -f1`
KEYS_LOG_OUTPUT="$INDEX:$DEVICE_SERIAL_ID"
echo $KEYS_LOG_OUTPUT >>$KEYS_LOG_PATHNAME
KEYS_LOG_NEW_LENGTH=`wc -l $KEYS_LOG_PATHNAME | column -t | cut -d" " -f1`
echo "$KEYS_LOG_PATHNAME record count: $KEYS_LOG_OLD_LENGTH -> $KEYS_LOG_NEW_LENGTH"

DUPLICATES_COUNT=`cut -d: -f1 $KEYS_LOG_PATHNAME | sort | uniq -d | wc -l | column -t`
if [ $DUPLICATES_COUNT -eq 0 ]; then
DUPLICATES_COUNT=`cut -d: -f1 $KEYS_LOG_PATHNAME | sort | uniq -d | wc -l | column -t`
if [ $DUPLICATES_COUNT -eq 0 ]; then
echo "No duplicate key index used :)"
else
else
echo "ERROR: Duplicate key index count: $DUPLICATES_COUNT"
DUPLICATES=`cut -d: -f1 $KEYS_LOG_PATHNAME | sort | uniq -d | xargs echo`
echo "ERROR: Duplicate key index: $DUPLICATES"
fi
fi

# Restore default "configuration/keys.db"
#
mv ${CONFIGURATION_KEY_FILENAME}_default $CONFIGURATION_KEY_FILENAME
# Restore default "configuration/keys.db"
#
mv ${CONFIGURATION_KEY_FILENAME}_default $CONFIGURATION_KEY_FILENAME
fi