summaryrefslogtreecommitdiffstats
path: root/rbutil/mknwzboot/mknwzboot.c
blob: 1fc105ae4831740ef7c6e9b0b01fefcabfeb576c (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 by Amaury Pouly
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "mknwzboot.h"
#include "upg.h"

#include "install_script.h"

struct nwz_model_desc_t
{
    /* Descriptive name of this model */
    const char *model_name;
    /* Model name used in the Rockbox header in ".sansa" files - these match the
       -add parameter to the "scramble" tool */
    const char *rb_model_name;
    /* Model number used to initialise the checksum in the Rockbox header in
       ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */
    const int rb_model_num;
    /* Codename used in upgtool */
    const char *codename;
};

static const struct nwz_model_desc_t nwz_models[] =
{
    { "Sony NWZ-E450 Series", "e450", 100, "nwz-e450" },
    { "Sony NWZ-E460 Series", "e460", 101, "nwz-e460" },
    { "Sony NWZ-E470 Series", "e470", 103, "nwz-e470" },
    { "Sony NWZ-E580 Series", "e580", 102, "nwz-e580" },
    { "Sony NWZ-A10 Series", "a10", 104, "nwz-a10" },
};

#define NR_NWZ_MODELS     (sizeof(nwz_models) / sizeof(nwz_models[0]))

void dump_nwz_dev_info(const char *prefix)
{
    printf("%smknwzboot models:\n", prefix);
    for(int i = 0; i < NR_NWZ_MODELS; i++)
    {
        printf("%s  %s: rb_model=%s rb_num=%d codename=%s\n", prefix,
            nwz_models[i].model_name, nwz_models[i].rb_model_name,
            nwz_models[i].rb_model_num, nwz_models[i].codename);
    }
}

/* read a file to a buffer */
static void *read_file(const char *file, size_t *size)
{
    FILE *f = fopen(file, "rb");
    if(f == NULL)
    {
        printf("[ERR] Cannot open file '%s' for reading: %m\n", file);
        return NULL;
    }
    fseek(f, 0, SEEK_END);
    *size = ftell(f);
    fseek(f, 0, SEEK_SET);
    void *buffer = malloc(*size);
    if(fread(buffer, *size, 1, f) != 1)
    {
        free(buffer);
        fclose(f);
        printf("[ERR] Cannot read file '%s': %m\n", file);
        return NULL;
    }
    fclose(f);
    return buffer;
}

/* write a file from a buffer */
static bool write_file(const char *file, void *buffer, size_t size)
{
    FILE *f = fopen(file, "wb");
    if(f == NULL)
    {
        printf("[ERR] Cannot open file '%s' for writing: %m\n", file);
        return false;
    }
    if(fwrite(buffer, size, 1, f) != 1)
    {
        fclose(f);
        printf("[ERR] Cannot write file '%s': %m\n", file);
        return false;
    }
    fclose(f);
    return true;
}

static unsigned int be2int(unsigned char* buf)
{
   return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}

static int find_model(uint8_t *boot, size_t boot_size)
{
    if(boot_size < 8)
    {
        printf("[ERR] Boot file is too small to be valid\n");
        return -1;
    }
    /* find model by comparing magic scramble value */
    int model = 0;
    for(; model < NR_NWZ_MODELS; model++)
        if(memcmp(boot + 4, nwz_models[model].rb_model_name, 4) == 0)
            break;
    if(model == NR_NWZ_MODELS)
    {
        printf("[ERR] This player is not supported: %.4s\n", boot + 4);
        return -1;
    }
    printf("[INFO] Bootloader file for %s\n", nwz_models[model].model_name);
    /* verify checksum */
    uint32_t sum = nwz_models[model].rb_model_num;
    for(int i = 8; i < boot_size; i++)
        sum += boot[i];
    if(sum != be2int(boot))
    {
        printf("[ERR] Checksum mismatch\n");
        return -1;
    }
    return model;
}

static bool get_model_keysig(int model, char key[NWZ_KEY_SIZE], char sig[NWZ_SIG_SIZE])
{
    const char *codename = nwz_models[model].codename;
    for(int i = 0; g_model_list[i].model; i++)
        if(strcmp(g_model_list[i].model, codename) == 0)
        {
            if(decrypt_keysig(g_model_list[i].kas, key, sig) == 0)
                return true;
            printf("[ERR] Cannot decrypt kas '%s'\n", g_model_list[i].kas);
            return false;
        }
    printf("[ERR] Codename '%s' matches to entry in upg database\n", codename);
    return false;
}

void nwz_printf(void *u, bool err, color_t c, const char *f, ...)
{
    (void)err;
    (void)c;
    bool *debug = u;
    va_list args;
    va_start(args, f);
    if(err || *debug)
        vprintf(f, args);
    va_end(args);
}

static void *memdup(void *data, size_t size)
{
    void *buf = malloc(size);
    memcpy(buf, data, size);
    return buf;
}

int mknwzboot(const char *bootfile, const char *outfile, bool debug)
{
    size_t boot_size;
    uint8_t *boot = read_file(bootfile, &boot_size);
    if(boot == NULL)
    {
        printf("[ERR] Cannot open boot file\n");
        return 1;
    }
    /* check that it is a valid scrambled file */
    int model = find_model(boot, boot_size);
    if(model < 0)
    {
        free(boot);
        printf("[ERR] Invalid boot file\n");
        return 2;
    }
    /* find keys */
    char key[NWZ_KEY_SIZE];
    char sig[NWZ_SIG_SIZE];
    if(!get_model_keysig(model, key, sig))
    {
        printf("[ERR][INTERNAL] Cannot get keys for model\n");
        return 3;
    }
    /* create the upg file */
    struct upg_file_t *upg = upg_new();
    /* first file is the install script: we have to copy data because upg_free()
     * will free it */
    upg_append(upg, memdup(install_script, LEN_install_script), LEN_install_script);
    /* second file is the bootloader content (expected to be a tar file): we have
     * to copy data because upg_free() will free it */
    upg_append(upg, memdup(boot + 8, boot_size - 8), boot_size - 8);
    free(boot);
    /* write file to buffer */
    size_t upg_size;
    void *upg_buf = upg_write_memory(upg, key, sig, &upg_size, &debug, nwz_printf);
    upg_free(upg);
    if(upg_buf == NULL)
    {
        printf("[ERR] Cannot create UPG file\n");
        return 4;
    }
    if(!write_file(outfile, upg_buf, upg_size))
    {
        free(upg_buf);
        printf("[ERR] Cannpt write UPG file\n");
        return 5;
    }
    free(upg_buf);
    return 0;
}