summaryrefslogtreecommitdiff
path: root/utils/nwztools/emmctools/emmctool.c
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2012-11-03 14:04:27 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2012-11-03 14:04:27 +0100
commit99f20b85f6856fa636e41c6987e34bf343a235ae (patch)
tree60f974cc307f457752e557e5a95c62222d5f374b /utils/nwztools/emmctools/emmctool.c
parent6d04ed343c76b38b5cd632ff0e0ba472ecade583 (diff)
downloadrockbox-99f20b85f6856fa636e41c6987e34bf343a235ae.tar.gz
rockbox-99f20b85f6856fa636e41c6987e34bf343a235ae.zip
nwztools: various fix and enhancements
Change-Id: Iaa89df27b7a0c4eb9fc6603c431de3d1fe791fa1
Diffstat (limited to 'utils/nwztools/emmctools/emmctool.c')
-rw-r--r--utils/nwztools/emmctools/emmctool.c60
1 files changed, 52 insertions, 8 deletions
diff --git a/utils/nwztools/emmctools/emmctool.c b/utils/nwztools/emmctools/emmctool.c
index 8fa7b0907b..26226b6f07 100644
--- a/utils/nwztools/emmctools/emmctool.c
+++ b/utils/nwztools/emmctools/emmctool.c
@@ -32,9 +32,10 @@
32#include "nvp.h" 32#include "nvp.h"
33 33
34bool g_debug = false; 34bool g_debug = false;
35char *g_out_prefix = NULL; 35static char *g_out_prefix = NULL;
36FILE *g_in_file = NULL; 36static FILE *g_in_file = NULL;
37bool g_force = false; 37bool g_force = false;
38static int g_nvp_node = -1;
38 39
39#define let_the_force_flow(x) do { if(!g_force) return x; } while(0) 40#define let_the_force_flow(x) do { if(!g_force) return x; } while(0)
40#define continue_the_force(x) if(x) let_the_force_flow(x) 41#define continue_the_force(x) if(x) let_the_force_flow(x)
@@ -164,15 +165,52 @@ static int do_emmc(void)
164 return 0; 165 return 0;
165} 166}
166 167
168static int do_nvp_extract(void)
169{
170 if(!g_out_prefix)
171 {
172 cprintf(GREY, "You must specify an output prefix to extract a NVP node\n");
173 return 1;
174 }
175 if(!nvp_is_valid_node(g_nvp_node))
176 {
177 cprintf(GREY, "Invalid NVP node %d\n", g_nvp_node);
178 return 3;
179 }
180
181 FILE *f = fopen(g_out_prefix, "wb");
182 if(!f)
183 {
184 cprintf(GREY, "Cannot open output file: %m\n");
185 return 2;
186 }
187
188 int size = nvp_get_node_size(g_nvp_node);
189 void *buffer = malloc(size);
190 int ret = nvp_read_node(g_nvp_node, 0, buffer, size);
191 if(ret < 0)
192 cprintf(GREY, "NVP read error: %d\n", ret);
193 else
194 {
195 cprintf(YELLOW, "%d ", ret);
196 cprintf(GREEN, "bytes written\n");
197 fwrite(buffer, 1, ret, f);
198 }
199 free(buffer);
200 fclose(f);
201 return 0;
202}
203
167static void usage(void) 204static void usage(void)
168{ 205{
169 printf("Usage: emmctool [options] img\n"); 206 printf("Usage: emmctool [options] img\n");
170 printf("Options:\n"); 207 printf("Options:\n");
171 printf(" -o <prefix>\tSet output prefix\n"); 208 printf(" -o <prefix>\t\tSet output prefix\n");
172 printf(" -f/--force\tForce to continue on errors\n"); 209 printf(" -f/--force\t\tForce to continue on errors\n");
173 printf(" -?/--help\tDisplay this message\n"); 210 printf(" -?/--help\t\tDisplay this message\n");
174 printf(" -d/--debug\tDisplay debug messages\n"); 211 printf(" -d/--debug\t\tDisplay debug messages\n");
175 printf(" -c/--no-color\tDisable color output\n"); 212 printf(" -c/--no-color\t\tDisable color output\n");
213 printf(" -e/--nvp-ex <node>\tExtract a NVP node\n");
176 exit(1); 214 exit(1);
177} 215}
178 216
@@ -186,10 +224,11 @@ int main(int argc, char **argv)
186 {"debug", no_argument, 0, 'd'}, 224 {"debug", no_argument, 0, 'd'},
187 {"no-color", no_argument, 0, 'c'}, 225 {"no-color", no_argument, 0, 'c'},
188 {"force", no_argument, 0, 'f'}, 226 {"force", no_argument, 0, 'f'},
227 {"nvp-ex", required_argument, 0, 'e'},
189 {0, 0, 0, 0} 228 {0, 0, 0, 0}
190 }; 229 };
191 230
192 int c = getopt_long(argc, argv, "?dcfo:", long_options, NULL); 231 int c = getopt_long(argc, argv, "?dcfo:e:", long_options, NULL);
193 if(c == -1) 232 if(c == -1)
194 break; 233 break;
195 switch(c) 234 switch(c)
@@ -211,6 +250,9 @@ int main(int argc, char **argv)
211 case 'o': 250 case 'o':
212 g_out_prefix = optarg; 251 g_out_prefix = optarg;
213 break; 252 break;
253 case 'e':
254 g_nvp_node = strtoul(optarg, NULL, 0);
255 break;
214 default: 256 default:
215 abort(); 257 abort();
216 } 258 }
@@ -232,6 +274,8 @@ int main(int argc, char **argv)
232 int ret = nvp_init(EMMC_NVP_SIZE, &nvp_read, g_debug); 274 int ret = nvp_init(EMMC_NVP_SIZE, &nvp_read, g_debug);
233 if(ret) return ret; 275 if(ret) return ret;
234 ret = do_emmc(); 276 ret = do_emmc();
277 if(ret == 0 && g_nvp_node >= 0)
278 ret = do_nvp_extract();
235 279
236 fclose(g_in_file); 280 fclose(g_in_file);
237 281