single init dependency for comma service (#587)

* single init dependency for comma service

* lil more

* speed up

* lil cleanup

* lil more
This commit is contained in:
Adeeb Shihadeh
2026-05-04 19:33:33 -07:00
committed by GitHub
parent 5134117f72
commit 1a0962a4e2
13 changed files with 286 additions and 277 deletions
@@ -1,2 +1,2 @@
[Unit]
After=fs_setup.service
After=comma-init.service
+266
View File
@@ -0,0 +1,266 @@
#!/bin/bash
# this file boots and sets up all state necessary for the comma service, including
# filesystem mounting and booting Qualcomm peripherals, such as WLAN and DSPs.
PATH=/usr/sbin:/usr/bin:/sbin:/bin
function log_console {
# log to the serial console to make boot time debugging ez pz
local msg="comma-init: $*"
echo "$msg"
echo "$msg" > /dev/console
}
function run_init {
local name="$1"
local start_time end_time elapsed_us elapsed_tenths elapsed ret
log_console "$name started"
start_time="${EPOCHREALTIME/./}"
"$name"
ret=$?
end_time="${EPOCHREALTIME/./}"
elapsed_us=$((end_time - start_time))
elapsed_tenths=$(((elapsed_us + 50000) / 100000))
printf -v elapsed "%d.%d" "$((elapsed_tenths / 10))" "$((elapsed_tenths % 10))"
log_console "$name finished after ${elapsed}s"
return $ret
}
function init_filesystems {
local failed=0
local pids=()
local pid
function wait_for_block {
local device="$1"
local i
for ((i = 0; i < 150; i++)); do
if [[ -b "$device" ]]; then
return 0
fi
sleep 0.02
done
log_console "timed out waiting for $device"
return 1
}
function mount_fs {
local what="$1"
local where="$2"
local type="$3"
local options="$4"
if [[ "$what" == /dev/* ]] && ! wait_for_block "$what"; then
failed=1
return 1
fi
if mount --mkdir -t "$type" -o "$options" "$what" "$where"; then
log_console "mounted $where"
return 0
fi
log_console "failed mounting $where"
failed=1
return 1
}
function mount_fs_bg {
mount_fs "$@" &
pids+=("$!")
}
# mount base filesystems
mount_fs_bg /dev/sde9 /dsp ext4 ro
mount_fs_bg /dev/sde4 /firmware vfat ro
mount_fs_bg /dev/sda2 /persist squashfs ro,nosuid,nodev,noexec
mount_fs_bg /dev/sda10 /systemrw ext4 relatime,data=ordered,noauto_da_alloc,discard,noexec,nodev
mount_fs_bg /dev/sda12 /data ext4 discard,noatime,nodiratime,nosuid,nodev
mount_fs_bg /dev/sda11 /cache ext4 relatime,data=ordered,noauto_da_alloc,discard,noexec,nodev,nosuid
mount_fs_bg tmpfs /var tmpfs rw,nosuid,nodev,size=128M,mode=755
mount_fs_bg tmpfs /tmp tmpfs rw,nosuid,nodev,size=150M,mode=1777
mount_fs_bg tmpfs /rwtmp tmpfs rw,nosuid,nodev,size=100M,mode=1777
for pid in "${pids[@]}"; do
wait "$pid" || failed=1
done
# *** setup RW areas ***
systemd-tmpfiles --create /usr/comma/tmpfiles.conf
mkdir -p /var/log/
chown root:syslog /var/log
if ! mount -t tmpfs -o rw,nosuid,nodev,size=128M,mode=755 tmpfs /var/log; then
log_console "failed mounting /var/log"
failed=1
fi
mkdir -p /rwtmp/home_work
mkdir -p /rwtmp/home_upper
chmod 755 /rwtmp/*
if ! mount -t overlay overlay -o lowerdir=/usr/default/home,upperdir=/rwtmp/home_upper,workdir=/rwtmp/home_work /home; then
log_console "failed mounting /home"
failed=1
fi
chown comma:comma /data/
mkdir -p /data/etc
touch /data/etc/timezone
touch /data/etc/localtime
mkdir -p /data/etc/netplan
mkdir -p /data/etc/NetworkManager/system-connections
chown -R comma:comma /cache/
mkdir -p /data/ssh
chown comma: /data/ssh
rm -rf /data/tmp/
mkdir -p /data/tmp/
if [[ ! -d /data/persist ]]; then
sudo cp -r /system/persist /data
fi
if [[ "$failed" -ne 0 ]]; then
log_console "mounts failed"
return 1
fi
}
function init_qcom {
# don't restart whole SoC on subsystem crash
for i in {0..7}; do
echo "related" > /sys/bus/msm_subsys/devices/subsys${i}/restart_level
done
# raise scaling_max so policy=performance can reach the BOOST top step
echo 2649600 > /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq
echo 2649600 > /sys/devices/system/cpu/cpufreq/policy4/scaling_max_freq
# setup firmware
echo -n "/firmware/image" > /sys/module/firmware_class/parameters/path
count=0
while [ ! -s /firmware/image/adsp.mdt ]; do
# wait 10s for /firmware mounted
count=$(( $count + 1 ))
if [ $count -ge 1000 ]; then
echo "[ERROR] /firmware not mounted"
fi
sleep 0.01
done
# boot audio + compute DSPs
echo 1 > /sys/kernel/boot_adsp/boot
echo 1 > /sys/kernel/boot_cdsp/boot
# boot wifi
echo 1 > /sys/kernel/boot_wlan/boot_wlan
/usr/bin/irsc_util /etc/sec_config
# ipa
echo 1 > /dev/ipa
}
function init_gpio {
local pins=(
49 # SOM_ST_IO
134 # ST_BOOT0
41 # PANDA_1V8_EN_N
50 # LTE_RST_N
116 # LTE_PWRKEY
124 # ST_RST_N
34 # GPS_PWR_EN
33 # GPS_SAFEBOOT_N
32 # GPS_RST_N
52 # LTE_BOOT
1264 # POWER ALERT
)
echo "initializing gpio"
for p in ${pins[@]}; do
if [[ ! -d /sys/class/gpio/gpio$p ]]; then
echo $p > /sys/class/gpio/export
fi
until [ -d /sys/class/gpio/gpio$p ]; do
sleep .05
done
done
}
function init_sound {
local state
echo "waiting for sound card to come online"
while true; do
if [[ -d /proc/asound/sdm845tavilsndc && -r /proc/asound/card0/state ]]; then
read -r state < /proc/asound/card0/state
[[ "$state" == "ONLINE" ]] && break
fi
sleep 0.01
done
echo "sound card online"
while ! /usr/comma/sound/tinymix set "SEC_MI2S_RX Audio Mixer MultiMedia1" 1; do
sleep 0.01
done
echo "tinymix controls ready"
if [[ "$(< /sys/firmware/devicetree/base/model)" == *mici* ]]; then
/usr/comma/sound/tinymix set "MultiMedia1 Mixer SEC_MI2S_TX" 1
else
/usr/comma/sound/tinymix set "MultiMedia1 Mixer TERT_MI2S_TX" 1
/usr/comma/sound/tinymix set "TERT_MI2S_TX Channels" Two
fi
}
function init_screen_calibration {
while ! mountpoint -q /persist; do
sleep 0.01
done
/usr/comma/screen_calibration.py
}
function init_hostname {
local serial
while [ ! -r /proc/cmdline ]; do
sleep 0.01
done
read -r cmdline < /proc/cmdline
serial="${cmdline#*androidboot.serialno=}"
serial="${serial%% *}"
echo "serial: '$serial'"
sysctl kernel.hostname="comma-$serial"
}
function init_debug {
while ! mountpoint -q /cache; do
sleep 0.01
done
sudo -u comma /usr/comma/debug.py
}
run_init init_filesystems &
run_init init_qcom &
run_init init_gpio &
run_init init_sound &
run_init init_screen_calibration &
run_init init_hostname &
run_init init_debug &
wait
log_console "********** init done **********"
-3
View File
@@ -29,9 +29,6 @@ else
echo "timed out waiting for magic, ${SECONDS}s"
fi
sudo chown comma: /data
sudo chown comma: /data/media
handle_setup_keys () {
# install default SSH key while still in setup
if [[ ! -e /data/params/d/GithubSshKeys && ! -e /data/continue.sh ]]; then
-104
View File
@@ -1,104 +0,0 @@
#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
log() {
echo "fs_setup[$$]: $(cut -d' ' -f1 /proc/uptime) $*" > /dev/kmsg
}
wait_for_block() {
local device="$1"
local i
for ((i = 0; i < 150; i++)); do
if [[ -b "$device" ]]; then
return 0
fi
sleep 0.02
done
log "timed out waiting for $device"
return 1
}
mount_fs() {
local what="$1"
local where="$2"
local type="$3"
local options="$4"
if [[ "$what" == /dev/* ]] && ! wait_for_block "$what"; then
failed=1
return 1
fi
log "mounting $where"
if mount --mkdir -t "$type" -o "$options" "$what" "$where"; then
log "mounted $where"
return 0
fi
log "failed mounting $where"
failed=1
return 1
}
log "start"
failed=0
mount_fs /dev/sde9 /dsp ext4 ro
mount_fs /dev/sde4 /firmware vfat ro
mount_fs /dev/sda2 /persist squashfs ro,nosuid,nodev,noexec
mount_fs /dev/sda10 /systemrw ext4 relatime,data=ordered,noauto_da_alloc,discard,noexec,nodev
mount_fs /dev/sda12 /data ext4 discard,noatime,nodiratime,nosuid,nodev
mount_fs /dev/sda11 /cache ext4 relatime,data=ordered,noauto_da_alloc,discard,noexec,nodev,nosuid
mount_fs tmpfs /var tmpfs rw,nosuid,nodev,size=128M,mode=755
mount_fs tmpfs /tmp tmpfs rw,nosuid,nodev,size=150M,mode=1777
mount_fs tmpfs /rwtmp tmpfs rw,nosuid,nodev,size=100M,mode=1777
# Ensure the symlinks in the read only rootfs are
# backed by real files and directories on userdata.
# tmpfiles
systemd-tmpfiles --create /usr/comma/tmpfiles.conf
# /var/log/ tmpfs
mkdir -p /var/log/
chown root:syslog /var/log
mount -t tmpfs -o rw,nosuid,nodev,size=128M,mode=755 tmpfs /var/log
# setup /home
mkdir -p /rwtmp/home_work
mkdir -p /rwtmp/home_upper
chmod 755 /rwtmp/*
mount -t overlay overlay -o lowerdir=/usr/default/home,upperdir=/rwtmp/home_upper,workdir=/rwtmp/home_work /home
# /etc
mkdir -p /data/etc
touch /data/etc/timezone
touch /data/etc/localtime
mkdir -p /data/etc/netplan
mkdir -p /data/etc/NetworkManager/system-connections
# /cache
chown -R comma:comma /cache/
# /data/ssh
mkdir -p /data/ssh
chown comma: /data/ssh
# /data/tmp - clear out
rm -rf /data/tmp/
mkdir -p /data/tmp/
# /data/persist
if [[ ! -d /data/persist ]]; then
sudo cp -r /system/persist /data
fi
if [[ "$failed" -ne 0 ]]; then
log "mounts failed"
exit 1
fi
log "done"
-142
View File
@@ -1,142 +0,0 @@
#!/bin/bash
function init_qcom {
# don't restart whole SoC on subsystem crash
for i in {0..7}; do
echo "related" | sudo tee /sys/bus/msm_subsys/devices/subsys${i}/restart_level
done
# raise scaling_max so policy=performance can reach the BOOST top step
echo 2649600 | sudo tee /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq
echo 2649600 | sudo tee /sys/devices/system/cpu/cpufreq/policy4/scaling_max_freq
# setup firmware
echo -n "/firmware/image" > /sys/module/firmware_class/parameters/path
count=0
while [ ! -s /firmware/image/adsp.mdt ]; do
# wait 10s for /firmware mounted
count=$(( $count + 1 ))
if [ $count -ge 100 ]; then
echo "[ERROR] /firmware not mounted"
fi
sleep 0.1
done
# boot wifi
echo 1 > /sys/kernel/boot_wlan/boot_wlan
/usr/bin/irsc_util /etc/sec_config
# boot audio + compute DSPs
echo 1 > /sys/kernel/boot_adsp/boot
echo 1 > /sys/kernel/boot_cdsp/boot
# ipa
echo 1 > /dev/ipa
echo "qcom init done"
}
function init_gpio {
local pins=(
49 # SOM_ST_IO
134 # ST_BOOT0
41 # PANDA_1V8_EN_N
50 # LTE_RST_N
116 # LTE_PWRKEY
124 # ST_RST_N
34 # GPS_PWR_EN
33 # GPS_SAFEBOOT_N
32 # GPS_RST_N
52 # LTE_BOOT
1264 # POWER ALERT
)
echo "initializing gpio"
for p in ${pins[@]}; do
echo $p
echo $p > /sys/class/gpio/export
until [ -d /sys/class/gpio/gpio$p ]; do
sleep .05
done
done
}
function init_sound {
echo "waiting for sound card to come online"
while [ ! -d /proc/asound/sdm845tavilsndc ] || [ "$(cat /proc/asound/card0/state 2> /dev/null)" != "ONLINE" ] ; do
sleep 0.01
done
echo "sound card online"
while ! /usr/comma/sound/tinymix controls | grep -q "SEC_MI2S_RX Audio Mixer MultiMedia1"; do
sleep 0.01
done
echo "tinymix controls ready"
/usr/comma/sound/tinymix set "SEC_MI2S_RX Audio Mixer MultiMedia1" 1
if grep -q mici /sys/firmware/devicetree/base/model; then
/usr/comma/sound/tinymix set "MultiMedia1 Mixer SEC_MI2S_TX" 1
else
/usr/comma/sound/tinymix set "MultiMedia1 Mixer TERT_MI2S_TX" 1
/usr/comma/sound/tinymix set "TERT_MI2S_TX Channels" Two
fi
}
function init_screen_calibration {
while ! mountpoint -q /persist; do
sleep 0.1
done
/usr/comma/screen_calibration.py
}
function init_hostname {
local serial
while [ ! -r /proc/cmdline ]; do
sleep 0.1
done
serial="$(cat /proc/cmdline | sed -e 's/^.*androidboot.serialno=//' -e 's/ .*$//')"
echo "serial: '$serial'"
sysctl kernel.hostname="comma-$serial"
}
function init_debug {
while ! mountpoint -q /cache; do
sleep 0.1
done
sudo -u comma /usr/comma/debug.py
}
function run_init {
local name="$1"
local start_time end_time elapsed
log_init "$name started"
start_time="$EPOCHREALTIME"
"$name"
end_time="$EPOCHREALTIME"
elapsed="$(awk "BEGIN { printf \"%.1f\", $end_time - $start_time }")"
log_init "$name finished after ${elapsed}s"
}
function log_init {
local msg="hardware-init: $*"
echo "$msg"
echo "$msg" > /dev/console
}
run_init init_qcom &
run_init init_gpio &
run_init init_sound &
run_init init_screen_calibration &
run_init init_hostname &
run_init init_debug &
wait
@@ -0,0 +1,12 @@
[Unit]
Description=comma init
DefaultDependencies=no
Before=local-fs.target sysinit.target network.target comma.service magic.service lte.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/comma/comma-init.sh
[Install]
WantedBy=sysinit.target
@@ -1,5 +1,6 @@
[Unit]
After=local-fs.target
After=comma-init.service local-fs.target
Requires=comma-init.service
[Service]
Type=oneshot
@@ -1,11 +0,0 @@
[Unit]
Description=Setup rootfs
Before=local-fs.target
DefaultDependencies=no
[Service]
Type=oneshot
ExecStart=/usr/comma/fs_setup.sh
[Install]
WantedBy=multi-user.target
@@ -1,11 +0,0 @@
[Unit]
Description=Hardware init
DefaultDependencies=no
Before=network.target lte.service
[Service]
Type=oneshot
ExecStart=/usr/comma/hardware-init.sh
[Install]
WantedBy=multi-user.target
@@ -1,6 +1,6 @@
[Unit]
Description=LTE
After=hardware-init.service network.target
After=comma-init.service network.target
[Service]
Restart=no
@@ -1,5 +1,7 @@
[Unit]
Description=Magic
After=comma-init.service
Requires=comma-init.service
[Service]
Type=simple
@@ -1,6 +1,6 @@
[Unit]
Description=Qualcomm TFTP server
After=fs_setup.service
After=comma-init.service
[Service]
Type=simple
+1 -2
View File
@@ -1,9 +1,8 @@
#!/bin/bash -e
# Enable our services
systemctl enable fs_setup.service
systemctl enable comma-init.service
systemctl enable comma.service
systemctl enable hardware-init.service
systemctl enable lte.service
systemctl enable magic.service
systemctl enable varwatch.service