summaryrefslogtreecommitdiff
path: root/common_modules/module_manager/utilities/module_binary_to_c_array.c
blob: e418fbc1343dcf7cd92d44a7f39aa4e18999181c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

// Portions of this file were generated with AI assistance.

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


/* Define the file handles.  */

FILE	*source_file;
FILE    *array_file;


int main(int argc, char* argv[])
{

int             alpha;
int             alpha1;
int             alpha2;
int             alpha3;
unsigned long   address;
unsigned long   column;


    /* Determine if the proper number of files are provided.  */
    if (argc != 3)
    {

        /* Print an error message out and wait for user key hit.  */
		printf("module_binary_to_c_array\n");
		printf("(c) 2024 Microsoft Corp\n");
		printf("(c) 2026 Eclipse ThreadX contributors\n");
		printf("v6.5.2.202603\n");
        printf("**** Error: invalid input parameter for module_binary_to_c_array **** \n");
        printf("     Command Line Should be:\n\n");
        printf("     > module_binary_to_c_array source_binary_file c_array_file <cr> \n\n");
        return(1);
    }

    /* Attempt to open the source file for reading.  */
    source_file =  fopen(argv[1], "rb");

    /* Determine if the source file was opened properly.  */
    if (source_file == NULL)
    {

        /* Print an error message out and wait for user key hit.  */
        printf("**** Error: open failed on binary source file **** \n");
        printf("            File: %s   ", argv[1]);
        return(2);
    }

	/* Determine if the binary file is a valid ThreadX module.  */
	alpha =   fgetc(source_file);
	alpha1 =  fgetc(source_file);
	alpha2 =  fgetc(source_file);
	alpha3 =  fgetc(source_file);

	if ((alpha != 0x4D && alpha != 0x55) || (alpha1 != 0x4F && alpha1 != 0x44) || (alpha2 != 0x44 && alpha2 != 0x4F) || (alpha3 != 0x55 && alpha3 != 0x4D))
	{

        /* Print an error message out and wait for user key hit.  */
        printf("**** Error: invalid format of binary input file **** \n");
        printf("            File: %s   ", argv[1]);

        /* Close the source file.  */
        fclose(source_file);

        return(3);
	}

	/* Attempt to open the dump file for writing.  */
    array_file =  fopen(argv[2], "w");

    /* Determine if the dump file was opened properly.  */
    if (array_file == NULL)
    {

        /* Print an error message out and wait for user key hit.  */
        printf("**** Error: open failed on C array file **** \n");
        printf("            File: %s   ", argv[2]);
        return(4);
    }

    fprintf(array_file, "/**************************** Module-Binary-to-C-array Utility **********************************/\n");
	fprintf(array_file, "/*                                                                                              */\n");
	fprintf(array_file, "/* Copyright (c) 2024 Microsoft Corp                                                            */\n");
	fprintf(array_file, "/* Copyright (c) 2026 Eclipse ThreadX contributors                                              */\n");
	fprintf(array_file, "/* v6.5.2.202603                                                                                */\n");
	fprintf(array_file, "/*                                                                                              */\n");
	fprintf(array_file, "/************************************************************************************************/\n\n");
    fprintf(array_file, "/* \n");
	fprintf(array_file, "   Input Binary file:   %30s\n", argv[1]);
	fprintf(array_file, "   Output C Array file: %30s\n", argv[2]);
	fprintf(array_file, "*/\n\n");

	/* Now print out the sections in a C array.  */
	fprintf(array_file, "unsigned char  module_code[] = {\n\n");
	fprintf(array_file, "/* Address                                            Contents                                        */\n\n");

	/* Seek to the beginning of the source file. */
    fseek(source_file, 0, SEEK_SET);

	/* Initialize the variables.  */
	address =  0;
	column =   0;

	do
	{

		/* Get character from the input file.  */
		alpha =   fgetc(source_file);

		/* Have we reached EOF?  */
		if (alpha == EOF)
			break;

		/* Print out character with a leading comma, except on the first character.  */
		if (column == 0)
		{
			if (address != 0)
				fprintf(array_file, ",\n");
			fprintf(array_file, "/* 0x%08lX */   0x%02X", address, (unsigned int) alpha);
		}
		else
			fprintf(array_file, ", 0x%02X", (unsigned int) alpha);

		/* Move column forward.  */
		column++;

		/* Are we at the end of the column?  */
		if (column >= 16)
		{

			column =  0;
    	}

		/* Move address forward.  */
		address++;
	} while (alpha != EOF);

	/* Finally, finish the C array containing the module code.  */
	fprintf(array_file, "};\n\n");

	/* Close files.  */
	fclose(source_file);
    fclose(array_file);

	return 0;
}