mkimxboot: add partial support for the Zen X-Fi Style
[maemo-rb.git] / rbutil / mktccboot / main.c
blob4dd5d0c6c4da41964f77034805340679ddec8347
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Dave Chapman
12 * Based on mkboot, Copyright (C) 2005 by Linus Nielsen Feltzing
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 ****************************************************************************/
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include "mktccboot.h"
32 #include "telechips.h"
34 static void usage(void)
36 printf("Usage: mktccboot <firmware file> <boot file> <output file>\n");
38 exit(1);
41 int main(int argc, char *argv[])
43 char *infile, *bootfile, *outfile;
44 int fdout = -1;
45 int n, of_size, boot_size, patched_size;
46 unsigned char *of_buf;
47 unsigned char *boot_buf = NULL;
48 unsigned char* image = NULL;
49 int ret = 0;
51 if(argc < 3) {
52 usage();
55 infile = argv[1];
56 bootfile = argv[2];
57 outfile = argv[3];
59 /* Read OF and boot files */
60 of_buf = file_read(infile, &of_size);
61 if (!of_buf)
63 ret = 1;
64 goto error_exit;
67 /* Validate input file */
68 if (test_firmware_tcc(of_buf, of_size))
70 printf("[ERR] Unknown OF file used, aborting\n");
71 ret = 2;
72 goto error_exit;
75 boot_buf = file_read(bootfile, &boot_size);
76 if (!boot_buf)
78 ret = 3;
79 goto error_exit;
82 /* Allocate buffer for patched firmware */
83 image = malloc(of_size + boot_size);
84 if (image == NULL)
86 printf("[ERR] Could not allocate memory, aborting\n");
87 ret = 4;
88 goto error_exit;
91 /* Create the patched firmware */
92 image = patch_firmware_tcc(of_buf, of_size, boot_buf, boot_size,
93 &patched_size);
94 if (!image)
96 printf("[ERR] Error creating patched firmware, aborting\n");
97 ret = 5;
98 goto error_exit;
101 fdout = open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
102 if (fdout < 0)
104 perror(outfile);
105 ret = 6;
106 goto error_exit;
109 n = write(fdout, image, patched_size);
110 if (n != patched_size)
112 printf("[ERR] Could not write output file %s\n",outfile);
113 ret = 7;
114 goto error_exit;
117 error_exit:
119 if (fdout >= 0)
120 close(fdout);
122 if (of_buf)
123 free(of_buf);
125 if (boot_buf)
126 free(boot_buf);
128 if (image)
129 free(image);
131 return ret;