Last active
January 20, 2026 10:17
-
-
Save Geofferey/c6ba1f3db0185033b3a0755e06ad496e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| 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 (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 | |
| # | |
| # NOTE: In this packed format the 2nd byte is typically: <digit_1><A filler nibble> | |
| # Your original script incorrectly used <digit_1><parity>, producing 0x91 instead of 0x9A for IMEIs ending in 1. | |
| # | |
| # Layout produced: | |
| # 08 [digit_1 A] [rev(2-3)] [rev(4-5)] [rev(6-7)] [rev(8-9)] [rev(10-11)] [rev(12-13)] [parity digit_14] | |
| # | |
| hex_secret_setting="08${digit_1}A$(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" | |
| # Expect 9 bytes => 18 hex chars like: 08 9A 09 10 56 07 29 85 18 | |
| if [ ${#nv_content} -ne 18 ]; then | |
| echo "Error: NV content must be exactly 18 hex characters (9 bytes)." | |
| exit 1 | |
| fi | |
| imei="" | |
| # Byte2: <digit_1><filler> (we take digit_1) | |
| # nv_content positions: [0..1]=08, [2..3]=byte2, [4..]=rest | |
| byte2="${nv_content:2:2}" | |
| digit_1="${byte2:0:1}" | |
| imei="$imei$digit_1" | |
| # Bytes 3..8 are swapped pairs for digits 2..13 | |
| # Start at index 4 (byte3) for 6 bytes => 12 hex chars (digits 2..13) | |
| for i in $(seq 4 2 14); do | |
| pair="${nv_content:$i:2}" | |
| reversed_pair="${pair:1:1}${pair:0:1}" | |
| imei="$imei$reversed_pair" | |
| done | |
| # Last byte holds: <parity><digit_14> | |
| last="${nv_content:16:2}" | |
| digit_14="${last:1:1}" | |
| parity="${last:0:1}" | |
| imei="${imei}${digit_14}${parity}" | |
| 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 ${nv_item} 1 | tail -n1 | cut -d '[' -f2 | cut -d ']' -f1 | |
| echo | |
| } | |
| # 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 ${nv_item} ${nv_content} 1 | |
| echo | |
| # SAFETY: do not write device identifiers. | |
| # This prints what would be written so you can debug encoding/format. | |
| echo "REPLACED: NV item /$nv_item with:" | |
| echo "" | |
| echo "${nv_content}" | |
| } | |
| 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 (dry-run) | |
| write_nv_item_content "$new_nv_content" | |
| # Wait for a moment to ensure the write operation completes | |
| sleep 2 | |
| # Verify the change (read still allowed; no write is performed) | |
| 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 "" | |
| echo "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" | |
| #fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment