← All posts

Making a Millennium payphone answer to the server

Orange diagram with the Millennium Manager (NCC) in the middle, joined by double-headed arrows to two payphone terminals, a desk terminal with datajack, a system manager terminal and a remote client workstation. Blue circles and question marks mark the arrowheads that point at the terminals.
Section 1 divider of the Millennium Telco Maximizer user guide, version 2, March 1996. The arrows run both ways, but I couldn't find the NCC ever calling a terminal, hence the blue question marks.

This is the first entry in Clanking the Millennium, a series of short write-ups about things I've done to Nortel Millennium payphones. A lot of the work involves LLM coding agents, mostly for the tedious parts: walking through disassembly, cross-referencing addresses, writing the scaffolding that tests a patch. I decide what to look for and I check what comes back. They save me from reading a few hundred thousand bytes of Z180 assembly by hand.

The subject this time is incoming calls. Millennium terminals ring, but ask one to answer as a modem and nothing happens, whatever the documentation says.

What the documents promise

The Millennium Database Design Report describes a field called CALL MODE in the feature configuration table, which tells the terminal how to treat an incoming call. The text is the same in MSR 1.8 and in the preliminary MSR 2.1 schema from June 1999:

Value Meaning
0 Ringing disabled, no answering of incoming data calls
1 Ringing enabled, answering voice calls only
2 Ringing disabled, answering as data call immediately
3 Ringing enabled, answering data call after specified number of rings

MSR 1.8 puts INCOMING DATA CALL DELAY right after it, the number of rings before a mode 3 terminal picks up (default 15, range 0 to 255), and the delay is still on the terminal screen in the 2.1 Reference Guide. The craft interface numbers the choices from 1 to 4, so what I call mode 2 and mode 3 here are the third and fourth entries in the terminal's own menu. The Reference Guide even has a validation error for an out-of-range Incoming Call Mode.

The 2.1 documents quietly back away from it, though. The Terminal Management Guide tells the operator to enter 0 to deny incoming calls or 1 to answer voice calls, and never mentions 2 or 3. In the 2.1 schema the field after CALL MODE is INCOMING CALL DIGIT OUTPULSE, and the Feature Configuration Guide uses it, together with mode 1, for an "incoming call anti-fraud" feature: the terminal sends a digit to the switch when a voice call is answered. Elsewhere in the same schema, the call statistics have a counter for answered incoming calls.

So the platform clearly had a design in which something dials the payphone, the payphone picks up with its modem, and a data session follows. Reading the System Manager's Handbook, though, I couldn't find the NCC ever doing that. What it describes is a call-back flag: during a card authorisation, an alarm or a cash box collection, the NCC asks the terminal to dial back, either immediately or at its next scheduled call-in. The terminal always makes the call.

That's a strange thing to leave out. Elcotel's Poll Manager reaches its phones over their modems, and the .C94 option files I wrote about in the Elcotel post carry an incoming-call switch. Protel and other North American payphone makers supported calls in both directions too, from the terminal and to it. The Millennium never did, even though its documentation lists the modes.

A word on naming. In Millennium documents a "call-in" is the terminal phoning the NCC, as in "failure to call in" reports and periodic call-in intervals. I've been using the word for the opposite direction, and the more correct term for what I built would be a call-out from the NCC. I'm keeping "call-in" in the title and the filenames because that's what the feature became known as while I worked on it. It does fit in one respect: once the terminal answers, it opens with its ordinary call-in dialog, as if it had dialled out.

Does the firmware do it?

It doesn't. Taking 06CNB02, the MTR1.7 build for PTT BIH/BH Telecom, I traced how ring detection works and what it does with the CALL MODE byte.

Detection starts at a sampler that runs on a timer and reads the line status port. Bit 7 is the ring signal. It takes 10 consecutive samples to call the ring on, 6 to call it off, and a 250-tick timer declares the call abandoned after about five seconds of silence. Ring events go to a ring task, which tells the call-control task to draw the prompt, Zvoni molim / Odgovorite on this build, ** Ringing ** Please answer on English ones. A person lifts the handset and the voice path takes over.

The CALL MODE byte lives in RAM at 0xE34B, loaded from table 26 of the NCC download. Exactly one routine in the whole ROM reads it: the one that configures ring detection when the phone starts up. Mode 0 turns detection off. Modes 1 and 3 turn it on with the ringer enabled. Mode 2 turns it on with the ringer silenced. That's the whole implementation: the data modes only decide whether the bell sounds. Nothing ever seizes the line.

