Changeset 14177


Ignorieren:
Zeitstempel:
20.03.2017 07:56:45 (vor 6 Wochen)
Autor:
er13
Nachricht:

sfk related code:

  • eliminate code clones by introducing freetz_bin_functions-library
Ort:
trunk/tools
Dateien:
1 hinzugefügt
2 bearbeitet

Legende:

Unverändert
Hinzugefügt
Entfernt
  • trunk/tools/extract-images

    r14046 r14177  
    1919fi 
    2020 
    21 # 
    22 # $1 - input file 
    23 # $2 - binary pattern 
    24 # 
    25 # returns: 
    26 #   offsets of all pattern matches 
    27 getHexOffsetsOfAllMatches() { 
    28     "$mod_tools/sfk" hexfind "$1" -bin "/$2/" -quiet | sed -nr -e 's|.*hit at offset (0x[0-9A-F]+)|\1|p' 
    29 } 
    30 getDecOffsetsOfAllMatches() { 
    31     local matches=$(getHexOffsetsOfAllMatches "$@") 
    32     [ -z "$matches" ] && return 
    33     echo "$matches" | xargs printf "%d\n" 
    34 } 
    35  
    36 # 
    37 # $1 - input file 
    38 # $2 - offset 
    39 # $3 - length 
    40 # 
    41 getHexContentAtOffset() { 
    42     "$mod_tools/sfk" hexdump -offlen "$2" "$3" -nofile -pure "$1" | tr -d $'\n' 
    43 } 
    44  
    45 # 
    46 # inverts endianness of each line of stdin 
    47 # each line is assumed to be a hex-string representation of the data and is processed separately 
    48 # if hex-string length is not muptiple of 8 it is zero padded 
    49 # 
    50 invertEndianness() { 
    51     sed -r -e ':zero_pad;s/^(.{1,7})((.{8})*)$/0\1\2/;tzero_pad' | sed -r -e 's/(..)(..)(..)(..)/\4\3\2\1/g' 
    52 } 
    53  
    5421# Parameterized helper function for 'extract_bootloader' doing the actual work 
    5522do_extract_bl() 
     
    5825 
    5926    # Find bootloader candidates 
    60     declare -a bootloader_offs=($(getDecOffsetsOfAllMatches "$input_file" "${BOOTLOADER_MAGIC[${ENDIAN_TYPE}]}")) 
     27    declare -a bootloader_offs=($(getDecOffsetsOfAllMatches "$input_file" bin "${BOOTLOADER_MAGIC[${ENDIAN_TYPE}]}")) 
    6128 
    6229    # Find bootloader candidates and isolate their offsets 
     
    13198    squashfs_length=$((0x${squashfs_length})) 
    13299 
    133     declare -a nmi_offsets=($(getDecOffsetsOfAllMatches "$input_file" "$NMI_BOOT_PATTERN")) 
     100    declare -a nmi_offsets=($(getDecOffsetsOfAllMatches "$input_file" bin "$NMI_BOOT_PATTERN")) 
    134101    for (( i=0; i<${#nmi_offsets[@]}; i++ )); do 
    135102        nmi_offset="${nmi_offsets[i]}" 
     
    162129    ) 
    163130    for rootfs_type in ext2fs squashfs; do 
    164         declare -a rootfs_offs=($(getDecOffsetsOfAllMatches "$input_file" "${rootfs_magic[${rootfs_type}]}")) 
     131        declare -a rootfs_offs=($(getDecOffsetsOfAllMatches "$input_file" bin "${rootfs_magic[${rootfs_type}]}")) 
    165132        if [ ${#rootfs_offs[@]} -gt 0 ]; then 
    166133            break; 
     
    179146 
    180147    # Find kernel candidates 
    181     declare -a kernel_offs=($(getDecOffsetsOfAllMatches "$input_file" "$KERNEL_MAGIC")) 
     148    declare -a kernel_offs=($(getDecOffsetsOfAllMatches "$input_file" bin "$KERNEL_MAGIC")) 
    182149 
    183150    for (( i=0; i<${#kernel_offs[@]}; i++ )); do 
     
    297264        ;; 
    298265esac 
     266 
     267. "$mod_tools/freetz_bin_functions" 
    299268 
    300269# Magic byte sequences (hex) to identify kernel, SquashFS and bootloader 
  • trunk/tools/remove-nmi-vector

    r14095 r14177  
    3232# 
    3333 
    34  
    35 SFK_TOOL="$(dirname "$0")/sfk" 
     34. "$(dirname $(readlink -f ${0}))/freetz_bin_functions" 
    3635 
    3736INPUT_FILE="$1" 
     
    5554) 
    5655 
    57 # 
    58 # $1 - pattern type (bin, pat/text, spat/spats - see "sfk help pat" for details) 
    59 # $2 - pattern 
    60 # 
    61 # returns: 
    62 #   offsets of all pattern matches 
    63 getHexOffsetsOfAllMatches() { 
    64     "$SFK_TOOL" hexfind "$INPUT_FILE" -$1 "/$2/" -quiet | sed -nr -e 's|.*hit at offset (0x[0-9A-F]+)|\1|p' 
    65 } 
    66  
    67 # 
    68 # $1 - offset 
    69 # $2 - length 
    70 # 
    71 getHexContentAtOffset() { 
    72     "$SFK_TOOL" hexdump -offlen "$1" "$2" -nofile -pure "$INPUT_FILE" | tr -d $'\n' 
    73 } 
    74  
    7556getInputFileSize() { 
    7657    stat -L -c "%s" "$INPUT_FILE" 
     
    7859 
    7960 
    80 declare -a nmiBootOffsets=($(getHexOffsetsOfAllMatches bin "${NMI_BOOT_PATTERN}")) 
     61declare -a nmiBootOffsets=($(getHexOffsetsOfAllMatches "$INPUT_FILE" bin "${NMI_BOOT_PATTERN}")) 
    8162 
    8263if [ ${#nmiBootOffsets[@]} -eq 0 ]; then 
     
    9778 
    9879# NMI vector length is encoded within the NMI block 4 bytes before 'NMI Boot' 
    99 nmiVectorLength="0x$(getHexContentAtOffset $((${nmiBootOffset} - 4)) 4)" 
     80nmiVectorLength="0x$(getHexContentAtOffset "$INPUT_FILE" $((${nmiBootOffset} - 4)) 4)" 
    10081if [ $((${nmiVectorLength})) -ne 256 -a $((${nmiVectorLength})) -ne 4096 ]; then 
    10182    echo "[Error] Unexpected/unsupported NMI vector length: $((${nmiVectorLength}))" >&2 
     
    10586# check if the image contains the TI-checksum at the end 
    10687TI_CHECKSUM_MAGIC_BE="23DE53C4" 
    107 if [ "$(getHexContentAtOffset $(($(getInputFileSize) - 8)) 4)" == "${TI_CHECKSUM_MAGIC_BE}" ]; then 
     88if [ "$(getHexContentAtOffset "$INPUT_FILE" $(($(getInputFileSize) - 8)) 4)" == "${TI_CHECKSUM_MAGIC_BE}" ]; then 
    10889    # TI checksum found 
    10990    imageChecksumSize=8 
     
    11495 
    11596# real image size is encoded within the NMI block 8 bytes before 'NMI Boot' 
    116 imageSizeWithinNMI="0x$(getHexContentAtOffset $((${nmiBootOffset} - 8)) 4)" 
     97imageSizeWithinNMI="0x$(getHexContentAtOffset "$INPUT_FILE" $((${nmiBootOffset} - 8)) 4)" 
    11798imageSizeWithinNMIPadded256=$(( ${imageSizeWithinNMI} + (256 - ${imageSizeWithinNMI} % 256) % 256 )) 
    11899imageSizeExpected=$(printf "0x%08X" $(( $(getInputFileSize) - ${nmiVectorLength} - ${imageChecksumSize} ))) 
     
    123104 
    124105# get first 32 bytes of the NMI vector 
    125 nmiVectorStartSequence=$(getHexContentAtOffset ${nmiVectorOffset} 32) 
     106nmiVectorStartSequence=$(getHexContentAtOffset "$INPUT_FILE" ${nmiVectorOffset} 32) 
    126107# and check if they contain one of the known sequences 
    127108for v in v1 v2 v3; do 
Hinweis: Hilfe zur Verwendung der Changeset-Ansicht finden Sie unter TracChangeset.