diff options
Diffstat (limited to 'rbutil/mktccboot/main.c')
-rw-r--r-- | rbutil/mktccboot/main.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/rbutil/mktccboot/main.c b/rbutil/mktccboot/main.c new file mode 100644 index 0000000000..4dd5d0c6c4 --- /dev/null +++ b/rbutil/mktccboot/main.c | |||
@@ -0,0 +1,133 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 by Dave Chapman | ||
11 | * | ||
12 | * Based on mkboot, Copyright (C) 2005 by Linus Nielsen Feltzing | ||
13 | * | ||
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. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
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" | ||
33 | |||
34 | static void usage(void) | ||
35 | { | ||
36 | printf("Usage: mktccboot <firmware file> <boot file> <output file>\n"); | ||
37 | |||
38 | exit(1); | ||
39 | } | ||
40 | |||
41 | int main(int argc, char *argv[]) | ||
42 | { | ||
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; | ||
50 | |||
51 | if(argc < 3) { | ||
52 | usage(); | ||
53 | } | ||
54 | |||
55 | infile = argv[1]; | ||
56 | bootfile = argv[2]; | ||
57 | outfile = argv[3]; | ||
58 | |||
59 | /* Read OF and boot files */ | ||
60 | of_buf = file_read(infile, &of_size); | ||
61 | if (!of_buf) | ||
62 | { | ||
63 | ret = 1; | ||
64 | goto error_exit; | ||
65 | } | ||
66 | |||
67 | /* Validate input file */ | ||
68 | if (test_firmware_tcc(of_buf, of_size)) | ||
69 | { | ||
70 | printf("[ERR] Unknown OF file used, aborting\n"); | ||
71 | ret = 2; | ||
72 | goto error_exit; | ||
73 | } | ||
74 | |||
75 | boot_buf = file_read(bootfile, &boot_size); | ||
76 | if (!boot_buf) | ||
77 | { | ||
78 | ret = 3; | ||
79 | goto error_exit; | ||
80 | } | ||
81 | |||
82 | /* Allocate buffer for patched firmware */ | ||
83 | image = malloc(of_size + boot_size); | ||
84 | if (image == NULL) | ||
85 | { | ||
86 | printf("[ERR] Could not allocate memory, aborting\n"); | ||
87 | ret = 4; | ||
88 | goto error_exit; | ||
89 | } | ||
90 | |||
91 | /* Create the patched firmware */ | ||
92 | image = patch_firmware_tcc(of_buf, of_size, boot_buf, boot_size, | ||
93 | &patched_size); | ||
94 | if (!image) | ||
95 | { | ||
96 | printf("[ERR] Error creating patched firmware, aborting\n"); | ||
97 | ret = 5; | ||
98 | goto error_exit; | ||
99 | } | ||
100 | |||
101 | fdout = open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); | ||
102 | if (fdout < 0) | ||
103 | { | ||
104 | perror(outfile); | ||
105 | ret = 6; | ||
106 | goto error_exit; | ||
107 | } | ||
108 | |||
109 | n = write(fdout, image, patched_size); | ||
110 | if (n != patched_size) | ||
111 | { | ||
112 | printf("[ERR] Could not write output file %s\n",outfile); | ||
113 | ret = 7; | ||
114 | goto error_exit; | ||
115 | } | ||
116 | |||
117 | error_exit: | ||
118 | |||
119 | if (fdout >= 0) | ||
120 | close(fdout); | ||
121 | |||
122 | if (of_buf) | ||
123 | free(of_buf); | ||
124 | |||
125 | if (boot_buf) | ||
126 | free(boot_buf); | ||
127 | |||
128 | if (image) | ||
129 | free(image); | ||
130 | |||
131 | return ret; | ||
132 | } | ||
133 | |||