The byte after it, at 0xE34C, has no readers at all in this ROM. In the table as the firmware lays it out, it's the anti-fraud field, called INCOMING CALL DIGIT OUTPULSE in the 2.1 schema. MSR 1.8 lists an INCOMING DATA CALL DELAY at that spot instead, but table 26 has no separate delay field, so the ring count has nowhere else to live. A byte the manager already provisions and this ROM ignores makes a good home for it in a patch.

06CNB02 isn't special. I compared it with every other dump I could get hold of: the 256 KiB MTR1 control ROMs, from 12AAV08 and 06CAA17 up to NPE1S01, and the 1 MiB MTR2 images in the NLA, NOA, NOK and NQA families, from the muCCC archive. Every one has the same arrangement, a ring sampler feeding a ring task, next to the same native routine for placing an outgoing data call. Only the addresses move, and the editions differ in smaller ways: a different task number for the ring handler, the older kernels keeping their stack where newer ones keep scratch space, MTR2 builds with anywhere from four to ten places that write the modem's control register.

What answering involves

If the firmware were to answer a call, it would have to do what it already does when it dials out, minus the dialling. The outgoing path is a state machine run by one of the kernel tasks: close the line relay, set up the modem, arm carrier detection, dial, wait for carrier. State 12 means a data call is established. Everything above it, the NCC packet layer and the dialog queue, only cares about reaching that state.

The modem is the other question. It's driven through bit-banged serial writes on a PIO port, two bytes per setting. The originate setup writes 0x1B to control register 0. The answer setup is the same write with bit 0 clear, 0x1A, plus 0x08 in register 1. Both values show up in the register writes the firmware already makes on the way to dialling.

So the patch has to do these things when the right ring arrives:

  • check that nobody is using the phone, and that the line wasn't released in the last two seconds;
  • seize the line and put the modem in answer mode;
  • enter the native data-call state machine at state 10, the point after dialling, and let it carry on to 12 by itself;
  • set the NCC state and queue a CALL_IN dialog, so the packet layer behaves as though the terminal had placed the call.

Condensed, with the guard checks trimmed to the first one, the routine in my patch looks like this (Z180 mnemonics, hex addresses are 06CNB03 RAM and ROM):

answer:
    LD   A,(0FAF1h)     ; hook switch: 0 = on-hook
    OR   A
    JP   NZ,done        ; handset is up, so a human got there first

    ; ... more guards: UI active, no data call, NCC idle, line released > 2 s ago

    ; act as if the NCC task had started a data call, but skip the dial
    LD   A,1
    LD   (8813h),A      ; NCC comm state = data call setup
    LD   A,(0FAEFh)
    OR   02h
    LD   (0FAEFh),A     ; terminal state: busy with data
    LD   HL,(880Eh)
    LD   A,L
    OR   20h
    LD   L,A
    LD   (880Eh),HL     ; pending dialogs |= CALL_IN

    CALL 4B4Dh          ; native routine: close the line relay

    LD   L,01h          ; modem control register 1
    LD   E,08h
    CALL 1300h          ; bank stub into the native register write
    LD   L,00h          ; modem control register 0
    LD   E,1Ah          ; same as originate (1Bh) with bit 0 clear = answer mode
    CALL 1300h

    ; ... turn off ring detection, arm the native carrier scan, start the 30 s timeout

    LD   A,0Ah
    LD   (87E6h),A      ; native data-call state 10; its handler walks 10 > 11 > 12
done:
    RET

Fitting it into a full ROM

The image has no spare room in the useful places, but it does have 31 KB of zeros after the last module I added, and a 500-byte zero run in the always-mapped bank. The module itself, about 330 bytes, goes in the former. Two six-byte detours hook the ring-on handler and the ring-abandoned handler, jumping to small trampolines in the second. Each saves the bank register, maps the module's bank, calls in, and restores everything before returning.

The ring-on hook, as it sits in the ROM before and after patching:

; 2624Dh, in the ring task, testing the signal it was woken with
; original:
    LD   HL,(0FAF5h)    ; pending signal word
    LD   A,L
    AND  10h            ; RING_ON?
; patched (6 bytes, same length):
    CALL 130Ch          ; trampoline in the zero run in bank 0
    NOP
    NOP
    NOP

; 130Ch, the trampoline
    PUSH BC
    IN0  B,(39h)        ; current bank base register
    PUSH BC             ; remember it
    LD   B,31h
    OUT0 (39h),B        ; map the bank that holds the module
    CALL ring_on        ; in the module, logical 75xxh
    POP  BC
    OUT0 (39h),B        ; put the old bank back
    POP  BC
    RET                 ; flags come out exactly as the module left them

