diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2012-10-04 12:25:22 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2012-10-04 12:27:17 +0200 |
commit | 283277e5ab95b3c74a9bee0bef25cb55d55e016b (patch) | |
tree | ad8931fe5b318b3dafea300c184554383ab0673b /utils/atj2137/atjboottool | |
parent | 0a334e2669f6b59dbbd22624984f3d005ef73634 (diff) | |
download | rockbox-283277e5ab95b3c74a9bee0bef25cb55d55e016b.tar.gz rockbox-283277e5ab95b3c74a9bee0bef25cb55d55e016b.zip |
atj: provide default output prefix/filenames on unpacking
If no output prefix is specified, a default is picked:
- filename with extension replaced by .afi for FWU files
- filename with extension replaced by .fw/ for AFI files
- filename without extension and with / for FW files
Change-Id: I6497b8f4a49f1238e5db738429f687cad3ae8a5a
Diffstat (limited to 'utils/atj2137/atjboottool')
-rw-r--r-- | utils/atj2137/atjboottool/atjboottool.c | 83 |
1 files changed, 66 insertions, 17 deletions
diff --git a/utils/atj2137/atjboottool/atjboottool.c b/utils/atj2137/atjboottool/atjboottool.c index 3fb9874712..bbc2226cc7 100644 --- a/utils/atj2137/atjboottool/atjboottool.c +++ b/utils/atj2137/atjboottool/atjboottool.c | |||
@@ -8,6 +8,7 @@ | |||
8 | #include <ctype.h> | 8 | #include <ctype.h> |
9 | #include "misc.h" | 9 | #include "misc.h" |
10 | #include "elf.h" | 10 | #include "elf.h" |
11 | #include <sys/stat.h> | ||
11 | 12 | ||
12 | #ifndef MIN | 13 | #ifndef MIN |
13 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | 14 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
@@ -19,6 +20,7 @@ | |||
19 | 20 | ||
20 | bool g_debug = false; | 21 | bool g_debug = false; |
21 | char *g_out_prefix = NULL; | 22 | char *g_out_prefix = NULL; |
23 | char *g_in_file = NULL; | ||
22 | bool g_force = false; | 24 | bool g_force = false; |
23 | 25 | ||
24 | #define let_the_force_flow(x) do { if(!g_force) return x; } while(0) | 26 | #define let_the_force_flow(x) do { if(!g_force) return x; } while(0) |
@@ -872,6 +874,37 @@ static int do_sthg_fwu_v3(uint8_t *buf, int *size, uint8_t *unk, uint8_t *block) | |||
872 | return 0; | 874 | return 0; |
873 | } | 875 | } |
874 | 876 | ||
877 | /* [add]: string to add when there is no extension | ||
878 | * [replace]: string to replace extension */ | ||
879 | static void build_out_prefix(char *add, char *replace, bool slash) | ||
880 | { | ||
881 | if(g_out_prefix) | ||
882 | return; | ||
883 | /** copy input filename with extra space */ | ||
884 | g_out_prefix = malloc(strlen(g_in_file) + strlen(add) + 16); | ||
885 | strcpy(g_out_prefix, g_in_file); | ||
886 | /** remove extension and add '/' */ | ||
887 | char *filename = strrchr(g_out_prefix, '/'); | ||
888 | // have p points to the beginning or after the last '/' | ||
889 | filename = (filename == NULL) ? g_out_prefix : filename + 1; | ||
890 | // extension ? | ||
891 | char *dot = strrchr(filename, '.'); | ||
892 | if(dot) | ||
893 | { | ||
894 | *dot = 0; // cut at the dot | ||
895 | strcat(dot, replace); | ||
896 | } | ||
897 | else | ||
898 | strcat(filename, add); // add extra string | ||
899 | |||
900 | if(slash) | ||
901 | { | ||
902 | strcat(filename, "/"); | ||
903 | /** make sure the directory exists */ | ||
904 | mkdir(g_out_prefix, S_IRWXU | S_IRGRP | S_IROTH); | ||
905 | } | ||
906 | } | ||
907 | |||
875 | static int do_fwu(uint8_t *buf, int size) | 908 | static int do_fwu(uint8_t *buf, int size) |
876 | { | 909 | { |
877 | struct fwu_hdr_t *hdr = (void *)buf; | 910 | struct fwu_hdr_t *hdr = (void *)buf; |
@@ -931,6 +964,8 @@ static int do_fwu(uint8_t *buf, int size) | |||
931 | let_the_force_flow(__LINE__); | 964 | let_the_force_flow(__LINE__); |
932 | } | 965 | } |
933 | 966 | ||
967 | build_out_prefix(".afi", ".afi", false); | ||
968 | |||
934 | if(g_version[ver].version == 3) | 969 | if(g_version[ver].version == 3) |
935 | { | 970 | { |
936 | uint8_t unk[32]; | 971 | uint8_t unk[32]; |
@@ -940,15 +975,16 @@ static int do_fwu(uint8_t *buf, int size) | |||
940 | int ret = do_sthg_fwu_v3(buf, &size, unk, block); | 975 | int ret = do_sthg_fwu_v3(buf, &size, unk, block); |
941 | continue_the_force(ret); | 976 | continue_the_force(ret); |
942 | 977 | ||
943 | if(g_out_prefix) | 978 | cprintf(GREY, "Descrambling to %s... ", g_out_prefix); |
979 | FILE *f = fopen(g_out_prefix, "wb"); | ||
980 | if(f) | ||
944 | { | 981 | { |
945 | FILE *f = fopen(g_out_prefix, "wb"); | 982 | fwrite(buf, size, 1, f); |
946 | if(f) | 983 | fclose(f); |
947 | { | 984 | cprintf(RED, "Ok\n"); |
948 | fwrite(buf, size, 1, f); | ||
949 | fclose(f); | ||
950 | } | ||
951 | } | 985 | } |
986 | else | ||
987 | cprintf(RED, "Failed: %m\n"); | ||
952 | } | 988 | } |
953 | 989 | ||
954 | return 0; | 990 | return 0; |
@@ -1096,6 +1132,8 @@ static int do_afi(uint8_t *buf, int size) | |||
1096 | cprintf_field(" Reserved: ", "%x %x %x\n", afi->hdr.res[0], | 1132 | cprintf_field(" Reserved: ", "%x %x %x\n", afi->hdr.res[0], |
1097 | afi->hdr.res[1], afi->hdr.res[2]); | 1133 | afi->hdr.res[1], afi->hdr.res[2]); |
1098 | 1134 | ||
1135 | build_out_prefix(".fw", "", true); | ||
1136 | |||
1099 | cprintf(BLUE, "Entries\n"); | 1137 | cprintf(BLUE, "Entries\n"); |
1100 | for(int i = 0; i < AFI_ENTRIES; i++) | 1138 | for(int i = 0; i < AFI_ENTRIES; i++) |
1101 | { | 1139 | { |
@@ -1117,17 +1155,19 @@ static int do_afi(uint8_t *buf, int size) | |||
1117 | uint32_t chk = afi_checksum(buf + entry->offset, entry->size); | 1155 | uint32_t chk = afi_checksum(buf + entry->offset, entry->size); |
1118 | cprintf(RED, "%s\n", chk == entry->checksum ? "Ok" : "Mismatch"); | 1156 | cprintf(RED, "%s\n", chk == entry->checksum ? "Ok" : "Mismatch"); |
1119 | 1157 | ||
1120 | if(g_out_prefix) | 1158 | char *name = malloc(strlen(g_out_prefix) + strlen(filename) + 16); |
1159 | sprintf(name, "%s%s", g_out_prefix, filename); | ||
1160 | |||
1161 | cprintf(GREY, "Unpacking to %s... ", name); | ||
1162 | FILE *f = fopen(name, "wb"); | ||
1163 | if(f) | ||
1121 | { | 1164 | { |
1122 | char *name = malloc(strlen(g_out_prefix) + strlen(filename) + 16); | 1165 | fwrite(buf + entry->offset, entry->size, 1, f); |
1123 | sprintf(name, "%s%s", g_out_prefix, filename); | 1166 | fclose(f); |
1124 | FILE *f = fopen(name, "wb"); | 1167 | cprintf(RED, "Ok\n"); |
1125 | if(f) | ||
1126 | { | ||
1127 | fwrite(buf + entry->offset, entry->size, 1, f); | ||
1128 | fclose(f); | ||
1129 | } | ||
1130 | } | 1168 | } |
1169 | else | ||
1170 | cprintf(RED, "Failed: %m\n"); | ||
1131 | } | 1171 | } |
1132 | 1172 | ||
1133 | cprintf(BLUE, "Post Header\n"); | 1173 | cprintf(BLUE, "Post Header\n"); |
@@ -1260,6 +1300,8 @@ static int do_fw(uint8_t *buf, int size) | |||
1260 | cprintf_field(" MTP PID: ", "0x%x\n", hdr->mtp_pid); | 1300 | cprintf_field(" MTP PID: ", "0x%x\n", hdr->mtp_pid); |
1261 | cprintf_field(" FW Version: ", "%.64s\n", hdr->fw_ver); | 1301 | cprintf_field(" FW Version: ", "%.64s\n", hdr->fw_ver); |
1262 | 1302 | ||
1303 | build_out_prefix(".unpack", "", true); | ||
1304 | |||
1263 | cprintf(BLUE, "Entries\n"); | 1305 | cprintf(BLUE, "Entries\n"); |
1264 | for(int i = 0; i < AFI_ENTRIES; i++) | 1306 | for(int i = 0; i < AFI_ENTRIES; i++) |
1265 | { | 1307 | { |
@@ -1280,12 +1322,17 @@ static int do_fw(uint8_t *buf, int size) | |||
1280 | { | 1322 | { |
1281 | char *name = malloc(strlen(g_out_prefix) + strlen(filename) + 16); | 1323 | char *name = malloc(strlen(g_out_prefix) + strlen(filename) + 16); |
1282 | sprintf(name, "%s%s", g_out_prefix, filename); | 1324 | sprintf(name, "%s%s", g_out_prefix, filename); |
1325 | |||
1326 | cprintf(GREY, "Unpacking to %s... ", name); | ||
1283 | FILE *f = fopen(name, "wb"); | 1327 | FILE *f = fopen(name, "wb"); |
1284 | if(f) | 1328 | if(f) |
1285 | { | 1329 | { |
1286 | fwrite(buf + (entry->block_offset << 9), entry->size, 1, f); | 1330 | fwrite(buf + (entry->block_offset << 9), entry->size, 1, f); |
1287 | fclose(f); | 1331 | fclose(f); |
1332 | cprintf(RED, "Ok\n"); | ||
1288 | } | 1333 | } |
1334 | else | ||
1335 | cprintf(RED, "Failed: %m\n"); | ||
1289 | } | 1336 | } |
1290 | } | 1337 | } |
1291 | 1338 | ||
@@ -1315,6 +1362,7 @@ static void usage(void) | |||
1315 | printf(" --fw\tUnpack a FW archive file\n"); | 1362 | printf(" --fw\tUnpack a FW archive file\n"); |
1316 | printf("The default is to try to guess the format.\n"); | 1363 | printf("The default is to try to guess the format.\n"); |
1317 | printf("If several formats are specified, all are tried.\n"); | 1364 | printf("If several formats are specified, all are tried.\n"); |
1365 | printf("If no output prefix is specified, a default one is picked.\n"); | ||
1318 | exit(1); | 1366 | exit(1); |
1319 | } | 1367 | } |
1320 | 1368 | ||
@@ -1380,7 +1428,8 @@ int main(int argc, char **argv) | |||
1380 | return 1; | 1428 | return 1; |
1381 | } | 1429 | } |
1382 | 1430 | ||
1383 | FILE *fin = fopen(argv[optind], "r"); | 1431 | g_in_file = argv[optind]; |
1432 | FILE *fin = fopen(g_in_file, "r"); | ||
1384 | if(fin == NULL) | 1433 | if(fin == NULL) |
1385 | { | 1434 | { |
1386 | perror("Cannot open boot file"); | 1435 | perror("Cannot open boot file"); |