From 7e60e9d14c946c1059313df55d671b79591b03d8 Mon Sep 17 00:00:00 2001 From: zvecr Date: Wed, 29 Sep 2021 21:09:41 +0100 Subject: [PATCH 1/4] Bundle applet-mdflash.bin into exe --- Makefile | 4 +- mdloader_common.c | 122 +++++++++++++++++++--------------------------- 2 files changed, 50 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index cbffc63..40a3460 100644 --- a/Makefile +++ b/Makefile @@ -14,13 +14,11 @@ OBJFILES = $(patsubst %.c,%.o,$(SRCFILES)) OBJS = $(addprefix $(OBJDIR)/,$(OBJFILES)) all: $(OBJDIR)/$(OUTNAME) - $(info Copying applets to ${OBJDIR}...) - @cp applet-*.bin $(OBJDIR) $(info Done!) $(OBJDIR)/$(OUTNAME): $(OBJS) $(info Creating $@...) - @$(CC) $(CFLAGS) $(OBJS) -o $@ + @$(CC) $(CFLAGS) $(OBJS) -o $@ -Wl,--format=binary -Wl,applet-mdflash.bin -Wl,--format=default @rm -f $(OBJDIR)/*.o $(OBJS): | $(OBJDIR) diff --git a/mdloader_common.c b/mdloader_common.c index 4321de2..d7aec82 100644 --- a/mdloader_common.c +++ b/mdloader_common.c @@ -752,94 +752,70 @@ int main(int argc, char *argv[]) if (verbose) printf("Device ID: %08X\n", mcu->cidr); //Load applet - FILE *fIn; - char appletfname[128] = ""; - strlower(mcu->name); + extern uint8_t data[] asm("_binary_applet_mdflash_bin_start"); + extern uint8_t data_end[] asm("_binary_applet_mdflash_bin_end"); + size_t filebytes = (size_t)(data_end - data); - //sprintf(appletfname, "applet-flash-%s.bin", mcu->name); - sprintf(appletfname, "applet-mdflash.bin"); //make filename non-dependent upon mcu->name - - printf("Applet file: %s\n", appletfname); - - fIn = fopen(appletfname, "rb"); - if (!fIn) + char *appletbuf = (char *)calloc(filebytes,1); + if (appletbuf == NULL) { - printf("Error: Could not open applet file: %s\n", appletfname); + printf("Error: Could not allocate memory for applet file!\n"); goto closePort; } - else - { - char *appletbuf; - int filebytes; - int readbytes; - - filebytes = filesize(appletfname); - if (filebytes == 0) - { - printf("Error: Applet file is empty!\n"); - fclose(fIn); - goto closePort; - } - - appletbuf = (char *)calloc(filebytes,1); - if (appletbuf == NULL) - { - printf("Error: Could not allocate memory for applet file!\n"); - fclose(fIn); - goto closePort; - } - readbytes = (int)fread(appletbuf, 1, filebytes, fIn); - fclose(fIn); - - if (readbytes != filebytes) - { - printf("Error: Applet read error!\n"); - goto closePort; - } + int readbytes=0; + for (uint8_t *byte=data; byte Date: Thu, 30 Sep 2021 01:09:47 +0100 Subject: [PATCH 2/4] incbin --- Makefile | 2 +- incbin/.travis.yml | 12 ++ incbin/README.md | 167 +++++++++++++++++ incbin/UNLICENSE | 24 +++ incbin/incbin.c | 291 +++++++++++++++++++++++++++++ incbin/incbin.h | 368 +++++++++++++++++++++++++++++++++++++ incbin/test/.gitignore | 1 + incbin/test/Makefile | 5 + incbin/test/asserts.c | 20 ++ incbin/test/loremipsum.txt | 1 + incbin/test/onebyte.txt | 1 + incbin/test/sevenbytes.txt | 1 + mdloader_common.c | 16 +- 13 files changed, 900 insertions(+), 9 deletions(-) create mode 100644 incbin/.travis.yml create mode 100644 incbin/README.md create mode 100644 incbin/UNLICENSE create mode 100644 incbin/incbin.c create mode 100644 incbin/incbin.h create mode 100644 incbin/test/.gitignore create mode 100644 incbin/test/Makefile create mode 100644 incbin/test/asserts.c create mode 100644 incbin/test/loremipsum.txt create mode 100644 incbin/test/onebyte.txt create mode 100644 incbin/test/sevenbytes.txt diff --git a/Makefile b/Makefile index 40a3460..599496b 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ all: $(OBJDIR)/$(OUTNAME) $(OBJDIR)/$(OUTNAME): $(OBJS) $(info Creating $@...) - @$(CC) $(CFLAGS) $(OBJS) -o $@ -Wl,--format=binary -Wl,applet-mdflash.bin -Wl,--format=default + @$(CC) $(CFLAGS) $(OBJS) -o $@ @rm -f $(OBJDIR)/*.o $(OBJS): | $(OBJDIR) diff --git a/incbin/.travis.yml b/incbin/.travis.yml new file mode 100644 index 0000000..4337da6 --- /dev/null +++ b/incbin/.travis.yml @@ -0,0 +1,12 @@ +language: c + +os: +- linux +- osx + +compiler: +- gcc +- clang + +script: +- make -C test test diff --git a/incbin/README.md b/incbin/README.md new file mode 100644 index 0000000..cada41f --- /dev/null +++ b/incbin/README.md @@ -0,0 +1,167 @@ +# incbin + +Include binary files in your C/C++ applications with ease + +## Example + +``` c + #include "incbin.h" + + INCBIN(Icon, "icon.png"); + + // This translation unit now has three symbols + // const unsigned char gIconData[]; + // const unsigned char *const gIconEnd; // a marker to the end, take the address to get the ending pointer + // const unsigned int gIconSize; + + // Reference in other translation units like this + INCBIN_EXTERN(Icon); + + // This translation unit now has three extern symbols + // Use `extern "C"` in case of writing C++ code + // extern const unsigned char gIconData[]; + // extern const unsigned char *const gIconEnd; // a marker to the end, take the address to get the ending pointer + // extern const unsigned int gIconSize; +``` + +## Portability + +Known to work on the following compilers + +* GCC +* Clang +* PathScale +* Intel +* Solaris & Sun Studio +* Green Hills +* SNC (ProDG) +* Diab C++ (WindRiver) +* XCode +* ArmCC +* RealView +* ImageCraft +* Stratus VOS C +* TinyCC +* cparser & libfirm +* LCC +* MSVC _See MSVC below_ + +If your compiler is not listed, as long as it supports GCC inline assembler, this +should work. + +## MISRA +INCBIN can be used in MISRA C setting. However it should be independently checked +due to its use of inline assembly to achieve what it does. Independent verification +of the header has been done several times based on commit: 7e327a28ba5467c4202ec37874beca7084e4b08c + +## Alignment + +The data included by this tool will be aligned on the architectures word boundary +unless some variant of SIMD is detected, then it's aligned on a byte boundary that +respects SIMD convention just incase your binary data may be used in vectorized +code. The table of the alignments for SIMD this header recognizes is as follows: + +| SIMD | Alignment | +|-----------------------------------------|-----------| +| SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2 | 16 | +| Neon | 16 | +| AVX, AVX2 | 32 | +| AVX512 | 64 | + +## Prefix +By default, `incbin.h` emits symbols with a `g` prefix. This can be adjusted by +defining `INCBIN_PREFIX` before including `incbin.h` with a desired prefix. For +instance + +``` c + #define INCBIN_PREFIX g_ + #include "incbin.h" + INCBIN(test, "test.txt"); + + // This translation unit now has three symbols + // const unsigned char g_testData[]; + // const unsigned char *const g_testEnd; + // const unsigned int g_testSize; +``` + +You can also choose to have no prefix by defining the prefix with nothing, for example: + +``` c + #define INCBIN_PREFIX + #include "incbin.h" + INCBIN(test, "test.txt"); + + // This translation unit now has three symbols + // const unsigned char testData[]; + // const unsigned char *const testEnd; + // const unsigned int testSize; +``` + +## Style +By default, `incbin.h` emits symbols with `CamelCase` style. This can be adjusted +by defining `INCBIN_STYLE` before including `incbin.h` to change the style. There +are two possible styles to choose from + +* INCBIN_STYLE_CAMEL (CamelCase) +* INCBIN_STYLE_SNAKE (snake_case) + +For instance: + +``` c + #define INCBIN_STYLE INCBIN_STYLE_SNAKE + #include "incbin.h" + INCBIN(test, "test.txt"); + + // This translation unit now has three symbols + // const unsigned char gtest_data[]; + // const unsigned char *const gtest_end; + // const unsigned int gtest_size; +``` + +Combining both the style and prefix allows for you to adjust `incbin.h` to suite +your existing style and practices. + +## Overriding Linker Output section +By default, `incbin.h` emits into the read-only linker output section used on +the detected platform. If you need to override this for whatever reason, you +can manually specify the linker output section. + +For example, to emit data into program memory for +[esp8266/Arduino](github.com/esp8266/Arduino): + +``` c +#define INCBIN_OUTPUT_SECTION ".irom.text" +#include "incbin.h" +INCBIN(Foo, "foo.txt"); +// Data is emitted into program memory that never gets copied to RAM +``` + +## Explanation + +`INCBIN` is a macro which uses the inline assembler provided by almost all +compilers to include binary files. It achieves this by utilizing the `.incbin` +directive of the inline assembler. It then uses the assembler to calculate the +size of the included binary and exports two global symbols that can be externally +referenced in other translation units which contain the data and size of the +included binary data respectively. + +## MSVC + +Supporting MSVC is slightly harder as MSVC lacks an inline assembler which can +include data. To support this we ship a tool which can process source files +containing `INCBIN` macro usage and generate an external source file containing +the data of all of them combined. This file is named `data.c` by default. +Just include it into your build and use the `incbin.h` to reference data as +needed. It's suggested you integrate this tool as part of your projects's +pre-build events so that this can be automated. A more comprehensive list of +options for this tool can be viewed by invoking the tool with `-help` + +If you're using a custom prefix, be sure to specify the prefix on the command +line with `-p ` so that everything matches up; similarly, if you're +using a custom style, be sure to specify the style on the command line with +`-S