Initial GXFP5130 userspace prototype

This commit is contained in:
2026-08-03 11:43:42 +03:00
commit 088d8ef75c
30 changed files with 5318 additions and 0 deletions

225
tools/analyze_capture.c Normal file
View 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;
}

222
tools/capture_to_pgm.c Normal file
View File

@@ -0,0 +1,222 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Experimental offline decoder only.
* The 12-bit unpack and transpose candidates are adapted from Metrohan's
* gxfp5130-linux decoder. They are not part of the capture/library path and
* are not yet verified for this 0x2504 ChicagoHU device.
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IMAGE_OFFSET 8u
#define PACKED_LENGTH 7680u
#define CRC_LENGTH 4u
#define SAMPLE_COUNT 5120u
#define RAW_WIDTH 64u
#define RAW_HEIGHT 80u
#define TRANSPOSED_WIDTH 80u
#define TRANSPOSED_HEIGHT 64u
#define SAMPLE_MAX 4095u
static uint16_t get_le16(const uint8_t *p)
{
return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
}
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 uint32_t get_goodix_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 int read_file(const char *path, uint8_t **out, size_t *out_length)
{
FILE *file = fopen(path, "rb");
long end;
uint8_t *data;
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;
}
static void unpack_four(const uint8_t *p, uint16_t out[4])
{
out[0] = (uint16_t)(((p[0] & 0x0fu) << 8) | p[1]);
out[1] = (uint16_t)((p[3] << 4) | (p[0] >> 4));
out[2] = (uint16_t)(((p[5] & 0x0fu) << 8) | p[2]);
out[3] = (uint16_t)((p[4] << 4) | (p[5] >> 4));
}
static int write_pgm(const char *path, const uint16_t *pixels,
size_t width, size_t height, int inverted)
{
FILE *file = fopen(path, "wb");
size_t i;
if (!file)
return -errno;
if (fprintf(file, "P5\n%zu %zu\n%u\n",
width, height, SAMPLE_MAX) < 0) {
fclose(file);
return -EIO;
}
for (i = 0; i < width * height; i++) {
uint16_t value = inverted
? (uint16_t)(SAMPLE_MAX - pixels[i])
: pixels[i];
uint8_t encoded[2] = {
(uint8_t)(value >> 8), (uint8_t)value
};
if (fwrite(encoded, 1, sizeof(encoded), file) !=
sizeof(encoded)) {
fclose(file);
return -EIO;
}
}
return fclose(file) == 0 ? 0 : -EIO;
}
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "capture_plaintext.bin";
uint8_t *frame = NULL;
size_t frame_length = 0;
uint16_t *raw = NULL;
uint16_t *transposed = NULL;
uint16_t declared;
const uint8_t *packed;
const uint8_t *crc_bytes;
uint32_t calculated_crc;
uint32_t stored_crc;
size_t offset;
size_t sample = 0;
int ret;
ret = read_file(path, &frame, &frame_length);
if (ret) {
fprintf(stderr, "%s: %s\n", path, strerror(-ret));
return EXIT_FAILURE;
}
if (frame_length < 3u) {
fprintf(stderr, "input is shorter than the frame header\n");
ret = -EBADMSG;
goto out;
}
declared = get_le16(frame + 1);
if (frame[0] != 0x20u || frame_length != 3u + declared ||
frame_length < IMAGE_OFFSET + PACKED_LENGTH + CRC_LENGTH + 1u) {
fprintf(stderr,
"unexpected frame: cmd=0x%02x declared=%u size=%zu\n",
frame[0], declared, frame_length);
ret = -EBADMSG;
goto out;
}
packed = frame + IMAGE_OFFSET;
crc_bytes = packed + PACKED_LENGTH;
calculated_crc = crc32_mpeg2(packed, PACKED_LENGTH);
stored_crc = get_goodix_crc(crc_bytes);
if (calculated_crc != stored_crc) {
fprintf(stderr,
"image CRC mismatch: calculated=%08x stored=%08x\n",
calculated_crc, stored_crc);
ret = -EBADMSG;
goto out;
}
raw = calloc(SAMPLE_COUNT, sizeof(*raw));
transposed = calloc(SAMPLE_COUNT, sizeof(*transposed));
if (!raw || !transposed) {
ret = -ENOMEM;
goto out;
}
for (offset = 0; offset < PACKED_LENGTH; offset += 6u) {
uint16_t values[4];
size_t i;
unpack_four(packed + offset, values);
for (i = 0; i < 4u; i++, sample++) {
size_t destination;
raw[sample] = values[i];
destination = (sample % RAW_WIDTH) *
TRANSPOSED_WIDTH +
sample / RAW_WIDTH;
transposed[destination] = values[i];
}
}
ret = write_pgm("candidate_raw.pgm", raw,
RAW_WIDTH, RAW_HEIGHT, 0);
if (!ret)
ret = write_pgm("candidate_transposed.pgm", transposed,
TRANSPOSED_WIDTH, TRANSPOSED_HEIGHT, 0);
if (!ret)
ret = write_pgm("candidate_inverted.pgm", raw,
RAW_WIDTH, RAW_HEIGHT, 1);
if (!ret)
ret = write_pgm("candidate_transposed_inverted.pgm",
transposed, TRANSPOSED_WIDTH,
TRANSPOSED_HEIGHT, 1);
if (ret) {
fprintf(stderr, "PGM write failed: %s\n", strerror(-ret));
goto out;
}
printf("Input CRC valid: 0x%08x\n", calculated_crc);
printf("Experimental outputs written:\n");
printf(" candidate_raw.pgm (64x80)\n");
printf(" candidate_transposed.pgm (80x64)\n");
printf(" candidate_inverted.pgm (64x80)\n");
printf(" candidate_transposed_inverted.pgm (80x64)\n");
out:
free(transposed);
free(raw);
free(frame);
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
}