xboot / xfel

Tiny FEL tools for allwinner SOC, support RISC-V D1 chip

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to tranfer binary from payolads to code

bdn76 opened this issue · comments

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

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;
}
sudo apt-get install xxd
xxd -g 1 -i payload.bin > payload.inc
+ #include "payload.inc"
...
- static const uint8_t payload[]