Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/all
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ for f in tools/install-phone tools/phone-provision tools/nv-backup tools/build-r
tools/local-key-init tools/fetch-published tools/fetch-toolchain tools/repo-publish tools/repo-build tools/release tools/release-upload tools/server/releases-nginx.sh \
tools/lib/alarm.sh tools/lib/alarm-setup tools/lib/signing.sh tools/lib/bundle-tar.sh rootfs/build-inside tests/*; do
[[ -f $f ]] || continue # tests/owl is the OWL conformance suite's folder
[[ $f == *.py ]] && continue
bash -n "$f" || { echo "not ok - $f parses" >&2; exit 1; }
done
python3 -I -c "import ast, sys; ast.parse(open(sys.argv[1]).read())" tools/bundle-manifest || { echo "not ok - tools/bundle-manifest parses" >&2; exit 1; }
Expand All @@ -32,6 +33,7 @@ sh -n device/flame/initramfs/init
sh -n device/flame/initramfs/build
bash -n tools/build-flame-probe
echo "ok - scripts parse"
python3 tests/bt-uart-gpio.py
for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release; do
echo "# $t"
"$t"
Expand Down
90 changes: 90 additions & 0 deletions tests/bt-uart-gpio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Exercise GPIO ownership and low-state cleanup on both Pixel board layouts."""
from pathlib import Path
import runpy
import struct
from unittest.mock import patch

m = runpy.run_path(str(Path(__file__).resolve().parents[1] / 'tools/bt-uart-test'))
g = m['cmd_hci'].__globals__
for bank in ('gpp16', 'gpp14'):
for fail in ('none', 'gpio', 'tty', 'exchange', 'cleanup'):
events = []
class Lines:
def __init__(self, fd, offsets, consumer):
self.fd, self.offsets = fd, offsets
if fail == 'gpio' and fd == 14:
raise OSError('request failure')
events.append(('request', fd, offsets))
def set(self, offset, value):
assert offset in self.offsets
events.append(('set', self.fd, offset, value))
if fail == 'cleanup' and offset == 2 and value == 0:
raise OSError('low failure')
def close(self):
events.append(('release', self.fd))
def tty(*args, **kwargs):
if fail == 'tty':
raise OSError('tty failure')
return 30
def exchange(fd, reg, wake, delay):
assert fd == 30 and reg.fd == 16
assert wake.offset == 3
reg.set(2, 1)
wake.set(1)
if fail == 'exchange':
raise OSError('HCI failure')
return 0
with patch.dict(g, {'Lines': Lines, 'find_chip': lambda name: (int(name[3:]), name, 8),
'open_tty': tty, 'hci_exchange': exchange, 'say': lambda s: None}), \
patch.object(g['os'], 'close', lambda fd: events.append(('close', fd))):
try:
result = m['cmd_hci']('test', 115200, 200, bank + ':3')
assert result == 0
except OSError:
assert fail != 'none'
for e in events:
if e[0] == 'request':
fd, offsets = e[1:]
assert ('release', fd) in events and ('close', fd) in events
for offset in offsets:
low = ('set', fd, offset, 0)
if fail not in ('tty', 'gpio'):
assert low in events
assert events.index(low) < events.index(('release', fd))
if fail == 'gpio' and bank == 'gpp14':
assert ('close', 14) in events
for fail_command in (None, 0x0c03, 0x1001, 0xfc79):
events = []
class Power:
def __init__(self, name): self.name = name
def set(self, *args): events.append((self.name, *args))
commands = []
def command(fd, opcode):
commands.append(opcode)
if opcode == fail_command: return None
return {0x0c03: b'\0', 0x1001: b'\0' + struct.pack('<BHBHH', 11, 1, 11, 15, 2),
0xfc79: b'\0\x69'}[opcode]
with patch.dict(g, {'hci_cmd': command, 'read_exact': lambda *a: b'', 'say': lambda s: None}), \
patch.object(g['termios'], 'tcflush', lambda *a: None), \
patch.object(g['time'], 'sleep', lambda *a: None):
result = m['hci_exchange'](30, Power('reg'), Power('wake'), 200)
assert result == (1 if fail_command else 0)
if fail_command: assert commands[-1] == fail_command
print('PASS: Bluetooth GPIO routing, low-state cleanup and stop on each failed HCI command')

for baud, delay in ((123, 200), (115200, -1)):
with patch.dict(g, {'find_chip': lambda *a: (_ for _ in ()).throw(AssertionError("hardware acquired"))}):
try:
m['cmd_hci']('test', baud, delay, 'gpp14:3')
except SystemExit:
pass
else:
raise AssertionError("invalid arguments accepted")
with patch.object(g['os'], 'open', return_value=30), patch.object(g['os'], 'close') as close, patch.object(g['termios'], 'tcgetattr', side_effect=OSError("setup")):
try:
m['open_tty']('test', 115200, True)
except OSError:
pass
close.assert_called_once_with(30)
150 changes: 81 additions & 69 deletions tools/bt-uart-test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# power the BCM4389 BT core (BT_DEV_WAKE high, then
# BT_REG_ON high), send HCI_Reset, Read Local Version
# and Broadcom Read Verbose Config, print the answers,
# then BT_REG_ON low again. Proves the pins, the baud
# then BT_REG_ON low again. Failed identity reads are FAIL.
# Proves the pins, the baud
# clock (115200 is the chip's power-on rate) and RTS/CTS.
# --wake: BT_DEV_WAKE's bank and line; by default
# the board's own, from its compatible (cheetah
Expand All @@ -41,6 +42,7 @@
# CMU or PMU registers; the only hardware it drives is the tty and two GPIOs.

import array
import contextlib
import fcntl
import os
import select
Expand Down Expand Up @@ -117,18 +119,22 @@ def open_tty(path, baud, rtscts):
die(f"unsupported baud {baud}")
say(f"open {path} raw 8N1 {baud} rtscts={int(rtscts)}")
fd = os.open(path, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
attr = termios.tcgetattr(fd)
attr[0] = 0 # iflag
attr[1] = 0 # oflag
attr[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
if rtscts:
attr[2] |= termios.CRTSCTS
attr[3] = 0 # lflag
attr[4] = attr[5] = BAUD[baud]
attr[6][termios.VMIN] = 0
attr[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, attr)
termios.tcflush(fd, termios.TCIOFLUSH)
try:
attr = termios.tcgetattr(fd)
attr[0] = 0 # iflag
attr[1] = 0 # oflag
attr[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
if rtscts:
attr[2] |= termios.CRTSCTS
attr[3] = 0 # lflag
attr[4] = attr[5] = BAUD[baud]
attr[6][termios.VMIN] = 0
attr[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, attr)
termios.tcflush(fd, termios.TCIOFLUSH)
except BaseException:
os.close(fd)
raise
return fd


Expand Down Expand Up @@ -271,64 +277,70 @@ class WakeLine:
def cmd_hci(tty, baud, on_ms, wake):
wake_bank, wake_line = wake.split(":")
wake_line = int(wake_line)
chip_fd, path, _ = find_chip(BT_BANK)
if chip_fd is None:
die(f"no gpiochip labelled {BT_BANK}: is the PERIC0 pin controller enabled?")
wake_fd = wake_lines = None
if wake_bank == BT_BANK:
say(f"{BT_BANK} is {path}; request lines {BT_REG_ON} (BT_REG_ON) and "
f"{wake_line} (BT_DEV_WAKE) as outputs, low")
lines = Lines(chip_fd, [BT_REG_ON, wake_line], "bt-uart-test")
else:
wake_fd, wake_path, _ = find_chip(wake_bank)
if wake_fd is None:
die(f"no gpiochip labelled {wake_bank}")
say(f"{BT_BANK} is {path} (line {BT_REG_ON}, BT_REG_ON), {wake_bank} is "
f"{wake_path} (line {wake_line}, BT_DEV_WAKE); outputs, low")
lines = Lines(chip_fd, [BT_REG_ON], "bt-uart-test")
wake_lines = Lines(wake_fd, [wake_line], "bt-uart-test")
dev_wake = WakeLine(lines, wake_lines, wake_line)
fd = open_tty(tty, baud, rtscts=True)
rc = 1
try:
say("BT_DEV_WAKE high (keep the controller awake)")
dev_wake.set(1)
say("BT_REG_ON high (power the BT core); Wi-Fi's WL_REG_ON is not touched")
lines.set(BT_REG_ON, 1)
time.sleep(on_ms / 1000)
junk = read_exact(fd, 64, 0.1)
if junk:
say(f"bytes after power-on (ignored): {hexs(junk)}")
termios.tcflush(fd, termios.TCIFLUSH)
if baud not in BAUD or on_ms < 0:
die("unsupported baud or negative power-on delay")
with contextlib.ExitStack() as cleanup:
def request(bank, offsets, descriptions):
chip_fd, path, _ = find_chip(bank)
if chip_fd is None:
die(f"no gpiochip labelled {bank}" + (": is the PERIC0 pin controller enabled?" if bank == BT_BANK else ""))
cleanup.callback(os.close, chip_fd)
say(f"{bank} is {path}; request lines {descriptions} as outputs, low")
lines = Lines(chip_fd, offsets, "bt-uart-test")
cleanup.callback(lines.close)
return lines

if wake_bank == BT_BANK:
lines = request(BT_BANK, [BT_REG_ON, wake_line],
f"{BT_REG_ON} (BT_REG_ON) and {wake_line} (BT_DEV_WAKE)")
dev_wake = WakeLine(lines, None, wake_line)
else:
lines = request(BT_BANK, [BT_REG_ON], f"{BT_REG_ON} (BT_REG_ON)")
dev_wake = WakeLine(lines, request(wake_bank, [wake_line],
f"{wake_line} (BT_DEV_WAKE)"), wake_line)
fd = open_tty(tty, baud, rtscts=True)
cleanup.callback(os.close, fd)
cleanup.callback(dev_wake.set, 0)
cleanup.callback(lines.set, BT_REG_ON, 0)
cleanup.callback(say, "BT_REG_ON low, BT_DEV_WAKE low")
rc = hci_exchange(fd, lines, dev_wake, on_ms)
say("hci " + ("PASS" if rc == 0 else "FAIL"))
return rc

if hci_cmd(fd, 0x0c03) is None: # HCI_Reset
say("HCI_Reset FAIL. If 0 bytes: check RTS/CTS (try --baud 3000000 "
"only if the chip was left running), BT_REG_ON, the baud clock")
return 1
say("HCI_Reset PASS")

ver = hci_cmd(fd, 0x1001) # Read Local Version
if ver and len(ver) >= 9:
hci_ver, hci_rev, lmp_ver, manuf, subver = struct.unpack_from("<BHBHH", ver, 1)
say(f"version: HCI {hci_ver} rev 0x{hci_rev:04x} LMP {lmp_ver} "
f"manufacturer {manuf} LMP subversion 0x{subver:04x} "
f"({(subver >> 13) & 7}.{(subver >> 8) & 0x1f}.{subver & 0xff})")

cfg = hci_cmd(fd, 0xfc79) # BCM Read Verbose Config
if cfg and len(cfg) >= 2:
say(f"BCM chip id {cfg[1]} (btbcm prints this as 'BCM: chip id')")
rc = 0
finally:
say("BT_REG_ON low, BT_DEV_WAKE low")
lines.set(BT_REG_ON, 0)
dev_wake.set(0)
lines.close()
os.close(chip_fd)
if wake_lines:
wake_lines.close()
os.close(wake_fd)
os.close(fd)
say("hci " + ("PASS" if rc == 0 else "FAIL"))
def hci_exchange(fd, lines, dev_wake, on_ms):
rc = 1
say("BT_DEV_WAKE high (keep the controller awake)")
dev_wake.set(1)
say("BT_REG_ON high (power the BT core); Wi-Fi's WL_REG_ON is not touched")
lines.set(BT_REG_ON, 1)
time.sleep(on_ms / 1000)
junk = read_exact(fd, 64, 0.1)
if junk:
say(f"bytes after power-on (ignored): {hexs(junk)}")
termios.tcflush(fd, termios.TCIFLUSH)

if hci_cmd(fd, 0x0c03) is None: # HCI_Reset
say("HCI_Reset FAIL. If 0 bytes: check RTS/CTS (try --baud 3000000 "
"only if the chip was left running), BT_REG_ON, the baud clock")
return 1
say("HCI_Reset PASS")

ver = hci_cmd(fd, 0x1001) # Read Local Version
if not ver or len(ver) < 9:
say("Read Local Version FAIL")
return 1
hci_ver, hci_rev, lmp_ver, manuf, subver = struct.unpack_from("<BHBHH", ver, 1)
say(f"version: HCI {hci_ver} rev 0x{hci_rev:04x} LMP {lmp_ver} "
f"manufacturer {manuf} LMP subversion 0x{subver:04x} "
f"({(subver >> 13) & 7}.{(subver >> 8) & 0x1f}.{subver & 0xff})")

cfg = hci_cmd(fd, 0xfc79) # BCM Read Verbose Config
if not cfg or len(cfg) < 2:
say("Read Verbose Config FAIL")
return 1
say(f"BCM chip id {cfg[1]} (btbcm prints this as 'BCM: chip id')")
rc = 0
return rc


Expand Down