; module: ring_on
ring_on:
    PUSH AF
    PUSH BC
    PUSH DE
    PUSH HL
    LD   A,(0FAF5h)
    AND  10h            ; the task wakes for other reasons too; count only real rings
    CALL NZ,count_ring
    POP  HL
    POP  DE
    POP  BC
    POP  AF
    LD   HL,(0FAF5h)   ; run the three instructions we overwrote...
    LD   A,L
    AND  10h           ; ...so the caller's next branch sees the flags it expects
    RET

My first build crashed, and the cause was instructive. I'd reused the bank-switching call stub the ROM already has, which is a tail-call format meant for the first instruction of a function. Using it mid-function overwrote a word of the caller's stack frame, and a later RET jumped to address 0. The fix was to hook only at function entries or go through the trampolines.

Counting rings brought its own trap: the ring task is woken for reasons other than rings, so the hook must check the ring-on bit before counting, or the terminal answers early. And the modem driver rewrites the control register in three places, one of which clears answer mode when transmit goes off, so those writes go through a small shim that keeps bit 0 clear for the duration of an answered call.

The counting and the shim are short:

count_ring:
    LD   A,(0E34Bh)     ; CALL MODE
    CP   02h
    RET  C              ; modes 0 and 1 never answer
    LD   A,(8383h)      ; ring counter, in a spare RAM byte
    INC  A
    LD   (8383h),A
    LD   B,A
    LD   A,(0E34Bh)
    CP   02h
    JP   Z,answer       ; mode 2: first ring is enough
    LD   A,(0E34Ch)     ; mode 3: the configured ring count
    OR   A
    JP   NZ,have_n
    LD   A,01h          ; zero would never trigger, so treat it as one
have_n:
    CP   B              ; compare N with rings so far
    JP   Z,answer
    JP   C,answer       ; N below the count: catch up
    RET

; 1340h, replaces the three calls to the native register write that
; set control register 0 (CALL 6FDDh at 6F3Dh, 6F54h and 6F63h)
cr0_shim:
    PUSH AF
    LD   A,(8384h)      ; set while an inbound call is up
    OR   A
    JR   Z,native
    LD   E,1Ah          ; swap the originate value for answer mode
native:
    POP  AF
    JP   6FDDh          ; carry on into the original routine

Pointing it at the real manager

The goal is for the ordinary NCC manager to call up a terminal and provision it or read statistics back, so the answered call has to land in the firmware's own NCC code, the same code that handles a call the terminal placed itself.

Everything below ran in an emulator of these terminals that I've been building alongside the patches. It deserves a post of its own, so for now I'll only say what it did. In the emulator, a call to a terminal set to mode 2 runs like this:

ring, answer, line seized, state 10 > 11 > 12 (carrier)
terminal > manager    CALL_IN (terminal ID, firmware 06CNB03V1.0EMU)
manager  > terminal   TRANS_DATA (send me your data)
terminal > manager    TERM_STATUS
manager  > terminal   END_DATA
hang up, ring detection re-armed

The whole thing takes about ten seconds. The terminal speaks first, as it would after dialling out. That's a convenient choice for now and easy to change later, since it's decided in one place. With tables and statistics requests queued on the manager, the same exchange carries them in both directions: the downloads and the reads arrive over the inbound call just as they would over one the terminal dialled itself.

Another bug was encountered, and it was purely cosmetic. In mode 3, after two unanswered rings the display shows the ring prompt, and when the terminal answered on the third, the prompt stayed up through the whole call and afterwards. The ring prompt is cleared by an abandon message that the sampler sends five seconds after the last ring, and answering disables ring detection, so that message never came. The fix is for the answer path to send the abandon signal itself when it takes the call. The screen goes back to the idle display the moment the line is seized.

A call to the patched 06CNB03 in the emulator, set to answer after three rings. The log beside the terminal follows the ring bursts, the answer, the carrier and the CALL_IN exchange with the manager, and the display drops back to the idle prompt as soon as the terminal answers. About half a minute, no sound. Open the video.

Other firmware

Hook points found by scanning for address bytes get you one ROM. I wanted the patch to work on firmware I haven't seen yet, so the patcher locates ring handlers, modem writes, the NCC setup code and free space from the code's shape, and nothing is keyed on a file hash. Hashes are only recorded in the output so a build can be reproduced.

On MTR1, which is one 256 KiB chip, nineteen distinct images built, and all nineteen answered a simulated ring in the emulator. The last holdout, NPE1S01, looked dead at first, but the patch wasn't the problem: that firmware sends its display text to a different output port than the other builds, and the emulator needed to be told. Three older builds (12AAV08, 06CAA17 and 06JAA03) went further and completed a native NCC exchange with the real manager.

