Try Embedded

UDS diagnostic services: interview questions

12 UDS diagnostic services questions embedded interviewers ask, each with a model answer. What do a UDS request, its positive response and its negative…

What do a UDS request, its positive response and its negative response look like on the wire?

A request starts with a service identifier (SID). A positive response starts with the SID + 0x40 and echoes enough of the request to be matched to it:

request             22 F1 90               ReadDataByIdentifier, DID 0xF190
positive response   62 F1 90 54 45 31 ...  0x22 + 0x40, the DID, the data

A refusal always has the same three bytes: 7F, the SID that was refused, and a negative response code (NRC) that says why:

request             22 12 34
negative response   7F 22 31               requestOutOfRange: no such identifier

For a service with a sub-function (session control, security access, routine control and others), the byte after the SID selects the variant, and the positive response echoes it: 10 03 is answered with 50 03 and the session's timing.

These are the bytes after ISO-TP has reassembled the message. On CAN a short one travels in a single frame with a length byte in front, such as 03 22 F1 90.

One NRC is not a refusal: 7F <SID> 78 means the request was accepted and the real answer is still coming.

What are the default, extended and programming sessions for, and what is lost on a session change?

A diagnostic session is a mode of the server that decides which services and identifiers are available.

SessionValueFor
Default01always active after power-up; reading identification, trouble codes and live data
Programming02reflashing; in most ECUs entering it resets into the bootloader, which serves this session
Extended03workshop and end-of-line work: writing configuration, actuator tests, routines

The tester changes session with 10 <session>, and the positive response carries the server's P2 and P2* for that session.

A transition to a non-default session, or back to the default one, ends whatever was bound to the session:

  • Security is locked again.
  • Periodic transmissions stop.
  • Settings changed with CommunicationControl and ControlDTCSetting are restored.

Asking for the session that is already active is allowed and answered positively, and it re-initialises that session, so security locks then too.

Each ECU has its own session. A functional 10 03 moves every ECU that supports it; a physical one moves only its target.

What is the S3 timer, and how does TesterPresent keep a session alive?

S3 is the server's session timeout, 5000 ms by default. It runs whenever the server is in a non-default session, restarts on every request, and on expiry returns the server to the default session as if 10 01 had been received. That re-locks security and restores normal communication and DTC storage.

It exists for the tester that is unplugged in the middle of an extended session: without it the ECU would stay unlocked with its normal messages switched off.

A tester that wants to stay in a session while nothing else is being sent sends TesterPresent (3E) periodically, typically every 2 s. It is usually sent as 3E 80 on the functional address: one frame restarts every ECU's S3 timer, and the suppress bit means none of them answers.

What conformance tests probe:

  • S3 restarts on any complete request, including one answered negatively and one whose response is suppressed.
  • S3 does not run while the server is still producing a response, so a sequence of 78 responses does not time the session out.
  • In the default session S3 does nothing.

What does the suppressPositiveResponse bit do, and which services have it?

It is the top bit of the sub-function byte. When it is set, the server does the work and sends no positive response. 3E 00 is answered with 7E 00; 3E 80 is answered with silence. The low seven bits still select the sub-function.

Only services that have a sub-function byte have the bit: DiagnosticSessionControl, ECUReset, SecurityAccess, CommunicationControl, RoutineControl, TesterPresent, ControlDTCSetting, ReadDTCInformation and the like. ReadDataByIdentifier and WriteDataByIdentifier have no sub-function, so they have no such bit, and their byte after the SID is the high byte of the DID.

Two rules:

  • Negative responses are sent regardless. A tester that suppresses responses still learns of failures (on a physical request; functional addressing drops some of them for its own reasons).
  • After 78, the final response must be sent even if the request had the bit set. The tester has been told an answer is coming.

The bit exists for bus load. A tester that keeps thirty ECUs awake with TesterPresent every two seconds would otherwise get thirty answers each time.

What is the difference between NRC 0x11, 0x12, 0x7E and 0x7F?

All four say "not supported". They differ in what is missing, and whether it is missing always or only now:

