summaryrefslogtreecommitdiffstats
path: root/rbutil/mkrk27boot/mkrk27boot.c
blob: 4b51e281c799300ad96d14714c07f0438d83b8e3 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id:$
 *
 * Copyright (C) 2012 by Andrew Ryabinin
 *
 * based on firmware/test/fat/main.c
 *
 * 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 <string.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include "disk.h"
#include "dir.h"
#include "file.h"
#include "ata.h"
#include "storage.h"
#include "mkrk27boot.h"


const char *img_filename;
static char mkrk27_error_msg[256];

static void mkrk27_set_error(const char *msg, ...) {
    va_list ap;
    va_start(ap, msg);
    snprintf(mkrk27_error_msg, sizeof(mkrk27_error_msg), msg, ap);
    va_end(ap);
    return;
}



/* Extracts in_file from FAT image to out_file */
static int extract_file(const char *in_file, const char* out_file) {
    char buf[81920];
    int nread;

    int fd = open(in_file, O_RDONLY);
    if (fd < 0) {
        mkrk27_set_error("Failed to open file %s. ", in_file);
        return -1;
    }

    FILE *file = fopen(out_file, "wb");
    if (!file) {
        mkrk27_set_error("Failed to open file %s. ", out_file);
        close(fd);
        return -1;
    }

    while (nread = read(fd, buf, sizeof buf), nread > 0) {
        char *out_ptr = buf;
        int nwritten;

        do {
            nwritten = fwrite(buf, 1, nread, file);

            if (nwritten >= 0) {
                nread -= nwritten;
                out_ptr += nwritten;
            } else if (errno != EINTR) {
                mkrk27_set_error("Writting to file %s failed.", out_file);
                goto exit;
            }
        } while(nread > 0);
    }

    if (nread == 0) {
        fclose(file);
        close(fd);
        return 0;
    } else {
        mkrk27_set_error("Copy from %s to %s failed.", out_file, in_file);
    }
exit:
    fclose(file);
    close(fd);
    return -1;
}

/* Replace out_file in FAT image with in_file */
static int replace_file(const char *in_file, const char *out_file) {
    char buf[81920];
    int fd;
    int nread;

    fd = creat(out_file, 0666);
    if (fd < 0) {
        mkrk27_set_error("Failed to open file %s. ", out_file);
        return -1;
    }

    FILE *file = fopen(in_file, "rb");
    if (!file) {
        mkrk27_set_error("Failed to open file %s. ", in_file);
        close(fd);
        return -1;
    }

    while (nread = fread(buf, 1, sizeof buf, file), nread > 0) {
        char *out_ptr = buf;
        int nwritten;

        do {
            nwritten = write(fd, buf, nread);
            if (nwritten >= 0) {
                nread -= nwritten;
                out_ptr += nwritten;
            } else {
                mkrk27_set_error("Writting to file %s failed.", out_file);
                goto exit;
            }
        } while(nread > 0);
    }

    if (nread == 0) {
        fclose(file);
        close(fd);
        return 0;
    } else {
        mkrk27_set_error("Replacing %s with %s failed.", out_file, in_file);
    }

exit:
    fclose(file);
    close(fd);
    return -1;
}

static int mkrk27_init(const char *filename) {
    int i;
    int rc;
    srand(clock());

    img_filename = filename;

    if(ata_init()) {
        mkrk27_set_error("Warning! The disk is uninitialized\n");
        return -1;
    }

    struct partinfo *pinfo = disk_init();

    if (!pinfo) {
        mkrk27_set_error("Failed reading partitions\n");
        return -1;
    }

    for ( i=0; i<4; i++ ) {
        if ( pinfo[i].type == PARTITION_TYPE_FAT32
#ifdef HAVE_FAT16SUPPORT
             || pinfo[i].type == PARTITION_TYPE_FAT16
#endif
            ) {
            rc = fat_mount(IF_MV2(0,) IF_MD2(0,) pinfo[i].start);
            if(rc) {
                mkrk27_set_error("mount: %d",rc);
                return -1;
            }
            break;
        }
    }
    if ( i==4 ) {
        if(fat_mount(IF_MV2(0,) IF_MD2(0,) 0)) {
            mkrk27_set_error("FAT32 partition!");
            return -1;
        }
    }
    return 0;
}

extern void ata_exit(void);

static void mkrk27_deinit(void) {
    ata_exit();
}

/* copy file */
static int copy(const char *to, const char *from) {
    FILE* fd_to, *fd_from;
    char buf[4096];
    ssize_t nread;

    if (to == from) {
        return 0;
    }

    fd_from = fopen(from, "rb");
    if (!fd_from) {
        mkrk27_set_error("Failed to open file %s.", from);
        return -1;
    }

    fd_to = fopen(to, "wb");
    if (!fd_to) {
        mkrk27_set_error("Failed to open file %s.", to);
        goto out_error;
    }

    while (nread = fread(buf, 1, sizeof buf, fd_from), nread > 0) {
        char *out_ptr = buf;
        ssize_t nwritten;

        do {
            nwritten = fwrite(out_ptr, 1, nread, fd_to);

            if (nwritten >= 0)
            {
                nread -= nwritten;
                out_ptr += nwritten;
            }
            else if (errno != EINTR)
            {
                mkrk27_set_error( "Writing to file %s failed.", to);
                goto out_error;
            }
        } while (nread > 0);
    }

    if (nread == 0) {
        fclose(fd_to);
        fclose(fd_from);
        return 0;
    } else {
        mkrk27_set_error("Copy from %s to %s failed.", from, to);
    }

out_error:

    fclose(fd_from);
    fclose(fd_to);

    return -1;
}

char* mkrk27_get_error(void) {
    return mkrk27_error_msg;
}


/* Patch rk27 firmware.
    - img_file - original FAT image file, containing OF,
    - rkw_file - rkw file which will replace BASE.RKW from OF,
    - out_img_file - patched img file,
    - out_rkw_file - BASE.RKW extracted from OF.
*/
int mkrk27_patch(const char* img_file, const char* rkw_file, const char* out_img_file, const char* out_rkw_file) {
    if (copy(out_img_file, img_file)) {
        return -1;
    }
    if (mkrk27_init(out_img_file)) {
        return -1;
    }
    if (extract_file("/SYSTEM/BASE.RKW", out_rkw_file)) {
        return -1;
    }
    if (replace_file(rkw_file, "/SYSTEM/BASE.RKW") ||
        replace_file(rkw_file, "/SYSTEM00/BASE.RKW")) {
        return -1;
    }
    mkrk27_deinit();
    return 0;
}