Skip to content

Commit

Permalink
Happify Schmiddy
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ndrk committed Feb 5, 2023
1 parent c975dc6 commit 48a06c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
34 changes: 17 additions & 17 deletions mkopn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

def main():
if len(sys.argv) != 3:
print(f'Usage: {sys.argv[0]} COMPRESSED_ROOT_IMAGE_FILE OPN_OUTPUT_FILE')
print(f"Usage: {sys.argv[0]} COMPRESSED_ROOT_IMAGE_FILE OPN_OUTPUT_FILE")
sys.exit(1)

installer_file = pathlib.Path(__file__).parent / 'installer.sh'
installer_file = pathlib.Path(__file__).parent / "installer.sh"
root_image_file = pathlib.Path(sys.argv[1])
opn_file = pathlib.Path(sys.argv[2])

Expand All @@ -24,31 +24,31 @@ def main():
root_image_padding_size = padded_root_image_file_size - root_image_file_size
root_image_offset = header_size + padded_installer_file_size

with opn_file.open(mode='w+b', buffering=0) as opn:
with opn_file.open(mode="w+b", buffering=0) as opn:
# write empty header (is populated later)
opn.write(b'\x00' * 4096)
opn.write(b"\x00" * 4096)

# write installer
with installer_file.open(mode='rb') as installer:
with installer_file.open(mode="rb") as installer:
shutil.copyfileobj(installer, opn)

# write installer padding
opn.write(b'\n' * installer_padding_size)
opn.write(b"\n" * installer_padding_size)

# write root image
with root_image_file.open(mode='rb') as root_image:
with root_image_file.open(mode="rb") as root_image:
shutil.copyfileobj(root_image, opn)

# write root image padding
opn.write(b'\x00' * root_image_padding_size)
opn.write(b"\x00" * root_image_padding_size)

# magic prefix
opn.seek(0)
opn.write(b'ALDIMAGE')
opn.write(b"ALDIMAGE")

# installer size
opn.seek(96)
opn.write(padded_installer_file_size.to_bytes(8, byteorder='big'))
opn.write(padded_installer_file_size.to_bytes(8, byteorder="big"))

# installer checksum
installer_checksum = hashlib.sha256()
Expand All @@ -60,11 +60,11 @@ def main():

# version e.g. 2.8.5.11: 00 02 00 08 00 05 00 0B
opn.seek(192)
opn.write(b'\x00\x02\x00\x08\x00\x05\x00\x0B')
opn.write(b"\x00\x02\x00\x08\x00\x05\x00\x0B")

# root image offset
opn.seek(136)
opn.write(root_image_offset.to_bytes(8, byteorder='big'))
opn.write(root_image_offset.to_bytes(8, byteorder="big"))

# header checksum
opn.seek(56)
Expand All @@ -73,6 +73,6 @@ def main():
opn.write(header_checksum.digest())

# print checksums
print(f'Installer checksum: {installer_checksum.hexdigest()}')
print(f'Header checksum: {header_checksum.hexdigest()}')
print(f'Root image offset: {root_image_offset}')
print(f"Installer checksum: {installer_checksum.hexdigest()}")
print(f"Header checksum: {header_checksum.hexdigest()}")
print(f"Root image offset: {root_image_offset}")
32 changes: 19 additions & 13 deletions mkopn/installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ stty -cread
set -eux

# sanity check
[ ${1} = /dev/mmcblk0p3 ] || exit 1
[ ${2} = /dev/mmcblk0p4 ] || exit 1
[ "${1}" = /dev/mmcblk0p3 ] || exit 1
[ "${2}" = /dev/mmcblk0p4 ] || exit 1

IMAGE_FILE=${3}
IMAGE_FILE="${3}"

# write root
ROOT_IMAGE_OFFSET=$(printf "%d" "0x$(hexdump -s136 -n8 -v -e "8/1 \"%02x\"" ${IMAGE_FILE})")
dd if=${IMAGE_FILE} bs=1024 skip=$((${ROOT_IMAGE_OFFSET} / 1024)) | gunzip -c | dd of=/dev/mmcblk0p3
ROOT_IMAGE_OFFSET=$(printf "%d" "0x$(hexdump -s136 -n8 -v -e "8/1 \"%02x\"" "${IMAGE_FILE}")")
dd if="${IMAGE_FILE}" bs=1024 skip=$((ROOT_IMAGE_OFFSET / 1024)) | gunzip -c | dd of=/dev/mmcblk0p3
sync

# recreate partitions
(
echo p # print the partition table
echo d # delete /dev/mmcblk0p4 partition
# print the partition table
echo p
# delete /dev/mmcblk0p4 partition
echo d
echo 4
echo d # delete /dev/mmcblk0p3 partition
# delete /dev/mmcblk0p3 partition
echo d
echo 3
echo n # add new /dev/mmcblk0p3 partition
# add new /dev/mmcblk0p3 partition
echo n
echo 3
echo
echo +27G
echo n # add new /dev/mmcblk0p4 partition
# add new /dev/mmcblk0p4 partition
echo n
echo 4
echo
echo
echo p # print the partition table
# print the partition table
echo p
echo w
) | fdisk /dev/mmcblk0
sfdisk --part-uuid /dev/mmcblk0 3 42424242-1120-1120-1120-424242424242
Expand All @@ -47,11 +53,11 @@ chown 1001:1001 /data/.image
umount /data

# disable image
[ -b ${IMAGE_FILE} ] || (rm -f ${IMAGE_FILE} && sync)
[ -b "${IMAGE_FILE}" ] || (rm -f "${IMAGE_FILE}" && sync)

# reboot
MOUNTED_FILE_SYSTEMS=$(mount | grep '^/dev' | cut -d' ' -f3)
[ ! -z "${MOUNTED_FILE_SYSTEMS}" ] && (umount -fr ${MOUNTED_FILE_SYSTEMS} && sync && sleep 2)
[ -n "${MOUNTED_FILE_SYSTEMS}" ] && (umount -fr "${MOUNTED_FILE_SYSTEMS}" && sync && sleep 2)
chest-ctl --reset
halt -f

Expand Down

0 comments on commit 48a06c3

Please sign in to comment.