diff --git a/openpilot/selfdrive/pandad/libpanda.a b/openpilot/selfdrive/pandad/libpanda.a index feaed22b50..81b8838675 100644 Binary files a/openpilot/selfdrive/pandad/libpanda.a and b/openpilot/selfdrive/pandad/libpanda.a differ diff --git a/openpilot/selfdrive/pandad/panda.cc b/openpilot/selfdrive/pandad/panda.cc index 408cf2b19d..8690bdbbf0 100644 --- a/openpilot/selfdrive/pandad/panda.cc +++ b/openpilot/selfdrive/pandad/panda.cc @@ -166,7 +166,7 @@ bool Panda::up_to_date() { bool allowed = std::string(selected) == filename; // Match the wrapper's existing alternate-image hardware eligibility exactly. if (hw_type == cereal::PandaState::PandaType::DOS || hw_type == cereal::PandaState::PandaType::TRES || - hw_type == cereal::PandaState::PandaType::RED_PANDA) { + hw_type == cereal::PandaState::PandaType::RED_PANDA || hw_type == cereal::PandaState::PandaType::CUATRO) { const std::string prefix = std::string(filename).substr(0, std::string(filename).find(".bin.signed")); for (const char *suffix : {"_remote", "_hkg_remote", "_tesla_wake", "_can_ignition_only", "_remote_can_ignition_only", "_hkg_remote_can_ignition_only", "_tesla_wake_can_ignition_only"}) { diff --git a/openpilot/selfdrive/pandad/panda.o b/openpilot/selfdrive/pandad/panda.o index 25fe4fe16b..e6999131c3 100644 Binary files a/openpilot/selfdrive/pandad/panda.o and b/openpilot/selfdrive/pandad/panda.o differ diff --git a/openpilot/selfdrive/pandad/pandad b/openpilot/selfdrive/pandad/pandad index bf42b44e91..48cf29a8ad 100755 Binary files a/openpilot/selfdrive/pandad/pandad and b/openpilot/selfdrive/pandad/pandad differ diff --git a/openpilot/selfdrive/pandad/pandad.o b/openpilot/selfdrive/pandad/pandad.o index 736e12d6b2..fef643c99c 100644 Binary files a/openpilot/selfdrive/pandad/pandad.o and b/openpilot/selfdrive/pandad/pandad.o differ diff --git a/openpilot/selfdrive/pandad/pandad.py b/openpilot/selfdrive/pandad/pandad.py index 7319460140..ded76e58cf 100755 --- a/openpilot/selfdrive/pandad/pandad.py +++ b/openpilot/selfdrive/pandad/pandad.py @@ -24,7 +24,7 @@ def get_expected_signature(mcu_type: McuType, choice: FirmwareChoice = DEFAULT_F def flash_panda(panda_serial: str, choice: FirmwareChoice = DEFAULT_FIRMWARE_CHOICE): panda = Panda(panda_serial) - if choice.alternate and panda.get_type() not in (Panda.HW_TYPE_DOS, Panda.HW_TYPE_TRES, Panda.HW_TYPE_RED_PANDA): + if choice.alternate and panda.get_type() not in (Panda.HW_TYPE_DOS, Panda.HW_TYPE_TRES, Panda.HW_TYPE_RED_PANDA, Panda.HW_TYPE_CUATRO): choice = FirmwareChoice() fw_path = firmware_path(FW_PATH, panda.get_mcu_type().config.app_fn, choice) fw_signature = get_expected_signature(panda.get_mcu_type(), choice) diff --git a/openpilot/selfdrive/pandad/tests/test_pandad_canprotocol.cc b/openpilot/selfdrive/pandad/tests/test_pandad_canprotocol.cc index 22ec54e6b9..fb2c8f7749 100644 --- a/openpilot/selfdrive/pandad/tests/test_pandad_canprotocol.cc +++ b/openpilot/selfdrive/pandad/tests/test_pandad_canprotocol.cc @@ -106,7 +106,7 @@ void PandaTest::test_can_recv(uint32_t rx_chunk_size) { class FirmwareHandle : public PandaCommsHandle { public: - explicit FirmwareHandle(unsigned char value) : value(value) {} + explicit FirmwareHandle(unsigned char value) : value(value) { hw_serial = std::string(24, 'a'); } int control_read(uint8_t request, uint16_t, uint16_t, unsigned char *data, uint16_t length, unsigned int) override { if ((request != 0xd3 && request != 0xd4) || length != 64) return -1; memset(data, value, length); @@ -133,8 +133,22 @@ void test_firmware_selection() { struct Restore { fs::path cwd = fs::current_path(); fs::path directory; - ~Restore() { fs::current_path(cwd); fs::remove_all(directory); } - } restore{fs::current_path(), directory}; + std::vector>> environment; + ~Restore() { + for (const auto &[key, value] : environment) { + if (value) setenv(key.c_str(), value->c_str(), 1); + else unsetenv(key.c_str()); + } + fs::current_path(cwd); + fs::remove_all(directory); + } + } restore{fs::current_path(), directory, {}}; + const std::string key = "STARPILOT_PANDA_FIRMWARE_" + std::string(24, 'a'); + for (const auto &name : {std::string("STARPILOT_PANDA_FIRMWARE_MAP"), std::string("STARPILOT_PANDA_FIRMWARE_NAME"), key}) { + const char *old = getenv(name.c_str()); + restore.environment.emplace_back(name, old ? std::make_optional(std::string(old)) : std::nullopt); + unsetenv(name.c_str()); + } fs::create_directories(fs::path(directory) / "panda/board/obj"); fs::create_directories(fs::path(directory) / "work/a/b"); for (const auto &[filename, value] : std::vector>{ @@ -153,6 +167,30 @@ void test_firmware_selection() { } CHECK(!FirmwarePanda(Type::UNKNOWN, 0x11).up_to_date()); CHECK(!FirmwarePanda(Type::UNKNOWN, 0x22).up_to_date()); + setenv("STARPILOT_PANDA_FIRMWARE_MAP", "1", 1); + for (auto type : {Type::DOS, Type::RED_PANDA, Type::TRES, Type::CUATRO}) { + const std::string prefix = type == Type::DOS ? "panda" : "panda_h7"; + for (const char *suffix : {"_remote", "_hkg_remote", "_tesla_wake", "_can_ignition_only", + "_remote_can_ignition_only", "_hkg_remote_can_ignition_only", "_tesla_wake_can_ignition_only"}) { + const std::string name = prefix + suffix + ".bin.signed"; + const auto path = fs::path(directory) / "panda/board/obj" / name; + { std::ofstream file(path, std::ios::binary); file << std::string(256, 0x33); } + setenv(key.c_str(), name.c_str(), 1); + CHECK(FirmwarePanda(type, 0x33).up_to_date()); + CHECK(!FirmwarePanda(type, 0x22).up_to_date()); + CHECK(!FirmwarePanda(Type::UNKNOWN, 0x33).up_to_date()); + setenv("STARPILOT_PANDA_FIRMWARE_NAME", name.c_str(), 1); + CHECK(!FirmwarePanda(type, 0x33).up_to_date()); + unsetenv("STARPILOT_PANDA_FIRMWARE_NAME"); + fs::remove(path); + CHECK(!FirmwarePanda(type, 0x33).up_to_date()); + } + setenv(key.c_str(), "../panda_h7.bin.signed", 1); + CHECK(!FirmwarePanda(type, 0x22).up_to_date()); + unsetenv(key.c_str()); + CHECK(!FirmwarePanda(type, 0x22).up_to_date()); + } + unsetenv("STARPILOT_PANDA_FIRMWARE_MAP"); fs::remove(fs::path(directory) / "panda/board/obj/panda.bin.signed"); CHECK(!FirmwarePanda(Type::DOS, 0x11).up_to_date()); } diff --git a/openpilot/starpilot/galaxy/tests/test_developer_ignition.py b/openpilot/starpilot/galaxy/tests/test_developer_ignition.py index 1f306dbd3b..65d65891c9 100644 --- a/openpilot/starpilot/galaxy/tests/test_developer_ignition.py +++ b/openpilot/starpilot/galaxy/tests/test_developer_ignition.py @@ -97,11 +97,11 @@ def test_actual_gateway_developer_confirm_and_vehicle_page_move(setup, monkeypat assert all(row['label'] != 'Use CAN Ignition Only' for row in gateway.page('vehicle', 'session', b'generation')['rows']) -def test_startup_uses_exact_manual_registered_cache_and_auto_remains_stock(setup): +def test_startup_uses_exact_registered_cache_for_manual_and_auto(setup): params, state, _, _ = setup params.put_bool('IgnoreIgnitionLine', True, block=True) put_cache(params, 'CarParamsPersistent', state.cp.as_reader(), block=True) assert startup_choice(params) == FirmwareChoice(can_only=True) params.put('VehicleSelection', json.loads(encode(None)), block=True) - assert startup_choice(params) == FirmwareChoice() + assert startup_choice(params) == FirmwareChoice(can_only=True) assert params.get_bool('IgnoreIgnitionLine') diff --git a/openpilot/starpilot/galaxy/tests/test_panda_wake_settings.py b/openpilot/starpilot/galaxy/tests/test_panda_wake_settings.py index 64e3030bea..72b5fdc6dc 100644 --- a/openpilot/starpilot/galaxy/tests/test_panda_wake_settings.py +++ b/openpilot/starpilot/galaxy/tests/test_panda_wake_settings.py @@ -87,13 +87,13 @@ def test_unknown_or_native_ui_context_is_denied(setup, mutation): assert not owner.apply(request) -def test_hkg_requires_canfd_and_system_long(setup): +def test_hkg_requires_canfd_but_not_system_long(setup): params, state, owner, _ = setup state.cp = vehicle('hyundai') params.put('VehicleSelection', json.loads(encode(state.cp.carFingerprint)), block=True) assert owner.panda_wake.rows(True) state.cp.openpilotLongitudinalControl = False - assert not owner.panda_wake.rows(True) + assert owner.panda_wake.rows(True) state.cp.openpilotLongitudinalControl = True state.cp.flags &= ~int(HyundaiFlags.CANFD) assert not owner.panda_wake.rows(True) @@ -188,8 +188,8 @@ def test_gateway_confirm_binds_vehicle_and_session(setup, monkeypatch): assert read_saved(params, 'RemoteStartBootsComma', 8)[0] == b'1' -@pytest.mark.parametrize('device', ['mici', 'unknown', 'pc']) -def test_unknown_hardware_and_cuatro_admission_denied(setup, device): +@pytest.mark.parametrize('device', ['unknown', 'pc']) +def test_unknown_hardware_admission_denied(setup, device): params, state, owner, _ = setup request = replace(row_request(owner.panda_wake.rows(True)[0], 'On'), confirmation=True, vehicle_fingerprint=state.cp.carFingerprint) @@ -199,15 +199,18 @@ def test_unknown_hardware_and_cuatro_admission_denied(setup, device): assert read_saved(params, 'RemoteStartBootsComma', 8)[0] is None -def test_auto_selection_cannot_apply_an_old_wake_choice(setup): +def test_auto_selection_uses_recognized_vehicle_wake_choice(setup): params, state, owner, _ = setup params.put_bool('RemoteStartBootsComma', True, block=True) params.put('VehicleSelection', json.loads(encode(None)), block=True) - assert not owner.panda_wake.rows(True) + assert owner.panda_wake.rows(True) assert read_saved(params, 'RemoteStartBootsComma', 8)[0] == b'1' -def test_cuatro_uses_stock_firmware_and_preserves_saved_intent(setup, monkeypatch): +@pytest.mark.parametrize('hardware', ['DOS', 'TRES', 'RED_PANDA', 'CUATRO']) +@pytest.mark.parametrize('choice', [FirmwareChoice(gm=True), FirmwareChoice(hkg=True), FirmwareChoice(tesla=True)]) +@pytest.mark.parametrize('protocol_ok', [False, True]) +def test_supported_hardware_uses_existing_alternate_firmware(setup, monkeypatch, hardware, choice, protocol_ok): params, _, _, firmware = setup from openpilot.selfdrive.pandad import pandad from panda import Panda @@ -215,9 +218,12 @@ def test_cuatro_uses_stock_firmware_and_preserves_saved_intent(setup, monkeypatc observed = [] class FakePanda: + F4_DEVICES = Panda.F4_DEVICES + H7_DEVICES = Panda.H7_DEVICES HW_TYPE_DOS = Panda.HW_TYPE_DOS HW_TYPE_TRES = Panda.HW_TYPE_TRES HW_TYPE_RED_PANDA = Panda.HW_TYPE_RED_PANDA + HW_TYPE_CUATRO = Panda.HW_TYPE_CUATRO HEALTH_PACKET_VERSION = 1 CAN_PACKET_VERSION = 2 bootstub = False @@ -226,10 +232,10 @@ def test_cuatro_uses_stock_firmware_and_preserves_saved_intent(setup, monkeypatc assert serial == 'fixture' def get_type(self): - return Panda.HW_TYPE_CUATRO + return getattr(Panda, 'HW_TYPE_' + hardware) def get_mcu_type(self): - return SimpleNamespace(config=SimpleNamespace(app_fn='panda_h7.bin.signed')) + return Panda.get_mcu_type(self) @staticmethod def get_signature_from_firmware(path): @@ -246,25 +252,113 @@ def test_cuatro_uses_stock_firmware_and_preserves_saved_intent(setup, monkeypatc return b'image' def get_packets_versions(self): - return (1, 2) + return (1, 2) if protocol_ok else (0, 0) def close(self): pass monkeypatch.setattr(pandad, 'Panda', FakePanda) monkeypatch.setattr(pandad, 'FW_PATH', firmware) - assert pandad.flash_panda('fixture', FirmwareChoice(gm=True)) == 'panda_h7.bin.signed' - assert observed == ['panda_h7.bin.signed'] + app = 'panda.bin.signed' if getattr(Panda, 'HW_TYPE_' + hardware) in Panda.F4_DEVICES else 'panda_h7.bin.signed' + expected = choice.filename(app) + if protocol_ok: + assert pandad.flash_panda('fixture', choice) == expected + else: + with pytest.raises(pandad.PandaProtocolMismatch): + pandad.flash_panda('fixture', choice) + assert observed == [expected] assert read_saved(params, 'RemoteStartBootsComma', 8)[0] == b'1' -def test_gm_requires_system_long_and_preserves_saved_intent(setup): +def test_gm_wake_is_independent_of_system_long_and_preserves_saved_intent(setup): params, state, owner, _ = setup params.put_bool('RemoteStartBootsComma', True, block=True) assert state.cp.openpilotLongitudinalControl assert owner.panda_wake.rows(True) state.cp.openpilotLongitudinalControl = False - assert supported_keys(state.cp) == () - assert not owner.panda_wake.rows(True) - assert resolve(state.cp, snapshot(params)) == FirmwareChoice() + assert supported_keys(state.cp) == ('RemoteStartBootsComma', 'IgnoreIgnitionLine') + assert owner.panda_wake.rows(True) + assert resolve(state.cp, snapshot(params)) == FirmwareChoice(gm=True) assert read_saved(params, 'RemoteStartBootsComma', 8) == (b'1', True) + + +@pytest.mark.parametrize('brand', ['gm', 'hyundai', 'tesla']) +@pytest.mark.parametrize('device', ['tici', 'tizi', 'mici']) +@pytest.mark.parametrize('system_long', [False, True]) +def test_auto_recognized_vehicle_can_save_and_select_wake_at_startup(setup, brand, device, system_long): + from openpilot.starpilot.panda_wake import startup_choice + from openpilot.starpilot.schema_cache import put_cache + params, state, owner, _ = setup + state.cp = vehicle(brand) + state.cp.openpilotLongitudinalControl = system_long + owner.panda_wake.device_type = lambda: device + params.put('VehicleSelection', json.loads(encode(None)), block=True) + put_cache(params, 'CarParamsPersistent', state.cp.as_reader(), block=True) + row = owner.panda_wake.rows(True)[0] + request = replace(row_request(row, 'On'), confirmation=True, vehicle_fingerprint=state.cp.carFingerprint) + assert owner.apply(request) + assert startup_choice(params) == FirmwareChoice(gm=brand == 'gm', hkg=brand == 'hyundai', tesla=brand == 'tesla') + + +@pytest.mark.parametrize('mutation', ['mismatch', 'selection_corrupt', 'cache_corrupt', 'cache_missing', + 'unknown', 'brand_mismatch', 'not_car', 'source_corrupt']) +def test_startup_rejects_invalid_or_mismatched_sources(setup, mutation): + from openpilot.starpilot.panda_wake import startup_choice + from openpilot.starpilot.schema_cache import put_cache + params, state, _, _ = setup + params.put_bool('RemoteStartBootsComma', True, block=True) + if mutation == 'unknown': + state.cp.carFingerprint = 'CHEVROLET UNKNOWN' + elif mutation == 'brand_mismatch': + state.cp.brand = 'tesla' + elif mutation == 'not_car': + state.cp.notCar = True + put_cache(params, 'CarParamsPersistent', state.cp.as_reader(), block=True) + if mutation == 'mismatch': + params.put('VehicleSelection', json.loads(encode(str(TESLA.TESLA_MODEL_3))), block=True) + elif mutation == 'selection_corrupt': + Path(params.get_param_path('VehicleSelection')).write_bytes(b'invalid') + elif mutation == 'cache_corrupt': + Path(params.get_param_path('CarParamsPersistent')).write_bytes(b'invalid') + elif mutation == 'cache_missing': + params.remove('CarParamsPersistent') + elif mutation == 'source_corrupt': + Path(params.get_param_path('IgnoreIgnitionLine')).write_bytes(b'broken') + assert startup_choice(params) == FirmwareChoice() + + +@pytest.mark.parametrize('change', ['selection', 'cache', 'setting']) +def test_startup_rejects_sources_changed_during_cache_validation(setup, monkeypatch, change): + from openpilot.starpilot import schema_cache + from openpilot.starpilot.panda_wake import startup_choice + params, state, _, _ = setup + params.put_bool('RemoteStartBootsComma', True, block=True) + schema_cache.put_cache(params, 'CarParamsPersistent', state.cp.as_reader(), block=True) + original = schema_cache.inspect_cache + def inspect(key, raw): + result = original(key, raw) + if change == 'selection': + params.put('VehicleSelection', json.loads(encode(None)), block=True) + elif change == 'cache': + params.remove('CarParamsPersistent') + else: + params.put_bool('IgnoreIgnitionLine', True, block=True) + return result + monkeypatch.setattr(schema_cache, 'inspect_cache', inspect) + assert startup_choice(params) == FirmwareChoice() + + +def test_ioniq6_c4_with_implicit_auto_and_factory_long_can_enable_remote_climate(setup): + from openpilot.starpilot.panda_wake import startup_choice + from openpilot.starpilot.schema_cache import put_cache + params, state, owner, _ = setup + state.cp = HKGInterface.get_non_essential_params(HKG.HYUNDAI_IONIQ_6) + state.cp.openpilotLongitudinalControl = False + owner.panda_wake.device_type = lambda: 'mici' + params.remove('VehicleSelection') + put_cache(params, 'CarParamsPersistent', state.cp.as_reader(), block=True) + row = owner.panda_wake.rows(True)[0] + assert row.key == 'HKGRemoteStartBootsComma' + request = replace(row_request(row, 'On'), confirmation=True, vehicle_fingerprint=state.cp.carFingerprint) + assert owner.apply(request) + assert startup_choice(params) == FirmwareChoice(hkg=True) diff --git a/openpilot/starpilot/panda_wake.py b/openpilot/starpilot/panda_wake.py index dae32b2e71..ae6a83c8e7 100644 --- a/openpilot/starpilot/panda_wake.py +++ b/openpilot/starpilot/panda_wake.py @@ -15,10 +15,9 @@ def supported_keys(cp): try: if cp is None or cp.notCar: return () - if cp.brand == 'gm' and cp.carFingerprint in tuple(GM) and cp.openpilotLongitudinalControl: + if cp.brand == 'gm' and cp.carFingerprint in tuple(GM): return ('RemoteStartBootsComma', 'IgnoreIgnitionLine') - if (cp.brand == 'hyundai' and cp.carFingerprint in tuple(HKG) and cp.flags & HyundaiFlags.CANFD and - cp.openpilotLongitudinalControl): + if (cp.brand == 'hyundai' and cp.carFingerprint in tuple(HKG) and cp.flags & HyundaiFlags.CANFD): return ('HKGRemoteStartBootsComma',) if cp.brand == 'tesla' and cp.carFingerprint in (TESLA.TESLA_MODEL_3, TESLA.TESLA_MODEL_Y, TESLA.TESLA_MODEL_X): return ('TeslaWakeOnCAN',) @@ -72,8 +71,10 @@ def startup_choice(params): from opendbc.car.structs import car selection = read_selection(params) sources = snapshot(params) + if any(not readable or raw not in (None, b'0', b'1') for _, raw, readable in sources): + return FirmwareChoice() raw, readable = read_saved(params, 'CarParamsPersistent', 1_048_576) - if not selection.readable or not selection.valid or selection.platform is None or not readable or raw is None: + if not selection.readable or not selection.valid or not readable or raw is None: return FirmwareChoice() try: inspected = inspect_cache('CarParamsPersistent', raw) diff --git a/openpilot/starpilot/ui/panda_wake_feature.py b/openpilot/starpilot/ui/panda_wake_feature.py index fcaa307e11..611737ab09 100644 --- a/openpilot/starpilot/ui/panda_wake_feature.py +++ b/openpilot/starpilot/ui/panda_wake_feature.py @@ -23,9 +23,9 @@ class PandaWakeFeature: def capability(self): cp = self.owner.vehicle_params() - keys = supported_keys(cp) if self.galaxy() and self.device_type() in ('tici', 'tizi') else () + keys = supported_keys(cp) if self.galaxy() and self.device_type() in ('tici', 'tizi', 'mici') else () selection = read_selection(self.owner.params) - if not keys or not selection.readable or not selection.valid or selection.platform is None: + if not keys or not selection.readable or not selection.valid: return None if selection.platform is not None and selection.platform != cp.carFingerprint: return None diff --git a/prebuilt.json b/prebuilt.json index 72a9bd0404..361e82c4f8 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "caeadba8e9b6df70b2b88db18b22f3c99c78841b0df86e1dee4c7511bee263d0", "schema_version": 1} +{"protected_inventory_sha256": "cc38a87c963b357f568aa5fa1d7a78030963a20b09f38786b68ef242d69e9d8a", "schema_version": 1}