summaryrefslogtreecommitdiff
path: root/utils/zenutils/source/shared/updater.cpp
blob: d6106c250deaabfe842b6323852009833cc722a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* zenutils - Utilities for working with creative firmwares.
 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "updater.h"
#include <file.h>
#include <pe.h>
#include <utils.h>


const char* zen::find_firmware_key(const byte* buffer, size_t len)
{
    char szkey1[] = "34d1";
    size_t cchkey1 = strlen(szkey1);
    char szkey2[] = "TbnCboEbn";
    size_t cchkey2 = strlen(szkey2);
    for (int i = 0; i < static_cast<int>(len); i++)
    {
        if (len >= cchkey1)
        {
            if (!strncmp((char*)&buffer[i], szkey1, cchkey1))
            {
                return (const char*)&buffer[i];
            }
        }
        if (len >= cchkey2)
        {
            if (!strncmp((char*)&buffer[i], szkey2, cchkey2))
            {
                return (const char*)&buffer[i];
            }
        }
    }
    return "";
}

dword zen::find_firmware_offset(byte* buffer, size_t len)
{
    for (dword i = 0; i < static_cast<dword>(len); i += 4)
    {
        dword size = *(dword*)&buffer[i];
        if (size != 0
            && buffer[i + 4]     != 0
            && buffer[i + 4 + 1] != 0
            && buffer[i + 4 + 2] != 0
            && buffer[i + 4 + 3] != 0)
        {
            return i;
        }
        if(i > 0xFF) /* Arbitrary guess */
            return 0;
    }
    return 0;
}

bool zen::find_firmware_archive(const std::string& filename, dword& va, dword& pa)
{
    shared::pe_file pef;
    if (!pef.read(filename))
    {
        return false;
    }
    shared::section_info data_section;
    if (!pef.find_section(".data", data_section))
    {
        return false;
    }
    shared::bytes buffer;
    if (!shared::read_file(filename, buffer, data_section.raw_address,
                           data_section.raw_size))
    {
        return false;
    }
    dword offset = find_firmware_offset(&buffer[0], buffer.size());
    if (!offset)
    {
        return false;
    }
    va = data_section.virtual_address + offset;
    pa = data_section.raw_address + offset;

    return true;
}