NRCNameMeans
11serviceNotSupportedthis ECU does not implement the service at all
7FserviceNotSupportedInActiveSessionthe service exists, but not in the active session
12subFunctionNotSupportedthe service exists, this sub-function does not
7EsubFunctionNotSupportedInActiveSessionthe sub-function exists, but not in the active session

So a tester that receives 7F or 7E knows that changing session may help, and one that receives 11 or 12 knows it will not.

The order in which they are checked is specified: service supported (11), supported in this session (7F), minimum length (13), then sub-function supported (12) and supported in this session (7E). A server that answered 12 for an unknown sub-function of a service that is not available in the session at all would fail conformance, where 7F is expected.

11 and 12 are also among the responses that are not sent to a functionally addressed request, so that the ECUs that do not support a broadcast request stay quiet.

When does a server answer 0x22 conditionsNotCorrect, 0x31 requestOutOfRange and 0x33 securityAccessDenied?

NRCThe request is refused because ofExamples
33 securityAccessDeniedwho is asking: the required security level is not unlockedwriting the VIN before SecurityAccess
22 conditionsNotCorrectthe state of the ECU or the vehiclean actuator test while the vehicle is moving; a write with the engine running; supply voltage too low to program flash
31 requestOutOfRangethe content of the requestan unknown DID; a DID that cannot be written; a value outside its limits; a memory address outside the allowed area

The same request could deserve all three, and the order is specified: security first, then conditions (and sequence, 24), then the parameters. The reason is security. An ECU that answered 31 for an unknown identifier before checking security would let a locked-out tester find out which identifiers exist.

A useful way to tell 22 from 31: with 22 the identical request will succeed later, once the state changes. With 31 it never will.

31 is also what ReadDataByIdentifier returns when none of the requested DIDs can be read in the active session.

How does ReadDataByIdentifier handle several DIDs in one request, and what if one is not supported?

One request may list several 2-byte identifiers. The response gives each in turn, its identifier before its data:

request    22 F1 86 01 01
response   62 F1 86 03 01 01 2E E8     DID F186: 03; DID 0101: 2E E8

The rules for the negative responses:

  • The request is the SID and one or more 2-byte identifiers, so its length must be odd and at least 3. Otherwise 13. Servers also cap the number of DIDs in one request, and more than that is 13 too.
  • A DID that is unknown, or not readable in the active session, is skipped. If at least one requested DID can be read, the response is positive and holds just those. Only if none can be read is the answer 31.
  • A DID that needs a security level the tester does not have gives 33.
  • If the combined response would be longer than the transport can carry, 14, responseTooLong.

The skip rule surprises people. It lets one tester script ask a whole family of ECU variants for the same list and take what each one has.

The tester cannot find record boundaries from the response alone. Each DID's length is fixed for that ECU and described in its data file (ODX or CDD).

What does ClearDiagnosticInformation clear, and when may the positive response be sent?

The request names a group of DTCs in three bytes. FF FF FF is all of them:

request    14 FF FF FF
response   54

For each selected DTC it:

  • resets the status byte to "not tested since the clear": the two testNotCompleted bits set and all the others cleared, which reads as 50;
  • removes the snapshot records and the extended data;
  • resets the counters, such as the occurrence, trip and ageing counters.

The positive response means the non-volatile memory has been updated, not that the clear has been queued. If the write takes longer than P2, the server sends 78 and answers when the memory is done. If the write fails, the answer is 72, generalProgrammingFailure.

Two properties are deliberate. It needs no security access and is available in the default session, because a workshop must always be able to clear faults after a repair. And it clears stored information, not the fault: if the fault is still present, its monitor fails again and the DTC returns.

What is the difference between the ECUReset types, and when is the response sent?

RequestReset typeActs as if
11 01hardResetpower had been removed and restored
11 02keyOffOnResetthe ignition had been switched off and on
11 03softResetthe application had been restarted

