1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2012 Amaury Pouly
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
28 static bool g_debug
= false;
29 static char *g_out_prefix
= NULL
;
31 static void usage(void)
33 printf("Usage: fwcrypt [options] content\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");
45 static int s_write(void *user
, int offset
, void *buf
, int size
)
48 if(fseek(f
, offset
, SEEK_SET
) != 0)
50 return fwrite(buf
, 1, size
, f
);
53 static void s_printf(void *user
, bool error
, const char *fmt
, ...)
55 if(!g_debug
&& !error
)
64 int main(int argc
, char **argv
)
66 struct samsung_firmware_t
*fw
= malloc(sizeof(struct samsung_firmware_t
));
67 memset(fw
, 0, sizeof(struct samsung_firmware_t
));
74 static struct option long_options
[] =
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'},
85 int c
= getopt_long(argc
, argv
, "?do:m:v:r:e:", long_options
, NULL
);
99 g_out_prefix
= optarg
;
102 strncpy(fw
->model
, optarg
, sizeof(fw
->model
));
103 if(strlen(optarg
) > sizeof(fw
->model
))
104 printf("Warning: truncate model string\n");
107 strncpy(fw
->region
, optarg
, sizeof(fw
->region
));
108 if(strlen(optarg
) > sizeof(fw
->region
))
109 printf("Warning: truncate region string\n");
112 strncpy(fw
->version
, optarg
, sizeof(fw
->version
));
113 if(strlen(optarg
) > sizeof(fw
->version
))
114 printf("Warning: truncate vesion string\n");
117 strncpy(fw
->extra
, optarg
, sizeof(fw
->extra
));
118 if(strlen(optarg
) > sizeof(fw
->extra
))
119 printf("Warning: truncate extra string\n");
126 if(optind
!= argc
- 1)
129 FILE *fin
= fopen(argv
[optind
], "rb");
132 printf("Cannot open file for reading: %m\n");
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
)
142 printf("Cannot read input file: %m\n");
150 FILE *f
= fopen(g_out_prefix
, "wb");
153 printf("Cannot open file for writing: %m\n");
158 enum samsung_error_t err
= samsung_write(s_write
, s_printf
, f
, fw
);
159 if(err
!= SAMSUNG_SUCCESS
)
161 printf("Error writing firmware: %d\n", err
);