Changeset 14181


Ignorieren:
Zeitstempel:
20.03.2017 21:59:49 (vor 3 Monaten)
Autor:
er13
Nachricht:

unpack-kernel script:

Datei:
1 bearbeitet

Legende:

Unverändert
Hinzugefügt
Entfernt
  • trunk/tools/unpack-kernel

    r14180 r14181  
    3838# $2 - magic sequence name (used in error messages) 
    3939# $3 - max expected number of matches (unlimited if omitted) 
     40# $4 - tight lower bound of the match, i.e. the returned offset 
     41#      must fulfill the condition "tight-lower-bound < offset" 
     42#      (no lower bound restriction if omitted) 
    4043# 
    4144getDecOffsetOf1stMagicSequenceMatch() { 
     45    local sequenceNamePrefix=${2}${2:+ } # sequence name with space added 
     46    local maxNumberOfMatches=$3          # unlimited if omitted 
     47    local lowerBound=${4:--1}            # -1        if omitted 
     48 
    4249    declare -a offsetCandidates=($(getDecOffsetsOfAllMatches "$inputFile" bin $(echo -n $1 | invertEndianness))) 
    43     if [ ${#offsetCandidates[@]} -eq 0 ]; then 
    44         echo >&2 "ERROR: ${2}${2:+ }magic sequence \"0x$1\" not found" 
    45         return 1 
    46     elif [ -n "$3" ] && [ ! ${#offsetCandidates[@]} -le "$3" ]; then 
    47         echo >&2 "ERROR: more ${2}${2:+ }magic sequence \"0x$1\" matches found (${#offsetCandidates[@]}) than expected ($3)" 
    48         return 2 
    49     fi 
    50     echo -n ${offsetCandidates[0]} 
     50 
     51    for ((i=0; i<${#offsetCandidates[@]}; i++)); do 
     52        if [ $((lowerBound)) -lt ${offsetCandidates[i]} ]; then 
     53            local numberOfMatchesFound=$((${#offsetCandidates[@]} - i)) 
     54            if [ -n "$maxNumberOfMatches" ] && [ ! $numberOfMatchesFound -le $maxNumberOfMatches ]; then 
     55                echo >&2 "ERROR: number of ${sequenceNamePrefix}magic sequence (0x$1) matches ($numberOfMatchesFound) exceeds the expected limit ($maxNumberOfMatches)" 
     56                return 1 
     57            fi 
     58 
     59            # match fulfilling all restrictions found 
     60            echo -n ${offsetCandidates[i]} 
     61            return 0 
     62        fi 
     63    done 
     64 
     65    local fullfiling=${4:+(fulfilling the restriction $(printf '0x%08X' $lowerBound) < offset) } 
     66    echo >&2 "ERROR: no ${sequenceNamePrefix}magic sequence (0x$1) ${fullfiling}found" 
     67    return 1 
    5168} 
    5269 
    53 { feed1281_Offset=$(getDecOffsetOf1stMagicSequenceMatch FEED1281 "TI-AR7 kernel" 1); } || exit 1 
     70# 
     71# $1 - LZMA-record offset 
     72# $2 - name of the file decompressed stream to be written to 
     73# 
     74# returns the error code of unlzma 
     75# 
     76decompressLzmaStream() { 
     77    local lzmaRecordOffset=$1 
     78    local outputFile="$2" 
    5479 
    55 if [ $feed1281_Offset -ne 0 ]; then 
    56     { feed9112_Offset=$(getDecOffsetOf1stMagicSequenceMatch FEED9112 "GRX5 kernel" 1); } || exit 1 
    57     if [ $feed9112_Offset -ne 0 ]; then 
    58         echo >&2 "ERROR: GRX5 kernel magic sequence \"0xFEED9112\" found at unexpected offset"; exit 1 
    59     fi 
    60 fi 
     80    # unused 
     81    local lzmaCompressedLen=$(getLEu32AtOffset      "$inputFile" $((lzmaRecordOffset +  4))) 
     82    local lzmaUncompressedLen=$(getLEu32AtOffset    "$inputFile" $((lzmaRecordOffset +  8))) 
     83    local lzmaCompressedChecksum=$(getLEu32AtOffset "$inputFile" $((lzmaRecordOffset + 12))) 
    6184 
    62 { lzmaRecordOffset=$(getDecOffsetOf1stMagicSequenceMatch 075A0201 "EVA-LZMA-record"); } || exit 1 
    63 lzmaCompressedLenOffset=$((lzmaRecordOffset + 4)) 
    64 lzmaCompressedLen=$(getLEu32AtOffset "$inputFile" $lzmaCompressedLenOffset) 
    65 lzmaUncompressedLenOffset=$((lzmaRecordOffset + 8)) 
    66 lzmaUncompressedLen=$(getLEu32AtOffset "$inputFile" $lzmaUncompressedLenOffset) 
    67 lzmaCompressedChecksumOffset=$((lzmaRecordOffset + 12)) 
    68 lzmaCompressedChecksum=$(getLEu32AtOffset "$inputFile" $lzmaCompressedChecksumOffset) 
    69 lzmaStreamOffset=$((lzmaRecordOffset + 16)) 
    70  
    71 ( 
    72     # LZMA properties (1 byte) + LZMA dictionary size (4 bytes) 
    73     dd if="$inputFile" bs=1 skip=$lzmaStreamOffset          count=5 2>/dev/null 
    74     # uncompressed length 
    75     dd if="$inputFile" bs=1 skip=$lzmaUncompressedLenOffset count=4 2>/dev/null 
    76     # padding 
    77     dd if=/dev/zero    bs=1                                 count=4 2>/dev/null 
    78     # compressed data 
    79     #   +1 = tail expects number of bytes, whereas lzmaStreamOffset is zero-based 
    80     #   +8 = skip LZMA properties (1 byte) + LZMA dictionary size (4 bytes) + padding (3 bytes) 
    81     tail -c "+$(((lzmaStreamOffset + 1) + 8))" "$inputFile" 2>/dev/null | head -c $lzmaCompressedLen 2>/dev/null 
    82 ) | "$UNLZMA" > "$2" 2>/dev/null 
    83  
    84 [ $? -ne 0 ] && { echo >&2 "ERROR: failed to unpack kernel image"; exit 1; } 
     85    local lzmaUncompressedLenOffset=$((lzmaRecordOffset + 8)) 
     86    local lzmaStreamOffset=$((lzmaRecordOffset + 16)) 
     87    ( 
     88        # LZMA properties (1 byte) + LZMA dictionary size (4 bytes) 
     89        dd if="$inputFile" bs=1 skip=$lzmaStreamOffset          count=5 2>/dev/null 
     90        # uncompressed length 
     91        dd if="$inputFile" bs=1 skip=$lzmaUncompressedLenOffset count=4 2>/dev/null 
     92        # padding 
     93        dd if=/dev/zero    bs=1                                 count=4 2>/dev/null 
     94        # compressed data 
     95        #   +1 = tail expects number of bytes, whereas lzmaStreamOffset is zero-based 
     96        #   +8 = skip LZMA properties (1 byte) + LZMA dictionary size (4 bytes) + padding (3 bytes) 
     97        tail -c "+$(((lzmaStreamOffset + 1) + 8))" "$inputFile" 2>/dev/null | head -c $lzmaCompressedLen 2>/dev/null 
     98    ) | "$UNLZMA" > "$outputFile" 2>/dev/null 
     99} 
    85100 
    86101getLoadAddr() { 
     
    95110} 
    96111 
     112TI_AR7_MAGIC=FEED1281 
     113DUAL_KERNEL_MAGIC=FEED9112 
     114SECOND_KERNEL_MAGIC=FEEDB007 
     115EVA_LZMA_RECORD_MAGIC=075A0201 
     116 
     117# 1st kernel (all boxes) 
     118{ feed1281_Offset=$(getDecOffsetOf1stMagicSequenceMatch $TI_AR7_MAGIC "TI-AR7" 1); } || exit 1 
     119{ feed1281_lzmaRecordOffset=$(getDecOffsetOf1stMagicSequenceMatch $EVA_LZMA_RECORD_MAGIC "TI-AR7-LZMA-record" "" $feed1281_Offset); } || exit 1 
     120[ $((feed1281_lzmaRecordOffset - feed1281_Offset)) -eq 12 ] || { echo >&2 "ERROR: TI-AR7-LZMA-record is expected to be located exactly 12 bytes after TI-AR7 magic"; exit 1; }; 
     121decompressLzmaStream $feed1281_lzmaRecordOffset "$outputFile" 
     122[ $? -ne 0 ] && { echo >&2 "ERROR: failed to unpack kernel"; exit 1; } 
     123 
     124# 2nd kernel (GRX5 boxes only) 
     125if [ $feed1281_Offset -ne 0 ]; then 
     126    { feed9112_Offset=$(getDecOffsetOf1stMagicSequenceMatch $DUAL_KERNEL_MAGIC "DUAL-kernel" 1); } || exit 1 
     127    if [ $feed9112_Offset -ne 0 ]; then 
     128        echo >&2 "ERROR: DUAL-kernel magic sequence (0x$DUAL_KERNEL_MAGIC) found at unexpected offset ($(printf '0x%08X' $feed9112_Offset))"; exit 1 
     129    fi 
     130 
     131    # Note: feedb007_Offset could probably also be located as (getEntryAddrOffset(feed1281_Offset) + 4) padded to a 4-byte boundary (TODO: 4-byte to be verified) 
     132    { feedb007_Offset=$(getDecOffsetOf1stMagicSequenceMatch $SECOND_KERNEL_MAGIC "2nd kernel" 1); } || exit 1 
     133    { feedb007_lzmaRecordOffset=$(getDecOffsetOf1stMagicSequenceMatch $EVA_LZMA_RECORD_MAGIC "2nd-kernel-LZMA-record" "" $feedb007_Offset); } || exit 1 
     134    [ $((feedb007_lzmaRecordOffset - feedb007_Offset)) -eq 12 ] || { echo >&2 "ERROR: 2nd-kernel-LZMA-record is expected to be located exactly 12 bytes after 2nd-kernel magic"; exit 1; }; 
     135    decompressLzmaStream $feedb007_lzmaRecordOffset "${outputFile}.FEEDB007" 
     136    [ $? -ne 0 ] && { echo >&2 "ERROR: failed to unpack 2nd-kernel"; exit 1; } 
     137fi 
     138 
     139### 
     140 
    97141feed1281_LoadAddr=$(getLoadAddr $feed1281_Offset) 
    98142feed1281_EntryAddr=$(getEntryAddr $feed1281_Offset) 
     
    103147 
    104148    if [ "$feed1281_LoadAddr" != "$feed9112_LoadAddr" ]; then 
    105         echo >&2 "WARNING: load addresses stored in TI-AR7 record ($feed1281_LoadAddr) and GRX5 record ($feed9112_LoadAddr) do not match" 
     149        echo >&2 "WARNING: load addresses stored in TI-AR7 record ($feed1281_LoadAddr) and DUAL-kernel record ($feed9112_LoadAddr) do not match" 
    106150    fi 
    107151 
    108152    if [ "$feed1281_EntryAddr" != "$feed9112_EntryAddr" ]; then 
    109         echo >&2 "WARNING: entry addresses stored in TI-AR7 record ($feed1281_EntryAddr) and GRX5 record ($feed9112_EntryAddr) do not match" 
     153        echo >&2 "WARNING: entry addresses stored in TI-AR7 record ($feed1281_EntryAddr) and DUAL-kernel record ($feed9112_EntryAddr) do not match" 
    110154    fi 
    111155fi 
    112156 
    113157echo "load=$feed1281_LoadAddr entry=$feed1281_EntryAddr" 
     158 
     159if [ -n "$feedb007_Offset" ]; then 
     160    feedb007_LoadAddr=$(getLoadAddr $feedb007_Offset) 
     161    feedb007_EntryAddr=$(getEntryAddr $feedb007_Offset) 
     162    echo "load2ndKernel=$feedb007_LoadAddr entry2ndKernel=$feedb007_EntryAddr" 
     163fi 
Hinweis: Hilfe zur Verwendung der Changeset-Ansicht finden Sie unter TracChangeset.