diff options
Diffstat (limited to 'utils/samsungtools/fwcrypt.c')
-rw-r--r-- | utils/samsungtools/fwcrypt.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/utils/samsungtools/fwcrypt.c b/utils/samsungtools/fwcrypt.c new file mode 100644 index 0000000000..f920406e12 --- /dev/null +++ b/utils/samsungtools/fwcrypt.c | |||
@@ -0,0 +1,170 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2012 Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "samsung.h" | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <stdarg.h> | ||
25 | #include <getopt.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | static bool g_debug = false; | ||
29 | static char *g_out_prefix = NULL; | ||
30 | |||
31 | static void usage(void) | ||
32 | { | ||
33 | printf("Usage: fwcrypt [options] content\n"); | ||
34 | printf("Options:\n"); | ||
35 | printf(" -o <prefix>\tSet output file\n"); | ||
36 | printf(" -m/--model <model>\tSet model\n"); | ||
37 | printf(" -v/--version <ver>\tSet version\n"); | ||
38 | printf(" -r/--region <region>\tSet region\n"); | ||
39 | printf(" -e/--extra <extra>\tSet extra\n"); | ||
40 | printf(" -?/--help\tDisplay this message\n"); | ||
41 | printf(" -d/--debug\tDisplay debug messages\n"); | ||
42 | exit(1); | ||
43 | } | ||
44 | |||
45 | static int s_write(void *user, int offset, void *buf, int size) | ||
46 | { | ||
47 | FILE *f = user; | ||
48 | if(fseek(f, offset, SEEK_SET) != 0) | ||
49 | return 0; | ||
50 | return fwrite(buf, 1, size, f); | ||
51 | } | ||
52 | |||
53 | static void s_printf(void *user, bool error, const char *fmt, ...) | ||
54 | { | ||
55 | if(!g_debug && !error) | ||
56 | return; | ||
57 | (void) user; | ||
58 | va_list args; | ||
59 | va_start(args, fmt); | ||
60 | vprintf(fmt, args); | ||
61 | va_end(args); | ||
62 | } | ||
63 | |||
64 | int main(int argc, char **argv) | ||
65 | { | ||
66 | struct samsung_firmware_t *fw = malloc(sizeof(struct samsung_firmware_t)); | ||
67 | memset(fw, 0, sizeof(struct samsung_firmware_t)); | ||
68 | |||
69 | if(argc <= 1) | ||
70 | usage(); | ||
71 | |||
72 | while(1) | ||
73 | { | ||
74 | static struct option long_options[] = | ||
75 | { | ||
76 | {"help", no_argument, 0, '?'}, | ||
77 | {"debug", no_argument, 0, 'd'}, | ||
78 | {"model", required_argument, 0, 'm'}, | ||
79 | {"version", required_argument, 0, 'v'}, | ||
80 | {"region", required_argument, 0, 'r'}, | ||
81 | {"extra", required_argument, 0, 'e'}, | ||
82 | {0, 0, 0, 0} | ||
83 | }; | ||
84 | |||
85 | int c = getopt_long(argc, argv, "?do:m:v:r:e:", long_options, NULL); | ||
86 | if(c == -1) | ||
87 | break; | ||
88 | switch(c) | ||
89 | { | ||
90 | case -1: | ||
91 | break; | ||
92 | case 'd': | ||
93 | g_debug = true; | ||
94 | break; | ||
95 | case '?': | ||
96 | usage(); | ||
97 | break; | ||
98 | case 'o': | ||
99 | g_out_prefix = optarg; | ||
100 | break; | ||
101 | case 'm': | ||
102 | strncpy(fw->model, optarg, sizeof(fw->model)); | ||
103 | if(strlen(optarg) > sizeof(fw->model)) | ||
104 | printf("Warning: truncate model string\n"); | ||
105 | break; | ||
106 | case 'r': | ||
107 | strncpy(fw->region, optarg, sizeof(fw->region)); | ||
108 | if(strlen(optarg) > sizeof(fw->region)) | ||
109 | printf("Warning: truncate region string\n"); | ||
110 | break; | ||
111 | case 'v': | ||
112 | strncpy(fw->version, optarg, sizeof(fw->version)); | ||
113 | if(strlen(optarg) > sizeof(fw->version)) | ||
114 | printf("Warning: truncate vesion string\n"); | ||
115 | break; | ||
116 | case 'e': | ||
117 | strncpy(fw->extra, optarg, sizeof(fw->extra)); | ||
118 | if(strlen(optarg) > sizeof(fw->extra)) | ||
119 | printf("Warning: truncate extra string\n"); | ||
120 | break; | ||
121 | default: | ||
122 | abort(); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | if(optind != argc - 1) | ||
127 | usage(); | ||
128 | |||
129 | FILE *fin = fopen(argv[optind], "rb"); | ||
130 | if(fin == NULL) | ||
131 | { | ||
132 | printf("Cannot open file for reading: %m\n"); | ||
133 | samsung_free(fw); | ||
134 | return 1; | ||
135 | } | ||
136 | fseek(fin, 0, SEEK_END); | ||
137 | fw->data_size = ftell(fin); | ||
138 | fseek(fin, 0, SEEK_SET); | ||
139 | fw->data = malloc(fw->data_size); | ||
140 | if((int)fread(fw->data, 1, fw->data_size, fin) != fw->data_size) | ||
141 | { | ||
142 | printf("Cannot read input file: %m\n"); | ||
143 | samsung_free(fw); | ||
144 | return 2; | ||
145 | } | ||
146 | fclose(fin); | ||
147 | |||
148 | if(g_out_prefix) | ||
149 | { | ||
150 | FILE *f = fopen(g_out_prefix, "wb"); | ||
151 | if(f == NULL) | ||
152 | { | ||
153 | printf("Cannot open file for writing: %m\n"); | ||
154 | samsung_free(fw); | ||
155 | return 1; | ||
156 | } | ||
157 | |||
158 | enum samsung_error_t err = samsung_write(s_write, s_printf, f, fw); | ||
159 | if(err != SAMSUNG_SUCCESS) | ||
160 | { | ||
161 | printf("Error writing firmware: %d\n", err); | ||
162 | samsung_free(fw); | ||
163 | return 3; | ||
164 | } | ||
165 | fclose(f); | ||
166 | } | ||
167 | samsung_free(fw); | ||
168 | |||
169 | return 0; | ||
170 | } | ||