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
What persistence buys: the scenarios to measure it, and the two programs they need
vcvkk33e0e44234af
diff --git a/userland/rc.jobs.persist0 b/userland/rc.jobs.persist0new file mode 100644index 0000000..7e29b6c--- /dev/null+++ b/userland/rc.jobs.persist0@@ -0,0 +1,20 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# The boot before the series of rc.jobs.persist5: the store is forgotten first, so that what+# follows starts from a volume as empty as every boot of this port used to be. Run this one+# WITHOUT var_store=1 -- nothing is restored and nothing saved, and it doubles as the measure of+# a return to the loader with no saver registered: not one var_store line should be in its log.+/redwood/bin/var_store erase+/redwood/bin/var_store show+/redwood/bin/vol_ls fs /private/var+/redwood/bin/vol_ls cat /private/var/redwood-stamp 4096+/redwood/bin/keychain_test keep+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Data/Application+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Shared/AppGroup+/redwood/bin/var_store stamp /private/var/redwood-stamp+sleep 120+/bin/ps -A+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/keychain_test readdiff --git a/userland/rc.jobs.persist1 b/userland/rc.jobs.persist1new file mode 100644index 0000000..caf4701--- /dev/null+++ b/userland/rc.jobs.persist1@@ -0,0 +1,22 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# Boot 1 of the series of rc.jobs.persist5, which is this list with its two minutes of quiet in+# place of the three lines below: an icon of the home screen picked up and carried to the right+# (a long press and then a drag, all of it one touch -- rwtouch drag ... GRAB), then home. Whether+# it stays there is what the later boots' hash of IconState.plist says; ./xl screenshot during the+# run shows it.+/redwood/bin/var_store show+/redwood/bin/vol_ls fs /private/var+/redwood/bin/vol_ls cat /private/var/redwood-stamp 4096+/redwood/bin/keychain_test keep+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Data/Application+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Shared/AppGroup+/redwood/bin/var_store stamp /private/var/redwood-stamp+/redwood/bin/rwtouch wait 55 drag ICONX ICONY DROPX ICONY 800 600 900+/redwood/bin/rwtouch wait 3 button home+sleep 55+/bin/ps -A+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/keychain_test readdiff --git a/userland/rc.jobs.persist5 b/userland/rc.jobs.persist5new file mode 100644index 0000000..2833212--- /dev/null+++ b/userland/rc.jobs.persist5@@ -0,0 +1,25 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# What persistence buys, boot after boot (var_store=1 ufs_write=1; see rc.jobs.persist0 and+# rc.jobs.persist1, which are this list with a little before it). Everything here is a measurement,+# nothing here saves: the job com.redwood.varstore writes the volume when the kernel says the run is+# ending. Boot it five times over and compare the boots:+# the store at the start, and how much room the volume has;+# the line every run before this one left, and the keychain item an earlier boot wrote ("keep"+# writes one only if there is none, so every later boot reads the first boot's);+# the containers iOS gives its applications -- the same identifiers boot after boot, or new ones;+# the home screen's layout by hash and by the second it was written;+# and, after two minutes of iOS left to itself, the same again, with the process list.+/redwood/bin/var_store show+/redwood/bin/vol_ls fs /private/var+/redwood/bin/vol_ls cat /private/var/redwood-stamp 4096+/redwood/bin/keychain_test keep+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Data/Application+/redwood/bin/vol_ls ls /private/var/mobile/Containers/Shared/AppGroup+/redwood/bin/var_store stamp /private/var/redwood-stamp+sleep 120+/bin/ps -A+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls hash /private/var/mobile/Library/SpringBoard/IconState.plist+/redwood/bin/keychain_test readdiff --git a/userland/src/keychain_test.c b/userland/src/keychain_test.cindex 5d835fb..42edeb2 100644--- a/userland/src/keychain_test.c+++ b/userland/src/keychain_test.c@@ -3,11 +3,24 @@* Security.framework answers each time: 0 is success, -25300 "not found", -25291 "not available"* (what the keychain answered while the kernel's AppleKeyStore stand-in did not wrap keys),* -34018 "missing entitlement" (built unsigned: it is signed with keychain_test.entitlements).+ *+ * usage: keychain_test [keep|read|add|delete]+ * (none) the whole round trip, as before: delete, add, read back, delete+ * keep read it; if it is not there, write one that says when -- so a later boot's "read"+ * says which boot wrote it. For the data volume kept in the flash (var_store=1): the+ * keychain's database is a file under /private/var like any other, and whether an item+ * written in one boot can still be decrypted in the next is the question (the class keys+ * are a reversible transformation in the kernel, iokit/Kernel/MiniAppleKeyStore.cpp, not+ * a key of a Secure Enclave this phone has not got, so it should be -- and now is).+ * read read it and print it, and nothing else+ * add write one that says when, over whatever is there+ * delete remove it*/#include <CoreFoundation/CoreFoundation.h>#include <Security/Security.h>#include <stdio.h>#include <string.h>+#include <time.h>static CFMutableDictionaryRefquery(void)@@ -20,30 +33,117 @@ query(void)return q;}-int-main(void)+/* The item's bytes, or NULL; the caller releases. */+static CFDataRef+item_read(OSStatus *status)+{+ CFMutableDictionaryRef q = query();+ CFDataRef back = NULL;++ CFDictionarySetValue(q, kSecReturnData, kCFBooleanTrue);+ *status = SecItemCopyMatching(q, (CFTypeRef *)&back);+ CFRelease(q);+ return back;+}++static void+print_item(const char *what, OSStatus status, CFDataRef back)+{+ char text[128] = "";++ if (back != NULL && CFGetTypeID(back) == CFDataGetTypeID()) {+ CFIndex n = CFDataGetLength(back);++ if (n > (CFIndex)sizeof(text) - 1) {+ n = (CFIndex)sizeof(text) - 1;+ }+ memcpy(text, CFDataGetBytePtr(back), (size_t)n);+ text[n] = 0;+ }+ printf("keychain_test: %s: %d%s%s\n", what, (int)status, back != NULL ? ", " : "", text);+}++static OSStatus+item_add(void){- static const char secret[] = "hello from the keychain";CFMutableDictionaryRef q = query();- CFDataRef data = CFDataCreate(NULL, (const UInt8 *)secret, sizeof(secret) - 1), back = NULL;+ char secret[64];+ CFDataRef data;OSStatus status;- setvbuf(stdout, NULL, _IOLBF, 0);- status = SecItemDelete(q);- printf("keychain_test: delete before: %d\n", (int)status);+ snprintf(secret, sizeof(secret), "written by run of %lld", (long long)time(NULL));+ data = CFDataCreate(NULL, (const UInt8 *)secret, (CFIndex)strlen(secret));CFDictionarySetValue(q, kSecValueData, data);CFDictionarySetValue(q, kSecAttrAccessible, kSecAttrAccessibleAfterFirstUnlock);status = SecItemAdd(q, NULL);- printf("keychain_test: add: %d\n", (int)status);- CFRelease(q);- q = query();- CFDictionarySetValue(q, kSecReturnData, kCFBooleanTrue);- status = SecItemCopyMatching(q, (CFTypeRef *)&back);- printf("keychain_test: read back: %d, %s\n", (int)status, back != NULL && CFGetTypeID(back) == CFDataGetTypeID() &&- CFEqual(back, data) ? "THE SAME BYTES" : back != NULL ? "other bytes" : "nothing");+ printf("keychain_test: add \"%s\": %d\n", secret, (int)status);+ CFRelease(data);CFRelease(q);- q = query();- status = SecItemDelete(q);- printf("keychain_test: delete: %d\n", (int)status);+ return status;+}++int+main(int argc, char **argv)+{+ const char *what = argc > 1 ? argv[1] : "";+ CFMutableDictionaryRef q;+ CFDataRef data, back = NULL;+ OSStatus status;++ setvbuf(stdout, NULL, _IOLBF, 0);++ if (strcmp(what, "keep") == 0) {+ back = item_read(&status);+ print_item("read", status, back);+ if (back != NULL) {+ CFRelease(back);+ return 0; /* an earlier boot's item, still readable: nothing to do */+ }+ return item_add() == 0 ? 0 : 1;+ }+ if (strcmp(what, "read") == 0) {+ back = item_read(&status);+ print_item("read", status, back);+ if (back != NULL) {+ CFRelease(back);+ }+ return status == 0 ? 0 : 1;+ }+ if (strcmp(what, "add") == 0) {+ return item_add() == 0 ? 0 : 1;+ }+ if (strcmp(what, "delete") == 0) {+ q = query();+ status = SecItemDelete(q);+ printf("keychain_test: delete: %d\n", (int)status);+ CFRelease(q);+ return status == 0 ? 0 : 1;+ }++ /* no argument: the whole round trip, as this program always did */+ {+ static const char secret[] = "hello from the keychain";++ q = query();+ data = CFDataCreate(NULL, (const UInt8 *)secret, sizeof(secret) - 1);+ status = SecItemDelete(q);+ printf("keychain_test: delete before: %d\n", (int)status);+ CFDictionarySetValue(q, kSecValueData, data);+ CFDictionarySetValue(q, kSecAttrAccessible, kSecAttrAccessibleAfterFirstUnlock);+ status = SecItemAdd(q, NULL);+ printf("keychain_test: add: %d\n", (int)status);+ CFRelease(q);+ back = item_read(&status);+ printf("keychain_test: read back: %d, %s\n", (int)status, back != NULL && CFGetTypeID(back) == CFDataGetTypeID() &&+ CFEqual(back, data) ? "THE SAME BYTES" : back != NULL ? "other bytes" : "nothing");+ if (back != NULL) {+ CFRelease(back);+ }+ q = query();+ status = SecItemDelete(q);+ printf("keychain_test: delete: %d\n", (int)status);+ CFRelease(q);+ CFRelease(data);+ }return 0;}diff --git a/userland/src/rwtouch.c b/userland/src/rwtouch.cindex ff4072e..89c374c 100644--- a/userland/src/rwtouch.c+++ b/userland/src/rwtouch.c@@ -5,8 +5,11 @@* wait SECONDS pause* tap X Y a short touch* swipe X0 Y0 X1 Y1 [MS] drag from one point to the other in MS milliseconds (default 300)- * drag X0 Y0 X1 Y1 MS HOLD the same, then stay at the end HOLD milliseconds before letting go- * (what is dragged is held where it was taken, neither flung nor sprung back)+ * drag X0 Y0 X1 Y1 MS HOLD [GRAB]+ * the same, then stay at the end HOLD milliseconds before letting go+ * (what is dragged is held where it was taken, neither flung nor sprung back);+ * GRAB milliseconds of staying still first, for what has to be picked up+ * before it will move -- an icon of the home screen wants about 700* hold X Y MS touch and stay* zigzag X Y0 Y1 MS N [STEP] touch at Y0, go to Y1 in MS milliseconds and back again, N times, then let go;* a sample every STEP milliseconds (default 8, what the touch screen gives, as@@ -83,11 +86,20 @@ sleep_until(uint64_t start, uint64_t at)* 60 frames, and an application that is moved by the samples drew no more frames than that.)*/static void-drag(double x0, double y0, double x1, double y1, int ms, int hold)+drag(double x0, double y0, double x1, double y1, int ms, int hold, int grab){uint64_t start, total_us = (uint64_t)(ms > 0 ? ms : 1) * 1000;post(REDWOOD_TOUCH_DOWN, x0, y0);+ /*+ * grab: stay where the finger landed before moving at all. An icon of the home screen is+ * picked up by a long press and only then dragged, and it has to be one touch throughout --+ * a press that ends puts up the icon's menu instead.+ */+ for (int waited = 0; waited < grab; waited += 100) {+ usleep(100000);+ post(REDWOOD_TOUCH_MOVE, x0, y0);+ }start = mach_absolute_time();for (uint64_t at = SAMPLE_MS * 1000;; at += SAMPLE_MS * 1000) {double part;@@ -189,11 +201,14 @@ main(int argc, char **argv)} else if (strcmp(step, "swipe") == 0 && left >= 4) {int ms = left >= 5 && argv[i + 5][0] >= '0' && argv[i + 5][0] <= '9' ? atoi(argv[i + 5]) : 0;- drag(atof(argv[i + 1]), atof(argv[i + 2]), atof(argv[i + 3]), atof(argv[i + 4]), ms ? ms : 300, 0);+ drag(atof(argv[i + 1]), atof(argv[i + 2]), atof(argv[i + 3]), atof(argv[i + 4]), ms ? ms : 300, 0, 0);i += ms ? 5 : 4;} else if (strcmp(step, "drag") == 0 && left >= 6) {- drag(atof(argv[i + 1]), atof(argv[i + 2]), atof(argv[i + 3]), atof(argv[i + 4]), atoi(argv[i + 5]), atoi(argv[i + 6]));- i += 6;+ int grab = left >= 7 && argv[i + 7][0] >= '0' && argv[i + 7][0] <= '9' ? atoi(argv[i + 7]) : 0;++ drag(atof(argv[i + 1]), atof(argv[i + 2]), atof(argv[i + 3]), atof(argv[i + 4]), atoi(argv[i + 5]),+ atoi(argv[i + 6]), grab);+ i += grab ? 7 : 6;} else if (strcmp(step, "notify") == 0 && left >= 1) {uint32_t status = notify_post(argv[i + 1]);