guino/WR301CH1KW

[FR] Size calculation in `build.sh` to support sizes over 60Kb or so

JayGoldberg opened this issue · 1 comments

Forgive me, as my understanding of hex and binary are pretty bad.

Original config_backup.bin is 95232 bytes. Minus 512b is 94720 bytes.

in build.sh:

SZ=$(printf '%X' `stat -c %s new_config_backup.tgz`)

%X prints a HEX number in uppercase, so we get $SZ as 17200 for the size above.

In the exported config we can see the size 0072 0100 before IPCAM (4950 4341 4D)
image

But further in the script we take the first pair and second pair of hex chars '17' and '20'

echo -ne "\x${SZ:2:2}\x${SZ:0:2}\x00\x00IPCAM" >> nh.bin

but leave the trailing zero out. So the size is written out incorrectly. This works fine for smaller file sizes that fit in a single word.

Does that make sense?

Is it trivial to patch build.sh to handle larger config sizes?

guino commented

@JayGoldberg you are correct that my build.sh has a limit of 64Kb size based on the fact I am only writing the size as 2 bytes. I honestly did not recall doing that when writing the script and my backup is only about 6Kb in size so it never crossed my mind supporting bigger backup files. In fact my build.sh requires the size to be more than 255 bytes and less than 65536 bytes (which is not the case of your backup because your backup does not seem to be compressed while mine is).

This is an adjusted build.sh to not compress the backup and to support the bigger size of the tar file:

#!/bin/bash

# Create new package with our contents
tar cpf new_config_backup.tgz mnt/mtd/ipc/conf/
# Create new package file with 'IPCAM' added to the end of file
echo -n "IPCAM" | cat new_config_backup.tgz - > new_config_backup.tgz.md5
# Get the MD5 sum of the package with 'IPCAM' at the end of file
MD=$(md5sum new_config_backup.tgz.md5 | awk '{print $1}')
# Clean up (we no longer need this file)
rm -f new_config_backup.tgz.md5
# Get size of package (without IPCAM at the end of file)
SZ=$(printf '%06X' `stat -c %s new_config_backup.tgz`)
# Create new header for our config backup starting by signature
echo -ne 'PIHC\x01\x10' > nh.bin
# Fill in blank space
dd if=/dev/zero bs=18 count=1 >> nh.bin
# Add in size of package/payload
echo -ne "\x${SZ:4:2}\x${SZ:2:2}\x${SZ:0:2}\x00IPCAM" >> nh.bin
# Fill in blank space
dd if=/dev/zero bs=195 count=1 >> nh.bin
# Add in MD5 sum of package/payload
echo -n "$MD" >> nh.bin
# Fill in blank space
dd if=/dev/zero bs=252 count=1 >> nh.bin
# Create new config_backup with header and package/payload combined
cat nh.bin new_config_backup.tgz > new_config_backup.bin
# Clean up header and package files (no longer needed)
rm -rf nh.bin new_config_backup.tgz

That said I am not sure the backup will work without the garbage bytes found in your backup but it's worth a shot.
In any case my biggest hope for you to root your other cameras (that you did not open) still is to just make the changes in the config settings of your rooted camera (the one you opened), make a backup on that rooted camera and restore it on the other (non-rooted) cameras.

PS: Good catch on your side by inspecting the file in such detail as I did not recall that field/size limitation.