Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Code cleanup #123

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ latest
*.egg-info
build
dist
/.idea
6 changes: 5 additions & 1 deletion asahi_firmware/bluetooth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: MIT
import logging, os, os.path, re, sys
import logging
import os
import os.path
import re
import sys
from collections import namedtuple, defaultdict

from .core import FWFile
Expand Down
6 changes: 5 additions & 1 deletion asahi_firmware/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SPDX-License-Identifier: MIT
import tarfile, io, logging
import io
import logging
import tarfile
from hashlib import sha256


class FWFile(object):
def __init__(self, name, data):
self.name = name
Expand All @@ -19,6 +22,7 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.sha)


class FWPackage(object):
def __init__(self, target):
self.path = target
Expand Down
1 change: 1 addition & 0 deletions asahi_firmware/img4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import asn1


def img4p_extract(data):
decoder = asn1.Decoder()
decoder.start(data)
Expand Down
12 changes: 10 additions & 2 deletions asahi_firmware/multitouch.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# SPDX-License-Identifier: MIT
import logging
import os
import plistlib
import struct
import xml.etree.ElementTree as ET
import plistlib, base64, struct, os, logging
from .img4 import img4p_extract

from .core import FWFile
from .img4 import img4p_extract

log = logging.getLogger("asahi_firmware.multitouch")


def load_plist_xml(d):
root = ET.fromstring(d.decode("ascii"))

idmap = {}

def unmunge(el, idmap):
if "ID" in el.attrib:
idmap[el.attrib["ID"]] = el
Expand All @@ -29,6 +35,7 @@ def unmunge(el, idmap):

return plistlib.loads(ET.tostring(pl))


def plist_to_bin(plist):
iface_offset = None

Expand Down Expand Up @@ -123,6 +130,7 @@ def add_padding(l):

return hdr + blob


class MultitouchFWCollection(object):
def __init__(self, source_path):
self.fwfiles = []
Expand Down
10 changes: 7 additions & 3 deletions asahi_firmware/update.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# SPDX-License-Identifier: MIT
import pathlib, tempfile, subprocess
import pathlib
import subprocess
import tempfile

from .core import FWPackage
from .wifi import WiFiFWCollection
from .bluetooth import BluetoothFWCollection
from .core import FWPackage
from .multitouch import MultitouchFWCollection
from .wifi import WiFiFWCollection


def update_firmware(source, dest, manifest):
raw_fw = source.joinpath("all_firmware.tar.gz")
Expand All @@ -30,6 +33,7 @@ def update_firmware(source, dest, manifest):

pkg.save_manifest(manifest)


if __name__ == "__main__":
import argparse
import logging
Expand Down
10 changes: 9 additions & 1 deletion asahi_firmware/wifi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# SPDX-License-Identifier: MIT
import sys, os, os.path, pprint, statistics, logging
import logging
import os
import os.path
import sys

from .core import FWFile

log = logging.getLogger("asahi_firmware.wifi")


class FWNode(object):
def __init__(self, this=None, leaves=None):
if leaves is None:
Expand All @@ -25,6 +30,7 @@ def print(self, depth=0, tag=""):
for k, v in self.leaves.items():
v.print(depth + 1, k)


class WiFiFWCollection(object):
EXTMAP = {
"trx": "bin",
Expand All @@ -33,6 +39,7 @@ class WiFiFWCollection(object):
"txcb": "txcap_blob",
}
DIMS = ["C", "s", "P", "M", "V", "m", "A"]

def __init__(self, source_path):
self.root = FWNode()
self.load(source_path)
Expand Down Expand Up @@ -138,6 +145,7 @@ def process_nvram(self, data):
def print(self):
self.root.print()


if __name__ == "__main__":
col = WiFiFWCollection(sys.argv[1])
if len(sys.argv) > 2:
Expand Down
8 changes: 7 additions & 1 deletion src/diskutil.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# SPDX-License-Identifier: MIT
import plistlib, subprocess, sys, logging
import logging
import plistlib
import subprocess
import sys
from dataclasses import dataclass


@dataclass
class Partition:
name: str
Expand All @@ -16,8 +20,10 @@ class Partition:
container: object = None
os: object = None


class DiskUtil:
FREE_THRESHOLD = 16 * 1024 * 1024

def __init__(self):
self.verbose = "-v" in sys.argv

Expand Down
20 changes: 18 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#!/usr/bin/python3
# SPDX-License-Identifier: MIT
import os, os.path, shlex, subprocess, sys, time, termios, json, getpass
import getpass
import json
import os.path
import shlex
import subprocess
import termios
import time
from dataclasses import dataclass

