Skip to content

Instantly share code, notes, and snippets.

@Geofferey
Created May 18, 2025 18:33
Show Gist options
  • Save Geofferey/c6ba1f3db0185033b3a0755e06ad496e to your computer and use it in GitHub Desktop.
Save Geofferey/c6ba1f3db0185033b3a0755e06ad496e to your computer and use it in GitHub Desktop.
#!/bin/sh
## This script was originally part of:
# https://hotspottoolkit.com/product/inseego-tool-download/
# It can be used to repair lost/broken IMEI on NVTL M2000B
# and similar on Inseego devices
echo "Content-type: text/plain"
echo ""
# Function to reverse a string
reverse_string() {
echo "$1" | rev
}
# Function to convert IMEI to NV item content
imei_to_nv_item() {
secret_setting="$1"
if [ ${#secret_setting} -ne 15 ]; then
echo "Error: IMEI string must be exactly 15 characters long."
exit 1
fi
# Extract individual digit segments from the IMEI
digit_1="${secret_setting:0:1}" # First digit
parity="${secret_setting:14:1}" # Fifteenth digit (parity check digit)
digit_2_3="${secret_setting:1:2}" # Second and third digits
digit_4_5="${secret_setting:3:2}" # Fourth and fifth digits
digit_6_7="${secret_setting:5:2}" # Sixth and seventh digits
digit_8_9="${secret_setting:7:2}" # Eighth and ninth digits
digit_10_11="${secret_setting:9:2}" # Tenth and eleventh digits
digit_12_13="${secret_setting:11:2}" # Twelfth and thirteenth digits
digit_14="${secret_setting:13:1}" # Fourteenth digit
# Combine segments into the final hex format
hex_secret_setting="08${digit_1}${parity}$(reverse_string ${digit_2_3})$(reverse_string ${digit_4_5})$(reverse_string ${digit_6_7})$(reverse_string ${digit_8_9})$(reverse_string ${digit_10_11})$(reverse_string ${digit_12_13})${parity}${digit_14}"
echo "$hex_secret_setting"
}
# Function to decode NV item content to IMEI
decode_nv_item_to_imei() {
nv_content="$1"
imei=""
# Process the pairs, starting from the 4th character
for i in $(seq 4 2 $((${#nv_content} - 3))); do
pair="${nv_content:$i:2}"
reversed_pair="${pair:1:1}${pair:0:1}"
imei="$imei$reversed_pair"
done
# Add the last two characters as they are
imei="$imei${nv_content: -2}"
echo "$imei"
}
# Function to get the NV item content using modem2_cli
get_nv_item_content() {
nv_item="nvm/num/550"
modem2_cli efs_read <<EOF
$nv_item
1
EOF
}
# Function to write the NV item content using modem2_cli
write_nv_item_content() {
nv_item="nvm/num/550"
nv_content="$1"
modem2_cli efs_write <<EOF
$nv_item
$nv_content
1
EOF
}
read -r post_data
# Extract IMEI from POST data
imei=$(echo "$post_data" | awk -F'=' '{print $2}')
if [ -z "$imei" ]; then
echo "Error: IMEI value is missing."
exit 1
fi
# Start IMEI to NV item conversion
new_nv_content=$(imei_to_nv_item "$imei")
# Log the NV content to be written
write_nv_item_content "$new_nv_content"
# Wait for a moment to ensure the write operation completes
sleep 2
# Verify the change
nv_content=$(get_nv_item_content | grep "Content:" | awk -F'[][]' '{print $2}')
# Decode the NV content and verify the IMEI
verified_imei=$(decode_nv_item_to_imei "$nv_content")
echo "Original IMEI: $imei"
#echo "Verified IMEI: $verified_imei"
# Check if the original IMEI matches the verified IMEI
if [ "$imei" = "$verified_imei" ]; then
echo "IMEI verification successful."
else
echo "IMEI verification failed. Original: $imei, Verified: $verified_imei"
f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment