Skip to content

Commit ecf0039

Browse files
committed
asahi_firmware.isp: Add ISP extractor
Co-developed-by: Eileen Yoon <[email protected]> Signed-off-by: Eileen Yoon <[email protected]> Signed-off-by: Hector Martin <[email protected]>
1 parent 64d7697 commit ecf0039

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

asahi_firmware/isp.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# SPDX-License-Identifier: MIT
2+
import struct, os, logging
3+
from collections import namedtuple
4+
from .core import FWFile
5+
6+
SetFile = namedtuple('SetFile', ['sensor', 'magic', 'name', 'size'])
7+
8+
ISP_PREFIX = "apple/isp_"
9+
ISP_SETFILE_ALIGNMENT = 0x1000
10+
ISP_SETFILES = [
11+
SetFile(0x248, 0x18200103, "1820_01XX", 0x442c),
12+
SetFile(0x248, 0x18220201, "1822_02XX", 0x442c),
13+
# SetFile(0x343, 0x52210211, "5221_02XX", 0x4870),
14+
# SetFile(0x354, 0x92510208, "9251_02XX", 0xa5ec),
15+
# SetFile(0x356, 0x48200107, "4820_01XX", 0x9324),
16+
# SetFile(0x356, 0x48200206, "4820_02XX", 0x9324),
17+
# SetFile(0x364, 0x87200103, "8720_01XX", 0x36ac),
18+
# SetFile(0x364, 0x87230101, "8723_01XX", 0x361c),
19+
# SetFile(0x372, 0x38200108, "3820_01XX", 0xfdb0),
20+
# SetFile(0x372, 0x38200205, "3820_02XX", 0xfdb0),
21+
# SetFile(0x372, 0x38201104, "3820_11XX", 0xfdb0),
22+
# SetFile(0x372, 0x38201204, "3820_12XX", 0xfdb0),
23+
# SetFile(0x405, 0x97200102, "9720_01XX", 0x92c8),
24+
# SetFile(0x405, 0x97210102, "9721_01XX", 0x9818),
25+
# SetFile(0x405, 0x97230101, "9723_01XX", 0x92c8),
26+
# SetFile(0x414, 0x25200102, "2520_01XX", 0xa444),
27+
# SetFile(0x503, 0x78200109, "7820_01XX", 0xb268),
28+
# SetFile(0x503, 0x78200206, "7820_02XX", 0xb268),
29+
# SetFile(0x505, 0x39210102, "3921_01XX", 0x89b0),
30+
# SetFile(0x514, 0x28200108, "2820_01XX", 0xa198),
31+
# SetFile(0x514, 0x28200205, "2820_02XX", 0xa198),
32+
# SetFile(0x514, 0x28200305, "2820_03XX", 0xa198),
33+
# SetFile(0x514, 0x28200405, "2820_04XX", 0xa198),
34+
SetFile(0x558, 0x19210106, "1921_01XX", 0xad40),
35+
SetFile(0x558, 0x19220201, "1922_02XX", 0xad40),
36+
# SetFile(0x603, 0x79200109, "7920_01XX", 0xad2c),
37+
# SetFile(0x603, 0x79200205, "7920_02XX", 0xad2c),
38+
# SetFile(0x603, 0x79210104, "7921_01XX", 0xad90),
39+
# SetFile(0x613, 0x49200108, "4920_01XX", 0x9324),
40+
# SetFile(0x613, 0x49200204, "4920_02XX", 0x9324),
41+
# SetFile(0x614, 0x29210107, "2921_01XX", 0xed6c),
42+
# SetFile(0x614, 0x29210202, "2921_02XX", 0xed6c),
43+
# SetFile(0x614, 0x29220201, "2922_02XX", 0xed6c),
44+
# SetFile(0x633, 0x36220111, "3622_01XX", 0x100d4),
45+
# SetFile(0x703, 0x77210106, "7721_01XX", 0x936c),
46+
# SetFile(0x703, 0x77220106, "7722_01XX", 0xac20),
47+
# SetFile(0x713, 0x47210107, "4721_01XX", 0x936c),
48+
# SetFile(0x713, 0x47220109, "4722_01XX", 0x9218),
49+
# SetFile(0x714, 0x20220107, "2022_01XX", 0xa198),
50+
# SetFile(0x772, 0x37210106, "3721_01XX", 0xfdf8),
51+
# SetFile(0x772, 0x37211106, "3721_11XX", 0xfe14),
52+
# SetFile(0x772, 0x37220104, "3722_01XX", 0xfca4),
53+
# SetFile(0x772, 0x37230106, "3723_01XX", 0xfca4),
54+
# SetFile(0x814, 0x21230101, "2123_01XX", 0xed54),
55+
# SetFile(0x853, 0x76220112, "7622_01XX", 0x247f8),
56+
# SetFile(0x913, 0x75230107, "7523_01XX", 0x247f8),
57+
# SetFile(0xd56, 0x62210102, "6221_01XX", 0x1b80),
58+
# SetFile(0xd56, 0x62220102, "6222_01XX", 0x1b80),
59+
]
60+
ISP_SETFILE_MAP = {s.magic: s for s in ISP_SETFILES}
61+
ISP_SETFILE_COUNT = len(ISP_SETFILES)
62+
assert len(ISP_SETFILE_MAP) == ISP_SETFILE_COUNT
63+
64+
log = logging.getLogger("asahi_firmware.isp")
65+
66+
def round_up(x, y):
67+
return ((x + (y - 1)) & (-y))
68+
69+
def isp_setfile_header_check(hdr):
70+
return (
71+
(hdr[2] == 0x0) and
72+
(hdr[3] == 0x0) and
73+
(hdr[4] & 0xff000000 == hdr[4]) and (hdr[4]) and
74+
(hdr[5] & 0xffff0000 == hdr[5]) and (hdr[5]) and
75+
(hdr[6] == 0x0) and
76+
(hdr[7] == 0x3c00000)
77+
)
78+
79+
class ISPFWCollection(object):
80+
def __init__(self, source_path):
81+
self.fwfiles = []
82+
self.load(source_path)
83+
84+
def extract_isp(self, data):
85+
files = []
86+
found = 0
87+
for offset in range(0, len(data), ISP_SETFILE_ALIGNMENT):
88+
hdrdata = data[offset:offset + 8*4]
89+
if len(hdrdata) < 8*4:
90+
break
91+
92+
# search for valid header and magic constant at 4K boundary
93+
header = struct.unpack(">8L", hdrdata)
94+
if not isp_setfile_header_check(header): continue
95+
setfile = ISP_SETFILE_MAP.get(header[0], None)
96+
if not setfile:
97+
continue
98+
99+
size = round_up(setfile.size, 64) # align to be safe
100+
dat = data[offset:offset + size]
101+
sensor_name = f"{setfile.sensor:x}_{setfile.name}"
102+
103+
logging.info(f"isp-extract: {found + 1}/{ISP_SETFILE_COUNT}: Found sensor {sensor_name} data at offset {offset:#x}")
104+
yield FWFile(f"apple/isp_{setfile.name}.dat", dat)
105+
106+
found += 1
107+
108+
if found != ISP_SETFILE_COUNT:
109+
raise ValueError(f"isp-extract: Found {found}/{ISP_SETFILE_COUNT} calibration files. Please report this bug.")
110+
111+
logging.info(f"isp-extract: Found all {found}/{ISP_SETFILE_COUNT} sensor calibration files!")
112+
113+
def load(self, source_path):
114+
if os.path.isdir(source_path):
115+
bin_path = os.path.join(source_path, "appleh13camerad")
116+
else:
117+
bin_path = source_path
118+
119+
if not os.path.exists(bin_path):
120+
log.warn("appleh13camerad not found, cannot extract ISP camera calibration firmwares. Webcam output will be low quality.")
121+
return
122+
123+
log.info(f"Extracting firmware from camera daemon at {bin_path}")
124+
125+
with open(bin_path, "rb") as fd:
126+
data = fd.read()
127+
128+
for fwf in self.extract_isp(data):
129+
self.fwfiles.append((fwf.name, fwf))
130+
131+
def files(self):
132+
return self.fwfiles
133+
134+
if __name__ == '__main__':
135+
parser = argparse.ArgumentParser(prog="isp-extract", description='Extract ISP sensor calibration files')
136+
parser.add_argument('--input', help='path to appleh13camerad', default="appleh13camerad")
137+
parser.add_argument('--outdir', help='output directory prefix', default="/tmp/isp-extract")
138+
parser.add_argument('--install', help='install files (requires root)', action='store_true')
139+
args = parser.parse_args()
140+
141+
files = isp_extract(args.input)
142+
143+
if (args.install):
144+
if (platform.system() != "Linux"):
145+
raise RuntimeError("isp-extract: You can only install on linux")
146+
if (os.geteuid() != 0):
147+
raise RuntimeError("isp-extract: You must be root to install")
148+
outdir = os.path.join(ISP_INSTALL_PREFIX, ISP_DIRNAME)
149+
else:
150+
outdir = os.path.join(args.outdir, ISP_DIRNAME)
151+
152+
os.makedirs(outdir, exist_ok=True)
153+
for fname, dat in files:
154+
open(os.path.join(outdir, fname), "wb").write(dat)

asahi_firmware/update.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .bluetooth import BluetoothFWCollection
77
from .multitouch import MultitouchFWCollection
88
from .kernel import KernelFWCollection
9+
from .isp import ISPFWCollection
910

1011
def update_firmware(source, dest):
1112
raw_fw = source.joinpath("all_firmware.tar.gz")
@@ -27,6 +28,9 @@ def update_firmware(source, dest):
2728
col = MultitouchFWCollection(str(tmpdir.joinpath("fud_firmware")))
2829
pkg.add_files(sorted(col.files()))
2930

31+
col = ISPFWCollection(str(tmpdir))
32+
pkg.add_files(sorted(col.files()))
33+
3034
col = KernelFWCollection(str(source))
3135
pkg.add_files(sorted(col.files()))
3236

0 commit comments

Comments
 (0)