summaryrefslogtreecommitdiffstats
path: root/utils/samsungtools
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2012-11-29 17:29:13 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2012-11-29 17:30:54 +0100
commitd66c16749a5c07aadcd0eb97b9340d7fa5ea26b0 (patch)
tree4c1b30ca5073cf2f6f7a227715fca557fbf39999 /utils/samsungtools
parentee36a396cd1619585a83803630db2d79b6cbefbd (diff)
downloadrockbox-d66c16749a5c07aadcd0eb97b9340d7fa5ea26b0.tar.gz
rockbox-d66c16749a5c07aadcd0eb97b9340d7fa5ea26b0.tar.bz2
rockbox-d66c16749a5c07aadcd0eb97b9340d7fa5ea26b0.zip
Introduce samsungtools to decrypt samsung firmware
Samsung provides many firmware upgrade in the format of a .dat file, at least for nearly all YP's (checked for Q2, R0, T10, Z5). This is a simple cyclic xor which a fixed key, a md5 sum and a header specifying the model/version/region. Change-Id: Ib0461a74196383189fd2d8162da444a85a229c60
Diffstat (limited to 'utils/samsungtools')
-rw-r--r--utils/samsungtools/Makefile17
-rw-r--r--utils/samsungtools/fwdecrypt.c131
-rw-r--r--utils/samsungtools/samsung.c127
-rw-r--r--utils/samsungtools/samsung.h78
4 files changed, 353 insertions, 0 deletions
diff --git a/utils/samsungtools/Makefile b/utils/samsungtools/Makefile
new file mode 100644
index 0000000000..97af8bb873
--- /dev/null
+++ b/utils/samsungtools/Makefile
@@ -0,0 +1,17 @@
+DEFINES=
+CC=gcc
+LD=gcc
+CFLAGS=-g -std=c99 -W -Wall $(DEFINES) `pkg-config --cflags openssl`
+LDFLAGS=`pkg-config --libs openssl`
+BINS=fwdecrypt
+
+all: $(BINS)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+fwdecrypt: fwdecrypt.o samsung.o
+ $(LD) -o $@ $^ $(LDFLAGS)
+
+clean:
+ rm -fr *.o $(BINS)
diff --git a/utils/samsungtools/fwdecrypt.c b/utils/samsungtools/fwdecrypt.c
new file mode 100644
index 0000000000..67dfb315c2
--- /dev/null
+++ b/utils/samsungtools/fwdecrypt.c
@@ -0,0 +1,131 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2012 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "samsung.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <getopt.h>
+
+static bool g_debug = false;
+static char *g_out_prefix = NULL;
+
+static void usage(void)
+{
+ printf("Usage: fwdecrypt [options] firmware\n");
+ printf("Options:\n");
+ printf(" -o <prefix>\tSet output file\n");
+ printf(" -f/--force\tForce to continue on errors\n");
+ printf(" -?/--help\tDisplay this message\n");
+ printf(" -d/--debug\tDisplay debug messages\n");
+ exit(1);
+}
+
+static int s_read(void *user, int offset, void *buf, int size)
+{
+ FILE *f = user;
+ if(fseek(f, offset, SEEK_SET) != 0)
+ return 0;
+ return fread(buf, 1, size, f);
+}
+
+static void s_printf(void *user, bool error, const char *fmt, ...)
+{
+ if(!g_debug && !error)
+ return;
+ (void) user;
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+}
+
+int main(int argc, char **argv)
+{
+ if(argc <= 1)
+ usage();
+
+ while(1)
+ {
+ static struct option long_options[] =
+ {
+ {"help", no_argument, 0, '?'},
+ {"debug", no_argument, 0, 'd'},
+ {0, 0, 0, 0}
+ };
+
+ int c = getopt_long(argc, argv, "?do:", long_options, NULL);
+ if(c == -1)
+ break;
+ switch(c)
+ {
+ case -1:
+ break;
+ case 'd':
+ g_debug = true;
+ break;
+ case '?':
+ usage();
+ break;
+ case 'o':
+ g_out_prefix = optarg;
+ break;
+ default:
+ abort();
+ }
+ }
+
+ if(optind != argc - 1)
+ usage();
+
+ FILE *f = fopen(argv[optind], "rb");
+ if(f == NULL)
+ {
+ printf("Cannot open file for reading: %m\n");
+ return 1;
+ }
+
+ enum samsung_error_t err;
+ struct samsung_firmware_t *fw = samsung_read(s_read, s_printf,
+ f, &err);
+ if(fw == NULL || err != SAMSUNG_SUCCESS)
+ {
+ printf("Error reading firmware: %d\n", err);
+ samsung_free(fw);
+ return 1;
+ }
+ fclose(f);
+
+ if(g_out_prefix)
+ {
+ f = fopen(g_out_prefix, "wb");
+ if(f == NULL)
+ {
+ printf("Cannot open file for writing: %m\n");
+ return 2;
+ }
+ fwrite(fw->data, 1, fw->data_size, f);
+ fclose(f);
+ }
+
+ samsung_free(fw);
+
+ return 0;
+}
diff --git a/utils/samsungtools/samsung.c b/utils/samsungtools/samsung.c
new file mode 100644
index 0000000000..decf34f208
--- /dev/null
+++ b/utils/samsungtools/samsung.c
@@ -0,0 +1,127 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2012 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "samsung.h"
+#include <openssl/md5.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
+static uint8_t g_yp_key[128] =
+{
+ 0xa3, 0x04, 0xb9, 0xcd, 0x34, 0x13, 0x4a, 0x19, 0x19, 0x35, 0xdf, 0xbb, 0x8f, 0x3d, 0x7f, 0x09,
+ 0x42, 0x3c, 0x96, 0xc6, 0x41, 0xa9, 0x95, 0xf1, 0xd0, 0xac, 0x16, 0x37, 0x57, 0x1f, 0x28, 0xe7,
+ 0x0b, 0xc2, 0x12, 0x09, 0x39, 0x42, 0xd2, 0x96, 0xf5, 0x00, 0xd2, 0x23, 0xa4, 0x24, 0xe2, 0x8e,
+ 0x50, 0x3c, 0x6e, 0x23, 0xeb, 0x68, 0xed, 0x31, 0xb7, 0xee, 0xc0, 0xc7, 0x09, 0xf8, 0x0e, 0x9d,
+ 0x51, 0xed, 0x17, 0x95, 0x64, 0x09, 0xe0, 0xf9, 0xf0, 0xef, 0x86, 0xc0, 0x04, 0x46, 0x89, 0x8a,
+ 0x6e, 0x27, 0x69, 0xde, 0xc7, 0x9d, 0x1e, 0xee, 0x3c, 0x3f, 0x17, 0x05, 0x44, 0xbb, 0xbb, 0x1d,
+ 0x3d, 0x5d, 0x6e, 0xf2, 0xcf, 0x15, 0xd6, 0x3c, 0xcc, 0x7d, 0x67, 0x1a, 0xb8, 0xd2, 0x1b, 0x54,
+ 0x97, 0xa2, 0x58, 0x58, 0xf7, 0x4e, 0x5e, 0x50, 0x42, 0x69, 0xdc, 0xe7, 0x3a, 0x87, 0x2e, 0x22
+};
+
+static void cyclic_xor(void *data, int datasize, void *xor, int xorsize)
+{
+ for(int i = 0; i < datasize; i++)
+ *(uint8_t *)(data + i) ^= *(uint8_t *)(xor + (i % xorsize));
+}
+
+struct samsung_firmware_t *samsung_read(samsung_read_t read,
+ samsung_printf_t printf, void *user, enum samsung_error_t *err)
+{
+ struct yp_header_t yp_hdr;
+ struct yp_md5_t yp_md5;
+
+ /* read and check header */
+ if(read(user, 0, &yp_hdr, sizeof(yp_hdr)) != sizeof(yp_hdr))
+ {
+ printf(user, true, "Cannot read YP header\n");
+ *err = SAMSUNG_READ_ERROR;
+ return NULL;
+ }
+ if(strncmp(yp_hdr.signature, YP_SIGNATURE, sizeof(yp_hdr.signature)) != 0)
+ {
+ printf(user, true, "Bad YP signature\n");
+ *err = SAMSUNG_FORMAT_ERROR;
+ return NULL;
+ }
+
+ printf(user, false, "Model: %s\n", yp_hdr.model);
+ printf(user, false, "Version: %s %s %s\n", yp_hdr.version, yp_hdr.region, yp_hdr.extra);
+
+ /* allocate and read data */
+ struct samsung_firmware_t *fw = malloc(sizeof(struct samsung_firmware_t));
+ memset(fw, 0, sizeof(struct samsung_firmware_t));
+ fw->data_size = yp_hdr.datasize;
+ fw->data = malloc(fw->data_size);
+ strncpy(fw->version, yp_hdr.version, sizeof(yp_hdr.version));
+ strncpy(fw->region, yp_hdr.region, sizeof(yp_hdr.region));
+ strncpy(fw->extra, yp_hdr.extra, sizeof(yp_hdr.extra));
+ strncpy(fw->model, yp_hdr.model, sizeof(yp_hdr.model));
+
+ if(read(user, sizeof(yp_hdr), fw->data, fw->data_size) != fw->data_size)
+ {
+ printf(user, true, "Cannot read data\n");
+ *err = SAMSUNG_READ_ERROR;
+ samsung_free(fw);
+ return NULL;
+ }
+
+ /* read and check MD5 */
+ if(read(user, yp_hdr.datasize + sizeof(yp_hdr), &yp_md5, sizeof(yp_md5)) != sizeof(yp_md5))
+ {
+ printf(user, true, "Cannot read MD5 sum\n");
+ *err = SAMSUNG_READ_ERROR;
+ samsung_free(fw);
+ return NULL;
+ }
+
+ uint8_t actual_md5[MD5_DIGEST_LENGTH];
+ MD5_CTX c;
+ MD5_Init(&c);
+ MD5_Update(&c, fw->data, fw->data_size);
+ MD5_Final(actual_md5, &c);
+
+ if(memcmp(actual_md5, yp_md5.md5, MD5_DIGEST_LENGTH) != 0)
+ {
+ printf(user, true, "MD5 mismatch\n");
+ *err = SAMSUNG_MD5_ERROR;
+ samsung_free(fw);
+ return NULL;
+ }
+
+ /* decrypt */
+ cyclic_xor(fw->data, fw->data_size, g_yp_key, sizeof(g_yp_key));
+
+ /* success ! */
+ *err = SAMSUNG_SUCCESS;
+ return fw;
+}
+
+void samsung_free(struct samsung_firmware_t *fw)
+{
+ if(fw)
+ {
+ free(fw->data);
+ free(fw);
+ }
+}
diff --git a/utils/samsungtools/samsung.h b/utils/samsungtools/samsung.h
new file mode 100644
index 0000000000..4336e02651
--- /dev/null
+++ b/utils/samsungtools/samsung.h
@@ -0,0 +1,78 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2012 Amaury Pouly
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#ifndef __SAMSUNG_H__
+#define __SAMSUNG_H__
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/**
+ * Low-Level
+ */
+
+struct yp_header_t
+{
+ char signature[12];
+ uint32_t pad;
+ char version[8];
+ char region[4];
+ char extra[4];
+ char model[20];
+ uint32_t datasize;
+} __attribute__((packed));
+
+struct yp_md5_t
+{
+ uint8_t md5[16];
+} __attribute__((packed));
+
+#define YP_SIGNATURE "SAMSUNG YEPP"
+
+/**
+ * API
+ */
+
+struct samsung_firmware_t
+{
+ char version[8];
+ char region[4];
+ char extra[4];
+ char model[20];
+ void *data;
+ int data_size;
+};
+
+enum samsung_error_t
+{
+ SAMSUNG_SUCCESS = 0,
+ SAMSUNG_READ_ERROR = -1,
+ SAMSUNG_FORMAT_ERROR = -2,
+ SAMSUNG_MD5_ERROR = -3,
+};
+
+typedef int (*samsung_read_t)(void *user, int offset, void *buffer, int size);
+typedef void (*samsung_printf_t)(void *user, bool error, const char *fmt, ...);
+
+struct samsung_firmware_t *samsung_read(samsung_read_t read,
+ samsung_printf_t printf, void *user, enum samsung_error_t *err);
+void samsung_free(struct samsung_firmware_t *fw);
+
+#endif /* __SAMSUNG_H__ */