blob: 151a309a1b891b255f89000cf64cb3b088a360ff [file] [log] [blame]
Nolen Johnson802fd002019-08-07 23:56:48 -04001#include <stdio.h>
2#include <sys/mman.h>
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <string.h>
6
7#include "loki.h"
8
9#define BOOT_PATTERN1 "\x4f\xf4\x70\x40\xb3\x49\x2d\xe9" /* Samsung GS4 */
10#define BOOT_PATTERN2 "\x2d\xe9\xf0\x4f\xad\xf5\x82\x5d" /* LG */
11#define BOOT_PATTERN3 "\x2d\xe9\xf0\x4f\x4f\xf4\x70\x40" /* LG */
12#define BOOT_PATTERN4 "\x2d\xe9\xf0\x4f\xad\xf5\x80\x5d" /* LG G2 */
13
14int loki_find(const char* aboot_image)
15{
16 int aboot_fd;
17 struct stat st;
18 void *aboot, *ptr;
19 unsigned long aboot_base, check_sigs, boot_mmc;
20
21 aboot_fd = open(aboot_image, O_RDONLY);
22 if (aboot_fd < 0) {
23 printf("[-] Failed to open %s for reading.\n", aboot_image);
24 return 1;
25 }
26
27 if (fstat(aboot_fd, &st)) {
28 printf("[-] fstat() failed.\n");
29 return 1;
30 }
31
32 aboot = mmap(0, (st.st_size + 0xfff) & ~0xfff, PROT_READ, MAP_PRIVATE, aboot_fd, 0);
33 if (aboot == MAP_FAILED) {
34 printf("[-] Failed to mmap aboot.\n");
35 return 1;
36 }
37
38 check_sigs = 0;
39 aboot_base = *(unsigned int *)(aboot + 12) - 0x28;
40
41 /* Do a pass to find signature checking function */
42 for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
43 if (!memcmp(ptr, PATTERN1, 8) ||
44 !memcmp(ptr, PATTERN2, 8) ||
45 !memcmp(ptr, PATTERN3, 8) ||
46 !memcmp(ptr, PATTERN4, 8) ||
47 !memcmp(ptr, PATTERN5, 8)) {
48
49 check_sigs = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
50 break;
51 }
52
53 if (!memcmp(ptr, PATTERN6, 8)) {
54
55 check_sigs = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
56
57 /* Don't break, because the other LG patterns override this one */
58 continue;
59 }
60 }
61
62 if (!check_sigs) {
63 printf("[-] Could not find signature checking function.\n");
64 return 1;
65 }
66
67 printf("[+] Signature check function: %.08lx\n", check_sigs);
68
69 boot_mmc = 0;
70
71 /* Do a second pass for the boot_linux_from_emmc function */
72 for (ptr = aboot; ptr < aboot + st.st_size - 0x1000; ptr++) {
73 if (!memcmp(ptr, BOOT_PATTERN1, 8) ||
74 !memcmp(ptr, BOOT_PATTERN2, 8) ||
75 !memcmp(ptr, BOOT_PATTERN3, 8) ||
76 !memcmp(ptr, BOOT_PATTERN4, 8)) {
77
78 boot_mmc = (unsigned long)ptr - (unsigned long)aboot + aboot_base;
79 break;
80 }
81 }
82
83 if (!boot_mmc) {
84 printf("[-] Could not find boot_linux_from_mmc.\n");
85 return 1;
86 }
87
88 printf("[+] boot_linux_from_mmc: %.08lx\n", boot_mmc);
89
90 return 0;
91}