summaryrefslogtreecommitdiffstats
path: root/utils/elf2ape/main.cpp
diff options
context:
space:
mode:
authorEvan Lojewski <github@meklort.com>2020-09-25 18:54:04 -0600
committerGitHub <noreply@github.com>2020-09-25 18:54:04 -0600
commitf9459621dc74c3ff14407da65a1b77940cf6ec59 (patch)
tree2ac8207d619955d593ef302ca56e18f8c36d8baf /utils/elf2ape/main.cpp
parent038fef47787312dc7c043d754aad62f08db55b6c (diff)
downloadbcm5719-ortega-f9459621dc74c3ff14407da65a1b77940cf6ec59.tar.gz
bcm5719-ortega-f9459621dc74c3ff14407da65a1b77940cf6ec59.zip
ape: Fix ape on-disk format to match NVM endianness. (#115)
This closes GH-113
Diffstat (limited to 'utils/elf2ape/main.cpp')
-rw-r--r--utils/elf2ape/main.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/utils/elf2ape/main.cpp b/utils/elf2ape/main.cpp
index 829b040..127b1ab 100644
--- a/utils/elf2ape/main.cpp
+++ b/utils/elf2ape/main.cpp
@@ -104,6 +104,37 @@ uint64_t get_symbol_value(const char *search, elfio &reader)
bool save_to_file(const char *filename, void *buffer, size_t size)
{
+ union caster
+ {
+ void *pv;
+ uint32_t *p32;
+ } caster;
+ caster.pv = buffer;
+ uint32_t *words = caster.p32;
+
+ if (size % sizeof(uint32_t))
+ {
+ printf("Unexpected output size - must be a multiple of %lu\n", sizeof(uint32_t));
+ }
+
+ if (words[0] == htobe32(APE_HEADER_MAGIC))
+ {
+ // Expected BE image format
+ }
+ else if (words[0] == APE_HEADER_MAGIC)
+ {
+ // Image is stored BE in the non-volatile memeory. Swap it.
+ for (size_t i = 0; i < (size / sizeof(uint32_t)); i++)
+ {
+ words[i] = htobe32(words[i]);
+ }
+ }
+ else
+ {
+ printf("Unknown header 0x%08X\n", words[0]);
+ return false;
+ }
+
cout << "Writing to " << filename << "." << endl;
FILE *out = fopen(filename, "w+");
if (out)
@@ -294,7 +325,10 @@ int main(int argc, char const *argv[])
ape.header.crc = calculated_crc;
printf("Calculated CRC: 0x%08X\n", calculated_crc);
- save_to_file(options["output"].c_str(), ape.bytes, byteOffset);
+ if (!save_to_file(options["output"].c_str(), ape.bytes, byteOffset))
+ {
+ exit(-1);
+ }
// fstream infile;
// infile.open(options["input"], fstream::in | fstream::binary);
OpenPOWER on IntegriCloud