Sign up here and you can log into the forum!

unpack / repack bi2

Have a question about devices internals, memory layout, reverse engineering, etc---This is the place for anything so technical that it would cause a n00b's head to 'splode

unpack / repack bi2   

Postby TroNik » Fri Jan 10, 2014 3:12 am

Since there is no information anywhere how to unpack / repack a bi2 file for WDTV Live Plus and I spent a few hours to figure it out here is a guide.

File structure is as follows:
Code: Select all
[32 Byte md5sum][16 Byte header][squashfs image]

  • The 32 Byte md5sum is generated from both the header and the squashfs image.
  • The header consists of the byte size of the squashfs image + 16 followed by any string, WDLXTV releases use their version e.g. 0.5.1.1. A header example is: 19066896 0.5.1.1
  • The squashfs image has to be generated with squashfs3.4 (any other version won't work)

You need to download and build squashfs3.4

I've modified the script in unpack / repack a firmware to support two new commands: pack_bi2|unpack_bi2 (plus a couple of modifications in the pack command). See the following code for detailed steps.

Code: Select all
#!/bin/bash

CRAMFS_NAME=WDLXTV_PLUS
SQUASHFS_VERSION=0.5.1.1

cram_missing () { echo -e "please make mkcramfs-16k and cramfsck-16k available to your PATH\ndownload it here http://forum.wdlxtv.com/viewtopic.php?f=43&t=537" && quit; }
usage () { echo "usage: $0 <pack|unpack|pack_bi2|unpack_bi2> <firmware folder> <firmware>" && quit; }
quit () { rm -f $tmp && exit 1; }

tmp=`tempfile`
type -P cramfsck-16k &>/dev/null || cram_missing
type -P mkcramfs-16k &>/dev/null || cram_missing


[ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] && usage

if [ $1 == "pack" ]; then

#pack

[ -e $3 ] && echo "firmware \"$3\" already exists" && quit
[ ! -d $2 ] && echo "firmware folder \"$3\" does not exist" && quit

echo "create md5sum.txt ..."
#cwd=`pwd` && cd $2 && find  -not -name "md5sum.txt" -type f -exec md5sum '{}' ';' > md5sum.txt && cd $cwd
cwd=`pwd` && cd $2 && `cut -d" " -f3 md5sum.txt | xargs md5sum > md5sum.new` && mv md5sum.new md5sum.txt && cd $cwd

echo "chmod ..."
chmod 4755 $2/usr/bin/sudo
chmod 440 $2/etc/sudoers
chmod 440 $2/etc/sudoers.d/README
chmod 700 $2/root/.ssh

echo "create cramfs image ..."
mkcramfs-16k -n $CRAMFS_NAME $2 $tmp > /dev/null || quit

echo "sign image ..."
filesize=`printf %x \`stat -c %s $tmp\``
[ ${#filesize} -eq 7 ] && filesize="0$filesize"
sig=""
for i in 6 4 2 0 ; do
  sig="$sig\x${filesize:$i:2}"
done
sig="$sig\x00\x00\x00\x00"

echo -en "\xCE\xFA\xBE\xBA\x02\x00\x00\x00" >> $tmp
echo -en $sig | head -c 8 >> $tmp

md5sum $tmp | head -c 32 > $3
cat $tmp >> $3
echo "$3 created"

elif [ $1 == "unpack"  ]; then

#unpack

[ -e $2 ] && echo "firmware folder \"$2\" already exists" && quit
echo "stripping firmware ... "
dd if=$3 skip=2 count=`expr \`stat -c %s $3\` / 16 - 3` ibs=16 obs=4096 of=$tmp &> /dev/null
echo "unpack cramfs image ... "
cramfsck-16k -x $2 $tmp > /dev/null || quit
echo "unpacked to $2"

elif [ $1 == "pack_bi2"  ]; then

#pack_bi2

[ -e $3 ] && echo "firmware \"$3\" already exists" && quit
[ ! -d $2 ] && echo "firmware folder \"$3\" does not exist" && quit
echo "create squashfs image ..."
squashfs3.4/squashfs-tools/mksquashfs $2 $tmp -b 131072 -le -noappend > /dev/null || quit

echo "Adding header..."
headersize=16
filesize=$(stat -c%s $tmp)
filewithheadersize=$(($filesize + $headersize))
version=$SQUASHFS_VERSION
extraspaces=$(printf "%0.s " $(seq $headersize))
header=$(printf "%s %s%s" $filewithheadersize "$version" "$extraspaces" | head -c $headersize)
echo "Using header: $header"
echo "Checksuming..."
((echo "$header" | head -c $headersize) && cat $tmp) | md5sum | head -c 32 > $3
echo "$header" | head -c $headersize >> $3
cat $tmp >> $3
echo "$3 created"

elif [ $1 == "unpack_bi2"  ]; then

#unpack_bi2

[ -e $2 ] && echo "firmware folder \"$2\" already exists" && quit
echo "stripping firmware ... "
tail -c +49 $3 > $tmp
echo "unpack squashfs image ... "
squashfs3.4/squashfs-tools/unsquashfs -d $2 $tmp > /dev/null || quit
echo "unpacked to $2"
echo "Current header in $3:"
head -c 48 $3 | tail -c 16
echo ""

else

usage

fi

quit


I've successfully used the above script to repack a bi2 and flash my WDTV Live Plus.
TroNik
Developer
 
Posts: 73
Joined: Wed Dec 21, 2011 2:56 am

Re: unpack / repack bi2   

Postby mad_ady » Fri Jan 10, 2014 5:39 am

Thanks for digging up and consolidating this lost information :)
User avatar
mad_ady
Developer
 
Posts: 4552
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: unpack / repack bi2   

Postby zltnngy » Fri Jan 10, 2014 5:55 am

Nice guide.



A bit off topic and also not documented: WDTV Live (not plus) also have a partition (named sigmblocki) where "bi2" can be flashed. This partition is only used in the last official firmware to hold some large libs. In wdlxtv 0.5.1.1 it is totally free and you can create any filesystem on it. I currently have an ext3 on it. It is writable. Write speeds are abismal though (I don't think it is more than 20 kb/secs). But it is good for storing some config files which don't fit /config. If you want more space (sigmblocki is around 90 megs) you can store a compressed file system like squashfs on the ext3 partition and mount it. If you want to go really crazy you can then merge the folders on sigmblocki with the original folders on your r/o rootfs (I use aufs for this) to get a nice writable filesystem without using an usb drive.
zltnngy
WDTVer
 
Posts: 22
Joined: Fri Jul 13, 2012 3:34 am

Re: unpack / repack bi2   

Postby mad_ady » Fri Jan 10, 2014 11:51 am

The blocki partition is used by the wdtv to store online updates before flasing them. That's why it's the same size as the main partition. Apart from online flashing it's unused and can be used for nefarious purposes. However it may not be a good idea to use ext3 on it because it's a mtd device and you'll wear out the flash (afik ext3 won't move data around to do wear leveling and you'll burn your flash prematurely). You might try jffs2 if it's supported by the kernel...
User avatar
mad_ady
Developer
 
Posts: 4552
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: unpack / repack bi2   

Postby zltnngy » Sun Jan 12, 2014 1:52 pm

Thanks for the advice, I will try to minimize write operations from now on. I've been using ext3 for 3 months now, I hope i didn't do too much damage...
Jffs2 is in the kernel source, but I am short of time to compile a module.
zltnngy
WDTVer
 
Posts: 22
Joined: Fri Jul 13, 2012 3:34 am

Re: unpack / repack bi2   

Postby mad_ady » Sun Jan 12, 2014 11:15 pm

If you're going to use ext3, I suggest you try mounting it readonly when not writing to it (if you're writing via cron jobs for instance), and turning off journaling (reducing it to an ext2).
User avatar
mad_ady
Developer
 
Posts: 4552
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: unpack / repack bi2   

Postby TroNik » Sat Jan 18, 2014 3:26 am

I should had taken a look at the following script :)
Code: Select all
/usr/sbin/checkBlocki

Code: Select all
#!/bin/sh
### Copyright 2011 b-rad.cc
### http://wdlxtv.com & http://b-rad.cc & http://nextdimension.cc
### GPLv3 - full license located @ /usr/share/LICENSE
### Stipulations:
### - the full copyright notices in /osd/setup_about.xml must be left intact, as it is the publicly displayed copyright portion of this software as dictated by the GPLv3
### - this entire header must be left intact
##
##
## a WDLXTV compatible blocki is formatted as such:
##    [ 32 byte md5sum ][ 8 byte squashfs image size ][ 8 byte version number ][ squashfs filesystem image ]
##   the md5sum, image size, and version number are all ascii characters
##
## On WDLXTV_PLUS:
##   if version number does not equal the numeric version of the install WDLXTV
##   then a version matched image will be downloaded at first availability
##
## On all versions:
##   if you wish for your bi2 to be checked as clean you must match version number
##
## Optional argument:  checkBlocki <location>
##   if <location> is supplied it is checked instead of /dev/sigmblocki
##
## Return values:
##   0 : md5sum pass & version match
##   1 : md5sum failure
##   2 : md5sum pass & version mis-match
##
##

[ -z "$1" ] && blocki="/dev/sigmblocki" || blocki=$1
blockiMD5=`head -c 32 $blocki`
blockiSize=`head -c 40 $blocki | tail -c 8`
blockiVersion=`head -c 48 $blocki | tail -c 8 | tr -d ' '`
realVer=`cat /conf_src/version | tr -d [:cntrl:] | tail -c 7`
realMD5=`tail -c +33 $blocki | head -c $blockiSize | md5sum | head -c 32`
if [ "$realMD5" != "$blockiMD5" ] ; then
    logger -s -t checkBlocki "$blocki corrupt!"
    exit 1
elif [ "$realVer" != "$blockiVersion" ] ; then
    logger -s -t checkBlocki "$blocki version mismatch!"
    exit 2
fi
logger -s -t checkBlocki "$blocki OK"
exit 0
TroNik
Developer
 
Posts: 73
Joined: Wed Dec 21, 2011 2:56 am

Re: unpack / repack bi2   

Postby mad_ady » Tue Jan 21, 2014 12:14 am

Yeah, there's nothing like reinventing the wheel :P
User avatar
mad_ady
Developer
 
Posts: 4552
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: unpack / repack bi2   

Postby recliq » Tue Jan 21, 2014 8:47 am

Someone already invented the "wheel"? I was about to file a patent on that, calling it "the circular thing for easy movement"... :?
Here's my technical drawing attachment:
Code: Select all
(o)


...another dream burst like a soap bubble.
­WDLXTV Project Maintainer
-:] If you like my contributions feel free to donate for a beer or a new flash drive. ...and always remember: RTFM! (README, FAQ, WIKI) [:-
User avatar
recliq
WDLXTV Team
 
Posts: 5513
Joined: Thu Apr 15, 2010 8:09 am
Location: Kiel, Germany


Return to WDTV Live

Who is online

Users browsing this forum: No registered users and 2 guests

cron