|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 | #
|
5 | 5 |
|
6 |
| -from os.path import isdir, isfile, join |
| 6 | +from os.path import getsize, isdir, isfile, join |
7 | 7 | from shutil import copyfile
|
8 | 8 | import json
|
| 9 | +from zlib import crc32 |
9 | 10 | from platformio.util import get_systype
|
10 | 11 |
|
11 | 12 | from SCons.Script import DefaultEnvironment
|
|
57 | 58 | if (False == isfile(main_c)) and (False == isfile(main_cpp)):
|
58 | 59 | copyfile(join(FRAMEWORK_DIR, "template", "main.c"), main_c)
|
59 | 60 |
|
| 61 | + |
| 62 | +def gen_img_file(target, source, env): |
| 63 | + cmd = ["$OBJCOPY"] |
| 64 | + (target_firm, ) = target |
| 65 | + (source_elf, ) = source |
| 66 | + |
| 67 | + target_img = env.subst("$BUILD_DIR/$PROGNAME") + '.img' |
| 68 | + |
| 69 | + cmd.extend(["-O", "binary"]) |
| 70 | + cmd.append(source_elf.get_abspath()) |
| 71 | + cmd.append(target_img) |
| 72 | + env.Execute(env.VerboseAction(" ".join(cmd), " ")) |
| 73 | + |
| 74 | + # fix bin size to 0x80 boundary |
| 75 | + binsz = getsize(target_img) |
| 76 | + f_binsz = (binsz + 0x7F) & ~0x7F |
| 77 | + print("Binary size: %d" % binsz) |
| 78 | + f = open(target_img, "rb") |
| 79 | + f_bin = bytearray(f.read()) |
| 80 | + f.close() |
| 81 | + f_bin += bytes(f_binsz - binsz) |
| 82 | + # Fix header size |
| 83 | + f_bin[4:8] = f_binsz.to_bytes(4, "little") |
| 84 | + # Fix checksum |
| 85 | + f_bin[8:0xC] = crc32(f_bin).to_bytes(4, "little") |
| 86 | + # write final binary |
| 87 | + f = open(target_img, "wb") |
| 88 | + f.write(f_bin) |
| 89 | + f.close() |
| 90 | + |
60 | 91 | def gen_pac_file(target, source, env):
|
61 | 92 | (target_firm, ) = target
|
62 | 93 | (source_elf, ) = source
|
| 94 | + |
63 | 95 | # Generate image file
|
64 |
| - env.Execute( |
65 |
| - env.VerboseAction("$MKIMAGE " + source_elf.get_abspath() + ' "$BUILD_DIR/$PROGNAME"' + '.img', |
66 |
| - "Generating Firmware Image") |
67 |
| - ) |
| 96 | + if "darwin" in get_systype(): |
| 97 | + print("Generating Firmware Image") |
| 98 | + gen_img_file(target, source, env) |
| 99 | + else: |
| 100 | + env.Execute( |
| 101 | + env.VerboseAction("$MKIMAGE " + source_elf.get_abspath() + ' "$BUILD_DIR/$PROGNAME"' + '.img', |
| 102 | + "Generating Firmware Image") |
| 103 | + ) |
68 | 104 |
|
69 | 105 | # Generate pac file
|
70 | 106 | init_fdl = [
|
|
0 commit comments