Skip to content

Instantly share code, notes, and snippets.

@dgalli1
Created July 19, 2025 14:58
Show Gist options
  • Save dgalli1/e497a26828316ed778d189a3ae672d47 to your computer and use it in GitHub Desktop.
Save dgalli1/e497a26828316ed778d189a3ae672d47 to your computer and use it in GitHub Desktop.
patch pocketbook image
#!/bin/bash
set -euo pipefail
IMG="flash.img"
DEVICE="/dev/sdb"
SERIAL_STRING_OFFSET=0x05B00000
# Your new ASCII serial number
NEW_TEXT_SERIAL="YT4409007156...."
# Partition table offsets
PARTITION_START_OFFSET=0x1C6
PARTITION_SIZE_OFFSET=0x1CA
# --- Start of modifications ---
# 1. Make a backup of the original image
if [ ! -f "${IMG}.backup" ]; then
echo "Creating backup of ${IMG} to ${IMG}.backup"
cp "${IMG}" "${IMG}.backup"
else
echo "Backup ${IMG}.backup already exists. Restoring from it."
cp "${IMG}.backup" "${IMG}"
fi
# 2. Get the size of the target device in bytes
# Ensure the device exists
if [ ! -b "${DEVICE}" ]; then
echo "Error: Device ${DEVICE} not found."
exit 1
fi
DEVICE_SIZE_BYTES=$(lsblk -b -d -n -o SIZE "${DEVICE}")
if ! [[ "${DEVICE_SIZE_BYTES}" =~ ^[0-9]+$ ]]; then
echo "Error: Failed to determine device size for ${DEVICE}."
exit 1
fi
echo "Device ${DEVICE} size: ${DEVICE_SIZE_BYTES} bytes"
# 3. Read the partition start sector from the image (at 0x1C6)
# dd can read from an offset, and we use od to convert the 4-byte little-endian value to decimal
PARTITION_START_SECTOR=$(dd if="${IMG}" bs=1 skip=$((PARTITION_START_OFFSET)) count=4 2>/dev/null | od -t u4 -An | xargs)
if ! [[ "${PARTITION_START_SECTOR}" =~ ^[0-9]+$ ]]; then
echo "Error: Failed to read partition start sector from ${IMG}."
exit 1
fi
echo "Partition start sector: ${PARTITION_START_SECTOR}"
# 4. Calculate the new partition size in sectors
SECTOR_SIZE=512
NEW_PARTITION_SIZE_BYTES=$((DEVICE_SIZE_BYTES - PARTITION_START_SECTOR * SECTOR_SIZE))
NEW_PARTITION_SECTORS=$((NEW_PARTITION_SIZE_BYTES / SECTOR_SIZE))
echo "New partition size: ${NEW_PARTITION_SIZE_BYTES} bytes (${NEW_PARTITION_SECTORS} sectors)"
# 5. Write the new partition size to the image (at 0x1CA)
# We use perl to pack the 32-bit integer into little-endian format and pipe it to dd.
# dd will then write it at the correct offset without truncating the file.
perl -e "print pack('V', $NEW_PARTITION_SECTORS)" | dd of="${IMG}" bs=1 seek=$((PARTITION_SIZE_OFFSET)) conv=notrunc 2>/dev/null
echo "Partition size updated in ${IMG}."
# 6. Write the new serial number to the image (at 0x05B00000)
echo -n "${NEW_TEXT_SERIAL}" | dd of="${IMG}" bs=1 seek=$((SERIAL_STRING_OFFSET)) conv=notrunc 2>/dev/null
echo "Serial number updated in ${IMG}."
# --- Start of validation ---
echo "--- Starting validation ---"
function validate_partition_size() {
echo "Validating partition size..."
local written_sectors
written_sectors=$(dd if="${IMG}" bs=1 skip=$((PARTITION_SIZE_OFFSET)) count=4 2>/dev/null | od -t u4 -An | xargs)
if [ "${written_sectors}" == "${NEW_PARTITION_SECTORS}" ]; then
echo "✅ Partition size validation successful."
else
echo "❌ Partition size validation FAILED."
echo "Expected sectors: ${NEW_PARTITION_SECTORS}"
echo "Found sectors: ${written_sectors}"
exit 1
fi
}
function validate_serial_number() {
echo "Validating serial number..."
local written_serial
written_serial=$(dd if="${IMG}" bs=1 skip=$((SERIAL_STRING_OFFSET)) count=${#NEW_TEXT_SERIAL} 2>/dev/null)
if [ "${written_serial}" == "${NEW_TEXT_SERIAL}" ]; then
echo "✅ Serial number validation successful."
else
echo "❌ Serial number validation FAILED."
echo "Expected serial: ${NEW_TEXT_SERIAL}"
echo "Found serial: ${written_serial}"
exit 1
fi
}
validate_partition_size
validate_serial_number
echo "--- Validation finished successfully ---"
echo "--- Conversion finished ---"
echo "You can now write the modified ${IMG} to ${DEVICE}."
echo "For example: sudo dd if=${IMG} of=${DEVICE} bs=4M status=progress"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment