portapack-mayhem/firmware/tools/make_image_chunk.py

76 lines
1.9 KiB
Python
Raw Normal View History

2022-10-29 18:04:07 -04:00
#!/usr/bin/env python3
#
# Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
#
# This file is part of PortaPack.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import sys
import struct
usage_message = """
PortaPack image chunk writer
2023-03-21 16:27:32 -04:00
Usage: <command> <input_binary> <four-characer tag> <output_tagged_binary>
"""
def read_image(path):
f = open(path, 'rb')
data = f.read()
f.close()
return data
def write_image(data, path):
f = open(path, 'wb')
f.write(data)
f.close()
2023-03-21 14:18:38 -04:00
if len(sys.argv) == 4:
input_image = read_image(sys.argv[1])
2023-03-21 16:27:32 -04:00
input_image = bytearray(input_image)
2016-07-02 21:15:39 -04:00
tag = tuple(map(ord, sys.argv[2]))
output_path = sys.argv[3]
2023-03-21 14:18:38 -04:00
2023-03-21 16:27:32 -04:00
if input_image[4] & 8 == 8:
input_image = input_image[15:]
2023-03-21 14:18:38 -04:00
else:
2023-03-21 16:27:32 -04:00
input_image = input_image[7:]
if (len(input_image) & 3) != 0:
for i in range(4 - (len(input_image) & 3)):
input_image.append(0)
2023-03-21 14:18:38 -04:00
2023-03-21 16:31:59 -04:00
input_image += struct.pack('<4BI', tag[0], tag[1], tag[2], tag[3], len(input_image) - 4)
write_image(input_image, output_path)
2023-03-21 14:18:38 -04:00
elif len(sys.argv) == 2:
input_image = bytearray()
2016-07-02 21:15:39 -04:00
tag = (0, 0, 0, 0)
output_path = sys.argv[1]
2023-03-21 16:31:59 -04:00
input_image += struct.pack('<4BI', tag[0], tag[1], tag[2], tag[3], 0)
write_image(input_image, output_path)
2023-03-21 16:27:32 -04:00
else:
print(usage_message)
sys.exit(-1)
if len(tag) != 4:
print(usage_message)
sys.exit(-2)