Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

The prefix brings newer debianutils and launchctl, and preferenceloader, before the first boot

vcvkka0f697ea0acf
diff --git a/tools/jb-preinstall.py b/tools/jb-preinstall.py
new file mode 100644
index 0000000..c96b894
--- /dev/null
+++ b/tools/jb-preinstall.py
@@ -0,0 +1,190 @@
+#!/usr/bin/env python3
+"""Put packages of the repository into the prefix as installed, newest version first.
+
+usage: jb-preinstall.py PREFIX PACKAGES-INDEX DEBS-DIR NAME...
+ PREFIX the prefix tree, the directory that holds var/jb (jb/root of tools/mk-jb.sh)
+ PACKAGES-INDEX the repository's Packages file, already fetched
+ DEBS-DIR where the .deb files are kept (they are fetched into it when missing)
+ NAME... the packages to put in
+
+The bootstrap is a snapshot, and some of what it carries is older than the repository (debianutils,
+launchctl). apt on the phone would upgrade them, but only over the network and only after a boot,
+and what is in the image is what a first boot has; so the newest version of each is unpacked into
+the prefix here and written into dpkg's database, replacing an older entry if there is one.
+
+Nothing of the package's own scripts is run: these are plain file packages, and /var/jb/prep.sh does
+the little that the bootstrap's postinst scripts are needed for at every boot.
+"""
+import os
+import re
+import lzma
+import subprocess
+import sys
+import tarfile
+
+
+def version_parts(version):
+ """A Debian version as (epoch, upstream, revision), each ready for compare_part."""
+ epoch, _, rest = version.partition(':') if ':' in version else ('0', '', version)
+ if not rest:
+ epoch, rest = '0', version
+ upstream, _, revision = rest.rpartition('-')
+ if not upstream:
+ upstream, revision = rest, ''
+ return int(epoch or 0), upstream, revision
+
+
+def compare_part(a, b):
+ """Debian's own order for one part: digit runs numerically, and "~" before everything else."""
+ def order(c):
+ if c == '~':
+ return -1
+ if c.isalpha():
+ return ord(c)
+ return ord(c) + 256
+
+ i = j = 0
+ while i < len(a) or j < len(b):
+ # the stretch that is not digits, character by character
+ while (i < len(a) and not a[i].isdigit()) or (j < len(b) and not b[j].isdigit()):
+ x = order(a[i]) if i < len(a) and not a[i].isdigit() else 0
+ y = order(b[j]) if j < len(b) and not b[j].isdigit() else 0
+ if x != y:
+ return -1 if x < y else 1
+ if i < len(a) and not a[i].isdigit():
+ i += 1
+ if j < len(b) and not b[j].isdigit():
+ j += 1
+ # and the stretch that is, as a number
+ first, second = i, j
+ while i < len(a) and a[i].isdigit():
+ i += 1
+ while j < len(b) and b[j].isdigit():
+ j += 1
+ x = int(a[first:i] or 0)
+ y = int(b[second:j] or 0)
+ if x != y:
+ return -1 if x < y else 1
+ return 0
+
+
+def newer(a, b):
+ """Is version a newer than version b?"""
+ ea, ua, ra = version_parts(a)
+ eb, ub, rb = version_parts(b)
+ if ea != eb:
+ return ea > eb
+ for x, y in ((ua, ub), (ra, rb)):
+ order = compare_part(x, y)
+ if order:
+ return order > 0
+ return False
+
+
+def stanzas(text):
+ for block in text.split('\n\n'):
+ if block.strip():
+ yield block.strip('\n')
+
+
+def field(stanza, name):
+ m = re.search(r'(?m)^%s: (.*)$' % name, stanza)
+ return m.group(1).strip() if m else None
+
+
+def main():
+ if len(sys.argv) < 5:
+ sys.exit(__doc__)
+ prefix, index_path, debs, names = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
+ jb = os.path.join(prefix, 'var/jb')
+ status_path = os.path.join(jb, 'Library/dpkg/status')
+ index = open(index_path, encoding='utf-8', errors='replace').read()
+
+ best = {}
+ for stanza in stanzas(index):
+ name = field(stanza, 'Package')
+ if name in names:
+ version = field(stanza, 'Version') or '0'
+ if name not in best or newer(version, field(best[name], 'Version') or '0'):
+ best[name] = stanza
+
+ status = open(status_path, encoding='utf-8', errors='replace').read()
+ installed = {field(s, 'Package'): field(s, 'Version') for s in stanzas(status)}
+ done = []
+ for name in names:
+ if name not in best:
+ print('jb-preinstall.py: %s: not in the repository' % name)
+ continue
+ stanza = best[name]
+ version = field(stanza, 'Version')
+ have = installed.get(name)
+ if have is not None and not newer(version, have):
+ print('jb-preinstall.py: %s %s is already there' % (name, have))
+ continue
+ filename = field(stanza, 'Filename')
+ deb = os.path.join(debs, os.path.basename(filename))
+ if not os.path.exists(deb):
+ url = os.environ.get('MIRROR', 'https://apt.procurs.us').rstrip('/') + '/' + filename
+ # curl, as the rest of tools/mk-jb.sh fetches: the repository turns away urllib
+ if subprocess.run(['curl', '-sL', '--fail', '-o', deb, url]).returncode != 0:
+ sys.exit('jb-preinstall.py: cannot fetch %s' % url)
+ # a .deb is an ar archive; its data holds the tree, under ./var/jb for a rootless package.
+ # The member is found in the listing: "ar p" says nothing and still succeeds for a name that
+ # is not in the archive, so asking for one that may not be there tells us nothing.
+ listing = subprocess.run(['ar', 't', deb], capture_output=True, text=True)
+ member = next((line.strip() for line in listing.stdout.split('\n') if line.startswith('data.tar')), None)
+ if member is None:
+ sys.exit('jb-preinstall.py: %s: no data member in %s' % (name, deb))
+ data = subprocess.run(['ar', 'p', deb, member], capture_output=True).stdout
+ if not data:
+ sys.exit('jb-preinstall.py: %s: %s is empty in %s' % (name, member, deb))
+ if member.endswith('.zst'):
+ data = subprocess.run(['zstd', '-dc'], input=data, capture_output=True).stdout
+ elif member.endswith('.lzma'):
+ data = lzma.decompress(data, format=lzma.FORMAT_ALONE)
+ # .gz, .bz2 and .xz tarfile knows by itself
+ tmp = deb + '.tar'
+ with open(tmp, 'wb') as f:
+ f.write(data)
+ with tarfile.open(tmp) as tar:
+ for member in tar.getmembers():
+ path = os.path.normpath(member.name.lstrip('./'))
+ # only into the prefix, and never climbing out of it
+ if not path.startswith('var/jb/') or '..' in path.split('/'):
+ continue
+ member.name = './' + path
+ target = os.path.join(prefix, path)
+ if os.path.islink(target) or (os.path.exists(target) and not os.path.isdir(target)):
+ os.unlink(target) # the older version's file makes way
+ # "fully_trusted": the prefix is full of links to absolute paths of its own
+ # (/var/jb/bin/run-parts -> /var/jb/usr/bin/run-parts), which the careful filter
+ # refuses; where the files land is checked above instead.
+ tar.extract(member, prefix, set_attrs=True, filter='fully_trusted')
+ os.unlink(tmp)
+ # dpkg's database: the new stanza in place of the old one, or added
+ entry = '\n'.join(line for line in stanza.split('\n')
+ if not line.startswith(('Filename:', 'Size:', 'MD5sum:', 'SHA1:', 'SHA256:', 'SHA512:',
+ 'Description-md5:'))) + '\nStatus: install ok installed\n'
+ if have is not None:
+ status = '\n\n'.join(entry if field(s, 'Package') == name else s for s in stanzas(status)) + '\n'
+ else:
+ status = status.rstrip('\n') + '\n\n' + entry + '\n'
+ done.append('%s %s%s' % (name, version, ' (was %s)' % have if have else ''))
+ # A package that still writes to Cydia Substrate's old directory turns the link ElleKit put there
+ # into a real one when it is unpacked here; put the tweaks where they belong and the link back.
+ old_dir = os.path.join(jb, 'Library/MobileSubstrate/DynamicLibraries')
+ new_dir = os.path.join(jb, 'usr/lib/TweakInject')
+ if os.path.isdir(old_dir) and not os.path.islink(old_dir):
+ os.makedirs(new_dir, exist_ok=True)
+ for entry in sorted(os.listdir(old_dir)):
+ os.replace(os.path.join(old_dir, entry), os.path.join(new_dir, entry))
+ os.rmdir(old_dir)
+ os.symlink('/var/jb/usr/lib/TweakInject', old_dir)
+ print('jb-preinstall.py: the tweaks moved to usr/lib/TweakInject, the old directory a link again')
+ open(status_path, 'w', encoding='utf-8').write(status)
+ if done:
+ print('jb-preinstall.py: ' + ', '.join(done))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/mk-jb.sh b/tools/mk-jb.sh
index ebc6b07..497bfa0 100755
--- a/tools/mk-jb.sh
+++ b/tools/mk-jb.sh
@@ -98,6 +98,17 @@ ln -s /var/jb/usr/lib/TweakInject "$OUT/root/var/jb/Library/MobileSubstrate/Dyna
echo
} >> "$OUT/root/var/jb/Library/dpkg/status"
+# Newer versions of what the bootstrap carries, and anything else worth having before the first boot:
+# the bootstrap is a snapshot, and some of it is older than the repository (debianutils, launchctl).
+# apt on the phone would upgrade them, but only over the network and only after a boot, while what is
+# in the image is what a first boot has. PREINSTALL names them; mobilesubstrate is not among them
+# because there is no such package -- ElleKit above Provides it.
+PREINSTALL=${PREINSTALL-"debianutils launchctl preferenceloader"}
+if [ -n "$PREINSTALL" ]; then
+ get "$MIRROR/dists/iphoneos-arm64-rootless/$SUITE/main/binary-iphoneos-arm64/Packages" Packages
+ MIRROR="$MIRROR" python3 "$ROOT/tools/jb-preinstall.py" "$OUT/root" "$OUT/debs/Packages" "$OUT/debs" $PREINSTALL
+fi
+
# the repositories Sileo starts with: Procursus itself, where everything here came from
mkdir -p "$OUT/root/var/jb/etc/apt/sources.list.d"
cat > "$OUT/root/var/jb/etc/apt/sources.list.d/procursus.sources" <<EOF
diff --git a/userland/rc.jobs.preinstalled b/userland/rc.jobs.preinstalled
new file mode 100644
index 0000000..dda59ab
--- /dev/null
+++ b/userland/rc.jobs.preinstalled
@@ -0,0 +1,11 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# What the prefix brings with it before anything is installed over the network
+# (RC_JOBS=rc.jobs.preinstalled): the versions in dpkg's database, that launchctl and preferenceloader
+# are really there, and that the tweak loader finds PreferenceLoader and puts it into Settings.
+/redwood/bin/sound_test springboard
+/var/jb/bin/sh /var/jb/prep.sh
+/var/jb/usr/bin/dpkg-query -W -f${binary:Package}_${Version}_${Status}\n debianutils launchctl preferenceloader ellekit
+/var/jb/usr/bin/launchctl version
+/var/jb/bin/ls -la /var/jb/usr/lib/TweakInject/
+/var/jb/usr/bin/uiopen prefs:
+/redwood/bin/rwtouch wait 20
diff --git a/userland/src/inject_tweaks.c b/userland/src/inject_tweaks.c
index 94dd90a..6580755 100644
--- a/userland/src/inject_tweaks.c
+++ b/userland/src/inject_tweaks.c
@@ -35,7 +35,15 @@
#include <sys/stat.h>
#include <unistd.h>
-#define TWEAK_DIR "/var/jb/usr/lib/TweakInject"
+/*
+ * Both places a package may put a tweak: the one ElleKit uses now, and the one Cydia Substrate used,
+ * which is a symbolic link to it on a phone -- but a package unpacked on this machine can leave it a
+ * real directory, and some packages still write there. Looking in both costs nothing.
+ */
+static const char *const tweak_dirs[] = {
+ "/var/jb/usr/lib/TweakInject",
+ "/var/jb/Library/MobileSubstrate/DynamicLibraries",
+};
static void
say(const char *format, ...)
@@ -207,35 +215,44 @@ load_the_tweaks(void)
char identifier[256];
const char *name = executable_name();
const char *who = bundle_identifier(identifier, sizeof(identifier)) ? identifier : NULL;
- struct dirent *entry;
int opened = 0, tried = 0;
- DIR *dir;
- dir = opendir(TWEAK_DIR);
- if (dir == NULL) {
- return; /* no prefix, or no tweaks in it */
- }
- while ((entry = readdir(dir)) != NULL) {
- char plist[1024], dylib[1024];
- size_t length = strlen(entry->d_name);
+ for (unsigned d = 0; d < sizeof(tweak_dirs) / sizeof(tweak_dirs[0]); d++) {
+ const char *where = tweak_dirs[d];
+ struct dirent *entry;
+ struct stat st;
+ DIR *dir;
- if (length < 7 || strcmp(entry->d_name + length - 6, ".plist") != 0) {
+ /* the second is usually a link to the first: then it is the same directory, done already */
+ if (d > 0 && lstat(where, &st) == 0 && S_ISLNK(st.st_mode)) {
continue;
}
- snprintf(plist, sizeof(plist), "%s/%s", TWEAK_DIR, entry->d_name);
- snprintf(dylib, sizeof(dylib), "%s/%.*s.dylib", TWEAK_DIR, (int)(length - 6), entry->d_name);
- if (!wanted_here(plist, name, who)) {
- continue;
+ dir = opendir(where);
+ if (dir == NULL) {
+ continue; /* no prefix, or no tweaks in it */
}
- tried++;
- if (dlopen(dylib, RTLD_NOW | RTLD_GLOBAL) != NULL) {
- opened++;
- say("redwood: tweaks: %s\n", dylib);
- } else {
- say("redwood: tweaks: %s: %s\n", dylib, dlerror());
+ while ((entry = readdir(dir)) != NULL) {
+ char plist[1024], dylib[1024];
+ size_t length = strlen(entry->d_name);
+
+ if (length < 7 || strcmp(entry->d_name + length - 6, ".plist") != 0) {
+ continue;
+ }
+ snprintf(plist, sizeof(plist), "%s/%s", where, entry->d_name);
+ snprintf(dylib, sizeof(dylib), "%s/%.*s.dylib", where, (int)(length - 6), entry->d_name);
+ if (!wanted_here(plist, name, who)) {
+ continue;
+ }
+ tried++;
+ if (dlopen(dylib, RTLD_NOW | RTLD_GLOBAL) != NULL) {
+ opened++;
+ say("redwood: tweaks: %s\n", dylib);
+ } else {
+ say("redwood: tweaks: %s: %s\n", dylib, dlerror());
+ }
}
+ closedir(dir);
}
- closedir(dir);
if (tried > 0) {
say("redwood: tweaks: %s (%s): %d of %d opened\n", name != NULL ? name : "?",
who != NULL ? who : "no bundle", opened, tried);