Skip to content
Merged
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
2 changes: 1 addition & 1 deletion omarchy-mac/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# omarchy-mac

Apple Silicon defaults and support services for Omarchy. Version: `0.2.0` (candidate). This add-on complements `omarchy` and `omarchy-settings`; it selects no kernel and contains no installer or repository trust configuration. It covers what stays on an installed Mac; installing one is the job of the [Omarchy Installer](https://github.com/omacom/omarchy-mac-installer), and boot support is the separate `omarchy-mac-boot` package beside this one.
Apple Silicon defaults and support services for Omarchy. Version: `0.2.1` (candidate). This add-on complements `omarchy` and `omarchy-settings`; it selects no kernel and contains no installer or repository trust configuration. It covers what stays on an installed Mac; installing one is the job of the [Omarchy Installer](https://github.com/omacom/omarchy-mac-installer), and boot support is the separate `omarchy-mac-boot` package beside this one.

## Build and stage

Expand Down
11 changes: 11 additions & 0 deletions omarchy-mac/entrypoints/disable-fingerprint
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
# A Neo's sensor key goes too: turning Touch ID on again rebuilds it.
omarchy-hw-apple-silicon || exit 0

# Waits for a Touch ID setup still running, which could otherwise install its
# sensor key after this removes it.
lock=/run/omarchy-mac/touchid.lock
if ! { mkdir -p -m 0755 "${lock%/*}" && exec {lock_fd}>>"$lock"; } 2>/dev/null; then
echo "Touch ID could not be turned off: its lock, $lock, could not be opened." >&2
exit 1
elif ! flock -w 300 "$lock_fd"; then
echo "Touch ID could not be turned off: another Touch ID setup is still running. Try again once it finishes." >&2
exit 1
fi

key=/usr/lib/firmware/updates/apple/msrk-j700.bin
if [[ -e /etc/omarchy-mac/touchid-on || -e /etc/modprobe.d/omarchy-mac-touchid.conf || -e $key || -e $key.new ]]; then
# A record left behind, on a read-only root say, loads the driver again.
Expand Down
22 changes: 17 additions & 5 deletions omarchy-mac/entrypoints/enable-fingerprint
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ if [[ -e /etc/omarchy-mac/touchid-on ]]; then
# options are what lets it reach ready after a restart.
options=/etc/modprobe.d/omarchy-mac-touchid.conf
if [[ ! -e $options ]]; then
{ mkdir -p -m 0755 "${options%/*}" &&
printf '%s\n' "# The owner turned on Touch ID; omarchy-mac's disable-fingerprint removes this." \
"options apple_sep xart_writes=1 provision_keybag=1" >"$options.new" &&
chmod 0644 "$options.new" && mv -fT "$options.new" "$options"; } ||
{ rm -f "$options.new" 2>/dev/null; echo "Touch ID's driver options could not be written to $options" >&2; exit 1; }
# Under the Touch ID lock, so a removal running now can't be undone here.
lock=/run/omarchy-mac/touchid.lock
if ! { mkdir -p -m 0755 "${lock%/*}" && exec {lock_fd}>>"$lock"; } 2>/dev/null || ! flock -w 300 "$lock_fd"; then
echo "Touch ID's driver options could not be written: another Touch ID setup or removal is still running." >&2
exit 1
fi
Comment on lines +22 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Lock errors get blamed elsewhere

If Touch ID is already enabled but the driver options are missing, enable-fingerprint reports that another setup is running even when it could not open the lock. The actual error is hidden, so retrying will not help when the lock path cannot be opened.

Separate the open-failure message from the timeout message, as disable-fingerprint and touchid-enable already do.

Suggested change
if ! { mkdir -p -m 0755 "${lock%/*}" && exec {lock_fd}>>"$lock"; } 2>/dev/null || ! flock -w 300 "$lock_fd"; then
echo "Touch ID's driver options could not be written: another Touch ID setup or removal is still running." >&2
exit 1
fi
if ! { mkdir -p -m 0755 "${lock%/*}" && exec {lock_fd}>>"$lock"; } 2>/dev/null; then
echo "Touch ID's driver options could not be written: its lock, $lock, could not be opened." >&2
exit 1
elif ! flock -w 300 "$lock_fd"; then
echo "Touch ID's driver options could not be written: another Touch ID setup or removal is still running." >&2
exit 1
fi

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #61 (merged here before this was addressed), with a test for the unopenable lock.

if [[ ! -e /etc/omarchy-mac/touchid-on ]]; then
echo "Touch ID was turned off while this ran, so setup goes on without it."
exit 0
elif [[ ! -e $options ]]; then
{ mkdir -p -m 0755 "${options%/*}" &&
printf '%s\n' "# The owner turned on Touch ID; omarchy-mac's disable-fingerprint removes this." \
"options apple_sep xart_writes=1 provision_keybag=1" >"$options.new" &&
chmod 0644 "$options.new" && mv -fT "$options.new" "$options"; } ||
{ rm -f "$options.new" 2>/dev/null; echo "Touch ID's driver options could not be written to $options" >&2; exit 1; }
fi
exec {lock_fd}>&-
fi
# The owner agreed already, so they are never asked again: a Touch ID that
# isn't ready yet needs a restart, and setup goes on with any other reader.
Expand Down
42 changes: 40 additions & 2 deletions omarchy-mac/lib/touchid-enable
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ keybag=${OMARCHY_MAC_TOUCHID_KEYBAG:-/var/lib/aurora-sep-keybag.bin}
esp=${OMARCHY_MAC_TOUCHID_ESP:-/boot/efi}
container=${OMARCHY_MAC_TOUCHID_CONTAINER:-/dev/disk/by-partlabel/iBootSystemContainer}
python=${OMARCHY_MAC_TOUCHID_PYTHON:-python3}
lock_wait=${OMARCHY_MAC_TOUCHID_LOCK_WAIT:-300}
omarchy-hw-apple-silicon || exit 0

# Once this run records the agreement, a failure takes it back, so a Touch ID
Expand Down Expand Up @@ -47,6 +48,43 @@ installed() {
return 1
}

# A sensor key counts only once it checks out whole. A broken one in the updates
# directory is rebuilt, and goes if that fails; one anywhere else would shadow
# or outlive a rebuild there.
root_key_installed() {
local directory key found=1 checked
for directory in "$firmware" "$firmware/vendor" "$firmware/updates"; do
key=$directory/apple/msrk-j700.bin
if [[ ! -e $key ]]; then
continue
elif ! command -v "$python" >/dev/null; then
fail "this Mac's sensor key at $key can't be checked without Python"
fi
checked=0
"$python" -I "$helpers/touchid-root-key" --check "$key" >/dev/null 2>&1 || checked=$?
if (( checked == 0 )); then
found=0
elif (( checked != 3 )); then
fail "this Mac's sensor key at $key could not be checked"
elif [[ $directory == "$firmware/updates" ]]; then
echo "The sensor key at $key is not whole, so it is read again."
return 1
else
fail "the sensor key at $key is not whole; remove it, then turn Touch ID on again"
fi
done
return $found
}

# One setup or removal at a time: two would share the sensor key's temporary
# copy, and one could install the other's unfinished key.
lock=${status_file%/*}/touchid.lock
if ! { mkdir -p -m 0755 "${lock%/*}" && exec {lock_fd}>>"$lock"; } 2>/dev/null; then
fail "its lock, $lock, could not be opened"
elif ! flock -w "$lock_wait" "$lock_fd"; then
fail "another Touch ID setup or removal is still running; try again once it finishes"
fi

eligibility=$("$helpers/touchid-eligibility")
[[ $eligibility == "available" ]] || fail "this Mac can't use it yet ($eligibility)"

Expand All @@ -66,13 +104,13 @@ if ! installed "$calibration"; then
{ rm -f "$carved.new"; fail "this Mac's calibration could not be read"; }
chmod 0644 "$carved.new" && mv -f "$carved.new" "$carved" || { rm -f "$carved.new"; fail "this Mac's calibration could not be installed"; }
fi
if tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,t8140 && ! installed apple/msrk-j700.bin; then
if tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,t8140 && ! root_key_installed; then
# Rebuilt only now, from the factory records asahi-installer leaves on the
# ESP and checked against this Mac's iBoot System Container; root-only, and
# it takes its name only once whole. Turning Touch ID off deletes it.
echo "Reading this Mac's Touch ID sensor key..."
mkdir -p -m 0755 "$firmware/updates/apple"
# No key was installed, so whatever takes its name from here is this run's:
# No whole key was installed, so whatever takes its name from here is this run's:
# a failure, even one the extractor reports after its rename, takes it back
# with the agreement.
rebuilt=1
Expand Down
46 changes: 41 additions & 5 deletions omarchy-mac/lib/touchid-root-key
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ The key is device-private. asahi-installer leaves this Mac's factory records
on the ESP, in asahi/all_firmware.tar.gz; the key is the MSRk record's IM4P
under the FDR sealing manifest (seal-<ECID>), with the trust bundle as secb,
as macOS stores the other records in its factory cache. Every byte comes from
those records, each of which must also be found in this Mac's own iBoot
System Container, so an archive from another Mac, or one swapped on the ESP,
is refused. The logic follows omarchy-mac-installer's factory_root_key()."""
those records, and each must also be found, byte for byte, somewhere in this
Mac's own iBoot System Container. That proves each record is present there, not
that it is the active or an authentic one. The logic follows
omarchy-mac-installer's factory_root_key().

With --check FILE it only says whether FILE is a whole sensor root key: it exits
3 when FILE is not one, and 1 when it could not tell."""

import argparse
import hashlib
Expand All @@ -20,6 +24,9 @@ import tarfile
import zlib

RECORD_MAXIMUM = 16 << 20
# Bounds on the whole archive: members read, and record bytes kept in memory.
MEMBER_MAXIMUM = 1 << 14
RETAINED_MAXIMUM = 32 << 20
# The records the key is rebuilt from; anything else in the cache is skipped.
RECORDS = ("MSRk-", "seal-", "trustobject-")
CONTAINER_MAXIMUM = 1 << 30
Expand Down Expand Up @@ -119,7 +126,7 @@ def check_root_key(data):
if len(manifest) < 2 or ia5(data, manifest[0]) != b"IM4M":
raise ValueError("missing manifest")
except (ValueError, IndexError) as error:
raise Refused(f"the rebuilt key is not a whole sensor root key: {error}") from error
raise Refused(f"the key is not a whole sensor root key: {error}") from error
return data


Expand Down Expand Up @@ -148,9 +155,13 @@ def factory_records(archive):
archive's com.apple.factorydata directory, read into memory only. Other
entries there (a subdirectory or record a later macOS adds) are skipped."""
records = {}
members = retained = 0
try:
with tarfile.open(archive, "r:gz") as tar:
for member in tar:
members += 1
if members > MEMBER_MAXIMUM:
raise Refused(f"{archive} holds more than {MEMBER_MAXIMUM} entries")
# asahi-installer archives the cache as com.apple.factorydata/
# at the archive's root; nothing elsewhere counts.
path = member.name.rstrip("/")
Expand All @@ -165,6 +176,9 @@ def factory_records(archive):
raise Refused(f"the factory record {name} in {archive} is too large")
if name in records:
raise Refused(f"{archive} holds the factory record {name} twice")
retained += member.size
if retained > RETAINED_MAXIMUM:
raise Refused(f"the factory records in {archive} are too large together")
with tar.extractfile(member) as fd:
records[name] = fd.read(RECORD_MAXIMUM + 1)
except (OSError, tarfile.TarError, EOFError, zlib.error) as error:
Expand Down Expand Up @@ -267,15 +281,37 @@ def apple_silicon():
return False


NOT_WHOLE = 3


def check(path):
try:
with open(path, "rb") as fd:
data = fd.read(RECORD_MAXIMUM + 1)
except OSError as error:
print(f"touchid-root-key: cannot read {path}: {error}", file=sys.stderr)
return 1
try:
check_root_key(data)
except Refused as error:
print(f"touchid-root-key: {path}: {error}", file=sys.stderr)
return NOT_WHOLE
return 0


def main():
if not apple_silicon():
print("touchid-root-key: this is not an Apple Silicon Mac", file=sys.stderr)
return 1
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
parser.add_argument("--archive", default="/boot/efi/asahi/all_firmware.tar.gz")
parser.add_argument("--container", default="/dev/disk/by-partlabel/iBootSystemContainer")
parser.add_argument("-o", "--output", required=True)
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument("-o", "--output")
action.add_argument("--check", metavar="FILE")
arguments = parser.parse_args()
if arguments.check:
return check(arguments.check)
try:
records = factory_records(arguments.archive)
_, record = one(records, "MSRk-")
Expand Down
Loading
Loading