bool zen::crypt_firmware(const char* key, byte* buffer, size_t len)
{
#if 1
    char key_cpy[255];
    unsigned int i;
    unsigned int tmp = 0;
    int key_length = strlen(key);
    
    for(i=0; i < strlen(key); i++)
        key_cpy[i] = key[i] - 1;
    
    for(i=0; i < len; i++)
    {
        buffer[i] ^= key_cpy[tmp] | 0x80;
        tmp = (tmp + 1) % key_length;
    }
    
    return true;
#else
    /* Determine if the key length is dword aligned. */
    int keylen = strlen(key);
    int keylen_rem = keylen % sizeof(dword);

    /* Determine how many times the key must be repeated to be dword aligned. */
    int keycycle = keylen_rem ? (sizeof(dword) / keylen_rem) : 1;
    int keyscount = (keylen * keycycle) / sizeof(dword);

    /* Allocate a buffer to hold the key as an array of dwords. */
    dword* keys = new dword[keyscount];

    /* Copy the key into the key array, whilst mutating it. */
    for (int i = 0; i < keyscount; i++)
    {
        dword val;
        int keyoffset = (i * sizeof(dword)) % keylen;
        if ((keyoffset+sizeof(dword)) < keylen)
        {
            val = *(dword*)&key[keyoffset];
        }
        else
        {
            val = key[keyoffset]
                | (key[(keyoffset + 1) % keylen] << 8)
                | (key[(keyoffset + 2) % keylen] << 16)
                | (key[(keyoffset + 3) % keylen] << 24);
        }
        keys[i] = (val - 0x01010101) | 0x80808080;
    }

    /* Determine the number of dwords in the buffer. */
    int len_div = len / sizeof(dword);

    /* Decrypt all dwords of the buffer. */
    for (int i = 0; i < len_div; i++)
    {
        ((dword*)buffer)[i] ^= keys[i % keyscount];
    }

    /* Determine the remaining number of bytes in the buffer. */
    int len_rem = len % sizeof(dword);

    /* Decrypt the remaining number of bytes in the buffer. */
    for (int i = len_div * sizeof(dword); i < len; i++)
    {
        buffer[i] ^= ((key[i % keylen] - 0x01) | 0x80);
    }

    return true;
#endif
}
n>r0 bt 1f and #(~LCD_SD),r0 1: mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 shll r1 neg r2,r0 bt 1f and #(~LCD_SD),r0 1: shll r1 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 neg r2,r0 bt 1f and #(~LCD_SD),r0 1: mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 shll r1 neg r2,r0 bt 1f and #(~LCD_SD),r0 1: shll r1 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 neg r2,r0 bt 1f and #(~LCD_SD),r0 1: mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 #else /* HAVE_LCD_CHARCELLS */ /* further optimized version, exploits that SD is on bit 0 for recorders */ .align 2 .multi_transfer: mov.b @r4+,r1 /* load data byte from memory */ nop .single_transfer: shll16 r1 /* shift data to most significant byte */ shll8 r1 not r1,r1 /* and invert for use with negc */ shll r1 /* shift the MSB into carry */ negc r2,r0 /* carry to SD, SC low */ shll r1 /* next shift here for alignment */ mov.b r0,@r3 /* set data to port */ or #(LCD_SC),r0 /* rise SC (independent of SD level) */ mov.b r0,@r3 /* set to port */ negc r2,r0 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 shll r1 negc r2,r0 shll r1 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 negc r2,r0 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 shll r1 negc r2,r0 shll r1 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 negc r2,r0 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 shll r1 negc r2,r0 shll r1 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 negc r2,r0 mov.b r0,@r3 or #(LCD_SC),r0 mov.b r0,@r3 #endif /* HAVE_LCD_CHARCELLS */ add #-1,r5 /* decrease byte count */ tst r5,r5 /* r5 == 0 ? */ bf .multi_transfer /* no: next iteration */ or #(LCD_CS|LCD_DS|LCD_SD|LCD_SC),r0 /* restore port */ rts mov.b r0,@r3 /* This is the place to reenable the interrupts, if we have disabled * them. See above. */ .align 2 .lcdr: .long LCDR .end: .size _lcd_write_command,.end-_lcd_write_command #elif CONFIG_CPU == MCF5249 .section .icode,"ax",@progbits .align 2 .global lcd_write_command .type lcd_write_command,@function lcd_write_command: move.l (4,%sp),%d0 lea MBAR2,%a1 move.l #~8,%d1 and.l %d1,(0xb4,%a1) move.w %d0,0xf0000000 rts .align 2 .global lcd_write_command_ex .type lcd_write_command_ex,@function lcd_write_command_ex: lea MBAR2,%a1 move.l (4,%sp),%d0 /* Command */ move.l #~8,%d1 /* Set A0 = 0 */ and.l %d1,(0xb4,%a1) move.w %d0,0xf0000000 /* Write to LCD */ move.l (8,%sp),%d0 /* Data */ not.l %d1 /* Set A0 = 1 */ or.l %d1,(0xb4,%a1) move.w %d0,0xf0000000 /* Write to LCD */ rts .align 2 .global lcd_write_data .type lcd_write_data,@function lcd_write_data: move.l (4,%sp),%a0 /* Data pointer */ move.l (8,%sp),%d0 /* Length */ lea MBAR2,%a1 moveq #8,%d1 or.l %d1,(0xb4,%a1) .loop: move.b (%a0)+,%d1 move.w %d1,0xf0000000 subq.l #1,%d0 bne .loop rts #endif