aboutsummaryrefslogtreecommitdiffstats
path: root/keyboards/tkc1800/config.h
diff options
context:
space:
mode:
authorEmīls Delle <emiilsdelle@gmail.com>2019-01-21 06:15:46 +0200
committerDrashna Jaelre <drashna@live.com>2019-01-20 20:15:46 -0800
commit58993d3cde0d52addb4a503daf1ded36b26b8abc (patch)
treebba89638b7245ff70d300c82497e60ef202abd1e /keyboards/tkc1800/config.h
parentb0d308eea1a66f9daaee523882c6d050cd88f1c5 (diff)
downloadfirmware-58993d3cde0d52addb4a503daf1ded36b26b8abc.tar.gz
firmware-58993d3cde0d52addb4a503daf1ded36b26b8abc.tar.bz2
firmware-58993d3cde0d52addb4a503daf1ded36b26b8abc.zip
[Keyboard] Tmo50 initial commit (#4891)
* Create ISO HHKB keymapping for GH60 * Add media controls to Fn layer * Use M(x) instead of F(x), add Copyright text * Create initial firmware for TMO50 * Correct year and author * Fix repos README * Remove unnecessary code * Change include guard in tmo50.h
Diffstat (limited to 'keyboards/tkc1800/config.h')
0 files changed, 0 insertions, 0 deletions
5'>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
#!/usr/bin/env python3

"""
CFE Partition Tag

{
	u32 part_id;
	u32 part_size;
	u16 flags;
	char part_name[33];
	char part_version[21];
	u32 part_crc32;
}

"""

import argparse
import os
import struct
import binascii


PART_NAME_SIZE = 33
PART_VERSION_SIZE = 21


def auto_int(x):
    return int(x, 0)


def str_to_bytes_pad(string, size):
    str_bytes = string.encode()
    num_bytes = len(str_bytes)
    if num_bytes >= size:
        str_bytes = str_bytes[: size - 1] + "\0".encode()
    else:
        str_bytes += "\0".encode() * (size - num_bytes)
    return str_bytes


def create_tag(args, in_bytes, size):
    # JAM CRC32 is bitwise not and unsigned
    crc = ~binascii.crc32(in_bytes) & 0xFFFFFFFF

    tag = bytearray()
    tag += struct.pack(">I", args.part_id)
    tag += struct.pack(">I", size)
    tag += struct.pack(">H", args.part_flags)
    tag += str_to_bytes_pad(args.part_name, PART_NAME_SIZE)
    tag += str_to_bytes_pad(args.part_version, PART_VERSION_SIZE)
    tag += struct.pack(">I", crc)

    return tag


def create_output(args):
    in_st = os.stat(args.input_file)
    in_size = in_st.st_size

    in_f = open(args.input_file, "r+b")
    in_bytes = in_f.read(in_size)
    in_f.close()

    tag = create_tag(args, in_bytes, in_size)

    out_f = open(args.output_file, "w+b")
    out_f.write(tag)
    out_f.close()


def main():
    global args

    parser = argparse.ArgumentParser(description="")

    parser.add_argument(
        "--flags",
        dest="part_flags",
        action="store",
        type=auto_int,
        help="Partition Flags",
    )

    parser.add_argument(
        "--id",
        dest="part_id",
        action="store",
        type=auto_int,
        help="Partition ID",
    )

    parser.add_argument(
        "--input-file",
        dest="input_file",
        action="store",
        type=str,
        help="Input file",
    )

    parser.add_argument(
        "--output-file",
        dest="output_file",
        action="store",
        type=str,
        help="Output file",
    )

    parser.add_argument(
        "--name",
        dest="part_name",
        action="store",
        type=str,
        help="Partition Name",
    )

    parser.add_argument(
        "--version",
        dest="part_version",
        action="store",
        type=str,
        help="Partition Version",
    )

    args = parser.parse_args()

    if (
        (not args.part_flags)
        or (not args.part_id)
        or (not args.input_file)
        or (not args.output_file)
        or (not args.part_name)
        or (not args.part_version)
    ):
        parser.print_help()
    else:
        create_output(args)


main()