Advertisement
Guest User

Untitled

a guest
Nov 11th, 2011
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.55 KB | None | 0 0
  1. #!/bin/bash
  2. # by neXus April 2011
  3. # v1.1
  4. # Changelog
  5. # 26/04/2011 - Fixe size issue for webui, escape var to allow white space in path
  6.  
  7. function Get_Bytes
  8. {
  9. xxd -ps -s $2 -l 4 -g 16 -c 4 "$1"
  10. }
  11.  
  12. function Get_LowHigh
  13. {
  14.  echo $1 | sed -r 's/([0-9a-f][0-9a-f])+([0-9a-f][0-9a-f])+([0-9a-f][0-9a-f])+([0-9a-f][0-9a-f])/\4\3\2\1/'
  15. }
  16.  
  17.  
  18. function Get_Version
  19. {
  20.     Get_Bytes "$1" 12 | awk '{printf("%s.%s.%s.%s",strtonum("0x"substr($1,1,2)),strtonum("0x"substr($1,3,2)),strtonum("0x"substr($1,5,2)),strtonum("0x"substr($1,7,2)))}'
  21. }
  22.  
  23.  
  24. function Get_Hash
  25. {
  26.     xxd -ps -s $2 -c 1 "$1" | awk '{sum+=strtonum("0x"$1)} END{printf "%08x",sum}'
  27. }
  28.  
  29.  
  30. function WebUI_Unpack
  31. {
  32.     SEEK=16
  33.     END=`stat --printf %s "$1"`
  34.     BASE=$1_extracted  
  35.     echo "Extracting to $BASE"
  36.     while [ $SEEK -ne $END ]; do
  37.    
  38.         #Get size of filename
  39.         N_SIZE=`echo $(Get_LowHigh $(Get_Bytes "$1" $SEEK)) | awk '{print strtonum("0x"$1)}'`
  40.         let SEEK+=4
  41.        
  42.         #Get file name
  43.         NAME=`xxd -s $SEEK -l $N_SIZE "$1" |xxd -r`
  44.         let SEEK+=$N_SIZE
  45.        
  46.         # Test for bit. if 01 its a file else folder
  47.         STR_BIT=`xxd -ps -s $SEEK -l 1 "$1"`
  48.         let SEEK+=1
  49.         if [ $STR_BIT = "00" ]; then
  50.             echo ">Folder $NAME"
  51.             mkdir -p $BASE$NAME
  52.  
  53.         else
  54.  
  55.             #Get file size
  56.             F_SIZE=`echo $(Get_LowHigh $(Get_Bytes "$1" $SEEK)) | awk '{print strtonum("0x"$1)}'`
  57.             let SEEK+=4
  58.             echo ">File $NAME ($F_SIZE)"
  59.        
  60.             # Check file path
  61.             [  ! -d $BASE`dirname $NAME` ] && mkdir -p $BASE`dirname $NAME`
  62.             # Dump the file
  63.             dd if="$1" of=$BASE$NAME bs=1 skip=$SEEK count=$F_SIZE > /dev/null 2>&1
  64.             let SEEK+=$F_SIZE
  65.         fi
  66.    
  67.     done
  68.  
  69.  
  70. }
  71.  
  72. function WebUI_Pack
  73. {
  74.    
  75.     # create binary package
  76.     read -p "Type the name of bin output: " BIN_FILE
  77.     read -p "Type the version to set : (xx.xx.xx.xx) " VERS
  78.     [ -z "$BIN_FILE" ] && echo "Please set a name" && exit 1
  79.     OUTPUT="`pwd`/$BIN_FILE"   
  80.     BASE="$1"
  81.     [ -f "$OUTPUT" ] && rm "$OUTPUT"
  82.     DATA="$OUTPUT.data"
  83.     HEADER="$OUTPUT.header"
  84.     # find files and folder
  85.     cd "$1"
  86.     echo "Remove existing headers"
  87.     find -iname "*.header" -delete
  88.     echo ">Adding version to output"
  89.         echo "00:"`echo $VERS |awk -F. '{printf("%02x%02x%02x%02x",$1,$2,$3,$4)}'` |xxd -r >"$DATA"
  90.    
  91.     for item in `find | sed '/^.$/d' `; do
  92.     # debug for item in `cat files.list | sed '/^$/d' |sed 's/\//\.\//'`;do
  93.     echo "Packing -> $item"
  94.  
  95.         NAME=`echo $item |sed 's/^\.//g'`
  96.         N_SIZE=$(Get_LowHigh `echo -n $NAME | wc -m | awk '{printf("%08x",$1)}'`)
  97.  
  98.     if [ -f $item ]; then
  99.    
  100.         F_SIZE=$(Get_LowHigh `stat --printf %s $item | awk '{printf("%08x",$1)}'`)
  101.  
  102.         # create the header file we need to process by step
  103.         RAW_HEX=$N_SIZE`echo -n $NAME | xxd -ps`"01"$F_SIZE
  104.  
  105.     else
  106.  
  107.         # create the header file for folder
  108.         RAW_HEX=$N_SIZE`echo -n $NAME | xxd -ps`"00"
  109.     fi
  110.  
  111.         CAR=`echo $RAW_HEX -n |wc -m`
  112.         POS=0
  113.         count=0
  114.         while [ $POS -lt $CAR  ]; do
  115.                 PART=`echo ${RAW_HEX:$POS:16}`
  116.                 let POS+=16
  117.                 echo "$count: $PART" | xxd -r >> "$item.header"
  118.             let count++
  119.         done
  120.         # create the output file with header and file or header only if folder
  121.         cat "$item.header" >> "$DATA"
  122.         [ -f "$item" ] && dd if="$item" of="$DATA" conv=notrunc oflag=append > /dev/null 2>&1
  123.    
  124.     done   
  125.     # create header with size / crc / version
  126.     echo ">Calculating checksum"
  127.     CHKS=$(Get_LowHigh $(Get_Hash "$DATA" 0))
  128.     echo ">Get Size"
  129.     SIZE=$(Get_LowHigh `stat --printf %s "$DATA" | awk '{printf("%08x",$1+12)}'`)
  130.     echo ">Write Header"
  131.     echo -n "0: BD9A0C44 $CHKS $SIZE"  | xxd -r > "$HEADER"
  132.     cat "$HEADER" "$DATA" > "$OUTPUT"
  133.     echo ">Check Integrity :"
  134.     echo -e "\t->Version : $(Get_Version "$OUTPUT")"
  135.     sum32=$(Get_LowHigh $(Get_Bytes "$OUTPUT" 4))
  136.         calc_sum32=$(Get_Hash "$OUTPUT" 12)
  137.     [ ! "$sum32" = "$calc_sum32" ] && echo " !!!! checksum mismatch ! File Corrupted" && exit 1
  138.     echo -e "\t->Checksum verified ($sum32)"
  139.     echo
  140.     echo "Firmware successfully packed to $OUTPUT"
  141.     find -iname "*.header" -delete
  142. }
  143.  
  144. # main actions
  145.  
  146. function webui
  147. {
  148.  
  149. if [ -f "$1" ]; then
  150.  
  151.     echo "Checking for file : $1"
  152.     echo ">Version  : " $(Get_Version "$1")
  153.     sum32=$(Get_LowHigh $(Get_Bytes "$1" 4))
  154.     calc_sum32=$(Get_Hash "$1" 12)
  155.     [ ! "$sum32" = "$calc_sum32" ] && echo "checksum mismatch ! File Corrupted" && exit 1
  156.     echo ">Checksum : $sum32"
  157.     echo
  158.     read -p "> Unpack ?(y/n)" ANS
  159.     [ ! "$ANS" = "y" ] && exit 0
  160.     echo "Unpacking Files..."
  161.     WebUI_Unpack $1
  162.  
  163.  
  164. else
  165.     echo "Packing a webui"
  166.     WebUI_Pack "$1"
  167.  
  168. fi
  169.  
  170. }
  171.  
  172. function firmware
  173. {
  174. if [ -f "$1" ]; then
  175.     echo "Unpacking firmware $1"
  176.     LINUX_SIZE=`echo $(Get_LowHigh $(Get_Bytes "$1" 12)) | awk '{print strtonum("0x"$1)}'`
  177.     ROOTFS_SIZE=`echo $(Get_LowHigh $(Get_Bytes "$1" 16)) | awk '{print strtonum("0x"$1)}'`
  178.     mkdir -p "$1"_extracted
  179.     echo ">Extracting linux.bin ($LINUX_SIZE)"
  180.     dd if="$1" of="$1_extracted"/linux.bin bs=1 skip=20 count=$LINUX_SIZE > /dev/null 2>&1
  181.     echo ">Extracting rootfs.img ($ROOTFS_SIZE)"
  182.     dd if="$1" of="$1_extracted"/rootfs.img bs=1 skip=$((20+$LINUX_SIZE)) count=$ROOTFS_SIZE > /dev/null 2>&1
  183.     echo "Done extracting firmware"
  184.    
  185. else
  186.     echo "Packing firmware"
  187.     echo ">Looking for file to pack in $1"
  188.     [ ! -f "$1/linux.bin" -a ! -f "$1/rootfs.img" ] && echo "Missing files !!" && exit 1
  189.     LINUX_SIZE=`stat --printf %s $1/linux.bin`
  190.     echo -e "\t linux.bin ($LINUX_SIZE)..OK"
  191.     ROOTFS_SIZE=`stat --printf %s $1/rootfs.img`
  192.     echo -e "\t rom1fs ($ROOTFS_SIZE)..OK"
  193.     echo
  194.     read -p ">Give a name to your firmware : " NAME
  195.     LINUX_SIZE=$(Get_LowHigh `echo -n $LINUX_SIZE | awk '{printf("%08x",$1)}'`)
  196.     ROOTFS_SIZE=$(Get_LowHigh `echo -n $ROOTFS_SIZE | awk '{printf("%08x",$1)}'`)
  197.     [ -f "$NAME" ] && rm "$NAME"
  198.     echo ">Generate header"
  199.     HEADER1="0000000: 424E4547 01000000 01000000 $LINUX_SIZE"
  200.     HEADER2="0000010: $ROOTFS_SIZE"
  201.     echo $HEADER1 |xxd -r > "$NAME.header"
  202.     echo $HEADER2 |xxd -r >>"$NAME.header"
  203.     echo ">Generate firmware"
  204.     cat "$NAME.header" "$1/linux.bin" "$1/rootfs.img" > "$NAME"
  205.     rm "$NAME.header"
  206.     echo "Done genrating $NAME"
  207. fi
  208. }
  209.  
  210.  
  211. function show_usage
  212. {
  213. cat <<EOT
  214.     .:: Foscam Resource ToolKit ::.
  215.  
  216.     $0 --webui|--firm file|folder
  217.  
  218.     You can use this piece of script to :
  219.         - Pack and unpack webui resource
  220.         - Pack and unpack firmware resource
  221.  
  222.     Usage :
  223.    
  224.     Unpack webui :      $0 --webui file.bin
  225.  
  226.     Pack webui :        $0 --webui webui_folder
  227.  
  228.     Unpack firmware :   $0 --firm file.bin
  229.  
  230.     Pack firmware :     $0 --firm firmware_folder
  231.  
  232.     (note the firmware folder must contain rootfs.img and linux.bin files)
  233.  
  234. EOT
  235.  
  236. }
  237.  
  238. # main programm
  239.  
  240. [ $# -ne 2 ] && show_usage && exit 0
  241.  
  242. if [ $1 = "--webui" ]; then
  243.     webui "$2"
  244. elif [ $1 = "--firm" ]; then
  245.     firmware "$2"
  246. else
  247.     show_usage
  248. fi
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement