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

Sound: iOS plays through a device of ours that it takes for the iPhone's speaker

vcvkka2eee90cfed3
diff --git a/README.md b/README.md
index 4149006..255462f 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,7 @@
| Кнопки: на телефоне настоящие — питание (боковая кнопка iPhone) и громкость; в QEMU — клавишами | Зарядка под нашим ядром (телефон только разряжается) |
| Большое ядро на 2,4 ГГц, загрузка до экрана блокировки ~20 с (телефон) | Safari (лежит в отдельном «App cryptex», которого у нас нет) |
| Снимки экрана и запись экрана с телефона | Сохранение данных: /private/var в памяти, каждая загрузка — «новое устройство» |
+| Звук iOS в QEMU (см. ниже; с громкостью; «Сейчас играет» — с оговоркой) | Динамики и микрофоны самого телефона: под нашим ядром они молчат |
Это стенд для исследования, а не прошивка: сеанс длится заданное число секунд, потом ядро возвращается в загрузчик
и отдаёт журнал.
@@ -78,6 +79,32 @@ qemu/run-ios.sh 1200 -display gtk > logs/qemu.log # сеанс на 20 ми
- Сразу после появления SpringBoard система ещё занята (регистрирует приложения): подождите секунд 30–40,
иначе жесты «растягиваются» и не распознаются.
+### Звук
+
+Звуком в iOS заведует `mediaserverd`; в него встаёт наше устройство вывода (`RedwoodAudio.driver`), которое iOS
+принимает за собственный динамик iPhone, и то, что она играет, приходит в ядро (`/dev/redwood-audio`):
+
+```sh
+REST=1 userland/build.sh
+AUDIO=pipewire qemu/run-ios.sh 1200 -display gtk > logs/qemu.log # или AUDIO=pa, alsa, sdl; AUDIO=файл.wav — в файл
+```
+
+Для вывода на звуковую карту компьютера QEMU нужен свой модуль (в Arch — пакет `qemu-audio-pipewire`, `qemu-audio-pa`
+и т. п.); без `AUDIO` звук играется «в никуда», но внутри iOS всё происходит так же. Проверено в QEMU записью:
+тон из своей программы через очередь звука и системные звуки (блокировка, сигнал) доходят до ядра; громкость
+действует (кнопки и ползунок в Пункте управления меняют уровень, индикатор громкости появляется). Под эмуляцией
+цепочка обработки звука iPhone не успевает за реальным временем, и запись идёт с пропусками. Модуль «Сейчас играет»
+в Пункте управления в QEMU пуст, пока `mediaserverd` не перезапустится хотя бы раз (так же и без нашего
+устройства); после перезапуска показывает «Не исполняется» с кнопками.
+`RC_JOBS=rc.jobs.sound` добавляет в образ сценарий, который сам проигрывает тон и несколько системных звуков
+(`userland/src/sound_test.c`); когда звук смолкает, ядро пишет в журнал строку
+`redwood-audio: N s written at R frames a second, M s of it sound, peak P% of full scale`.
+
+На телефоне динамики пока молчат (ими управляет звуковой процессор Qualcomm, который наше ядро не запускает),
+но то, что iOS играет, можно забрать: параметр ядра `rec_sound=N` сохраняет первые N секунд звука в памяти, после
+сеанса `./xl sound out.wav` достаёт их (в QEMU — `pmemsave` по адресу из строки `redwood-audio: recording … at 0x…`,
+затем `tools/sound.py`). В QEMU запись проверена; на самом телефоне звук ещё не проверялся вовсе.
+
Замечания:
- Память гостя — 7 ГБ (ядру достаётся то же окно 4,2 ГБ, что и на телефоне), отображённые из разрежённого файла
@@ -185,6 +212,8 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7 cpus=4" ./xl run #
| `cpus=N` | использовать N ядер процессора: то, на котором ядро запущено, и ещё N−1 ближайших к нему по номеру (после 7 — 6, 5, 4, потом малые 3, 2, 1); их запускает само ядро через PSCI. `cpumask=M` — выбрать по позиции в этом порядке (бит 0 — загрузочное). Без параметра — одно ядро. **На телефоне не проверено**, порядок проверки — в конце раздела |
| `cpufreq=1` | поднять частоту до максимума (иначе 300 МГц) всем группам ядер, в которых есть наши |
| `rec_secs=N` | записывать экран N секунд (`rec_start=S`, `rec_mb=M`, не больше 640); потом `./xl recording out.mp4`. `rec_ring=1` — по кругу: когда память кончилась, запись идёт поверх самых старых кадров, и к концу сеанса в ней последнее (для того, что случается раз за долгий сеанс: сеанс кончают сразу после — пятью пальцами); `rec_step=N` — каждый N-й пиксель вместо третьего (4 — в 1,8 раза больше кадров). Память на запись отнимается у системы на весь сеанс: с `rec_mb=640` в долгом сеансе iOS убивала процессы ради памяти, вплоть до SpringBoard (респринги), с 256 — нет. `XL_REC_KEEP=1 ./xl recording` оставляет саму запись в `logs/recording.raw`; `tools/recording.py logs/recording.raw out.mp4 --frames DIR` раскладывает её на кадры с именами по времени ядра в миллисекундах — тому же, что в строках `glframes=1` |
+| `rec_sound=N` | сохранить первые N секунд звука, который играет iOS (тишина между звуками места не занимает); потом `./xl sound out.wav` |
+| `speaker_probe=N` | только телефон, **ещё ни разу не запускалось**: посмотреть на звуковое железо, ничего не включая. `1` — только чтение (состояние выводов, имена питаний у RPMh, один регистр тактового генератора); `2` — ещё и спросить у усилителей динамиков, кто они, по I2C, для чего на время перенастраиваются два вывода (GPIO 12 и 13). Ответы — строки `speaker:` в журнале |
| `-v` | текст загрузки на экране (по умолчанию у `xl run`, если `XL_CMDLINE` не задана) |
| `applog=1` / `applog=2` | stderr приложений в журнал / плюс os_log всех процессов |
| `applog_name=ИМЯ[,ИМЯ]` | вместе с `applog=1`: os_log только тех программ, чьи имена файлов начинаются с одного из ИМЁН (так читают расширения приложений — виджеты, обои: их запускает launchd по просьбе других процессов, описания задания у нас для них нет) |
@@ -379,12 +408,15 @@ fastboot boot ──► UEFI (Mu-Silicium, redwood) ──► xnuloader (EFI-п
├─ packfs (корень, сжатый образ в ОЗУ), ramfs (/private/var, /tmp)
├─ minicrypto вместо corecrypto, заглушки AMFI и Sandbox (MAC-политики)
├─ замены драйверов: IOSurface, IOMobileFramebuffer, AppleKeyStore, IONetworkStack
- └─ ввод: /dev/redwood-touch ◄─ Goodix GT9916 (телефон) / virtio-input (QEMU)
+ ├─ ввод: /dev/redwood-touch ◄─ Goodix GT9916 (телефон) / virtio-input (QEMU)
+ └─ звук: /dev/redwood-audio ─► virtio-sound (QEMU) / запись в память (rec_sound)
▼
launchd iOS 16.5 → logd, notifyd, cfprefsd, runningboardd, backboardd, SpringBoard, приложения
- └─ в backboardd, SpringBoard и mediaserverd внедряются наши библиотеки
- (libredwood*.dylib): виртуальные HID-службы (сенсор, кнопки) и подмена
- нескольких ObjC-методов
+ ├─ в backboardd, SpringBoard и mediaserverd внедряются наши библиотеки
+ │ (libredwood*.dylib): виртуальные HID-службы (сенсор, кнопки) и подмена
+ │ нескольких ObjC-методов
+ └─ mediaserverd получает наше устройство вывода (RedwoodAudio.driver — подключаемый
+ модуль CoreAudio), которое пишет смикшированный звук в /dev/redwood-audio
```
Userland не изменён ни в одном байте: всё, чего ему не хватает, либо делает ядро (замены драйверов), либо
@@ -394,17 +426,17 @@ Userland не изменён ни в одном байте: всё, чего е
| Путь | Что |
|---|---|
-| `xl` | управление телефоном по USB: загрузчик, запуск ядра, журнал, снимки экрана |
+| `xl` | управление телефоном по USB: загрузчик, запуск ядра, журнал, снимки экрана, запись экрана и звука |
| `xnuloader/` | загрузчик: `loader.c` (UEFI-приложение), `mkpayload.py` (контейнер «ядро + образ + аргументы») |
| `xnu/` | наши правки XNU: патч, новые файлы, `apply.sh`/`export.sh` |
| `xnu-deps/` | всё для сборки XNU на Linux: `setup.sh`, обёртки инструментов, заглушки |
| `xnu-build.sh`, `xnu-env.sh` | сборка ядра |
-| `userland/` | наш init, вставки (`src/inject.c` — backboardd, `src/inject_sb.c` — SpringBoard, `src/inject_media.c` — mediaserverd), тесты, сборка образов |
+| `userland/` | наш init, вставки (`src/inject.c` — backboardd, `src/inject_sb.c` — SpringBoard, `src/inject_media.c` — mediaserverd), звуковое устройство (`src/audio_plugin.c`), тесты, сборка образов |
| `ios165/` | добыча файлов iOS из IPSW и выбор того, что идёт в образ |
| `mkpackfs.py` | сборщик образов packfs (формат описан в `bsd/miscfs/packfs/packfs.h` ядра) |
| `qemu/` | запуск в QEMU, касания, снимки экрана, разбор стеков |
| `uefi/`, `mkxnuimg.py` | порт UEFI на redwood и упаковка загрузочного образа для `fastboot boot` |
-| `tools/` | `recording.py` (запись экрана → mp4), `update-prebuilt.sh`, `new-workspace.sh` (вторая рабочая копия со своим ядром и QEMU), `mk-zstddec.sh`, `mk-mesa.sh` (Mesa для `GL=1`), `mk-ath11k.sh` (драйвер Wi-Fi для ядра), `qmi-test/` и `wpa-test/` (проверки на компьютере) |
+| `tools/` | `recording.py` (запись экрана → mp4), `sound.py` (запись звука → wav), `update-prebuilt.sh`, `new-workspace.sh` (вторая рабочая копия со своим ядром и QEMU), `mk-zstddec.sh`, `mk-mesa.sh` (Mesa для `GL=1`), `mk-ath11k.sh` (драйвер Wi-Fi для ядра), `qmi-test/` и `wpa-test/` (проверки на компьютере) |
| `wifi/lx/` | прослойка, на которой драйвер Wi-Fi из Linux собирается для нашего ядра (см. «Драйвер Wi-Fi») |
| `prebuilt/` | собранные ядро, загрузчик, наши программы и куски UEFI |
diff --git a/ios165/select-system.py b/ios165/select-system.py
index b08b177..74e1632 100755
--- a/ios165/select-system.py
+++ b/ios165/select-system.py
@@ -81,6 +81,12 @@ SKIP_PREFIX = ('usr/share/tokenizer/', 'usr/share/firmware/', 'usr/share/mecabra
HEALTH + 'Assets/FitnessUIAssets.bundle/') + \
tuple(f'System/Library/{name}/' for name in SKIP_SYSTEM_LIBRARY)
BIG_PROGRAM = 10 << 20 # daemons bigger than this (locationd, lskdd, ...) are not on the way to the screen
+# sound, outside the directories above: the virtual audio device, a plug-in that all of mediaserverd's
+# audio goes through, with its tunings (signal processing chains) for this model and for any, and the
+# system's own sounds. Not the neural networks among the tunings, nor the sounds of the watch.
+AUDIO = ('Library/Audio/Plug-Ins/', 'Library/Audio/Tunings/D22/', 'Library/Audio/Tunings/Generic/',
+ 'System/Library/Audio/UISounds/')
+SKIP_AUDIO = ('System/Library/Audio/UISounds/nano/',)
KEEP_LANG = ('en', 'Base', 'English', 'en_US', 'en_GB')
@@ -124,6 +130,8 @@ def wanted_file(path, size):
lproj = re.search(r'/([^/]+)\.lproj/', path)
if lproj and lproj.group(1) not in KEEP_LANG:
return False
+ if path.startswith(AUDIO):
+ return not path.startswith(SKIP_AUDIO) and '.espresso.' not in path
if path.startswith(SKIP_PREFIX):
return False
if path.startswith('System/Library/Fonts/'):
diff --git a/prebuilt/userland/RedwoodAudio b/prebuilt/userland/RedwoodAudio
new file mode 100755
index 0000000..7784f1c
Binary files /dev/null and b/prebuilt/userland/RedwoodAudio differ
diff --git a/prebuilt/userland/sound_test b/prebuilt/userland/sound_test
new file mode 100755
index 0000000..062eb48
Binary files /dev/null and b/prebuilt/userland/sound_test differ
diff --git a/qemu/run-ios.sh b/qemu/run-ios.sh
index 9be70f2..d5391f1 100755
--- a/qemu/run-ios.sh
+++ b/qemu/run-ios.sh
@@ -11,6 +11,9 @@
# (it hands out an address by DHCP, answers DNS and passes connections on to
# this machine's network; no privileges needed). The kernel makes en0 of it
# (boot-arg virtio_net=1, added here); see userland/src/net_test.m for the rest.
+# AUDIO=<where> what plays the sound of iOS: a QEMU audio back end (pipewire, pa, alsa, sdl: each
+# needs its QEMU module, e.g. the package qemu-audio-pipewire) or FILE.wav to
+# write it into a file instead. Default: nowhere (the guest plays all the same).
# QEMU_INSTANCE=<n> to run several at once: each number has its own monitor and QMP ports
# (4444 + 10n, 4445 + 10n); the file qemu/instance sets it for a working copy.
# touch.py, screendump.sh and stop.sh follow the same rule.
@@ -58,11 +61,21 @@ if [ -n "$QEMU_NET" ] && [ "$QEMU_NET" != 0 ]; then
NET=(-netdev user,id=net0 -device virtio-net-device,netdev=net0)
CMDLINE="virtio_net=1 $CMDLINE"
fi
+# the sound device (QEMU 8.2 or newer has one for this machine): the kernel's sink for /dev/redwood-audio
+SOUND=()
+if qemu-system-aarch64 -device help 2>/dev/null | grep -q '"virtio-sound-device"'; then
+ case "${AUDIO:-none}" in
+ *.wav) SOUND=(-audiodev "wav,id=snd,path=$(readlink -f "$AUDIO")") ;;
+ *) SOUND=(-audiodev "${AUDIO:-none},id=snd") ;;
+ esac
+ SOUND+=(-device virtio-sound-device,audiodev=snd,streams=1) # one stream: output, no microphone of the host
+ CMDLINE="virtio_sound=1 $CMDLINE"
+fi
xnuloader/mkpayload.py -o qemu/payload.bin kernel=$K ramdisk=$ROOT --rdres "$(printf '%x' "$(stat -c %s "$IMG")")" \
--cmdline "loader_return_after=$SECS epoch=$(date +%s) virtio_input=1 $CMDLINE" >/dev/null || exit 1
QEMU_MEM=${RAM_GB}G exec qemu/run.sh $((SECS + 40)) \
-object memory-backend-file,id=ram,size=$RAM_SIZE,mem-path="$PWD/qemu/ram.img",share=off \
-machine memory-backend=ram \
- -global virtio-mmio.force-legacy=false -device virtio-tablet-device -device virtio-keyboard-device "${NET[@]}" \
+ -global virtio-mmio.force-legacy=false -device virtio-tablet-device -device virtio-keyboard-device "${NET[@]}" "${SOUND[@]}" \
-device ramfb -monitor tcp:127.0.0.1:$((4444 + 10 * INSTANCE)),server,nowait \
-qmp tcp:127.0.0.1:$((4445 + 10 * INSTANCE)),server,nowait "$@"
diff --git a/tools/sound.py b/tools/sound.py
new file mode 100755
index 0000000..585ad23
--- /dev/null
+++ b/tools/sound.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python3
+"""Make a WAV file of a sound recording left by the kernel (boot-arg rec_sound=N).
+
+usage: sound.py RECORDING OUT.wav
+
+RECORDING is the piece of memory as the loader hands it over ("xl sound"; under QEMU, the
+monitor's pmemsave at the address in the kernel's line "redwood-audio: recording ... at 0x..."):
+a header { "RWSND001", rate, channels, blocks }, then for every write to /dev/redwood-audio the
+time it was made (nanoseconds since the kernel started), the number of frames and the frames
+themselves, 16-bit samples. iOS writes only while something plays, so the recording holds no
+silence: here the blocks are put back where they were in time, with silence in between
+(a pause of more than a quarter of a second is a pause; anything shorter is the writer being
+early or late, and the blocks are joined).
+
+Says how long and how loud the result is, which is the way to tell sound from nothing without
+listening: a file that peaks at -90 dBFS is silence that was "played".
+"""
+import array
+import math
+import struct
+import sys
+import wave
+
+PAUSE = 0.25
+
+
+def db(level):
+ return f'{20 * math.log10(level / 32768):.1f} dBFS' if level > 0 else 'silence'
+
+
+def main():
+ if len(sys.argv) != 3:
+ sys.exit(__doc__)
+ data = open(sys.argv[1], 'rb').read()
+ magic, rate, channels, blocks, _ = struct.unpack_from('<8sIIII', data, 0)
+ if magic != b'RWSND001' or not rate or not channels:
+ sys.exit('sound.py: not a sound recording')
+ frame = 2 * channels
+ out = wave.open(sys.argv[2], 'wb')
+ out.setnchannels(channels)
+ out.setsampwidth(2)
+ out.setframerate(rate)
+ at, first, end, played, pauses = 24, None, 0, 0, 0 # end: frames written to the file so far
+ samples = array.array('h')
+ for _ in range(blocks):
+ if at + 16 > len(data):
+ break
+ nanoseconds, frames, _ = struct.unpack_from('<QII', data, at)
+ pcm = data[at + 16:at + 16 + frames * frame]
+ at += 16 + frames * frame
+ if len(pcm) < frames * frame:
+ break
+ if first is None:
+ first = nanoseconds
+ position = (nanoseconds - first) * rate // 10**9
+ if position > end + PAUSE * rate:
+ out.writeframes(bytes((position - end) * frame))
+ end = position
+ pauses += 1
+ out.writeframes(pcm)
+ samples.frombytes(pcm)
+ end += frames
+ played += frames
+ out.close()
+ if not played:
+ sys.exit('sound.py: the recording is empty: nothing was played')
+ peak = max(max(samples), -min(samples))
+ rms = math.sqrt(sum(s * s for s in samples) / len(samples))
+ print(f'sound.py: {sys.argv[2]}: {end / rate:.1f} s, of which {played / rate:.1f} s of sound in {pauses + 1} '
+ f'stretch{"es" if pauses else ""}; peak {db(peak)}, RMS {db(rms)}')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/update-prebuilt.sh b/tools/update-prebuilt.sh
index 2fe9ba4..b5f0c48 100755
--- a/tools/update-prebuilt.sh
+++ b/tools/update-prebuilt.sh
@@ -10,7 +10,8 @@ mkdir -p prebuilt/userland prebuilt/uefi
cp $K prebuilt/mach.development.bcm2837
cp xnuloader/loader.efi prebuilt/loader.efi
cp userland/hello prebuilt/userland/
-for f in init child stage0 hello_dyn svc_test surf_test rwtouch mg_test cpu_test libredwood.dylib libredwood-sb.dylib libredwood-media.dylib; do
+for f in init child stage0 hello_dyn svc_test surf_test rwtouch mg_test cpu_test sound_test libredwood.dylib libredwood-sb.dylib \
+ libredwood-media.dylib RedwoodAudio; do
cp userland/obj/$f prebuilt/userland/
done
if [ -f $MU/Build/redwoodPkg/RELEASE_CLANGPDB/FV/SILICIUM_UEFI.fd ]; then
diff --git a/userland/build.sh b/userland/build.sh
index c3434b1..0a7c267 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -106,6 +106,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then
$DYN -framework CoreFoundation -o obj/svc_test src/svc_test.c
$DYN -Wno-four-char-constants -framework CoreFoundation -framework IOSurface -o obj/surf_test src/surf_test.c
$DYN -o obj/rwtouch src/rwtouch.c
+ $DYN -framework CoreFoundation -framework AudioToolbox -framework AVFoundation -lobjc -o obj/sound_test src/sound_test.c
$DYN -framework CoreFoundation -lMobileGestalt -o obj/mg_test src/mg_test.c
$DYN -o obj/cpu_test src/cpu_test.c
$DYN -framework CoreFoundation -framework IOSurface -o obj/cow_test src/cow_test.c
@@ -126,6 +127,8 @@ if [ -d $IOS/rootfs/usr/lib ]; then
-o obj/libredwood-sb.dylib src/inject_sb.c
$DYN -dynamiclib -install_name /redwood/lib/libredwood-media.dylib -framework CoreFoundation -lobjc \
-o obj/libredwood-media.dylib src/inject_media.c
+ # the output device of the audio server: a plug-in for CoreAudio's HAL
+ $DYN -bundle -framework CoreFoundation -o obj/RedwoodAudio src/audio_plugin.c
if [ -n "$GL" ]; then
# the OpenGL ES library, and the table of functions that hands CoreAnimation's calls to it
# (Mesa says beside the library what it was built from: one from other patches is built again)
@@ -147,10 +150,16 @@ if [ -d $IOS/rootfs/usr/lib ]; then
[ -f obj/net_test ] && cp obj/net_test root/bin/
[ -f obj/keychain_test ] && cp obj/keychain_test root/bin/
[ -f obj/wifipw_test ] && cp obj/wifipw_test root/bin/
+ [ -f obj/sound_test ] && cp obj/sound_test root/bin/
for f in $EXTRA; do cp "$f" root/bin/; done
mkdir -p root/lib
cp obj/libredwood.dylib obj/libredwood-sb.dylib root/lib/
[ -f obj/libredwood-media.dylib ] && cp obj/libredwood-media.dylib root/lib/ # (as with cpu_test)
+ if [ -f obj/RedwoodAudio ]; then
+ mkdir -p root/lib/RedwoodAudio.driver
+ cp obj/RedwoodAudio root/lib/RedwoodAudio.driver/
+ cp src/audio_plugin.plist root/lib/RedwoodAudio.driver/Info.plist
+ fi
if [ -n "$GL" ]; then
cp obj/libOSMesa.8.dylib root/lib/
cp obj/gl_test root/bin/
@@ -218,6 +227,9 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
fi
mv sysroot/sbin/launchd sysroot/sbin/launchd.ios
cp obj/stage0 sysroot/sbin/launchd
+ # our audio device, where the audio server looks for such plug-ins (and for no others: Apple's are left out)
+ mkdir -p sysroot/System/Library/Audio/Plug-Ins/HAL
+ ln -s /redwood/lib/RedwoodAudio.driver sysroot/System/Library/Audio/Plug-Ins/HAL/RedwoodAudio.driver
# the OS cryptex: on a device a separate volume grafted at /System/Cryptexes/OS. It holds the dyld
# shared cache (dyld looks there first) and a few libraries and resources outside the cache.
mkdir -p sysroot/System/Cryptexes
diff --git a/userland/rc.jobs.sound b/userland/rc.jobs.sound
new file mode 100644
index 0000000..f994b21
--- /dev/null
+++ b/userland/rc.jobs.sound
@@ -0,0 +1,7 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# Something to hear (RC_JOBS=rc.jobs.sound): once SpringBoard is up, a tone of three seconds (1000 Hz on the
+# left, 440 Hz on the right), then the lock sound, a keyboard click and a fanfare, as SpringBoard would play
+# them. What comes out of /dev/redwood-audio shows whether it worked: the kernel's "redwood-audio:" lines,
+# a recording (boot-arg rec_sound=N), QEMU's sound device.
+/redwood/bin/sound_test springboard wait 5 tone 3 wait 3 system 1100 wait 3 system 1104 wait 3 file /System/Library/Audio/UISounds/New/Fanfare.caf
+/bin/ps -A
diff --git a/userland/src/audio_plugin.c b/userland/src/audio_plugin.c
new file mode 100644
index 0000000..2ab8dd6
--- /dev/null
+++ b/userland/src/audio_plugin.c
@@ -0,0 +1,704 @@
+/*
+ * RedwoodAudio.driver: the output device of iOS on this machine.
+ *
+ * Audio devices reach the audio server's hardware layer (the "HAL" of CoreAudio, inside
+ * mediaserverd) in two ways: kernel drivers through IOAudio2 -- that is how an iPhone's own
+ * speaker, codec and microphones arrive, and none of it exists here -- and plug-ins in
+ * /System/Library/Audio/Plug-Ins/HAL, which is how Bluetooth, AirPlay, CarPlay and USB audio
+ * arrive. This is such a plug-in. Its interface is the one macOS documents for user-space
+ * audio drivers (AudioServerPlugIn: a CFPlugIn with one COM-style table of functions; the HAL
+ * asks for objects and their properties, then runs the I/O); the iOS SDK has no header for it,
+ * so what is needed of it is declared below.
+ *
+ * Above the HAL sits the virtual audio device (/Library/Audio/Plug-Ins/HAL/VirtualAudio.plugin),
+ * through which everything in iOS is routed. It knows an iPhone's own devices by their names
+ * (their UIDs) and makes a port of each: "Speaker" becomes the speaker port, the route an iPhone
+ * with nothing plugged in plays through, with a volume, the volume display and the place in
+ * Control Center that go with it. So this presents two devices under those names:
+ *
+ * "Speaker" a stereo output whose samples are written to /dev/redwood-audio, where the
+ * kernel takes them to whatever can play them (see sys/redwood_audio.h), and a
+ * stereo input that is always silent (on an iPhone: what the amplifiers measure)
+ * "Hawking" one silent input and nothing else. The virtual audio device will not build the
+ * speaker route without a second virtual device of its own (its type 'vhaw', a
+ * mandatory companion of the default one in the iPhone X's routing tables) whose
+ * only port is this input; without it everything stays on the null port.
+ *
+ * CoreAudio's own null device ("Null_Device", described in the framework's Info.plist, playing
+ * into nowhere) stays as it is: it is the only device the virtual audio device takes while it
+ * starts, and ours are taken a moment later, when it says it allows all devices.
+ *
+ * What is played through the speaker port passes the iPhone X's chain of effects for its own
+ * speakers first ('d22_speaker_general', from /Library/Audio/Tunings/D22: equalisers,
+ * compressors, speaker protection), at the volume iOS has set.
+ *
+ * The device has no clock but the system's: it claims to play exactly REDWOOD_AUDIO_RATE frames
+ * a second of mach_absolute_time(), and the HAL's I/O thread paces itself by that.
+ */
+#include <CoreFoundation/CoreFoundation.h>
+#include <CoreFoundation/CFPlugInCOM.h>
+#include <CoreAudioTypes/CoreAudioTypes.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <mach/mach_time.h>
+#include <pthread.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "redwood_audio.h"
+
+/* ---- the AudioServerPlugIn interface (macOS: CoreAudio/AudioServerPlugIn.h, AudioHardwareBase.h) ---- */
+
+typedef UInt32 AudioObjectID;
+typedef struct {
+ UInt32 mSelector, mScope, mElement;
+} AudioObjectPropertyAddress;
+typedef struct {
+ AudioStreamBasicDescription mFormat;
+ AudioValueRange mSampleRateRange;
+} AudioStreamRangedDescription;
+typedef struct {
+ UInt32 mClientID;
+ pid_t mProcessID;
+ Boolean mIsNativeEndian;
+ CFStringRef mBundleID;
+} AudioServerPlugInClientInfo;
+typedef struct {
+ UInt64 mIOCycleCounter;
+ UInt32 mNominalIOBufferFrameSize;
+ AudioTimeStamp mCurrentTime, mInputTime, mOutputTime;
+ Float64 mMasterHostTicksPerFrame, mDeviceHostTicksPerFrame;
+} AudioServerPlugInIOCycleInfo;
+
+typedef struct AudioServerPlugInHostInterface AudioServerPlugInHostInterface;
+typedef const AudioServerPlugInHostInterface *AudioServerPlugInHostRef;
+struct AudioServerPlugInHostInterface {
+ OSStatus (*PropertiesChanged)(AudioServerPlugInHostRef, AudioObjectID, UInt32, const AudioObjectPropertyAddress *);
+ OSStatus (*CopyFromStorage)(AudioServerPlugInHostRef, CFStringRef, CFPropertyListRef *);
+ OSStatus (*WriteToStorage)(AudioServerPlugInHostRef, CFStringRef, CFPropertyListRef);
+ OSStatus (*DeleteFromStorage)(AudioServerPlugInHostRef, CFStringRef);
+ OSStatus (*RequestDeviceConfigurationChange)(AudioServerPlugInHostRef, AudioObjectID, UInt64, void *);
+};
+
+typedef struct AudioServerPlugInDriverInterface AudioServerPlugInDriverInterface;
+typedef AudioServerPlugInDriverInterface **AudioServerPlugInDriverRef;
+struct AudioServerPlugInDriverInterface {
+ void *_reserved;
+ HRESULT (*QueryInterface)(void *, REFIID, LPVOID *);
+ ULONG (*AddRef)(void *);
+ ULONG (*Release)(void *);
+ OSStatus (*Initialize)(AudioServerPlugInDriverRef, AudioServerPlugInHostRef);
+ OSStatus (*CreateDevice)(AudioServerPlugInDriverRef, CFDictionaryRef, const AudioServerPlugInClientInfo *, AudioObjectID *);
+ OSStatus (*DestroyDevice)(AudioServerPlugInDriverRef, AudioObjectID);
+ OSStatus (*AddDeviceClient)(AudioServerPlugInDriverRef, AudioObjectID, const AudioServerPlugInClientInfo *);
+ OSStatus (*RemoveDeviceClient)(AudioServerPlugInDriverRef, AudioObjectID, const AudioServerPlugInClientInfo *);
+ OSStatus (*PerformDeviceConfigurationChange)(AudioServerPlugInDriverRef, AudioObjectID, UInt64, void *);
+ OSStatus (*AbortDeviceConfigurationChange)(AudioServerPlugInDriverRef, AudioObjectID, UInt64, void *);
+ Boolean (*HasProperty)(AudioServerPlugInDriverRef, AudioObjectID, pid_t, const AudioObjectPropertyAddress *);
+ OSStatus (*IsPropertySettable)(AudioServerPlugInDriverRef, AudioObjectID, pid_t, const AudioObjectPropertyAddress *, Boolean *);
+ OSStatus (*GetPropertyDataSize)(AudioServerPlugInDriverRef, AudioObjectID, pid_t, const AudioObjectPropertyAddress *, UInt32,
+ const void *, UInt32 *);
+ OSStatus (*GetPropertyData)(AudioServerPlugInDriverRef, AudioObjectID, pid_t, const AudioObjectPropertyAddress *, UInt32,
+ const void *, UInt32, UInt32 *, void *);
+ OSStatus (*SetPropertyData)(AudioServerPlugInDriverRef, AudioObjectID, pid_t, const AudioObjectPropertyAddress *, UInt32,
+ const void *, UInt32, const void *);
+ OSStatus (*StartIO)(AudioServerPlugInDriverRef, AudioObjectID, UInt32);
+ OSStatus (*StopIO)(AudioServerPlugInDriverRef, AudioObjectID, UInt32);
+ OSStatus (*GetZeroTimeStamp)(AudioServerPlugInDriverRef, AudioObjectID, UInt32, Float64 *, UInt64 *, UInt64 *);
+ OSStatus (*WillDoIOOperation)(AudioServerPlugInDriverRef, AudioObjectID, UInt32, UInt32, Boolean *, Boolean *);
+ OSStatus (*BeginIOOperation)(AudioServerPlugInDriverRef, AudioObjectID, UInt32, UInt32, UInt32,
+ const AudioServerPlugInIOCycleInfo *);
+ OSStatus (*DoIOOperation)(AudioServerPlugInDriverRef, AudioObjectID, AudioObjectID, UInt32, UInt32, UInt32,
+ const AudioServerPlugInIOCycleInfo *, void *, void *);
+ OSStatus (*EndIOOperation)(AudioServerPlugInDriverRef, AudioObjectID, UInt32, UInt32, UInt32,
+ const AudioServerPlugInIOCycleInfo *);
+};
+
+#define kAudioServerPlugInTypeUUID CFUUIDGetConstantUUIDWithBytes(NULL, 0x44, 0x3A, 0xBA, 0xB8, 0xE7, 0xB3, 0x49, 0x1A, \
+ 0xB9, 0x85, 0xBE, 0xB9, 0x18, 0x70, 0x30, 0xDB)
+#define kAudioServerPlugInDriverInterfaceUUID CFUUIDGetConstantUUIDWithBytes(NULL, 0xEE, 0xA5, 0x77, 0x3D, 0xCC, 0x43, \
+ 0x49, 0xF1, 0x8E, 0x00, 0x8F, 0x96, 0xE7, 0xD2, 0x3B, 0x17)
+
+enum {
+ kAudioHardwareUnknownPropertyError = 'who?', kAudioHardwareBadPropertySizeError = '!siz',
+ kAudioHardwareIllegalOperationError = 'nope', kAudioHardwareBadObjectError = '!obj',
+ kAudioHardwareUnsupportedOperationError = 'unop', kAudioDeviceUnsupportedFormatError = '!dat',
+
+ kAudioObjectPropertyScopeGlobal = 'glob', kAudioObjectPropertyScopeInput = 'inpt', kAudioObjectPropertyScopeOutput = 'outp',
+
+ kAudioObjectClassID = 'aobj', kAudioPlugInClassID = 'aplg', kAudioDeviceClassID = 'adev', kAudioStreamClassID = 'astr',
+
+ kAudioObjectPropertyBaseClass = 'bcls', kAudioObjectPropertyClass = 'clas', kAudioObjectPropertyOwner = 'stdv',
+ kAudioObjectPropertyName = 'lnam', kAudioObjectPropertyManufacturer = 'lmak', kAudioObjectPropertyOwnedObjects = 'ownd',
+
+ kAudioPlugInPropertyBoxList = 'box#', kAudioPlugInPropertyTranslateUIDToBox = 'uidb',
+ kAudioPlugInPropertyDeviceList = 'dev#', kAudioPlugInPropertyTranslateUIDToDevice = 'uidd',
+ kAudioPlugInPropertyResourceBundle = 'rsrc',
+
+ kAudioDevicePropertyDeviceUID = 'uid ', kAudioDevicePropertyModelUID = 'muid', kAudioDevicePropertyTransportType = 'tran',
+ kAudioDevicePropertyRelatedDevices = 'akin', kAudioDevicePropertyClockDomain = 'clkd',
+ kAudioDevicePropertyDeviceIsAlive = 'livn', kAudioDevicePropertyDeviceIsRunning = 'goin',
+ kAudioDevicePropertyDeviceCanBeDefaultDevice = 'dflt', kAudioDevicePropertyDeviceCanBeDefaultSystemDevice = 'sflt',
+ kAudioDevicePropertyLatency = 'ltnc', kAudioDevicePropertyStreams = 'stm#', kAudioObjectPropertyControlList = 'ctrl',
+ kAudioDevicePropertySafetyOffset = 'saft', kAudioDevicePropertyNominalSampleRate = 'nsrt',
+ kAudioDevicePropertyAvailableNominalSampleRates = 'nsr#', kAudioDevicePropertyIsHidden = 'hidn',
+ kAudioDevicePropertyPreferredChannelsForStereo = 'dch2', kAudioDevicePropertyZeroTimeStampPeriod = 'ring',
+
+ kAudioStreamPropertyIsActive = 'sact', kAudioStreamPropertyDirection = 'sdir', kAudioStreamPropertyTerminalType = 'term',
+ kAudioStreamPropertyStartingChannel = 'schn', kAudioStreamPropertyLatency = 'ltnc',
+ kAudioStreamPropertyVirtualFormat = 'sfmt', kAudioStreamPropertyAvailableVirtualFormats = 'sfma',
+ kAudioStreamPropertyPhysicalFormat = 'pft ', kAudioStreamPropertyAvailablePhysicalFormats = 'pfta',
+
+ kAudioStreamTerminalTypeSpeaker = 'spkr', kAudioStreamTerminalTypeMicrophone = 'micr',
+
+ kAudioServerPlugInIOOperationReadInput = 'read', kAudioServerPlugInIOOperationWriteMix = 'rite',
+};
+
+/* ---- the devices ---------------------------------------------------------------------------------- */
+
+/*
+ * Object numbers are ours to choose. A device's streams follow it: its output (if it has one),
+ * then its input.
+ */
+enum {
+ OBJECT_PLUGIN = 1,
+ OBJECT_SPEAKER, OBJECT_SPEAKER_OUTPUT, OBJECT_SPEAKER_INPUT,
+ OBJECT_HAWKING, OBJECT_HAWKING_INPUT,
+ OBJECT_LAST = OBJECT_HAWKING_INPUT
+};
+
+struct device {
+ AudioObjectID object;
+ CFStringRef uid; /* the name the virtual audio device knows it by */
+ AudioObjectID output, input; /* its streams; 0 = none */
+ UInt32 input_channels;
+ /* its clock, running while anybody does I/O */
+ unsigned running; /* how many clients have started I/O */
+ UInt64 anchor; /* mach_absolute_time() when the first of them did */
+ UInt64 periods; /* whole timestamp periods since */
+};
+
+static struct device devices[] = {
+ { .object = OBJECT_SPEAKER, .uid = CFSTR("Speaker"), .output = OBJECT_SPEAKER_OUTPUT, .input = OBJECT_SPEAKER_INPUT,
+ .input_channels = 2 },
+ { .object = OBJECT_HAWKING, .uid = CFSTR("Hawking"), .input = OBJECT_HAWKING_INPUT, .input_channels = 1 },
+};
+#define DEVICES (sizeof(devices) / sizeof(devices[0]))
+
+#define TRANSPORT_BUILT_IN 'bltn'
+#define MANUFACTURER CFSTR("redwood")
+/* how often a device says where its clock is, in frames: also the largest I/O buffer the HAL will use */
+#define TIMESTAMP_PERIOD 16384
+
+static AudioServerPlugInHostRef host;
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+static int sink = -1; /* /dev/redwood-audio */
+static Float64 ticks_per_frame;
+static UInt64 written, refused; /* frames since the speaker's I/O started */
+static Boolean inactive[OBJECT_LAST + 1]; /* streams the HAL has switched off */
+
+static void
+say(const char *format, ...)
+{
+ char line[256];
+ va_list ap;
+ int n;
+
+ va_start(ap, format);
+ n = vsnprintf(line, sizeof(line), format, ap);
+ va_end(ap);
+ if (n > (int)sizeof(line) - 1) {
+ n = sizeof(line) - 1;
+ }
+ write(2, line, (size_t)n);
+}
+
+/* The device with this object number, or the one this stream belongs to; NULL = neither. */
+static struct device *
+device_of(AudioObjectID object)
+{
+ for (unsigned i = 0; i < DEVICES; i++) {
+ if (object == devices[i].object || (object != 0 && (object == devices[i].output || object == devices[i].input))) {
+ return &devices[i];
+ }
+ }
+ return NULL;
+}
+
+/* The one format of a stream: 16-bit samples at REDWOOD_AUDIO_RATE, so many channels. */
+static AudioStreamBasicDescription
+format_of(AudioObjectID stream)
+{
+ const struct device *d = device_of(stream);
+ UInt32 channels = stream == d->output ? REDWOOD_AUDIO_CHANNELS : d->input_channels;
+
+ return (AudioStreamBasicDescription) {
+ .mSampleRate = REDWOOD_AUDIO_RATE, .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
+ .mBytesPerPacket = 2 * channels, .mFramesPerPacket = 1, .mBytesPerFrame = 2 * channels,
+ .mChannelsPerFrame = channels, .mBitsPerChannel = 16,
+ };
+}
+
+static HRESULT
+query_interface(void *driver, REFIID uuid, LPVOID *interface)
+{
+ CFUUIDRef requested = CFUUIDCreateFromUUIDBytes(NULL, uuid);
+ Boolean ours = CFEqual(requested, IUnknownUUID) || CFEqual(requested, kAudioServerPlugInDriverInterfaceUUID);
+
+ CFRelease(requested);
+ if (interface == NULL || !ours) {
+ return E_NOINTERFACE;
+ }
+ *interface = driver; /* one driver, never freed: no counting */
+ return S_OK;
+}
+
+static ULONG
+add_ref(__unused void *driver)
+{
+ return 1;
+}
+
+static ULONG
+release(__unused void *driver)
+{
+ return 1;
+}
+
+static OSStatus
+initialize(__unused AudioServerPlugInDriverRef driver, AudioServerPlugInHostRef the_host)
+{
+ mach_timebase_info_data_t timebase;
+
+ host = the_host;
+ mach_timebase_info(&timebase);
+ ticks_per_frame = 1e9 * timebase.denom / timebase.numer / REDWOOD_AUDIO_RATE;
+ sink = open(REDWOOD_AUDIO_DEVICE, O_WRONLY);
+ say("redwood: audio: the speaker is with the HAL (%s: %s)\n", REDWOOD_AUDIO_DEVICE, sink < 0 ? strerror(errno) : "open");
+ return 0;
+}
+
+static OSStatus
+create_device(__unused AudioServerPlugInDriverRef driver, __unused CFDictionaryRef description,
+ __unused const AudioServerPlugInClientInfo *client, __unused AudioObjectID *object)
+{
+ return kAudioHardwareUnsupportedOperationError;
+}
+
+static OSStatus
+destroy_device(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object)
+{
+ return kAudioHardwareUnsupportedOperationError;
+}
+
+static OSStatus
+device_client(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object,
+ __unused const AudioServerPlugInClientInfo *client)
+{
+ return 0;
+}
+
+static OSStatus
+configuration_change(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object, __unused UInt64 action,
+ __unused void *info)
+{
+ return 0;
+}
+
+/* A property's value: `size` bytes of it, or a list with nothing in it, or (neither) no such property. */
+struct value {
+ UInt32 size;
+ bool empty;
+ bool string; /* a CFString: whoever is given it gets a reference of their own */
+ union {
+ UInt32 words[4];
+ Float64 number;
+ CFStringRef string;
+ AudioValueRange range;
+ AudioStreamBasicDescription format;
+ AudioStreamRangedDescription ranged;
+ } u;
+};
+
+static struct value
+property(AudioObjectID object, const AudioObjectPropertyAddress *address, UInt32 qualifier_size, const void *qualifier)
+{
+ UInt32 selector = address->mSelector, scope = address->mScope;
+ const struct device *d = device_of(object);
+ struct value v;
+
+ memset(&v, 0, sizeof(v));
+#define WORD(x) (v.u.words[0] = (x), v.size = sizeof(UInt32))
+#define LIST(x) (v.u.words[v.size / sizeof(UInt32)] = (x), v.size += sizeof(UInt32))
+#define STRING(x) (v.u.string = (x), v.string = true, v.size = sizeof(CFStringRef))
+ if (object != OBJECT_PLUGIN && d == NULL) {
+ return v;
+ }
+ switch (selector) {
+ case kAudioObjectPropertyBaseClass:
+ WORD(kAudioObjectClassID);
+ return v;
+ case kAudioObjectPropertyClass:
+ WORD(object == OBJECT_PLUGIN ? kAudioPlugInClassID : object == d->object ? kAudioDeviceClassID : kAudioStreamClassID);
+ return v;
+ case kAudioObjectPropertyOwner:
+ WORD(object == OBJECT_PLUGIN ? 0 : object == d->object ? OBJECT_PLUGIN : d->object);
+ return v;
+ }
+ if (object == OBJECT_PLUGIN) {
+ switch (selector) {
+ case kAudioObjectPropertyManufacturer:
+ STRING(MANUFACTURER);
+ break;
+ case kAudioObjectPropertyOwnedObjects:
+ case kAudioPlugInPropertyDeviceList:
+ for (unsigned i = 0; i < DEVICES; i++) {
+ LIST(devices[i].object);
+ }
+ break;
+ case kAudioPlugInPropertyBoxList:
+ v.empty = true;
+ break;
+ case kAudioPlugInPropertyTranslateUIDToBox:
+ WORD(0);
+ break;
+ case kAudioPlugInPropertyTranslateUIDToDevice:
+ WORD(0);
+ for (unsigned i = 0; i < DEVICES; i++) {
+ if (qualifier_size == sizeof(CFStringRef) && qualifier != NULL && *(const CFStringRef *)qualifier != NULL &&
+ CFEqual(*(const CFStringRef *)qualifier, devices[i].uid)) {
+ WORD(devices[i].object);
+ }
+ }
+ break;
+ case kAudioPlugInPropertyResourceBundle:
+ STRING(CFSTR(""));
+ break;
+ }
+ } else if (object == d->object) {
+ switch (selector) {
+ case kAudioObjectPropertyManufacturer:
+ STRING(MANUFACTURER);
+ break;
+ case kAudioObjectPropertyName:
+ case kAudioDevicePropertyDeviceUID:
+ case kAudioDevicePropertyModelUID:
+ STRING(d->uid);
+ break;
+ case kAudioDevicePropertyTransportType:
+ WORD(TRANSPORT_BUILT_IN);
+ break;
+ case kAudioDevicePropertyRelatedDevices:
+ WORD(d->object);
+ break;
+ case kAudioDevicePropertyClockDomain:
+ case kAudioDevicePropertyLatency:
+ case kAudioDevicePropertySafetyOffset:
+ case kAudioDevicePropertyIsHidden:
+ WORD(0);
+ break;
+ case kAudioDevicePropertyDeviceIsAlive:
+ WORD(1);
+ break;
+ case kAudioDevicePropertyDeviceCanBeDefaultDevice:
+ case kAudioDevicePropertyDeviceCanBeDefaultSystemDevice:
+ WORD(d->output != 0);
+ break;
+ case kAudioDevicePropertyDeviceIsRunning:
+ WORD(d->running != 0);
+ break;
+ case kAudioObjectPropertyOwnedObjects:
+ case kAudioDevicePropertyStreams:
+ /* the streams of one direction, or of both */
+ if (scope != kAudioObjectPropertyScopeInput && d->output != 0) {
+ LIST(d->output);
+ }
+ if (scope != kAudioObjectPropertyScopeOutput && d->input != 0) {
+ LIST(d->input);
+ }
+ v.empty = v.size == 0;
+ break;
+ case kAudioObjectPropertyControlList:
+ v.empty = true;
+ break;
+ case kAudioDevicePropertyNominalSampleRate:
+ v.u.number = REDWOOD_AUDIO_RATE;
+ v.size = sizeof(Float64);
+ break;
+ case kAudioDevicePropertyAvailableNominalSampleRates:
+ v.u.range.mMinimum = v.u.range.mMaximum = REDWOOD_AUDIO_RATE;
+ v.size = sizeof(AudioValueRange);
+ break;
+ case kAudioDevicePropertyPreferredChannelsForStereo:
+ v.u.words[0] = 1;
+ v.u.words[1] = 2;
+ v.size = 2 * sizeof(UInt32);
+ break;
+ case kAudioDevicePropertyZeroTimeStampPeriod:
+ WORD(TIMESTAMP_PERIOD);
+ break;
+ }
+ } else {
+ switch (selector) {
+ case kAudioStreamPropertyIsActive:
+ WORD(!inactive[object]);
+ break;
+ case kAudioStreamPropertyDirection:
+ WORD(object == d->input);
+ break;
+ case kAudioStreamPropertyTerminalType:
+ WORD(object == d->input ? kAudioStreamTerminalTypeMicrophone : kAudioStreamTerminalTypeSpeaker);
+ break;
+ case kAudioStreamPropertyStartingChannel:
+ WORD(1);
+ break;
+ case kAudioStreamPropertyLatency:
+ WORD(0);
+ break;
+ case kAudioStreamPropertyVirtualFormat:
+ case kAudioStreamPropertyPhysicalFormat:
+ v.u.format = format_of(object);
+ v.size = sizeof(AudioStreamBasicDescription);
+ break;
+ case kAudioStreamPropertyAvailableVirtualFormats:
+ case kAudioStreamPropertyAvailablePhysicalFormats:
+ v.u.ranged.mFormat = format_of(object);
+ v.u.ranged.mSampleRateRange.mMinimum = v.u.ranged.mSampleRateRange.mMaximum = REDWOOD_AUDIO_RATE;
+ v.size = sizeof(AudioStreamRangedDescription);
+ break;
+ }
+ }
+#undef WORD
+#undef LIST
+#undef STRING
+ return v;
+}
+
+static Boolean
+has_property(__unused AudioServerPlugInDriverRef driver, AudioObjectID object, __unused pid_t client,
+ const AudioObjectPropertyAddress *address)
+{
+ struct value v;
+
+ if (address == NULL) {
+ return false;
+ }
+ v = property(object, address, 0, NULL);
+ return v.size != 0 || v.empty;
+}
+
+static OSStatus
+is_property_settable(AudioServerPlugInDriverRef driver, AudioObjectID object, pid_t client,
+ const AudioObjectPropertyAddress *address, Boolean *settable)
+{
+ const struct device *d = device_of(object);
+
+ if (address == NULL || settable == NULL || !has_property(driver, object, client, address)) {
+ return kAudioHardwareUnknownPropertyError;
+ }
+ /* the rate and the formats can be "set", to the ones there are */
+ if (object == OBJECT_PLUGIN) {
+ *settable = false;
+ } else if (object == d->object) {
+ *settable = address->mSelector == kAudioDevicePropertyNominalSampleRate;
+ } else {
+ *settable = address->mSelector == kAudioStreamPropertyIsActive ||
+ address->mSelector == kAudioStreamPropertyVirtualFormat || address->mSelector == kAudioStreamPropertyPhysicalFormat;
+ }
+ return 0;
+}
+
+static OSStatus
+get_property_data_size(AudioServerPlugInDriverRef driver, AudioObjectID object, pid_t client,
+ const AudioObjectPropertyAddress *address, UInt32 qualifier_size, const void *qualifier, UInt32 *size)
+{
+ if (address == NULL || size == NULL || !has_property(driver, object, client, address)) {
+ return object < OBJECT_PLUGIN || object > OBJECT_LAST ? kAudioHardwareBadObjectError : kAudioHardwareUnknownPropertyError;
+ }
+ *size = property(object, address, qualifier_size, qualifier).size;
+ return 0;
+}
+
+static OSStatus
+get_property_data(AudioServerPlugInDriverRef driver, AudioObjectID object, pid_t client,
+ const AudioObjectPropertyAddress *address, UInt32 qualifier_size, const void *qualifier, UInt32 room, UInt32 *size,
+ void *data)
+{
+ struct value v;
+
+ if (address == NULL || size == NULL || data == NULL || !has_property(driver, object, client, address)) {
+ return object < OBJECT_PLUGIN || object > OBJECT_LAST ? kAudioHardwareBadObjectError : kAudioHardwareUnknownPropertyError;
+ }
+ pthread_mutex_lock(&lock);
+ v = property(object, address, qualifier_size, qualifier);
+ pthread_mutex_unlock(&lock);
+ if (v.size > room) {
+ /* a list may be asked for in part; anything else must fit */
+ if (address->mSelector != kAudioObjectPropertyOwnedObjects && address->mSelector != kAudioDevicePropertyStreams &&
+ address->mSelector != kAudioPlugInPropertyDeviceList) {
+ return kAudioHardwareBadPropertySizeError;
+ }
+ v.size = room - room % sizeof(UInt32);
+ }
+ memcpy(data, &v.u, v.size);
+ if (v.string) {
+ CFRetain(v.u.string);
+ }
+ *size = v.size;
+ return 0;
+}
+
+static OSStatus
+set_property_data(AudioServerPlugInDriverRef driver, AudioObjectID object, pid_t client,
+ const AudioObjectPropertyAddress *address, __unused UInt32 qualifier_size, __unused const void *qualifier, UInt32 size,
+ const void *data)
+{
+ Boolean settable = false;
+
+ if (address == NULL || data == NULL || is_property_settable(driver, object, client, address, &settable) != 0) {
+ return kAudioHardwareUnknownPropertyError;
+ }
+ if (!settable) {
+ return kAudioHardwareIllegalOperationError;
+ }
+ if (address->mSelector == kAudioStreamPropertyIsActive) {
+ if (size != sizeof(UInt32)) {
+ return kAudioHardwareBadPropertySizeError;
+ }
+ pthread_mutex_lock(&lock);
+ inactive[object] = *(const UInt32 *)data == 0;
+ pthread_mutex_unlock(&lock);
+ return 0;
+ }
+ if (address->mSelector == kAudioDevicePropertyNominalSampleRate) {
+ return size == sizeof(Float64) && *(const Float64 *)data == REDWOOD_AUDIO_RATE ? 0 : kAudioDeviceUnsupportedFormatError;
+ }
+ /* a format: the one we have, or nothing */
+ if (size != sizeof(AudioStreamBasicDescription)) {
+ return kAudioHardwareBadPropertySizeError;
+ } else {
+ const AudioStreamBasicDescription *wanted = data, format = format_of(object);
+
+ return wanted->mSampleRate == format.mSampleRate && wanted->mFormatID == format.mFormatID &&
+ wanted->mChannelsPerFrame == format.mChannelsPerFrame && wanted->mBitsPerChannel == format.mBitsPerChannel &&
+ (wanted->mFormatFlags & kAudioFormatFlagIsSignedInteger) ? 0 : kAudioDeviceUnsupportedFormatError;
+ }
+}
+
+static OSStatus
+start_io(__unused AudioServerPlugInDriverRef driver, AudioObjectID object, __unused UInt32 client)
+{
+ struct device *d = device_of(object);
+
+ if (d == NULL || object != d->object) {
+ return kAudioHardwareBadObjectError;
+ }
+ pthread_mutex_lock(&lock);
+ if (d->running++ == 0) {
+ d->anchor = mach_absolute_time();
+ d->periods = 0;
+ if (d->output != 0) {
+ written = refused = 0;
+ say("redwood: audio: playing\n");
+ }
+ }
+ pthread_mutex_unlock(&lock);
+ return 0;
+}
+
+static OSStatus
+stop_io(__unused AudioServerPlugInDriverRef driver, AudioObjectID object, __unused UInt32 client)
+{
+ struct device *d = device_of(object);
+
+ if (d == NULL || object != d->object) {
+ return kAudioHardwareBadObjectError;
+ }
+ pthread_mutex_lock(&lock);
+ if (d->running != 0 && --d->running == 0 && d->output != 0) {
+ say("redwood: audio: stopped after %llu.%02llu s (%llu frames not taken by %s)\n", written / REDWOOD_AUDIO_RATE,
+ written % REDWOOD_AUDIO_RATE * 100 / REDWOOD_AUDIO_RATE, refused, REDWOOD_AUDIO_DEVICE);
+ }
+ pthread_mutex_unlock(&lock);
+ return 0;
+}
+
+/* Where the device's clock was last at a multiple of TIMESTAMP_PERIOD frames, and when. */
+static OSStatus
+get_zero_time_stamp(__unused AudioServerPlugInDriverRef driver, AudioObjectID object, __unused UInt32 client,
+ Float64 *sample_time, UInt64 *host_time, UInt64 *seed)
+{
+ Float64 ticks_per_period = ticks_per_frame * TIMESTAMP_PERIOD;
+ UInt64 now = mach_absolute_time();
+ struct device *d = device_of(object);
+
+ if (d == NULL) {
+ return kAudioHardwareBadObjectError;
+ }
+ pthread_mutex_lock(&lock);
+ while (now >= d->anchor + (UInt64)((Float64)(d->periods + 1) * ticks_per_period)) {
+ d->periods++;
+ }
+ *sample_time = (Float64)d->periods * TIMESTAMP_PERIOD;
+ *host_time = d->anchor + (UInt64)((Float64)d->periods * ticks_per_period);
+ *seed = 1;
+ pthread_mutex_unlock(&lock);
+ return 0;
+}
+
+static OSStatus
+will_do_io_operation(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object, __unused UInt32 client,
+ UInt32 operation, Boolean *will_do, Boolean *in_place)
+{
+ *will_do = operation == kAudioServerPlugInIOOperationReadInput || operation == kAudioServerPlugInIOOperationWriteMix;
+ *in_place = true;
+ return 0;
+}
+
+static OSStatus
+begin_end_io_operation(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object, __unused UInt32 client,
+ __unused UInt32 operation, __unused UInt32 frames, __unused const AudioServerPlugInIOCycleInfo *cycle)
+{
+ return 0;
+}
+
+static OSStatus
+do_io_operation(__unused AudioServerPlugInDriverRef driver, __unused AudioObjectID object, AudioObjectID stream,
+ __unused UInt32 client, UInt32 operation, UInt32 frames, __unused const AudioServerPlugInIOCycleInfo *cycle, void *buffer,
+ __unused void *secondary)
+{
+ const struct device *d = device_of(stream);
+
+ if (d == NULL) {
+ return kAudioHardwareBadObjectError;
+ }
+ if (operation == kAudioServerPlugInIOOperationReadInput) {
+ /* nobody is listening on this machine: silence */
+ memset(buffer, 0, (size_t)frames * 2 * d->input_channels);
+ } else if (operation == kAudioServerPlugInIOOperationWriteMix && stream == d->output) {
+ /* the kernel takes it all or leaves it: this thread must not wait */
+ if (sink < 0 || write(sink, buffer, (size_t)frames * REDWOOD_AUDIO_FRAME) < 0) {
+ refused += frames;
+ }
+ written += frames;
+ }
+ return 0;
+}
+
+static AudioServerPlugInDriverInterface interface = {
+ NULL, query_interface, add_ref, release, initialize, create_device, destroy_device, device_client, device_client,
+ configuration_change, configuration_change, has_property, is_property_settable, get_property_data_size,
+ get_property_data, set_property_data, start_io, stop_io, get_zero_time_stamp, will_do_io_operation,
+ begin_end_io_operation, do_io_operation, begin_end_io_operation,
+};
+static AudioServerPlugInDriverInterface *driver = &interface;
+
+/* The factory named in the bundle's Info.plist. */
+void *
+RedwoodAudio_Create(__unused CFAllocatorRef allocator, CFUUIDRef type)
+{
+ return CFEqual(type, kAudioServerPlugInTypeUUID) ? &driver : NULL;
+}
diff --git a/userland/src/audio_plugin.plist b/userland/src/audio_plugin.plist
new file mode 100644
index 0000000..a2eff0c
--- /dev/null
+++ b/userland/src/audio_plugin.plist
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleIdentifier</key>
+ <string>com.redwood.audio</string>
+ <key>CFBundleName</key>
+ <string>RedwoodAudio</string>
+ <key>CFBundleExecutable</key>
+ <string>RedwoodAudio</string>
+ <key>CFBundlePackageType</key>
+ <string>BNDL</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+ <key>CFBundleSupportedPlatforms</key>
+ <array>
+ <string>iPhoneOS</string>
+ </array>
+ <!-- an AudioServerPlugIn: the type the HAL asks for, and the function that makes our driver (audio_plugin.c) -->
+ <key>CFPlugInTypes</key>
+ <dict>
+ <key>443ABAB8-E7B3-491A-B985-BEB9187030DB</key>
+ <array>
+ <string>7C3E5B0A-52D4-4F6B-9E0D-6B1A2D7F0C51</string>
+ </array>
+ </dict>
+ <key>CFPlugInFactories</key>
+ <dict>
+ <key>7C3E5B0A-52D4-4F6B-9E0D-6B1A2D7F0C51</key>
+ <string>RedwoodAudio_Create</string>
+ </dict>
+</dict>
+</plist>
diff --git a/userland/src/redwood_audio.h b/userland/src/redwood_audio.h
new file mode 100644
index 0000000..9cefd3f
--- /dev/null
+++ b/userland/src/redwood_audio.h
@@ -0,0 +1,39 @@
+/*
+ * /dev/redwood-audio: the sound iOS plays, on its way to whatever can make it heard.
+ *
+ * iOS expects its output devices from Apple's audio drivers through IOAudio2; neither exists
+ * here. Instead the audio server (mediaserverd) is given a plug-in of ours that presents one
+ * stereo output device, and the plug-in writes what the server has mixed for that device
+ * here. In the kernel the samples go to a sink -- a driver for something that plays them --
+ * and, if asked for, into a recording that outlives the kernel (see dev/redwood_audio.c).
+ *
+ * write(): whole frames in the format below. Never blocks: the writer is the audio server's
+ * real-time thread, which paces itself (48000 frames a second while anything
+ * plays, nothing at all otherwise). What the sink cannot take is dropped.
+ */
+#ifndef _SYS_REDWOOD_AUDIO_H_
+#define _SYS_REDWOOD_AUDIO_H_
+
+#include <stdint.h>
+
+#define REDWOOD_AUDIO_DEVICE "/dev/redwood-audio"
+
+/* The one format: 16-bit signed little-endian samples, left then right. */
+#define REDWOOD_AUDIO_RATE 48000
+#define REDWOOD_AUDIO_CHANNELS 2
+#define REDWOOD_AUDIO_FRAME 4 /* bytes */
+
+#ifdef KERNEL
+/*
+ * For drivers. A sink is offered frames as they are written and says how many it took (the
+ * rest are lost); it is called from the writer's thread and must not wait.
+ */
+typedef uint32_t (*redwood_audio_sink_t)(const int16_t *frames, uint32_t count);
+
+void redwood_audio_set_sink(const char *name, redwood_audio_sink_t sink);
+void redwood_audio_init(void);
+void redwood_virtio_sound_init(void); /* boot-arg virtio_sound=1: QEMU's virtio sound device */
+void redwood_speaker_init(void); /* boot-arg speaker_probe=N: a look at the phone's speaker amplifiers */
+#endif
+
+#endif /* _SYS_REDWOOD_AUDIO_H_ */
diff --git a/userland/src/sound_test.c b/userland/src/sound_test.c
new file mode 100644
index 0000000..fc4a77d
--- /dev/null
+++ b/userland/src/sound_test.c
@@ -0,0 +1,255 @@
+/*
+ * sound_test: make iOS play something, the way a program would.
+ *
+ * usage: sound_test STEP...
+ * wait SECONDS
+ * springboard wait until SpringBoard is up and has said that the ringer is on (at
+ * most ten minutes): until then system sounds are "played" in silence,
+ * as on an iPhone with its switch on silent
+ * tone SECONDS [LEFT_HZ RIGHT_HZ] a sine wave in each channel (default 1000 and 440 Hz) at half of
+ * full scale, through an audio queue: what a music player does
+ * system ID a system sound by its number (1100 is the lock sound,
+ * 1104 a keyboard click), as SpringBoard plays them
+ * file PATH a sound file, played as a system sound
+ * route where iOS says its sound goes: the audio session's outputs and volume,
+ * and the devices of the system's output context
+ * restart kill the audio server (launchd starts it again when it is next asked for):
+ * what every program then sees as "media services were reset"
+ *
+ * The samples come out of /dev/redwood-audio (see sys/redwood_audio.h); a tone is something one
+ * can recognize there without ears: two peaks in the spectrum, where they were put.
+ */
+#include <AudioToolbox/AudioToolbox.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <errno.h>
+#include <math.h>
+#include <notify.h>
+#include <objc/message.h>
+#include <objc/runtime.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define RATE 48000
+#define BUFFER_FRAMES 4096
+
+static double frequency[2] = { 1000, 440 };
+static uint64_t frame, frames_wanted;
+
+static void
+fill(__unused void *context, AudioQueueRef queue, AudioQueueBufferRef buffer)
+{
+ int16_t *samples = buffer->mAudioData;
+ uint32_t count = 0;
+
+ for (; count < BUFFER_FRAMES && frame < frames_wanted; count++, frame++) {
+ /* a hundredth of a second to come in and to go out: no click */
+ double edge = frame < RATE / 100 ? frame / (RATE / 100.0) :
+ frames_wanted - frame < RATE / 100 ? (frames_wanted - frame) / (RATE / 100.0) : 1;
+
+ for (int channel = 0; channel < 2; channel++) {
+ samples[2 * count + channel] = (int16_t)(16384 * edge * sin(2 * M_PI * frequency[channel] * frame / RATE));
+ }
+ }
+ if (count == 0) {
+ AudioQueueStop(queue, false);
+ return;
+ }
+ buffer->mAudioDataByteSize = count * 4;
+ AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
+}
+
+static int
+tone(double seconds)
+{
+ AudioStreamBasicDescription format = {
+ .mSampleRate = RATE, .mFormatID = kAudioFormatLinearPCM,
+ .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
+ .mBytesPerPacket = 4, .mFramesPerPacket = 1, .mBytesPerFrame = 4, .mChannelsPerFrame = 2, .mBitsPerChannel = 16,
+ };
+ AudioQueueRef queue;
+ OSStatus status;
+
+ frame = 0;
+ frames_wanted = (uint64_t)(seconds * RATE);
+ /* no run loop given: the queue asks for more on a thread of its own, also while we are busy talking */
+ status = AudioQueueNewOutput(&format, fill, NULL, NULL, NULL, 0, &queue);
+ if (status != 0) {
+ printf("sound_test: AudioQueueNewOutput: %d\n", (int)status);
+ return 1;
+ }
+ for (int i = 0; i < 3; i++) {
+ AudioQueueBufferRef buffer;
+
+ status = AudioQueueAllocateBuffer(queue, BUFFER_FRAMES * 4, &buffer);
+ if (status != 0) {
+ printf("sound_test: AudioQueueAllocateBuffer: %d\n", (int)status);
+ return 1;
+ }
+ fill(NULL, queue, buffer);
+ }
+ status = AudioQueueStart(queue, NULL);
+ printf("sound_test: a tone of %.0f and %.0f Hz for %.1f s: AudioQueueStart: %d\n", frequency[0], frequency[1], seconds,
+ (int)status);
+ if (status == 0) {
+ /* a slow machine may take its time: wait for the end, three times as long as it should take at most */
+ for (int tenths = 0; frame < frames_wanted && tenths < 30 * seconds + 100; tenths++) {
+ usleep(100000);
+ }
+ usleep(1500000); /* the last buffers are still to be played */
+ printf("sound_test: %llu of %llu frames handed over\n", frame, frames_wanted);
+ }
+ AudioQueueDispose(queue, true);
+ return status != 0 || frame < frames_wanted;
+}
+
+static void
+played(SystemSoundID sound, __unused void *context)
+{
+ printf("sound_test: system sound %u has been played\n", (unsigned)sound);
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+static int
+system_sound(SystemSoundID sound)
+{
+ AudioServicesAddSystemSoundCompletion(sound, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, played, NULL);
+ printf("sound_test: system sound %u\n", (unsigned)sound);
+ AudioServicesPlaySystemSound(sound);
+ if (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 15, false) == kCFRunLoopRunTimedOut) {
+ printf("sound_test: system sound %u: no word of its end in 15 s\n", (unsigned)sound);
+ return 1;
+ }
+ return 0;
+}
+
+/* SpringBoard keeps the state of the ringer switch (1: ring, 0: silent) in a notification of this name. */
+static void
+wait_for_ringer(void)
+{
+ uint64_t state = 0;
+ int token, seconds;
+
+ if (notify_register_check("com.apple.springboard.ringerstate", &token) != NOTIFY_STATUS_OK) {
+ return;
+ }
+ for (seconds = 0; seconds < 600 && (notify_get_state(token, &state) != NOTIFY_STATUS_OK || state == 0); seconds++) {
+ sleep(1);
+ }
+ printf("sound_test: the ringer is %s after %d s\n", state ? "on" : "still off", seconds);
+ notify_cancel(token);
+}
+
+/* [object selector] for methods without arguments that return an object. */
+static id
+send(id object, const char *selector)
+{
+ return object ? ((id (*)(id, SEL))objc_msgSend)(object, sel_registerName(selector)) : NULL;
+}
+
+static const char *
+text(id string)
+{
+ const char *utf8 = string ? ((const char *(*)(id, SEL))objc_msgSend)(string, sel_registerName("UTF8String")) : NULL;
+
+ return utf8 ? utf8 : "(none)";
+}
+
+/*
+ * What the audio session of this program says of the route (AVAudioSession, as an application would
+ * ask), and what the system's output context holds (AVOutputContext: what Control Center goes by).
+ */
+static void
+route(void)
+{
+ id session = send((id)objc_getClass("AVAudioSession"), "sharedInstance");
+ id outputs = send(send(session, "currentRoute"), "outputs");
+ id context = send((id)objc_getClass("AVOutputContext"), "sharedSystemAudioContext");
+ id devices = send(context, "outputDevices");
+ unsigned long count = outputs ? ((unsigned long (*)(id, SEL))objc_msgSend)(outputs, sel_registerName("count")) : 0;
+
+ printf("sound_test: route: %lu output%s, volume %.2f\n", count, count == 1 ? "" : "s",
+ session ? ((float (*)(id, SEL))objc_msgSend)(session, sel_registerName("outputVolume")) : -1.0f);
+ for (unsigned long i = 0; i < count; i++) {
+ id port = ((id (*)(id, SEL, unsigned long))objc_msgSend)(outputs, sel_registerName("objectAtIndex:"), i);
+
+ printf("sound_test: route: %s \"%s\" (%s)\n", text(send(port, "portType")), text(send(port, "portName")),
+ text(send(port, "UID")));
+ }
+ count = devices ? ((unsigned long (*)(id, SEL))objc_msgSend)(devices, sel_registerName("count")) : 0;
+ printf("sound_test: route: the system's output context %s, %lu device%s\n", context ? "is there" : "is missing", count,
+ count == 1 ? "" : "s");
+ for (unsigned long i = 0; i < count; i++) {
+ id device = ((id (*)(id, SEL, unsigned long))objc_msgSend)(devices, sel_registerName("objectAtIndex:"), i);
+
+ printf("sound_test: route: \"%s\" (%s)\n", text(send(device, "name")), text(send(device, "ID")));
+ }
+}
+
+extern int proc_listallpids(void *buffer, int size);
+extern int proc_name(int pid, void *buffer, uint32_t size);
+
+static void
+restart(void)
+{
+ static int pids[2048];
+ int count = proc_listallpids(pids, sizeof(pids));
+ char name[64];
+
+ for (int i = 0; i < count; i++) {
+ if (proc_name(pids[i], name, sizeof(name)) > 0 && strcmp(name, "mediaserverd") == 0) {
+ printf("sound_test: restart: killing mediaserverd[%d]: %s\n", pids[i],
+ kill(pids[i], SIGKILL) == 0 ? "done" : strerror(errno));
+ return;
+ }
+ }
+ printf("sound_test: restart: no mediaserverd is running\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ int failed = 0;
+
+ setvbuf(stdout, NULL, _IONBF, 0);
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "wait") == 0 && i + 1 < argc) {
+ usleep((useconds_t)(atof(argv[++i]) * 1e6));
+ } else if (strcmp(argv[i], "springboard") == 0) {
+ wait_for_ringer();
+ } else if (strcmp(argv[i], "route") == 0) {
+ route();
+ } else if (strcmp(argv[i], "restart") == 0) {
+ restart();
+ } else if (strcmp(argv[i], "tone") == 0 && i + 1 < argc) {
+ double seconds = atof(argv[++i]);
+
+ if (i + 2 < argc && atof(argv[i + 1]) > 0 && atof(argv[i + 2]) > 0) {
+ frequency[0] = atof(argv[++i]);
+ frequency[1] = atof(argv[++i]);
+ }
+ failed |= tone(seconds);
+ } else if (strcmp(argv[i], "system") == 0 && i + 1 < argc) {
+ failed |= system_sound((SystemSoundID)atoi(argv[++i]));
+ } else if (strcmp(argv[i], "file") == 0 && i + 1 < argc) {
+ CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)argv[i + 1],
+ (CFIndex)strlen(argv[i + 1]), false);
+ SystemSoundID sound = 0;
+ OSStatus status = AudioServicesCreateSystemSoundID(url, &sound);
+
+ i++;
+ if (status != 0) {
+ printf("sound_test: %s: AudioServicesCreateSystemSoundID: %d\n", argv[i], (int)status);
+ failed = 1;
+ } else {
+ failed |= system_sound(sound);
+ }
+ } else {
+ printf("sound_test: what is \"%s\"?\n", argv[i]);
+ return 2;
+ }
+ }
+ return failed;
+}
diff --git a/xl b/xl
index 1f45952..e52b120 100755
--- a/xl
+++ b/xl
@@ -10,6 +10,8 @@
# works during a run too (from another terminal)
# xl recording [F] fetch the screen recording the last run made (rec_secs=N among XL_CMDLINE) and
# make a video of it (default logs/recording.mp4)
+# xl sound [F] fetch the sound the last run recorded (rec_sound=N among XL_CMDLINE: the first N
+# seconds of what iOS played) and make a WAV file of it (default logs/sound.wav)
# xl selftest exercise only the kernel->loader return path
# xl log fetch the loader's current log; during a run, the kernel's log so far as well
# xl stop end the run in progress now (the kernel returns as when its time is up)
@@ -161,6 +163,14 @@ recording() { # recording [file.mp4]
tools/recording.py logs/recording.raw "$out" && { [ -n "$XL_REC_KEEP" ] || rm -f logs/recording.raw; }
}
+sound() { # sound [file.wav]
+ enter || return 1
+ local out=${1:-logs/sound.wav}
+ fb oem sound | grep -q OKAY || { echo "xl: the last run left no sound recording (boot-arg rec_sound=N)"; return 1; }
+ FB_TIMEOUT=120 fb get_staged logs/sound.raw >/dev/null
+ tools/sound.py logs/sound.raw "$out" && rm -f logs/sound.raw
+}
+
ramdisk() { # ramdisk [<file>|none]
enter || return 1
if [ "$1" = none ]; then fb oem ramdisk none >/dev/null; echo "xl: resident ramdisk dropped"; return; fi
@@ -220,6 +230,7 @@ case "$1" in
enter) enter && echo "xl: loader is up" ;;
screenshot) screenshot "$2" ;;
recording) recording "$2" ;;
+sound) sound "$2" ;;
ramdisk) ramdisk "$2" ;;
run) run "oem run $2" "run${2:+-cp$2}" ;;
selftest) run "oem selftest" selftest ;;
diff --git a/xnu/README.md b/xnu/README.md
index 11baf94..85403ea 100644
--- a/xnu/README.md
+++ b/xnu/README.md
@@ -67,6 +67,7 @@ xnu-deps/setup.sh # ~30 с: исходники зависимостей,
| `bsd/dev/redwood_wifi.c`, `bsd/sys/redwood_qrtr.h` | Wi-Fi телефона (WCN6750), начало пути (`wifi=N`): общая память процессоров Qualcomm (SMEM: чтение и выделение под её замком), сигнальные биты между нами и процессором Wi-Fi (SMP2P, обе стороны), почтовый ящик процессора питания, загрузка прошивки процессора Wi-Fi, его запуск через TrustZone и остановка по просьбе, GLINK (очереди в общей памяти, канал IPCRTR) и QRTR поверх него; `redwood_qrtr.h` — порт датаграмм для остального ядра (открыть, послать, найти службу, закрыть) |
| `bsd/dev/redwood_qmi.c`, `bsd/sys/redwood_qmi.h` | QMI — протокол сообщений служб на процессорах Qualcomm: кодирование по таблицам, запросы с ответами, оповещения; интерфейс как у Linux (`qmi_*`), чтобы `qmi.c` из ath11k собирался без правок, реализация своя; проверяется на компьютере (`tools/qmi-test/run.sh`) |
| `bsd/dev/redwood_wlfw.c` | временная замена `qmi.c` из ath11k: разговор со службой прошивки Wi-Fi вручную, в его порядке и его сообщениями (шаг 15 `wifi=N`); описания сообщений берутся из исходников ath11k при сборке (`tools/mk-wlfw.sh`) и в git не попадают |
+| `bsd/dev/redwood_audio.c`, `redwood_virtio_sound.c`, `redwood_speaker.c` | звук: `/dev/redwood-audio` (куда наше устройство вывода в mediaserverd пишет то, что играет iOS), запись его в память, virtio-sound для QEMU; первый взгляд на усилители динамиков телефона |
| `bsd/pthread/pthread_kext_static.c` | pthread kext, слинкованный в ядро |
Патч, помимо врезок под всё это, чинит то, что мешает собрать и запустить открытый XNU вне Apple: makefile'ы под
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 4d542cd..8c552c2 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -781,6 +781,8 @@ struct kernel_live {
uint64_t log_head, log_ring; /* the log: this much from the start of the boot, then a ring this big */
uint64_t recording; /* (a virtual address too) { physical address, bytes } of a screen recording */
uint64_t stop; /* ours to write: the host wants the run to end ("oem stop") */
+ uint64_t magic2; /* "XNULIVE2": the kernel is new enough to have what follows */
+ uint64_t sound; /* (a virtual address too) the same for a recording of the sound */
};
/*
@@ -844,6 +846,10 @@ static int charger_enabled;
/* The screen recording the last kernel left in memory, if it made one ("oem recording" stages it). */
static const char *recording_data;
static uint64_t recording_len;
+/* ... and the recording of the sound ("oem sound"). */
+static const char *sound_data;
+static uint64_t sound_len;
+#define KERNEL_LIVE_MAGIC2 0x324556494c554e58ULL
#define KERNEL_LIVE_MAGIC 0x214556494c554e58ULL
#define OTHER_CPU_NOT_STARTED 1000 /* run_on_other_cpu(): the firmware said yes but the core never arrived */
#define OTHER_CPU_HUNG 1001 /* ... the kernel stopped giving signs of life */
@@ -2124,6 +2130,18 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)
print("the kernel left a screen recording: %d bytes at %x\n", recording_len, recording[0]);
}
}
+ sound_data = NULL;
+ sound_len = 0;
+ if (kernel_live != NULL && kernel_live->magic2 == KERNEL_LIVE_MAGIC2 && kernel_live->sound != 0 &&
+ checkpoint != CHECKPOINT_SELFTEST) {
+ const uint64_t *sound = (const uint64_t *)(kernel_live->sound + slide);
+
+ if (sound[0] != 0 && sound[1] > 24 && sound[1] <= (1ULL << 30)) {
+ sound_data = (const char *)sound[0];
+ sound_len = sound[1];
+ print("the kernel left a sound recording: %d bytes at %x\n", sound_len, sound[0]);
+ }
+ }
const char *klog = (const char *)loader_ctx[CTX_LOG_PA];
uint64_t klog_len = loader_ctx[CTX_LOG_LEN];
uint64_t reason = loader_ctx[CTX_REASON];
@@ -2352,7 +2370,7 @@ fb_command(char *cmd)
{
if (busy && (starts_with(cmd, "oem ramdisk") || starts_with(cmd, "download:") || starts_with(cmd, "oem run") ||
starts_with(cmd, "oem selftest") || starts_with(cmd, "oem hang") || starts_with(cmd, "oem recording") ||
- starts_with(cmd, "oem clearscreen") || starts_with(cmd, "oem memmap"))) {
+ starts_with(cmd, "oem sound") || starts_with(cmd, "oem clearscreen") || starts_with(cmd, "oem memmap"))) {
fb_reply("FAIL", "a kernel is running (oem stop ends it)");
} else if (starts_with(cmd, "oem stop")) {
/* the kernel looks at this once a second and comes back as it does when its time is up */
@@ -2460,6 +2478,15 @@ fb_command(char *cmd)
staged_data = recording_data;
staged_len = recording_len;
fb_reply("OKAY", "");
+ } else if (starts_with(cmd, "oem sound")) {
+ /* stage what the kernel recorded of the sound (boot-arg rec_sound) */
+ if (sound_data == NULL) {
+ fb_reply("FAIL", "no sound recording");
+ return;
+ }
+ staged_data = sound_data;
+ staged_len = sound_len;
+ fb_reply("OKAY", "");
} else if (starts_with(cmd, "oem clearscreen")) {
if (SCREEN.base != 0) {
memset((void *)(uintptr_t)SCREEN.base, 0, (size_t)SCREEN.stride * SCREEN.height);