MTR2 is more awkward. Each terminal has two 512 KiB flash chips that together make one 1 MiB image, and it won't boot without both, so the patcher accepts either a merged file or the two chips, flash 0 first. My first mapping for NLA1X05 also turned out to test a terminal status byte where it should have read the handset switch. The patcher now looks for the byte that the firmware's own on-hook and off-hook handlers write.

The NLA, NOA and NOK builds I tried, plus the NQA1X01 dumps from the muCCC archive, pass the emulator self-test. When the signatures are ambiguous the patcher stops and writes nothing: NCA1X03, NKA1X02 and NKA1X05 each have two plausible NCC setup sites, and I'd rather it refuse than guess.

The easiest way to try it is the patcher tool on this site. You upload a ROM and it lists the patches that apply to that image. It builds the one you pick, boots the result in the same emulator for the checks described above (immediate answer, answer on the Nth ring, carrier accepted, and no pickup in modes 0 and 1), and shows the emulator's report next to the download. If you'd rather not send a ROM to my service, the source and API docs are on GitHub, and you can run it yourself.

What isn't proven

I haven't tried this on an actual terminal yet. I've already done deployed some code from this "series" though, and the terminal didn't explode (that will get its own post soon), so I'm hopeful.

The catch is that the anti-fraud byte now doubles as the ring count. 06CNB02 never reads it, but I found readers in NOA1X04 and the keyboard build NNK1F05, which use a non-zero value to pick between two timer values in the incoming-call handling. On those builds a mode 3 ring count would also flip that choice for a human who picks up. Mode 1, the setting anti-fraud is meant for, is unaffected because the patch ignores the byte there. I haven't looked at the other builds.

Security

Getting the downloads and statistics reads through an inbound call showed me the real problem. Anyone who can dial the terminal's number gets a session: they can send it packets, pull its statistics and push tables at it. Nothing in the inbound path asks who is calling. The terminal ID in the call-in dialog isn't a secret, and the CRC on the packets only catches line noise.

For anything beyond my bench, this needs authentication. The scrambling the TTY service uses for its login is obfuscation, not protection. DES would be a real improvement: single DES is weak by today's standards, but an attacker who can't tap the line has to phone the terminal for every guess, one slow modem session at a time, which makes brute force impractical. Someone who can capture a handshake off the line is a different case, and a much bigger project than dialling a number. What I'd want is a challenge-response, where the manager sends a random value and the terminal answers with it encrypted under a per-terminal key.

That isn't free. The answer path fits in about 330 bytes, while an encryption routine, somewhere to keep a key for each terminal and the code to handle the exchange would take far more room. It also needs a packet type that doesn't exist in the standard NCC protocol, so the manager would have to learn it as well as the terminal. Some firmware builds have plenty of spare space; others have little padding left, and a few may be out of reach for a patch this size.

References

The CALL MODE values and the INCOMING DATA CALL DELAY field come from the Millennium Database Design Report, MSR 1.8 (P0833657, issue 01.01, February 1996), in the feature configuration table description. The same values appear in the preliminary MSR 2.1 edition of the report (issue 00.02, June 1999), where the FEATRU table has INCOMING CALL DIGIT OUTPULSE after CALL MODE and the call statistics table has the answered incoming call counter.

The call-back flag is described in the Millennium System Manager's Handbook, MSR 1.8 (P0833658, issue 01.01, February 1996). The Incoming Call Mode validation error is in the System Management Reference Guide, MSR 1.8 (P0833660, issue 01.01, February 1996).

For MSR 2.1, the instruction to enter 0 or 1 for Call Mode is in the Millennium Manager interactive interface Terminal management guide (A0685013, issue 00.01, November 1998). The incoming call anti-fraud configuration is in the preliminary Feature Configuration Guide, version 1 (issue 00.01, June 1999), and the on-screen INCOMING DATA CALL DELAY field is in the preliminary MSR 2.1 System Management Reference Guide (issue 00.02, June 1999).

The header image is from the section 1 divider of the Millennium Telco Maximizer with user guide, version 2 (A0644859, issue 02, March 1996).

The Elcotel and Protel comparison refers to my Elcotel rate file post and the ModemBridge post.

The patcher service behind the tool page is GPL-3.0 licensed. Most of the firmware dumps I compared come from the muCCC millennium repository. The ring handling, CALL MODE byte, table 26 layout and patches described here come from my own disassembly of those images, not from a published specification.