86 lines
1.6 KiB
C
Raw Normal View History

2017-09-14 23:11:56 +08:00
#include <string.h>
#include "bootimg.h"
2018-01-29 03:12:35 +08:00
#include "format.h"
2017-09-14 23:11:56 +08:00
2018-02-10 03:34:13 +08:00
#define MATCH(s) (len >= (sizeof(s) - 1) && memcmp(buf, s, sizeof(s) - 1) == 0)
format_t check_fmt(const void *buf, size_t len) {
if (MATCH(CHROMEOS_MAGIC)) {
2017-09-14 23:11:56 +08:00
return CHROMEOS;
2018-02-10 03:34:13 +08:00
} else if (MATCH(BOOT_MAGIC)) {
2017-09-14 23:11:56 +08:00
return AOSP;
2018-02-10 03:34:13 +08:00
} else if (MATCH(ELF32_MAGIC)) {
2017-09-14 23:11:56 +08:00
return ELF32;
2018-02-10 03:34:13 +08:00
} else if (MATCH(ELF64_MAGIC)) {
2017-09-14 23:11:56 +08:00
return ELF64;
2018-02-10 03:34:13 +08:00
} else if (MATCH(GZIP_MAGIC)) {
2017-09-14 23:11:56 +08:00
return GZIP;
2018-02-10 03:34:13 +08:00
} else if (MATCH(LZOP_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZOP;
2018-02-10 03:34:13 +08:00
} else if (MATCH(XZ_MAGIC)) {
2017-09-14 23:11:56 +08:00
return XZ;
2018-02-10 03:34:13 +08:00
} else if (len >= 13 && memcmp(buf, "\x5d\x00\x00", 3) == 0
2017-09-14 23:11:56 +08:00
&& (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) {
return LZMA;
2018-02-10 03:34:13 +08:00
} else if (MATCH(BZIP_MAGIC)) {
2017-09-14 23:11:56 +08:00
return BZIP2;
2018-02-10 03:34:13 +08:00
} else if (MATCH(LZ4_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZ4;
2018-02-10 03:34:13 +08:00
} else if (MATCH(LZ4_LEG_MAGIC)) {
2017-09-14 23:11:56 +08:00
return LZ4_LEGACY;
2018-02-10 03:34:13 +08:00
} else if (MATCH(MTK_MAGIC)) {
2017-09-14 23:11:56 +08:00
return MTK;
2018-02-10 03:34:13 +08:00
} else if (MATCH(DTB_MAGIC)) {
2017-09-14 23:11:56 +08:00
return DTB;
2018-02-10 03:34:13 +08:00
} else if (MATCH(DHTB_MAGIC)) {
2018-01-29 22:16:02 +08:00
return DHTB;
2018-02-10 03:34:13 +08:00
} else if (MATCH(TEGRABLOB_MAGIC)) {
2018-01-30 05:20:18 +08:00
return BLOB;
2017-09-14 23:11:56 +08:00
} else {
return UNKNOWN;
}
}
2018-01-29 03:12:35 +08:00
void get_fmt_name(format_t fmt, char *name) {
2017-09-14 23:11:56 +08:00
char *s;
2018-01-29 03:12:35 +08:00
switch (fmt) {
2017-09-14 23:11:56 +08:00
case CHROMEOS:
s = "chromeos";
break;
case AOSP:
s = "aosp";
break;
case GZIP:
s = "gzip";
break;
case LZOP:
s = "lzop";
break;
case XZ:
s = "xz";
break;
case LZMA:
s = "lzma";
break;
case BZIP2:
s = "bzip2";
break;
case LZ4:
s = "lz4";
break;
case LZ4_LEGACY:
s = "lz4_legacy";
break;
case MTK:
s = "mtk";
break;
case DTB:
s = "dtb";
break;
default:
s = "raw";
}
strcpy(name, s);
}