summaryrefslogtreecommitdiffstats
path: root/rbutil/mkamsboot/main.c
blob: 5715248685ddae56cd4b93ad3f00899d6acda5b4 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * mkamsboot - a tool for merging bootloader code into an Sansa V2
 *             (AMS) firmware file
 *
 * Copyright (C) 2008 Dave Chapman
 *
 * 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 <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include <ucl/ucl.h>

#include "mkamsboot.h"

/* Header for ARM code binaries */
#include "dualboot.h"

/* Win32 compatibility */
#ifndef O_BINARY
#define O_BINARY 0
#endif

/* standalone executable */
int main(int argc, char* argv[])
{
    char *infile, *bootfile, *outfile;
    int fdout;
    off_t len;
    uint32_t n;
    unsigned char* buf;
    int firmware_size;
    int bootloader_size;
    unsigned char* of_packed;
    int of_packedsize;
    unsigned char* rb_packed;
    int rb_packedsize;
    int totalsize;
    char errstr[200];
    struct md5sums sum;
    char md5sum[33]; /* 32 digits + \0 */

    sum.md5 = md5sum;

/* VERSION comes frome the Makefile */
    fprintf(stderr,
"mkamsboot Version " VERSION "\n"
"This is free software; see the source for copying conditions.  There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
"\n");

    if(argc != 4) {
        printf("Usage: mkamsboot <firmware file> <boot file> <output file>\n");
        return 1;
    }

    infile = argv[1];
    bootfile = argv[2];
    outfile = argv[3];

    /* Load original firmware file */
    buf = load_of_file(infile, &len, &sum,
            &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));

    if (buf == NULL) {
        fprintf(stderr, "%s", errstr);
        fprintf(stderr, "[ERR]  Could not load %s\n", infile);
        return 1;
    }

    fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n");
    fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n",
            model_names[sum.model], hw_revisions[sum.model], sum.version);


    /* Load bootloader file */
    rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size,
            &rb_packedsize, errstr, sizeof(errstr));
    if (rb_packed == NULL) {
        fprintf(stderr, "%s", errstr);
        fprintf(stderr, "[ERR]  Could not load %s\n", bootfile);
        free(buf);
        free(of_packed);
        return 1;
    }

    printf("[INFO] Firmware patching has begun !\n\n");

    fprintf(stderr, "[INFO] Original firmware size:   %d bytes\n",
            firmware_size);
    fprintf(stderr, "[INFO] Packed OF size:           %d bytes\n",
            of_packedsize);
    fprintf(stderr, "[INFO] Bootloader size:          %d bytes\n",
            (int)bootloader_size);
    fprintf(stderr, "[INFO] Packed bootloader size:   %d bytes\n",
            rb_packedsize);
    fprintf(stderr, "[INFO] Dual-boot function size:  %d bytes\n",
            bootloader_sizes[sum.model]);
    fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n",
            (unsigned int)sizeof(nrv2e_d8));

    totalsize = total_size(sum.model, of_packedsize, rb_packedsize);

    fprintf(stderr, "[INFO] Total size of new image:  %d bytes\n", totalsize);

    if (totalsize > firmware_size) {
        fprintf(stderr, "[ERR]  No room to insert bootloader, aborting\n");
        free(buf);
        free(of_packed);
        free(rb_packed);
        return 1;
    }

    patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len,
            of_packed, of_packedsize, rb_packed, rb_packedsize);

    /* Write the new firmware */
    fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);

    if (fdout < 0) {
        fprintf(stderr, "[ERR]  Could not open %s for writing\n", outfile);
        free(buf);
        free(of_packed);
        free(rb_packed);
        return 1;
    }

    n = write(fdout, buf, len);

    if (n != (unsigned)len) {
        fprintf(stderr, "[ERR]  Could not write firmware file\n");
        free(buf);
        free(of_packed);
        free(rb_packed);
        return 1;
    }

    close(fdout);
    free(buf);
    free(of_packed);
    free(rb_packed);
    fprintf(stderr, "\n[INFO] Patching succeeded!\n");

    return 0;
}