import system, osenum, stub, diskutil, osinstall, asahi_firmware
import asahi_firmware
import diskutil
import osenum
import osinstall
import stub
import system
from util import *

PART_ALIGN = psize("1MiB")
Expand All @@ -21,6 +32,7 @@
MIN_MACOS_VERSION = "12.3"
MIN_MACOS_VERSION_EXPERT = "12.1"


@dataclass
class IPSW:
version: str
Expand All @@ -31,11 +43,13 @@ class IPSW:
expert_only: bool
url: str


@dataclass
class Device:
min_ver: str
expert_only: bool


CHIP_MIN_VER = {
0x8103: "11.0", # T8103, M1
0x6000: "12.0", # T6000, M1 Pro
Expand Down Expand Up @@ -85,6 +99,7 @@ class Device:
"https://updates.cdn-apple.com/2022SpringFCS/fullrestores/071-08757/74A4F2A1-C747-43F9-A22A-C0AD5FB4ECB6/UniversalMac_12.3_21E230_Restore.ipsw"),
]


class InstallerMain:
def __init__(self):
self.data = json.load(open("installer_data.json"))
Expand Down Expand Up @@ -828,6 +843,7 @@ def main_loop(self):
elif act == "q":
return False


if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
Expand Down
6 changes: 5 additions & 1 deletion src/osenum.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# SPDX-License-Identifier: MIT
import os, os.path, plistlib, subprocess, logging
import os.path
import plistlib
import subprocess
from dataclasses import dataclass

from util import *

UUID_SROS = "3D3287DE-280D-4619-AAAB-D97469CA9C71"
UUID_FROS = "C8858560-55AC-400F-BBB9-C9220A8DAC0D"


@dataclass
class OSInfo:
partition: object
Expand Down Expand Up @@ -48,6 +51,7 @@ def __str__(self):
else:
return f"[{lbl}] {col(BRIGHT, RED)}incomplete install{col()} (macOS {self.version} stub) [{self.sys_volume}, {self.vgid}]"


class OSEnum:
def __init__(self, sysinfo, dutil, sysdsk):
self.sysinfo = sysinfo
Expand Down
9 changes: 8 additions & 1 deletion src/osinstall.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# SPDX-License-Identifier: MIT
import os, shutil, sys, stat, subprocess, urlcache, zipfile, logging
import subprocess
import urlcache
import zipfile

from util import *


class OSInstaller(PackageInstaller):
PART_ALIGNMENT = 1024 * 1024

def __init__(self, dutil, data, template):
super().__init__()
self.dutil = dutil
Expand All @@ -18,12 +22,15 @@ def __init__(self, dutil, data, template):
@property
def default_os_name(self):
return self.template["default_os_name"]

@property
def min_size(self):
return sum(self.align(psize(part["size"])) for part in self.template["partitions"])

@property
def expandable(self):
return any(part.get("expand", False) for part in self.template["partitions"])

@property
def needs_firmware(self):
return any(p.get("copy_firmware", False) for p in self.template["partitions"])
Expand Down
12 changes: 9 additions & 3 deletions src/stub.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# SPDX-License-Identifier: MIT
import os, os.path, plistlib, shutil, sys, stat, subprocess, urlcache, zipfile, logging, json
import osenum
from asahi_firmware.wifi import WiFiFWCollection
import json
import os.path
import plistlib
import subprocess
import urlcache
import zipfile

from asahi_firmware.bluetooth import BluetoothFWCollection
from asahi_firmware.multitouch import MultitouchFWCollection
from asahi_firmware.wifi import WiFiFWCollection
from util import *


class StubInstaller(PackageInstaller):
def __init__(self, sysinfo, dutil, osinfo, ipsw_info):
super().__init__()
Expand Down
5 changes: 4 additions & 1 deletion src/system.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# SPDX-License-Identifier: MIT
import base64, plistlib, struct, subprocess, logging
import plistlib
import struct
import subprocess

from util import *


class SystemInfo:
def __init__(self):
self.fetch()
Expand Down
8 changes: 6 additions & 2 deletions src/urlcache.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# SPDX-License-Identifier: MIT
import os, sys, os.path, time, logging
import os.path
import time
from dataclasses import dataclass

from urllib import request

from util import *


@dataclass
class CacheBlock:
idx: int
data: bytes


class URLCache:
CACHESIZE = 128
BLOCKSIZE = 1 * 1024 * 1024
Expand Down Expand Up @@ -137,6 +140,7 @@ def flush_progress(self):
else:
return False


if __name__ == "__main__":
import sys, zipfile

Expand Down
Loading