From 1570d3dbe3777e58bb773dacd205f215753d11e7 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 15 Jul 2021 12:39:19 -0400 Subject: [PATCH] Add c utility for reading confluent multipart images --- confluent_osdeploy/utils/confluent_imginfo.c | 70 ++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 confluent_osdeploy/utils/confluent_imginfo.c diff --git a/confluent_osdeploy/utils/confluent_imginfo.c b/confluent_osdeploy/utils/confluent_imginfo.c new file mode 100644 index 00000000..fe8f1c9a --- /dev/null +++ b/confluent_osdeploy/utils/confluent_imginfo.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +int read_part(FILE* img, long int imgsize) { + char mountpath[65537]; + char devpath[65537]; + char fstype[65537]; + uint16_t shortlength; + uint32_t length; + uint64_t longlength; + fread(&shortlength, 2, 1, img); + shortlength = be16toh(shortlength); + fread(mountpath, 1, shortlength, img); + mountpath[shortlength] = 0; + fread(&length, 4, 1, img); + length = be32toh(length); + fseek(img, length, SEEK_CUR); // skip json section that we don't support + fread(&longlength, 8, 1, img); // minimum size in bytes + longlength = be64toh(longlength); + printf("%ld\t", longlength); + fread(&longlength, 8, 1, img); // default size in bytes + longlength = be64toh(longlength); + printf("%ld\t", longlength); + fread(&shortlength, 2, 1, img); // length of filesystem type + shortlength = be16toh(shortlength); + fread(fstype, 1, shortlength, img); + fstype[shortlength] = 0; + fread(&shortlength, 2, 1, img); // length of DEVICE + shortlength = be16toh(shortlength); + fread(devpath, 1, shortlength, img); + devpath[shortlength] = 0; + fread(&shortlength, 2, 1, img); + shortlength = be16toh(shortlength); + fseek(img, shortlength, SEEK_CUR); // Skip the padding + fread(&longlength, 8, 1, img); + longlength = be64toh(longlength); + printf("%ld\t", ftell(img) / 512); + printf("%ld\t", longlength / 512); + printf("%s\t%s\t%s\n", fstype, devpath, mountpath); + fseek(img, longlength, SEEK_CUR); + return (ftell(img) < imgsize); +} + +int main(int argc, char* argv[]) { + FILE* img; + long int imgsize; + char buffer[20]; + + img = fopen(argv[1], "rb"); + fseek(img, 0, SEEK_END); + imgsize = ftell(img); + fseek(img, 0, SEEK_SET); + if (fread(buffer, 1, 16, img) < 16) { + fprintf(stderr, "Unable to read header of image\n"); + exit(1); + } + if ((memcmp(buffer, "hsqs", 4) == 0) || (memcmp(buffer, "sqsh", 4) == 0)) { + printf("Format: squashfs\n"); + exit(0); + } + if (memcmp(buffer, "\x63\x7b\x9d\x26\xb7\xfd\x48\x30\x89\xf9\x11\xcf\x18\xfd\xff\xa1", 16) != 0) { + fprintf(stderr, "Unrecognized image format\n"); + exit(1); + } + printf("Format: confluent_multisquash\nminsize\tdefsize\toffset\tsize\tfstype\torigdev\tmount\n"); + fseek(img, 31, SEEK_SET); + while (read_part(img, imgsize)); +}