Initial GXFP5130 userspace prototype
This commit is contained in:
225
tools/analyze_capture.c
Normal file
225
tools/analyze_capture.c
Normal file
@@ -0,0 +1,225 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static uint16_t get_le16(const uint8_t *p)
|
||||
{
|
||||
return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
|
||||
}
|
||||
|
||||
static uint32_t get_le32(const uint8_t *p)
|
||||
{
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
|
||||
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
static uint32_t get_be32(const uint8_t *p)
|
||||
{
|
||||
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
|
||||
((uint32_t)p[2] << 8) | (uint32_t)p[3];
|
||||
}
|
||||
|
||||
static uint32_t get_goodix_image_crc(const uint8_t *p)
|
||||
{
|
||||
return ((uint32_t)p[2] << 24) | ((uint32_t)p[3] << 16) |
|
||||
((uint32_t)p[0] << 8) | (uint32_t)p[1];
|
||||
}
|
||||
|
||||
static uint32_t crc32_mpeg2(const uint8_t *data, size_t length)
|
||||
{
|
||||
uint32_t crc = UINT32_MAX;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
unsigned int bit;
|
||||
|
||||
crc ^= (uint32_t)data[i] << 24;
|
||||
for (bit = 0; bit < 8; bit++)
|
||||
crc = (crc & UINT32_C(0x80000000))
|
||||
? (crc << 1) ^ UINT32_C(0x04c11db7)
|
||||
: crc << 1;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
static void analyze_crc_candidate(const char *name, const uint8_t *data,
|
||||
size_t size, size_t offset, size_t span)
|
||||
{
|
||||
const uint8_t *stored;
|
||||
uint32_t calculated;
|
||||
uint32_t be;
|
||||
uint32_t le;
|
||||
uint32_t goodix;
|
||||
|
||||
printf("CRC candidate %s: offset=%zu length=%zu", name, offset, span);
|
||||
if (span < 4u || offset > size || span > size - offset) {
|
||||
printf(" OUT_OF_RANGE\n");
|
||||
return;
|
||||
}
|
||||
|
||||
stored = data + offset + span - 4u;
|
||||
calculated = crc32_mpeg2(data + offset, span - 4u);
|
||||
be = get_be32(stored);
|
||||
le = get_le32(stored);
|
||||
goodix = get_goodix_image_crc(stored);
|
||||
|
||||
printf(" data=%zu raw=%02x%02x%02x%02x calc=%08x",
|
||||
span - 4u, stored[0], stored[1], stored[2], stored[3],
|
||||
calculated);
|
||||
if (calculated == be)
|
||||
printf(" MATCH_BE");
|
||||
if (calculated == le)
|
||||
printf(" MATCH_LE");
|
||||
if (calculated == goodix)
|
||||
printf(" MATCH_GOODIX_2301");
|
||||
if (calculated != be && calculated != le && calculated != goodix)
|
||||
printf(" NO_MATCH");
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
static void dump_edge(const char *label, const uint8_t *data, size_t length)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
printf("%s (%zu bytes):", label, length);
|
||||
for (i = 0; i < length; i++) {
|
||||
if ((i % 16) == 0)
|
||||
printf("\n ");
|
||||
printf("%02x%c", data[i], (i % 16) == 15 ? '\n' : ' ');
|
||||
}
|
||||
if ((length % 16) != 0)
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
static int read_file(const char *path, uint8_t **out, size_t *out_length)
|
||||
{
|
||||
FILE *file;
|
||||
long end;
|
||||
uint8_t *data;
|
||||
|
||||
file = fopen(path, "rb");
|
||||
if (!file)
|
||||
return -errno;
|
||||
if (fseek(file, 0, SEEK_END) != 0 || (end = ftell(file)) < 0 ||
|
||||
fseek(file, 0, SEEK_SET) != 0) {
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
data = malloc(end > 0 ? (size_t)end : 1u);
|
||||
if (!data) {
|
||||
fclose(file);
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (end > 0 && fread(data, 1, (size_t)end, file) != (size_t)end) {
|
||||
free(data);
|
||||
fclose(file);
|
||||
return -EIO;
|
||||
}
|
||||
if (fclose(file) != 0) {
|
||||
free(data);
|
||||
return -EIO;
|
||||
}
|
||||
*out = data;
|
||||
*out_length = (size_t)end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *path = argc > 1 ? argv[1] : "capture_plaintext.bin";
|
||||
uint8_t *data = NULL;
|
||||
size_t size = 0;
|
||||
uint16_t declared;
|
||||
size_t expected_total;
|
||||
size_t edge;
|
||||
int ret;
|
||||
|
||||
ret = read_file(path, &data, &size);
|
||||
if (ret) {
|
||||
fprintf(stderr, "%s: %s\n", path, strerror(-ret));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (size < 3) {
|
||||
fprintf(stderr, "%s: file is shorter than the 3-byte header\n", path);
|
||||
free(data);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
declared = get_le16(data + 1);
|
||||
expected_total = 3u + (size_t)declared;
|
||||
printf("file: %s\n", path);
|
||||
printf("command: 0x%02x\n", data[0]);
|
||||
printf("declared_length: %u (0x%04x)\n", declared, declared);
|
||||
printf("file_size: %zu\n", size);
|
||||
printf("expected_total: %zu\n", expected_total);
|
||||
printf("length_match: %s\n", size == expected_total ? "yes" : "no");
|
||||
|
||||
edge = size < 32u ? size : 32u;
|
||||
dump_edge("first", data, edge);
|
||||
dump_edge("last", data + size - edge, edge);
|
||||
|
||||
printf("controlled CRC scope candidates:\n");
|
||||
analyze_crc_candidate("A", data, size, 3u, 7685u);
|
||||
analyze_crc_candidate("B", data, size, 8u, 7680u);
|
||||
analyze_crc_candidate("C", data, size, 3u, 7686u);
|
||||
analyze_crc_candidate("D", data, size, 8u, 7684u);
|
||||
|
||||
if (declared >= 6u && expected_total <= size) {
|
||||
const uint8_t *frame_payload = data + 3u;
|
||||
size_t frame_payload_length = declared;
|
||||
const uint8_t *image_data = frame_payload + 5u;
|
||||
size_t image_data_length = frame_payload_length - 6u;
|
||||
const uint8_t *frame_trailer = frame_payload +
|
||||
frame_payload_length - 1u;
|
||||
uint8_t sum = 0;
|
||||
size_t i;
|
||||
|
||||
printf("frame_payload_offset: 3\n");
|
||||
printf("frame_payload_length: %zu\n", frame_payload_length);
|
||||
printf("image_data_offset: 8\n");
|
||||
printf("image_data_length (DLL length - 6): %zu\n",
|
||||
image_data_length);
|
||||
printf("frame_trailer: 0x%02x\n", *frame_trailer);
|
||||
|
||||
for (i = 0; i < expected_total - 1u; i++)
|
||||
sum = (uint8_t)(sum + data[i]);
|
||||
printf("checksum_candidate target_AA: 0x%02x\n",
|
||||
(uint8_t)(UINT8_C(0xaa) - sum));
|
||||
printf("checksum_candidate target_00: 0x%02x\n",
|
||||
(uint8_t)(0u - sum));
|
||||
printf("sum_with_trailer_mod256: 0x%02x\n",
|
||||
(uint8_t)(sum + *frame_trailer));
|
||||
|
||||
if (image_data_length >= 4u) {
|
||||
const uint8_t *crc_bytes = image_data +
|
||||
image_data_length - 4u;
|
||||
uint32_t calculated = crc32_mpeg2(
|
||||
image_data, image_data_length - 4u);
|
||||
|
||||
printf("image_bytes_before_crc: %zu\n",
|
||||
image_data_length - 4u);
|
||||
printf("image_crc_raw: %02x %02x %02x %02x\n",
|
||||
crc_bytes[0], crc_bytes[1],
|
||||
crc_bytes[2], crc_bytes[3]);
|
||||
printf("crc32_mpeg2_calculated: 0x%08x\n", calculated);
|
||||
printf("crc_candidate_be: 0x%08x%s\n",
|
||||
get_be32(crc_bytes),
|
||||
calculated == get_be32(crc_bytes) ? " MATCH" : "");
|
||||
printf("crc_candidate_le: 0x%08x%s\n",
|
||||
get_le32(crc_bytes),
|
||||
calculated == get_le32(crc_bytes) ? " MATCH" : "");
|
||||
printf("crc_candidate_goodix_2301: 0x%08x%s\n",
|
||||
get_goodix_image_crc(crc_bytes),
|
||||
calculated == get_goodix_image_crc(crc_bytes)
|
||||
? " MATCH" : "");
|
||||
}
|
||||
} else {
|
||||
printf("frame analysis skipped: declared length is incomplete\n");
|
||||
}
|
||||
|
||||
free(data);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user