A hard reset re-initialises everything, including volatile memory. A key-off-on reset runs the normal shutdown first, so data that is written at shutdown gets stored, and it starts a new operation cycle for the fault memory. A soft reset restarts the application without the full power-up path, and what exactly it keeps is the manufacturer's definition.

The rule to get right is respond, then reset. The positive response 51 01 must be on the bus before the MCU restarts. Calling the reset function straight after queuing the response loses the frame in the transmit mailbox, and the tester reports a timeout for a reset that worked.

So the handler waits for the transmit confirmation (or a short fixed time), finishes any pending non-volatile write, and then resets. After the reset the server is in the default session with security locked.

Why must a SecurityAccess seed be unpredictable, and what does a server answer to a seed request when it is already unlocked?

The protection of seed and key rests on the seed being different each time, so that a recorded exchange cannot be replayed. If the seed can be predicted or made to repeat, that is lost. A seed taken from a counter, or from a pseudo-random generator that starts from the same value at every boot, comes round again, and an attacker who has seen one valid seed and key pair waits for that seed, or resets the ECU until it appears.

What a server needs:

  • A hardware random number generator, or at least a source that varies between boots, such as ADC noise or a free-running timer captured on a bus event.
  • A seed that is good for one answer. After any key, right or wrong, it is spent. Otherwise an attacker requests one seed and tries every key against it.
  • An attempt counter and a delay timer that survive a reset.

When that level is already unlocked, a seed request is answered positively with a seed of all zeros. It means "nothing to do". The server does not generate a new seed and does not lock again.

That also means an unlocked server must never issue an all-zero seed as a real challenge, so a generator that produces zero draws again.

How do the three RoutineControl sub-functions work, and how does a long routine report its result?

A routine is a named procedure addressed by a 16-bit routine identifier (RID): erase a memory area, check a downloaded image, run an actuator test.

RequestMeaning
31 01 <RID> [options]startRoutine
31 02 <RID>stopRoutine
31 03 <RID>requestRoutineResults

Each routine's specification says which of two designs it uses:

  • Synchronous. The positive response to start is sent when the routine has finished, and carries its result. While it runs, the server sends 7F 31 78 before P2 expires and again before each P2*. Right for jobs of up to a few seconds, such as erasing flash.
  • Asynchronous. Start is answered at once, meaning "started". The routine runs in the background, the tester polls with requestRoutineResults, and may end it with stopRoutine. Needed for long jobs during which the tester must stay able to talk.
tester   31 01 02 03       start routine 0x0203
ECU      7F 31 78          it takes longer than P2
ECU      71 01 02 03 00    done; one byte of result

Sequence errors get 24: asking for the results of a routine that was never started, or stopping one that is not running. A routine that moves something checks its preconditions first (22), is limited in time, and stops when the session ends.

Walk through the services a tester uses to reprogram an ECU.

StepRequestWhy
110 03 to all ECUsextended session
231 01 02 03check the programming preconditions: voltage, vehicle state
385 02 and 28 03 01 to allstop DTC storage and normal messages on every ECU
410 02 to the targetprogramming session: the ECU resets into its bootloader
527 ..unlock the programming security level
62E F1 5A ..write a fingerprint: who is programming, and when
731 01 FF 00 ..erase the memory area
834, 36 repeated, 37RequestDownload, the TransferData blocks, RequestTransferExit
931 01 FF 01check the image's integrity, signature and dependencies
1011 01, then 28 00, 85 01, 14 FF FF FFreset into the new software, restore the other ECUs, clear faults

Throughout, the tester sends 3E 80 functionally every two seconds, so that no ECU's S3 timer returns it to normal operation half-way.

What an interviewer listens for is the reason behind each step:

  • Steps 1 to 3 prepare the whole vehicle. The other ECUs would otherwise store "lost communication" faults while the target is silent, and their normal traffic would slow the download.
  • Erase and the integrity check take seconds, so they answer with 78 first.
  • The bootloader accepts the image only after the check in step 9. A download that was interrupted leaves the ECU in its bootloader, still able to be programmed.
  • If the tester disappears, S3 restores communication and DTC storage on the other ECUs.