1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2012 by Andrew Ryabinin
12 * based on firmware/test/fat/main.c
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
35 #include "mkrk27boot.h"
38 const char *img_filename
;
39 static char mkrk27_error_msg
[256];
41 static void mkrk27_set_error(const char *msg
, ...) {
44 snprintf(mkrk27_error_msg
, sizeof(mkrk27_error_msg
), msg
, ap
);
51 /* Extracts in_file from FAT image to out_file */
52 static int extract_file(const char *in_file
, const char* out_file
) {
56 int fd
= open(in_file
, O_RDONLY
);
58 mkrk27_set_error("Failed to open file %s. ", in_file
);
62 FILE *file
= fopen(out_file
, "wb");
64 mkrk27_set_error("Failed to open file %s. ", out_file
);
69 while (nread
= read(fd
, buf
, sizeof buf
), nread
> 0) {
74 nwritten
= fwrite(buf
, 1, nread
, file
);
79 } else if (errno
!= EINTR
) {
80 mkrk27_set_error("Writting to file %s failed.", out_file
);
91 mkrk27_set_error("Copy from %s to %s failed.", out_file
, in_file
);
99 /* Replace out_file in FAT image with in_file */
100 static int replace_file(const char *in_file
, const char *out_file
) {
105 fd
= creat(out_file
, 0666);
107 mkrk27_set_error("Failed to open file %s. ", out_file
);
111 FILE *file
= fopen(in_file
, "rb");
113 mkrk27_set_error("Failed to open file %s. ", in_file
);
118 while (nread
= fread(buf
, 1, sizeof buf
, file
), nread
> 0) {
123 nwritten
= write(fd
, buf
, nread
);
128 mkrk27_set_error("Writting to file %s failed.", out_file
);
139 mkrk27_set_error("Replacing %s with %s failed.", out_file
, in_file
);
148 static int mkrk27_init(const char *filename
) {
153 img_filename
= filename
;
156 mkrk27_set_error("Warning! The disk is uninitialized\n");
160 struct partinfo
*pinfo
= disk_init();
163 mkrk27_set_error("Failed reading partitions\n");
167 for ( i
=0; i
<4; i
++ ) {
168 if ( pinfo
[i
].type
== PARTITION_TYPE_FAT32
169 #ifdef HAVE_FAT16SUPPORT
170 || pinfo
[i
].type
== PARTITION_TYPE_FAT16
173 rc
= fat_mount(IF_MV2(0,) IF_MD2(0,) pinfo
[i
].start
);
175 mkrk27_set_error("mount: %d",rc
);
182 if(fat_mount(IF_MV2(0,) IF_MD2(0,) 0)) {
183 mkrk27_set_error("FAT32 partition!");
190 extern void ata_exit(void);
192 static void mkrk27_deinit(void) {
197 static int copy(const char *to
, const char *from
) {
198 FILE* fd_to
, *fd_from
;
206 fd_from
= fopen(from
, "rb");
208 mkrk27_set_error("Failed to open file %s.", from
);
212 fd_to
= fopen(to
, "wb");
214 mkrk27_set_error("Failed to open file %s.", to
);
218 while (nread
= fread(buf
, 1, sizeof buf
, fd_from
), nread
> 0) {
223 nwritten
= fwrite(out_ptr
, 1, nread
, fd_to
);
230 else if (errno
!= EINTR
)
232 mkrk27_set_error( "Writing to file %s failed.", to
);
243 mkrk27_set_error("Copy from %s to %s failed.", from
, to
);
254 char* mkrk27_get_error(void) {
255 return mkrk27_error_msg
;
259 /* Patch rk27 firmware.
260 - img_file - original FAT image file, containing OF,
261 - rkw_file - rkw file which will replace BASE.RKW from OF,
262 - out_img_file - patched img file,
263 - out_rkw_file - BASE.RKW extracted from OF.
265 int mkrk27_patch(const char* img_file
, const char* rkw_file
, const char* out_img_file
, const char* out_rkw_file
) {
266 if (copy(out_img_file
, img_file
)) {
269 if (mkrk27_init(out_img_file
)) {
272 if (extract_file("/SYSTEM/BASE.RKW", out_rkw_file
)) {
275 if (replace_file(rkw_file
, "/SYSTEM/BASE.RKW") ||
276 replace_file(rkw_file
, "/SYSTEM00/BASE.RKW")) {