summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Cerqueira <cyanogenmod@cerqueira.org>2014-06-18 18:28:43 +0100
committerRicardo Cerqueira <cyanogenmod@cerqueira.org>2014-07-02 02:10:29 +0100
commit4cd8756689d5c9394d5f476c005c02082a26d293 (patch)
treedeb0c857d817367339d4aafac1ff7ae83eed7b90
parentcaa0e79d9842276d2e1016041c5b7754bb0708c6 (diff)
update loki
We're going to need the unlok functionality Change-Id: I8f6cfaaf365b47d6fd79ac44468771529066df0f
-rw-r--r--device.mk3
-rw-r--r--releasetools/Android.mk21
-rw-r--r--releasetools/LICENSE.txt25
-rw-r--r--releasetools/loki.h88
-rw-r--r--releasetools/loki.sh7
-rw-r--r--releasetools/loki_find.c91
-rw-r--r--releasetools/loki_flash.c86
-rw-r--r--releasetools/loki_patch.c293
-rw-r--r--releasetools/loki_unlok.c152
-rw-r--r--releasetools/main.c56
10 files changed, 624 insertions, 198 deletions
diff --git a/device.mk b/device.mk
index 9f8d2ec..cd182f3 100644
--- a/device.mk
+++ b/device.mk
@@ -181,8 +181,7 @@ PRODUCT_PROPERTY_OVERRIDES += \
# This hw ships locked, work around it with loki
PRODUCT_PACKAGES += \
loki.sh \
- loki_patch \
- loki_flash
+ loki_tool
PRODUCT_PROPERTY_OVERRIDES += \
ro.radio.noril=true \
diff --git a/releasetools/Android.mk b/releasetools/Android.mk
index 8169a61..46e43fc 100644
--- a/releasetools/Android.mk
+++ b/releasetools/Android.mk
@@ -18,21 +18,14 @@ ifneq ($(filter v500 ,$(TARGET_DEVICE)),)
LOCAL_PATH := $(call my-dir)
+# build static binary
include $(CLEAR_VARS)
-LOCAL_MODULE := loki_patch
-LOCAL_MODULE_CLASS := EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_OUT)/bin
-LOCAL_SRC_FILES := loki_patch.c
-LOCAL_STATIC_LIBRARIES += libc
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := loki_flash
-LOCAL_MODULE_CLASS := EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_OUT)/bin
-LOCAL_SRC_FILES := loki_flash.c
-LOCAL_STATIC_LIBRARIES += libc
+LOCAL_SRC_FILES := loki_flash.c loki_patch.c loki_find.c loki_unlok.c main.c
+LOCAL_MODULE := loki_tool_static
+LOCAL_MODULE_STEM := loki_tool
+LOCAL_MODULE_TAGS := eng
+# LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+LOCAL_STATIC_LIBRARIES := libc
LOCAL_FORCE_STATIC_EXECUTABLE := true
include $(BUILD_EXECUTABLE)
diff --git a/releasetools/LICENSE.txt b/releasetools/LICENSE.txt
new file mode 100644
index 0000000..bc2a914
--- /dev/null
+++ b/releasetools/LICENSE.txt
@@ -0,0 +1,25 @@
+Copyright (c) 2013 Dan Rosenberg. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INFRAE OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/releasetools/loki.h b/releasetools/loki.h
new file mode 100644
index 0000000..c1d6b3e
--- /dev/null
+++ b/releasetools/loki.h
@@ -0,0 +1,88 @@
+#ifndef __LOKI_H_
+#define __LOKI_H_
+
+#define VERSION "2.1"
+
+#define BOOT_MAGIC_SIZE 8
+#define BOOT_NAME_SIZE 16
+#define BOOT_ARGS_SIZE 512
+
+#define BOOT_PARTITION "/dev/block/platform/msm_sdcc.1/by-name/boot"
+#define RECOVERY_PARTITION "/dev/block/platform/msm_sdcc.1/by-name/recovery"
+#define ABOOT_PARTITION "/dev/block/platform/msm_sdcc.1/by-name/aboot"
+
+#define PATTERN1 "\xf0\xb5\x8f\xb0\x06\x46\xf0\xf7"
+#define PATTERN2 "\xf0\xb5\x8f\xb0\x07\x46\xf0\xf7"
+#define PATTERN3 "\x2d\xe9\xf0\x41\x86\xb0\xf1\xf7"
+#define PATTERN4 "\x2d\xe9\xf0\x4f\xad\xf5\xc6\x6d"
+#define PATTERN5 "\x2d\xe9\xf0\x4f\xad\xf5\x21\x7d"
+#define PATTERN6 "\x2d\xe9\xf0\x4f\xf3\xb0\x05\x46"
+
+#define ABOOT_BASE_SAMSUNG 0x88dfffd8
+#define ABOOT_BASE_LG 0x88efffd8
+#define ABOOT_BASE_G2 0xf7fffd8
+#define ABOOT_BASE_VIPER 0x40100000
+
+struct boot_img_hdr {
+ unsigned char magic[BOOT_MAGIC_SIZE];
+ unsigned kernel_size; /* size in bytes */
+ unsigned kernel_addr; /* physical load addr */
+ unsigned ramdisk_size; /* size in bytes */
+ unsigned ramdisk_addr; /* physical load addr */
+ unsigned second_size; /* size in bytes */
+ unsigned second_addr; /* physical load addr */
+ unsigned tags_addr; /* physical addr for kernel tags */
+ unsigned page_size; /* flash page size we assume */
+ unsigned dt_size; /* device_tree in bytes */
+ unsigned unused; /* future expansion: should be 0 */
+ unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
+ unsigned char cmdline[BOOT_ARGS_SIZE];
+ unsigned id[8]; /* timestamp / checksum / sha1 / etc */
+};
+
+struct loki_hdr {
+ unsigned char magic[4]; /* 0x494b4f4c */
+ unsigned int recovery; /* 0 = boot.img, 1 = recovery.img */
+ char build[128]; /* Build number */
+
+ unsigned int orig_kernel_size;
+ unsigned int orig_ramdisk_size;
+ unsigned int ramdisk_addr;
+};
+
+int loki_patch(const char* partition_label, const char* aboot_image, const char* in_image, const char* out_image);
+int loki_flash(const char* partition_label, const char* loki_image);
+int loki_find(const char* aboot_image);
+int loki_unlok(const char* in_image, const char* out_image);
+
+#define PATCH "\xfe\xb5" \
+ "\x0d\x4d" \
+ "\xd5\xf8" \
+ "\x88\x04" \
+ "\xab\x68" \
+ "\x98\x42" \
+ "\x12\xd0" \
+ "\xd5\xf8" \
+ "\x90\x64" \
+ "\x0a\x4c" \
+ "\xd5\xf8" \
+ "\x8c\x74" \
+ "\x07\xf5\x80\x57" \
+ "\x0f\xce" \
+ "\x0f\xc4" \
+ "\x10\x3f" \
+ "\xfb\xdc" \
+ "\xd5\xf8" \
+ "\x88\x04" \
+ "\x04\x49" \
+ "\xd5\xf8" \
+ "\x8c\x24" \
+ "\xa8\x60" \
+ "\x69\x61" \
+ "\x2a\x61" \
+ "\x00\x20" \
+ "\xfe\xbd" \
+ "\xff\xff\xff\xff" \
+ "\xee\xee\xee\xee"
+
+#endif //__LOKI_H_
diff --git a/releasetools/loki.sh b/releasetools/loki.sh
index b85c746..17a96f5 100644
--- a/releasetools/loki.sh
+++ b/releasetools/loki.sh
@@ -1,7 +1,6 @@
#!/sbin/sh
#
-# This leverages the loki_patch utility created by djrbliss which allows us
-# to bypass the bootloader checks on jfltevzw and jflteatt
+# This leverages the loki_patch utility created by djrbliss
# See here for more information on loki: https://github.com/djrbliss/loki
#
@@ -12,7 +11,7 @@ export C=/tmp/loki_tmpdir
mkdir -p $C
dd if=/dev/block/platform/msm_sdcc.1/by-name/aboot of=$C/aboot.img
-/system/bin/loki_patch boot $C/aboot.img /tmp/boot.img $C/boot.lok || exit 1
-/system/bin/loki_flash boot $C/boot.lok || exit 1
+/system/bin/loki_tool patch boot $C/aboot.img /tmp/boot.img $C/boot.lok || exit 1
+/system/bin/loki_tool flash boot $C/boot.lok || exit 1
rm -rf $C
exit 0
diff --git a/releasetools/loki_find.c b/releasetools/loki_find.c
new file mode 100644
index 0000000..151a309
--- /dev/null
+++ b/releasetools/loki_find.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+
+#include "loki.h"
+
+#define BOOT_PATTERN1 "\x4f\xf4\x70\x40\xb3\x49\x2d\xe9" /* Samsung GS4 */
+#define BOOT_PATTERN2 "\x2d\xe9\xf0\x4f\xad\xf5\x82\x5d" /* LG */
+#define BOOT_PATTERN3 "\x2d\xe9\xf0\x4f\x4f\xf4\x70\x40" /* LG */
+#define BOOT_PATTERN4 "\x2d\xe9\xf0\x4f\xad\xf5\x80\x5d" /* LG G2 */
+
+int loki_find(const char* aboot_image)
+{
+ int aboot_fd;
+ struct stat st;
+ void *aboot, *ptr;
+ unsigned long aboot_base, check_sigs, boot_mmc;
+
+ aboot_fd = open(aboot_image, O_RDONLY);
+ if (aboot_fd < 0) {
+ printf("[-] Failed to open %s for reading.\n", aboot_image);
+ return 1;
+ }
+
+ if (fstat(aboot_fd, &st)) {
+ printf("[-] fstat() failed.\n");
+ return 1;
+ }
+
+ aboot = mmap(0, (st.st_size + 0xfff) & ~0xfff, PROT_READ, MAP_PRIVATE, aboot_fd, 0);
+ if (aboot == MAP_FAILED) {
+ printf("[-] Failed to mmap aboot.\n");
+ return 1;
+ }
+
+ check_sigs = 0;
+ aboot_base = *(unsigned int *)(aboot + 12) - 0x28;
+
+ /* Do a pass to find signature checking function */
+ for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
+ if (!memcmp(ptr, PATTERN1, 8) ||
+ !memcmp(ptr, PATTERN2, 8) ||
+ !memcmp(ptr, PATTERN3, 8) ||
+ !memcmp(ptr, PATTERN4, 8) ||
+ !memcmp(ptr, PATTERN5, 8)) {
+
+ check_sigs = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
+ break;
+ }
+
+ if (!memcmp(ptr, PATTERN6, 8)) {
+
+ check_sigs = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
+
+ /* Don't break, because the other LG patterns override this one */
+ continue;
+ }
+ }
+
+ if (!check_sigs) {
+ printf("[-] Could not find signature checking function.\n");
+ return 1;
+ }
+
+ printf("[+] Signature check function: %.08lx\n", check_sigs);
+
+ boot_mmc = 0;
+
+ /* Do a second pass for the boot_linux_from_emmc function */
+ for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
+ if (!memcmp(ptr, BOOT_PATTERN1, 8) ||
+ !memcmp(ptr, BOOT_PATTERN2, 8) ||
+ !memcmp(ptr, BOOT_PATTERN3, 8) ||
+ !memcmp(ptr, BOOT_PATTERN4, 8)) {
+
+ boot_mmc = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
+ break;
+ }
+ }
+
+ if (!boot_mmc) {
+ printf("[-] Could not find boot_linux_from_mmc.\n");
+ return 1;
+ }
+
+ printf("[+] boot_linux_from_mmc: %.08lx\n", boot_mmc);
+
+ return 0;
+}
diff --git a/releasetools/loki_flash.c b/releasetools/loki_flash.c
index 6576910..16bdd4d 100644
--- a/releasetools/loki_flash.c
+++ b/releasetools/loki_flash.c
@@ -6,6 +6,7 @@
* by Dan Rosenberg (@djrbliss)
*
*/
+
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
@@ -13,71 +14,20 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include "loki.h"
-#define VERSION "2.0"
-
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-
-struct boot_img_hdr {
- unsigned char magic[BOOT_MAGIC_SIZE];
- unsigned kernel_size; /* size in bytes */
- unsigned kernel_addr; /* physical load addr */
- unsigned ramdisk_size; /* size in bytes */
- unsigned ramdisk_addr; /* physical load addr */
- unsigned second_size; /* size in bytes */
- unsigned second_addr; /* physical load addr */
- unsigned tags_addr; /* physical addr for kernel tags */
- unsigned page_size; /* flash page size we assume */
- unsigned dt_size; /* device_tree in bytes */
- unsigned unused; /* future expansion: should be 0 */
- unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
- unsigned char cmdline[BOOT_ARGS_SIZE];
- unsigned id[8]; /* timestamp / checksum / sha1 / etc */
-};
-
-struct loki_hdr {
- unsigned char magic[4]; /* 0x494b4f4c */
- unsigned int recovery; /* 0 = boot.img, 1 = recovery.img */
- unsigned char build[128]; /* Build number */
-
- unsigned int orig_kernel_size;
- unsigned int orig_ramdisk_size;
- unsigned int ramdisk_addr;
-};
-
-#define PATTERN1 "\xf0\xb5\x8f\xb0\x06\x46\xf0\xf7"
-#define PATTERN2 "\xf0\xb5\x8f\xb0\x07\x46\xf0\xf7"
-#define PATTERN3 "\x2d\xe9\xf0\x41\x86\xb0\xf1\xf7"
-#define PATTERN4 "\x2d\xe9\xf0\x4f\xad\xf5\xc6\x6d"
-#define PATTERN5 "\x2d\xe9\xf0\x4f\xad\xf5\x21\x7d"
-#define PATTERN6 "\x2d\xe9\xf0\x4f\xf3\xb0\x05\x46"
-
-#define ABOOT_BASE_SAMSUNG 0x88dfffd8
-#define ABOOT_BASE_LG 0x88efffd8
-#define ABOOT_BASE_G2 0xf7fffd8
-
-int main(int argc, char **argv)
+int loki_flash(const char* partition_label, const char* loki_image)
{
-
int ifd, aboot_fd, ofd, recovery, offs, match;
void *orig, *aboot, *patch;
struct stat st;
struct boot_img_hdr *hdr;
struct loki_hdr *loki_hdr;
- char prop[256], outfile[1024], buf[4096];
-
- if (argc != 3) {
- printf("[+] Usage: %s [boot|recovery] [in.lok]\n", argv[0]);
- return 1;
- }
-
- printf("[+] loki_flash v%s\n", VERSION);
+ char outfile[1024];
- if (!strcmp(argv[1], "boot")) {
+ if (!strcmp(partition_label, "boot")) {
recovery = 0;
- } else if (!strcmp(argv[1], "recovery")) {
+ } else if (!strcmp(partition_label, "recovery")) {
recovery = 1;
} else {
printf("[+] First argument must be \"boot\" or \"recovery\".\n");
@@ -85,15 +35,15 @@ int main(int argc, char **argv)
}
/* Verify input file */
- aboot_fd = open("/dev/block/platform/msm_sdcc.1/by-name/aboot", O_RDONLY);
+ aboot_fd = open(ABOOT_PARTITION, O_RDONLY);
if (aboot_fd < 0) {
printf("[-] Failed to open aboot for reading.\n");
return 1;
}
- ifd = open(argv[2], O_RDONLY);
+ ifd = open(loki_image, O_RDONLY);
if (ifd < 0) {
- printf("[-] Failed to open %s for reading.\n", argv[2]);
+ printf("[-] Failed to open %s for reading.\n", loki_image);
return 1;
}
@@ -135,12 +85,16 @@ int main(int argc, char **argv)
for (offs = 0; offs < 0x10; offs += 0x4) {
- if (hdr->ramdisk_addr < ABOOT_BASE_SAMSUNG)
- patch = hdr->ramdisk_addr - ABOOT_BASE_G2 + aboot + offs;
- else if (hdr->ramdisk_addr < ABOOT_BASE_LG)
- patch = hdr->ramdisk_addr - ABOOT_BASE_SAMSUNG + aboot + offs;
- else
+ patch = NULL;
+
+ if (hdr->ramdisk_addr > ABOOT_BASE_LG)
patch = hdr->ramdisk_addr - ABOOT_BASE_LG + aboot + offs;
+ else if (hdr->ramdisk_addr > ABOOT_BASE_SAMSUNG)
+ patch = hdr->ramdisk_addr - ABOOT_BASE_SAMSUNG + aboot + offs;
+ else if (hdr->ramdisk_addr > ABOOT_BASE_VIPER)
+ patch = hdr->ramdisk_addr - ABOOT_BASE_VIPER + aboot + offs;
+ else if (hdr->ramdisk_addr > ABOOT_BASE_G2)
+ patch = hdr->ramdisk_addr - ABOOT_BASE_G2 + aboot + offs;
if (patch < aboot || patch > aboot + 0x40000 - 8) {
printf("[-] Invalid .lok file.\n");
@@ -167,8 +121,8 @@ int main(int argc, char **argv)
printf("[+] Loki validation passed, flashing image.\n");
snprintf(outfile, sizeof(outfile),
- "/dev/block/platform/msm_sdcc.1/by-name/%s",
- recovery ? "recovery" : "boot");
+ "%s",
+ recovery ? RECOVERY_PARTITION : BOOT_PARTITION);
ofd = open(outfile, O_WRONLY);
if (ofd < 0) {
diff --git a/releasetools/loki_patch.c b/releasetools/loki_patch.c
index 6fc63dd..abb9f8d 100644
--- a/releasetools/loki_patch.c
+++ b/releasetools/loki_patch.c
@@ -8,6 +8,7 @@
* by Dan Rosenberg (@djrbliss)
*
*/
+
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
@@ -15,39 +16,7 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
-
-#define VERSION "2.1"
-
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-
-struct boot_img_hdr {
- unsigned char magic[BOOT_MAGIC_SIZE];
- unsigned kernel_size; /* size in bytes */
- unsigned kernel_addr; /* physical load addr */
- unsigned ramdisk_size; /* size in bytes */
- unsigned ramdisk_addr; /* physical load addr */
- unsigned second_size; /* size in bytes */
- unsigned second_addr; /* physical load addr */
- unsigned tags_addr; /* physical addr for kernel tags */
- unsigned page_size; /* flash page size we assume */
- unsigned dt_size; /* device_tree in bytes */
- unsigned unused; /* future expansion: should be 0 */
- unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
- unsigned char cmdline[BOOT_ARGS_SIZE];
- unsigned id[8]; /* timestamp / checksum / sha1 / etc */
-};
-
-struct loki_hdr {
- unsigned char magic[4]; /* 0x494b4f4c */
- unsigned int recovery; /* 0 = boot.img, 1 = recovery.img */
- unsigned char build[128]; /* Build number */
-
- unsigned int orig_kernel_size;
- unsigned int orig_ramdisk_size;
- unsigned int ramdisk_addr;
-};
+#include "loki.h"
struct target {
char *vendor;
@@ -108,6 +77,14 @@ struct target targets[] = {
.lg = 1,
},
{
+ .vendor = "DoCoMo",
+ .device = "LG Optimus G Pro",
+ .build = "L04E10f",
+ .check_sigs = 0x88f1102c,
+ .hdr = 0x88f54418,
+ .lg = 1,
+ },
+ {
.vendor = "AT&T or HK",
.device = "LG Optimus G Pro",
.build = "E98010g or E98810b",
@@ -172,6 +149,22 @@ struct target targets[] = {
.lg = 1,
},
{
+ .vendor = "US Cellular",
+ .device = "LG Optimus F7",
+ .build = "US78011a",
+ .check_sigs = 0x88f112c8,
+ .hdr = 0x88f84518,
+ .lg = 1,
+ },
+ {
+ .vendor = "Sprint",
+ .device = "LG Optimus F7",
+ .build = "LG870ZV5_02",
+ .check_sigs = 0x88f11710,
+ .hdr = 0x88f842a8,
+ .lg = 1,
+ },
+ {
.vendor = "Virgin Mobile",
.device = "LG Optimus F3",
.build = "LS720ZV5",
@@ -204,6 +197,22 @@ struct target targets[] = {
.lg = 1,
},
{
+ .vendor = "AT&T",
+ .device = "LG G2",
+ .build = "D80010o",
+ .check_sigs = 0xf813428,
+ .hdr = 0xf904400,
+ .lg = 1,
+ },
+ {
+ .vendor = "Verizon",
+ .device = "LG G2",
+ .build = "VS98012b",
+ .check_sigs = 0xf813210,
+ .hdr = 0xf906440,
+ .lg = 1,
+ },
+ {
.vendor = "T-Mobile or Canada",
.device = "LG G2",
.build = "D80110c or D803",
@@ -252,6 +261,30 @@ struct target targets[] = {
.lg = 1,
},
{
+ .vendor = "DoCoMo",
+ .device = "LG G2",
+ .build = "L-01F",
+ .check_sigs = 0xf813538,
+ .hdr = 0xf8d41c0,
+ .lg = 1,
+ },
+ {
+ .vendor = "KT",
+ .device = "LG G Flex",
+ .build = "F340K",
+ .check_sigs = 0xf8124a4,
+ .hdr = 0xf8b6440,
+ .lg = 1,
+ },
+ {
+ .vendor = "KDDI",
+ .device = "LG G Flex",
+ .build = "LGL2310d",
+ .check_sigs = 0xf81261c,
+ .hdr = 0xf8b41c0,
+ .lg = 1,
+ },
+ {
.vendor = "International",
.device = "LG Optimus F5",
.build = "P87510e",
@@ -260,61 +293,118 @@ struct target targets[] = {
.lg = 1,
},
{
+ .vendor = "SKT",
+ .device = "LG Optimus LTE 3",
+ .build = "F260S10l",
+ .check_sigs = 0x88f11398,
+ .hdr = 0x88f8451c,
+ .lg = 1,
+ },
+ {
.vendor = "International",
- .device = "LG G Pad",
+ .device = "LG G Pad 8.3",
.build = "V50010a",
.check_sigs = 0x88f10814,
.hdr = 0x88f801b8,
.lg = 1,
},
+ {
+ .vendor = "International",
+ .device = "LG G Pad 8.3",
+ .build = "V50010c or V50010e",
+ .check_sigs = 0x88f108bc,
+ .hdr = 0x88f801b8,
+ .lg = 1,
+ },
+ {
+ .vendor = "Verizon",
+ .device = "LG G Pad 8.3",
+ .build = "VK81010c",
+ .check_sigs = 0x88f11080,
+ .hdr = 0x88fd81b8,
+ .lg = 1,
+ },
+ {
+ .vendor = "International",
+ .device = "LG Optimus L9 II",
+ .build = "D60510a",
+ .check_sigs = 0x88f10d98,
+ .hdr = 0x88f84aa4,
+ .lg = 1,
+ },
+ {
+ .vendor = "MetroPCS",
+ .device = "LG Optimus F6",
+ .build = "MS50010e",
+ .check_sigs = 0x88f10260,
+ .hdr = 0x88f70508,
+ .lg = 1,
+ },
+ {
+ .vendor = "Open EU",
+ .device = "LG Optimus F6",
+ .build = "D50510a",
+ .check_sigs = 0x88f10284,
+ .hdr = 0x88f70aa4,
+ .lg = 1,
+ },
+ {
+ .vendor = "KDDI",
+ .device = "LG Isai",
+ .build = "LGL22",
+ .check_sigs = 0xf813458,
+ .hdr = 0xf8d41c0,
+ .lg = 1,
+ },
+ {
+ .vendor = "KDDI",
+ .device = "LG",
+ .build = "LGL21",
+ .check_sigs = 0x88f10218,
+ .hdr = 0x88f50198,
+ .lg = 1,
+ },
+ {
+ .vendor = "KT",
+ .device = "LG Optimus GK",
+ .build = "F220K",
+ .check_sigs = 0x88f11034,
+ .hdr = 0x88f54418,
+ .lg = 1,
+ },
+ {
+ .vendor = "International",
+ .device = "LG Vu 3",
+ .build = "F300L",
+ .check_sigs = 0xf813170,
+ .hdr = 0xf8d2440,
+ .lg = 1,
+ },
+ {
+ .vendor = "Sprint",
+ .device = "LG Viper",
+ .build = "LS840ZVK",
+ .check_sigs = 0x4010fe18,
+ .hdr = 0x40194198,
+ .lg = 1,
+ },
+ {
+ .vendor = "International",
+ .device = "LG G Flex",
+ .build = "D95510a",
+ .check_sigs = 0xf812490,
+ .hdr = 0xf8c2440,
+ .lg = 1,
+ },
};
-#define PATTERN1 "\xf0\xb5\x8f\xb0\x06\x46\xf0\xf7"
-#define PATTERN2 "\xf0\xb5\x8f\xb0\x07\x46\xf0\xf7"
-#define PATTERN3 "\x2d\xe9\xf0\x41\x86\xb0\xf1\xf7"
-#define PATTERN4 "\x2d\xe9\xf0\x4f\xad\xf5\xc6\x6d"
-#define PATTERN5 "\x2d\xe9\xf0\x4f\xad\xf5\x21\x7d"
-#define PATTERN6 "\x2d\xe9\xf0\x4f\xf3\xb0\x05\x46"
-
-#define ABOOT_BASE_SAMSUNG 0x88dfffd8
-#define ABOOT_BASE_LG 0x88efffd8
-#define ABOOT_BASE_G2 0xf7fffd8
-
-unsigned char patch[] =
-"\xfe\xb5"
-"\x0d\x4d"
-"\xd5\xf8"
-"\x88\x04"
-"\xab\x68"
-"\x98\x42"
-"\x12\xd0"
-"\xd5\xf8"
-"\x90\x64"
-"\x0a\x4c"
-"\xd5\xf8"
-"\x8c\x74"
-"\x07\xf5\x80\x57"
-"\x0f\xce"
-"\x0f\xc4"
-"\x10\x3f"
-"\xfb\xdc"
-"\xd5\xf8"
-"\x88\x04"
-"\x04\x49"
-"\xd5\xf8"
-"\x8c\x24"
-"\xa8\x60"
-"\x69\x61"
-"\x2a\x61"
-"\x00\x20"
-"\xfe\xbd"
-"\xff\xff\xff\xff"
-"\xee\xee\xee\xee";
+static unsigned char patch[] = PATCH;
int patch_shellcode(unsigned int header, unsigned int ramdisk)
{
- int i, found_header, found_ramdisk;
+ unsigned int i;
+ int found_header, found_ramdisk;
unsigned int *ptr;
found_header = 0;
@@ -339,9 +429,8 @@ int patch_shellcode(unsigned int header, unsigned int ramdisk)
return -1;
}
-int main(int argc, char **argv)
+int loki_patch(const char* partition_label, const char* aboot_image, const char* in_image, const char* out_image)
{
-
int ifd, ofd, aboot_fd, pos, i, recovery, offset, fake_size;
unsigned int orig_ramdisk_size, orig_kernel_size, page_kernel_size, page_ramdisk_size, page_size, page_mask;
unsigned long target, aboot_base;
@@ -352,16 +441,9 @@ int main(int argc, char **argv)
struct loki_hdr *loki_hdr;
char *buf;
- if (argc != 5) {
- printf("Usage: %s [boot|recovery] [aboot.img] [in.img] [out.lok]\n", argv[0]);
- return 1;
- }
-
- printf("[+] loki_patch v%s\n", VERSION);
-
- if (!strcmp(argv[1], "boot")) {
+ if (!strcmp(partition_label, "boot")) {
recovery = 0;
- } else if (!strcmp(argv[1], "recovery")) {
+ } else if (!strcmp(partition_label, "recovery")) {
recovery = 1;
} else {
printf("[+] First argument must be \"boot\" or \"recovery\".\n");
@@ -369,21 +451,21 @@ int main(int argc, char **argv)
}
/* Open input files */
- aboot_fd = open(argv[2], O_RDONLY);
+ aboot_fd = open(aboot_image, O_RDONLY);
if (aboot_fd < 0) {
- printf("[-] Failed to open %s for reading.\n", argv[2]);
+ printf("[-] Failed to open %s for reading.\n", aboot_image);
return 1;
}
- ifd = open(argv[3], O_RDONLY);
+ ifd = open(in_image, O_RDONLY);
if (ifd < 0) {
- printf("[-] Failed to open %s for reading.\n", argv[3]);
+ printf("[-] Failed to open %s for reading.\n", in_image);
return 1;
}
- ofd = open(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ ofd = open(out_image, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (ofd < 0) {
- printf("[-] Failed to open %s for writing.\n", argv[4]);
+ printf("[-] Failed to open %s for writing.\n", out_image);
return 1;
}
@@ -400,27 +482,15 @@ int main(int argc, char **argv)
}
target = 0;
+ aboot_base = *(unsigned int *)(aboot + 12) - 0x28;
for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
if (!memcmp(ptr, PATTERN1, 8) ||
!memcmp(ptr, PATTERN2, 8) ||
- !memcmp(ptr, PATTERN3, 8)) {
-
- aboot_base = ABOOT_BASE_SAMSUNG;
- target = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
- break;
- }
-
- if (!memcmp(ptr, PATTERN4, 8)) {
-
- aboot_base = ABOOT_BASE_LG;
- target = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
- break;
- }
-
- if (!memcmp(ptr, PATTERN5, 8)) {
+ !memcmp(ptr, PATTERN3, 8) ||
+ !memcmp(ptr, PATTERN4, 8) ||
+ !memcmp(ptr, PATTERN5, 8)) {
- aboot_base = ABOOT_BASE_G2;
target = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
break;
}
@@ -434,7 +504,6 @@ int main(int argc, char **argv)
for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
if (!memcmp(ptr, PATTERN6, 8)) {
- aboot_base = ABOOT_BASE_LG;
target = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
break;
}
@@ -486,7 +555,7 @@ int main(int argc, char **argv)
return 1;
}
- printf("[+] Copied Loki image to %s.\n", argv[4]);
+ printf("[+] Copied Loki image to %s.\n", out_image);
return 0;
}
@@ -595,7 +664,7 @@ int main(int argc, char **argv)
close(ofd);
close(aboot_fd);
- printf("[+] Output file written to %s\n", argv[4]);
+ printf("[+] Output file written to %s\n", out_image);
return 0;
}
diff --git a/releasetools/loki_unlok.c b/releasetools/loki_unlok.c
new file mode 100644
index 0000000..f2e685a
--- /dev/null
+++ b/releasetools/loki_unlok.c
@@ -0,0 +1,152 @@
+/*
+ * loki_unlok
+ *
+ * A utility to revert the changes made by loki_patch.
+ *
+ * by Dan Rosenberg (@djrbliss)
+ *
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "loki.h"
+
+static unsigned char patch[] = PATCH;
+
+/* Find the original address of the ramdisk, which
+ * was embedded in the shellcode. */
+int find_ramdisk_addr(void *img, int sz)
+{
+
+ int i, ramdisk = 0;
+
+ for (i = 0; i < sz - (sizeof(patch) - 9); i++) {
+ if (!memcmp((char *)img + i, patch, sizeof(patch)-9)) {
+ ramdisk = *(int *)(img + i + sizeof(patch) - 5);
+ break;
+ }
+ }
+
+ return ramdisk;
+}
+
+int loki_unlok(const char* in_image, const char* out_image)
+{
+ int ifd, ofd;
+ unsigned int orig_ramdisk_size, orig_kernel_size, orig_ramdisk_addr;
+ unsigned int page_kernel_size, page_ramdisk_size, page_size, page_mask, fake_size;
+ void *orig;
+ struct stat st;
+ struct boot_img_hdr *hdr;
+ struct loki_hdr *loki_hdr;
+
+ ifd = open(in_image, O_RDONLY);
+ if (ifd < 0) {
+ printf("[-] Failed to open %s for reading.\n", in_image);
+ return 1;
+ }
+
+ ofd = open(out_image, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ if (ofd < 0) {
+ printf("[-] Failed to open %s for writing.\n", out_image);
+ return 1;
+ }
+
+ /* Map the original boot/recovery image */
+ if (fstat(ifd, &st)) {
+ printf("[-] fstat() failed.\n");
+ return 1;
+ }
+
+ orig = mmap(0, (st.st_size + 0x2000 + 0xfff) & ~0xfff, PROT_READ|PROT_WRITE, MAP_PRIVATE, ifd, 0);
+ if (orig == MAP_FAILED) {
+ printf("[-] Failed to mmap input file.\n");
+ return 1;
+ }
+
+ hdr = orig;
+ loki_hdr = orig + 0x400;
+
+ if (memcmp(loki_hdr->magic, "LOKI", 4)) {
+ printf("[-] Input file is not a Loki image.\n");
+
+ /* Copy the entire file to the output transparently */
+ if (write(ofd, orig, st.st_size) != st.st_size) {
+ printf("[-] Failed to copy Loki image.\n");
+ return 1;
+ }
+
+ printf("[+] Copied Loki image to %s.\n", out_image);
+
+ return 0;
+ }
+
+ page_size = hdr->page_size;
+ page_mask = hdr->page_size - 1;
+
+ /* Infer the size of the fake block based on the newer ramdisk address */
+ if (hdr->ramdisk_addr > 0x88f00000 || hdr->ramdisk_addr < 0xfa00000)
+ fake_size = page_size;
+ else
+ fake_size = 0x200;
+
+ orig_ramdisk_addr = find_ramdisk_addr(orig, st.st_size);
+ if (orig_ramdisk_addr == 0) {
+ printf("[-] Failed to find original ramdisk address.\n");
+ return 1;
+ }
+
+ /* Restore the original header values */
+ hdr->ramdisk_addr = orig_ramdisk_addr;
+ hdr->kernel_size = orig_kernel_size = loki_hdr->orig_kernel_size;
+ hdr->ramdisk_size = orig_ramdisk_size = loki_hdr->orig_ramdisk_size;
+
+ /* Erase the loki header */
+ memset(loki_hdr, 0, sizeof(*loki_hdr));
+
+ /* Write the image header */
+ if (write(ofd, orig, page_size) != page_size) {
+ printf("[-] Failed to write header to output file.\n");
+ return 1;
+ }
+
+ page_kernel_size = (orig_kernel_size + page_mask) & ~page_mask;
+
+ /* Write the kernel */
+ if (write(ofd, orig + page_size, page_kernel_size) != page_kernel_size) {
+ printf("[-] Failed to write kernel to output file.\n");
+ return 1;
+ }
+
+ page_ramdisk_size = (orig_ramdisk_size + page_mask) & ~page_mask;
+
+ /* Write the ramdisk */
+ if (write(ofd, orig + page_size + page_kernel_size, page_ramdisk_size) != page_ramdisk_size) {
+ printf("[-] Failed to write ramdisk to output file.\n");
+ return 1;
+ }
+
+ /* Write the device tree if needed */
+ if (hdr->dt_size) {
+
+ printf("[+] Writing device tree.\n");
+
+ /* Skip an additional fake_size (page_size of 0x200) bytes */
+ if (write(ofd, orig + page_size + page_kernel_size + page_ramdisk_size + fake_size, hdr->dt_size) != hdr->dt_size) {
+ printf("[-] Failed to write device tree to output file.\n");
+ return 1;
+ }
+ }
+
+ close(ifd);
+ close(ofd);
+
+ printf("[+] Output file written to %s\n", out_image);
+
+ return 0;
+}
diff --git a/releasetools/main.c b/releasetools/main.c
new file mode 100644
index 0000000..1d3d9d3
--- /dev/null
+++ b/releasetools/main.c
@@ -0,0 +1,56 @@
+/*
+ * loki_patch
+ *
+ * A utility to patch unsigned boot and recovery images to make
+ * them suitable for booting on the AT&T/Verizon Samsung
+ * Galaxy S4, Galaxy Stellar, and various locked LG devices
+ *
+ * by Dan Rosenberg (@djrbliss)
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "loki.h"
+
+static int print_help(const char* cmd) {
+ printf("Usage\n");
+ printf("> Patch partition file image:\n");
+ printf("%s [patch] [boot|recovery] [aboot.img] [in.img] [out.lok]\n", cmd);
+ printf("\n");
+ printf("> Flash loki image to boot|recovery:\n");
+ printf("%s [flash] [boot|recovery] [in.lok]\n", cmd);
+ printf("\n");
+ printf("> Find offset from aboot image:\n");
+ printf("%s [find] [aboot.img]\n", cmd);
+ printf("\n");
+ printf("> Revert Loki patching:\n");
+ printf("%s [unlok] [in.lok] [out.img]\n", cmd);
+ printf("\n");
+ return 1;
+}
+
+int main(int argc, char **argv) {
+ printf("Loki tool v%s\n", VERSION);
+
+ if (argc == 6 && strcmp(argv[1], "patch") == 0) {
+ // argv[2]: partition_label
+ // argv[3]: aboot_image
+ // argv[4]: in_image
+ // argv[5]: out_image
+ return loki_patch(argv[2], argv[3], argv[4], argv[5]);
+ } else if (argc == 4 && strcmp(argv[1], "flash") == 0) {
+ // argv[2]: partition_label
+ // argv[3]: loki_image
+ return loki_flash(argv[2], argv[3]);
+ } else if (argc == 3 && strcmp(argv[1], "find") == 0) {
+ // argv[2]: aboot_image
+ return loki_find(argv[2]);
+ } else if (argc == 4 && strcmp(argv[1], "unlok") == 0) {
+ // argv[2]: in_image
+ // argv[3]: out_image
+ return loki_unlok(argv[2], argv[3]);
+ }
+
+ return print_help(argv[0]);
+}