Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tranfer binary from payolads to code #62

Open
bdn76 opened this issue Apr 2, 2024 · 2 comments
Open

How to tranfer binary from payolads to code #62

bdn76 opened this issue Apr 2, 2024 · 2 comments

Comments

@bdn76
Copy link

bdn76 commented Apr 2, 2024

Hi!

I'm need to make some change in payloads for r528_t113 SPI. I'm make binary but how to remake corresponding C file in chips dir (r528_t113.c)? I'm talk about this section:

static int chip_spi_init(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen)
{
        static const uint8_t payload[] = {
                0x00, 0x00, 0xa0, 0xe3, 0x17, 0x0f, 0x08, 0xee, 0x15, 0x0f, 0x07, 0xee,
                0xd5, 0x0f, 0x07, 0xee, 0x9a, 0x0f, 0x07, 0xee, 0x95, 0x0f, 0x07, 0xee,
                0xff, 0xff, 0xff, 0xea, 0x5c, 0x00, 0x9f, 0xe5, 0x00, 0xd0, 0x80, 0xe5,
...

Tnx

@YuzukiTsuru
Copy link
Collaborator

Simple bin2array.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BYTES_PER_LINE 16// Number of bytes to display per line in the output C array

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Usage: %s input_file output_file\n", argv[0]);// Print usage information if the number of command-line arguments is not 3
        return 1;
    }

    FILE *inputFile, *outputFile;
    char *inputFileName = argv[1]; // Name of the input binary file
    char *outputFileName = argv[2];// Name of the output C array file
    char *funcName = argv[3];// Name of the output C array
    int fileSize;

    // Open the input binary file
    inputFile = fopen(inputFileName, "rb");
    if (inputFile == NULL) {
        printf("Unable to open input file\n");// Print an error message if the input file cannot be opened
        return 1;
    }

    // Get the size of the file
    fseek(inputFile, 0, SEEK_END);
    fileSize = ftell(inputFile);
    fseek(inputFile, 0, SEEK_SET);

    // Create the output C array file
    outputFile = fopen(outputFileName, "w");
    if (outputFile == NULL) {
        printf("Unable to create output file\n");// Print an error message if the output file cannot be created
        fclose(inputFile);
        return 1;
    }

    // Write the C array declaration to the output file
    fprintf(outputFile, "const unsigned char __attribute__((section(\".%s\"))) %s[%d] = {\n\t", funcName, funcName, fileSize);

    // Read the binary file byte by byte and write to the C array file
    for (int i = 0; i < fileSize; i++) {
        unsigned char byte;
        if (fread(&byte, 1, 1, inputFile) != 1) {
            printf("Error occurred while reading the file\n");// Print an error message if there is an error reading the file
            fclose(inputFile);
            fclose(outputFile);
            return 1;
        }
        fprintf(outputFile, "0x%02X", byte);// Write the byte value in hexadecimal format
        if (i < fileSize - 1) {
            fprintf(outputFile, ", ");// Write a comma as a separator
        }
        if ((i + 1) % BYTES_PER_LINE == 0) {
            fprintf(outputFile, "\n\t");// Move to a new line every 16 bytes
        }
    }

    fprintf(outputFile, "\n};");// Add a semicolon at the end

    // Write the C array length declaration to the output file
    fprintf(outputFile, "\n\nunsigned long long %s_length = %d;\n\t", funcName, fileSize);

    // Close the files
    fclose(inputFile);
    fclose(outputFile);

    printf("Conversion complete %s => %s\n", inputFileName, outputFileName);// Print a completion message

    return 0;
}

@aozima
Copy link
Contributor

aozima commented May 13, 2024

sudo apt-get install xxd
xxd -g 1 -i payload.bin > payload.inc
+ #include "payload.inc"
...
- static const uint8_t payload[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants