xref: /aosp_15_r20/external/gsc-utils/docs/usb_updater.md (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1*4f2df630SAndroid Build Coastguard WorkerEC update over USB
2*4f2df630SAndroid Build Coastguard Worker==================
3*4f2df630SAndroid Build Coastguard Worker
4*4f2df630SAndroid Build Coastguard Workerchip/g (Cr50) and common code (hammer, servo_micro/v4) update over USB protocols
5*4f2df630SAndroid Build Coastguard Workershare a lot in terms of protocol and ideas, but use different code bases.
6*4f2df630SAndroid Build Coastguard Worker
7*4f2df630SAndroid Build Coastguard Workerchip/g EC-side implementation is found in `chip/g/*upgrade*`, and the
8*4f2df630SAndroid Build Coastguard Workeruserspace tool which provides updates over USB among with supporting other
9*4f2df630SAndroid Build Coastguard Workerfeatures and interfaces is found in `extra/usb_updater/gsctool.c`.
10*4f2df630SAndroid Build Coastguard Worker
11*4f2df630SAndroid Build Coastguard WorkerCommon code uses implementations in `common/*update*.c` and
12*4f2df630SAndroid Build Coastguard Worker`include/*update*.h`, and `extra/usb_updater/usb_updater2.c` for the userspace
13*4f2df630SAndroid Build Coastguard Workerupdater.
14*4f2df630SAndroid Build Coastguard Worker
15*4f2df630SAndroid Build Coastguard WorkerCr50-specific notes
16*4f2df630SAndroid Build Coastguard Worker-------------------
17*4f2df630SAndroid Build Coastguard Worker
18*4f2df630SAndroid Build Coastguard WorkerThe Cr50 firmware image consists of multiple sections, of interest to the
19*4f2df630SAndroid Build Coastguard WorkerUSB updater are the RO and RW code sections, two of each. When firmware update
20*4f2df630SAndroid Build Coastguard Workersession is established, the Cr50 device reports locations of backup RW and RO
21*4f2df630SAndroid Build Coastguard Workersections (those not currently used by the device).
22*4f2df630SAndroid Build Coastguard Worker
23*4f2df630SAndroid Build Coastguard WorkerBased on this information the updater carves out the appropriate sections from
24*4f2df630SAndroid Build Coastguard Workerthe full Cr50 firmware binary image and sends them to the device for
25*4f2df630SAndroid Build Coastguard Workerprogramming into flash. Once the new sections are programmed and the device
26*4f2df630SAndroid Build Coastguard Workeris restarted, the new RO and RW are used if they pass verification and are
27*4f2df630SAndroid Build Coastguard Workerlogically newer than the existing sections.
28*4f2df630SAndroid Build Coastguard Worker
29*4f2df630SAndroid Build Coastguard WorkerThere are two ways to communicate with the Cr50 device: USB and `/dev/tpm0`
30*4f2df630SAndroid Build Coastguard Worker(when `gsctool` is running on a chromebook with the Cr50 device). Originally
31*4f2df630SAndroid Build Coastguard Workerdifferent protocols were used to communicate over different channels,
32*4f2df630SAndroid Build Coastguard Workerstarting with version 3 the same protocol is used.
33*4f2df630SAndroid Build Coastguard Worker
34*4f2df630SAndroid Build Coastguard WorkerCommon-code notes
35*4f2df630SAndroid Build Coastguard Worker-----------------
36*4f2df630SAndroid Build Coastguard Worker
37*4f2df630SAndroid Build Coastguard WorkerFor non-Cr50 or chip/g devices (common code), the layout is a bit different,
38*4f2df630SAndroid Build Coastguard Workeras devices usually have a single RO and a single RW, where RO is truly read-only
39*4f2df630SAndroid Build Coastguard Workerin production, and verifies RW before jumping to it.
40*4f2df630SAndroid Build Coastguard Worker
41*4f2df630SAndroid Build Coastguard WorkerFor testing and development, `usb_updater2` is provided, while production code
42*4f2df630SAndroid Build Coastguard Workerwill use `hammerd` (in `src/platform/hammerd`) to update the device.
43*4f2df630SAndroid Build Coastguard Worker
44*4f2df630SAndroid Build Coastguard WorkerUpdate protocol
45*4f2df630SAndroid Build Coastguard Worker---------------
46*4f2df630SAndroid Build Coastguard Worker
47*4f2df630SAndroid Build Coastguard WorkerThe host (either a local AP or a workstation) is the controller of the firmware
48*4f2df630SAndroid Build Coastguard Workerupdate protocol, it sends data to the Cr50 device, which processes it and
49*4f2df630SAndroid Build Coastguard Workerresponds.
50*4f2df630SAndroid Build Coastguard Worker
51*4f2df630SAndroid Build Coastguard WorkerThe encapsulation format is different between the `/dev/tpm0` and USB cases:
52*4f2df630SAndroid Build Coastguard Worker
53*4f2df630SAndroid Build Coastguard Worker      4 bytes      4 bytes         4 bytes               variable size
54*4f2df630SAndroid Build Coastguard Worker    +-----------+--------------+---------------+----------~~--------------+
55*4f2df630SAndroid Build Coastguard Worker    + total size| block digest |  dest address |           data           |
56*4f2df630SAndroid Build Coastguard Worker    +-----------+--------------+---------------+----------~~--------------+
57*4f2df630SAndroid Build Coastguard Worker     \           \                                                       /
58*4f2df630SAndroid Build Coastguard Worker      \           \                                                     /
59*4f2df630SAndroid Build Coastguard Worker       \           +----- FW update PDU sent over /dev/tpm0 -----------+
60*4f2df630SAndroid Build Coastguard Worker        \                                                             /
61*4f2df630SAndroid Build Coastguard Worker         +--------- USB frame, requires total size field ------------+
62*4f2df630SAndroid Build Coastguard Worker
63*4f2df630SAndroid Build Coastguard WorkerThe update protocol data units (PDUs) are passed over `/dev/tpm0`, the
64*4f2df630SAndroid Build Coastguard Workerencapsulation includes integrity verification and destination address of
65*4f2df630SAndroid Build Coastguard Workerthe data (more of this later). `/dev/tpm0` transactions pretty much do not
66*4f2df630SAndroid Build Coastguard Workerhave size limits, whereas the USB data is sent in chunks of the size
67*4f2df630SAndroid Build Coastguard Workerdetermined when the USB connection is set up. This is why USB requires an
68*4f2df630SAndroid Build Coastguard Workeradditional encapsulation into frames to communicate the PDU size to the
69*4f2df630SAndroid Build Coastguard Workerclient side so that the PDU can be reassembled before passing to the
70*4f2df630SAndroid Build Coastguard Workerprogramming function.
71*4f2df630SAndroid Build Coastguard Worker
72*4f2df630SAndroid Build Coastguard WorkerIn general, the protocol consists of two phases: connection establishment
73*4f2df630SAndroid Build Coastguard Workerand actual image transfer.
74*4f2df630SAndroid Build Coastguard Worker
75*4f2df630SAndroid Build Coastguard WorkerThe very first PDU of the transfer session is used to establish the
76*4f2df630SAndroid Build Coastguard Workerconnection. The first PDU does not have any data, and the `dest address`
77*4f2df630SAndroid Build Coastguard Workerfield is set to zero. Receiving such a PDU signals the programming function
78*4f2df630SAndroid Build Coastguard Workerthat the host intends to transfer a new image.
79*4f2df630SAndroid Build Coastguard Worker
80*4f2df630SAndroid Build Coastguard WorkerThe response to the first PDU varies depending on the protocol version.
81*4f2df630SAndroid Build Coastguard Worker
82*4f2df630SAndroid Build Coastguard WorkerNote that protocol versions before 5 are described here for completeness,
83*4f2df630SAndroid Build Coastguard Workerbut are not supported any more.
84*4f2df630SAndroid Build Coastguard Worker
85*4f2df630SAndroid Build Coastguard WorkerVersion 1 is used over `/dev/tpm0`. The response is either 4 or 1 bytes in
86*4f2df630SAndroid Build Coastguard Workersize. The 4 byte response is the *base address* of the backup RW section,
87*4f2df630SAndroid Build Coastguard Workerand there is no support for RO updates. The one byte response is an error
88*4f2df630SAndroid Build Coastguard Workerindication, possibly reporting flash erase failure, command format error, etc.
89*4f2df630SAndroid Build Coastguard Worker
90*4f2df630SAndroid Build Coastguard WorkerVersion 2 is used over USB. The response is 8 bytes in size. The first four
91*4f2df630SAndroid Build Coastguard Workerbytes are either the *base address* of the backup RW section (still no RO
92*4f2df630SAndroid Build Coastguard Workerupdates), or an error code, the same as in Version 1. The second 4 bytes
93*4f2df630SAndroid Build Coastguard Workerare the protocol version number (set to 2).
94*4f2df630SAndroid Build Coastguard Worker
95*4f2df630SAndroid Build Coastguard WorkerAll versions above 2 behave the same over `/dev/tpm0` and USB.
96*4f2df630SAndroid Build Coastguard Worker
97*4f2df630SAndroid Build Coastguard WorkerVersion 3 response is 16 bytes in size. The first 4 bytes are the error code
98*4f2df630SAndroid Build Coastguard Workerthe second 4 bytes are the protocol version (set to 3) and then 4 byte
99*4f2df630SAndroid Build Coastguard Worker*offset* of the RO section followed by the 4 byte *offset* of the RW section.
100*4f2df630SAndroid Build Coastguard Worker
101*4f2df630SAndroid Build Coastguard WorkerVersion 4 response in addition to version 3 provides header revision fields
102*4f2df630SAndroid Build Coastguard Workerfor active RO and RW images running on the target.
103*4f2df630SAndroid Build Coastguard Worker
104*4f2df630SAndroid Build Coastguard WorkerOnce the connection is established, the image to be programmed into flash
105*4f2df630SAndroid Build Coastguard Workeris transferred to the Cr50 in 1K PDUs. In versions 1 and 2 the address in
106*4f2df630SAndroid Build Coastguard Workerthe header is the absolute address to place the block to, in version 3 and
107*4f2df630SAndroid Build Coastguard Workerlater it is the offset into the flash.
108*4f2df630SAndroid Build Coastguard Worker
109*4f2df630SAndroid Build Coastguard WorkerProtocol version 5 includes RO and RW key ID information into the first PDU
110*4f2df630SAndroid Build Coastguard Workerresponse. The key ID could be used to tell between prod and dev signing
111*4f2df630SAndroid Build Coastguard Workermodes, among other things.
112*4f2df630SAndroid Build Coastguard Worker
113*4f2df630SAndroid Build Coastguard WorkerProtocol version 6 does not change the format of the first PDU response,
114*4f2df630SAndroid Build Coastguard Workerbut it indicates the target's ability to channel TPM vendor commands
115*4f2df630SAndroid Build Coastguard Workerthrough USB connection.
116*4f2df630SAndroid Build Coastguard Worker
117*4f2df630SAndroid Build Coastguard WorkerCommon-code updater also uses protocol version 6, but has a fairly different
118*4f2df630SAndroid Build Coastguard Worker`first_response_pdu` header, indicated by setting `1` in the higher 16-bit for
119*4f2df630SAndroid Build Coastguard Workerthe protocol version field (`header_type`). The response includes fields
120*4f2df630SAndroid Build Coastguard Workersuch as maximum PDU size (which is not fixed to 1KB like for Cr50), flash
121*4f2df630SAndroid Build Coastguard Workerprotection status, version string, and a minimum rollback version.
122*4f2df630SAndroid Build Coastguard Worker
123*4f2df630SAndroid Build Coastguard WorkerDetails can be found in `include/update_fw.h`.
124*4f2df630SAndroid Build Coastguard Worker
125*4f2df630SAndroid Build Coastguard Worker### State machine (update over USB)
126*4f2df630SAndroid Build Coastguard Worker
127*4f2df630SAndroid Build Coastguard WorkerThis describes the EC-side state machine for update over USB.
128*4f2df630SAndroid Build Coastguard Worker
129*4f2df630SAndroid Build Coastguard WorkerIDLE state:
130*4f2df630SAndroid Build Coastguard Worker
131*4f2df630SAndroid Build Coastguard Worker* If host sends update start PDU (a command without any payload, digest = 0
132*4f2df630SAndroid Build Coastguard Worker  and base = 0):
133*4f2df630SAndroid Build Coastguard Worker
134*4f2df630SAndroid Build Coastguard Worker  * Reply with `first_update_pdu` block. Go to OUTSIDE_BLOCK state.
135*4f2df630SAndroid Build Coastguard Worker
136*4f2df630SAndroid Build Coastguard Worker* If host sends a vendor command (see below), execute that, reply, and stay
137*4f2df630SAndroid Build Coastguard Worker  in IDLE state. Note that vendor commands are only accepted in IDLE state.
138*4f2df630SAndroid Build Coastguard Worker
139*4f2df630SAndroid Build Coastguard WorkerOUTSIDE_BLOCK (preparing to receive start of PDU):
140*4f2df630SAndroid Build Coastguard Worker
141*4f2df630SAndroid Build Coastguard Worker* If no data is received in 5 seconds, go back to IDLE state.
142*4f2df630SAndroid Build Coastguard Worker* If host sends `UPDATE_DONE` command (by setting `dest address` to
143*4f2df630SAndroid Build Coastguard Worker  `0xb007ab1e`), go back to IDLE state.
144*4f2df630SAndroid Build Coastguard Worker* If host sends a valid block start with a valid address, copy the rest
145*4f2df630SAndroid Build Coastguard Worker  of the payload and go to INSIDE_BLOCK state.
146*4f2df630SAndroid Build Coastguard Worker
147*4f2df630SAndroid Build Coastguard WorkerINSIDE_BLOCK (in a middle of a PDU):
148*4f2df630SAndroid Build Coastguard Worker
149*4f2df630SAndroid Build Coastguard Worker* If no data is received in 5 seconds, go back to IDLE state.
150*4f2df630SAndroid Build Coastguard Worker* Copy data to a buffer.
151*4f2df630SAndroid Build Coastguard Worker
152*4f2df630SAndroid Build Coastguard Worker  * If buffer is full (i.e. matches the total expected PDU size), write the
153*4f2df630SAndroid Build Coastguard Worker    data and go to OUTSIDE_BLOCK.
154*4f2df630SAndroid Build Coastguard Worker  * Else, stay in INSIDE_BLOCK.
155*4f2df630SAndroid Build Coastguard Worker
156*4f2df630SAndroid Build Coastguard Worker### Vendor commands (channeled TPM command, Cr50)
157*4f2df630SAndroid Build Coastguard Worker
158*4f2df630SAndroid Build Coastguard WorkerWhen channeling TPM vendor commands the USB frame looks as follows:
159*4f2df630SAndroid Build Coastguard Worker
160*4f2df630SAndroid Build Coastguard Worker      4 bytes      4 bytes         4 bytes       2 bytes      variable size
161*4f2df630SAndroid Build Coastguard Worker    +-----------+--------------+---------------+-----------+------~~~-------+
162*4f2df630SAndroid Build Coastguard Worker    + total size| block digest |    EXT_CMD    | vend. sub.|      data      |
163*4f2df630SAndroid Build Coastguard Worker    +-----------+--------------+---------------+-----------+------~~~-------+
164*4f2df630SAndroid Build Coastguard Worker
165*4f2df630SAndroid Build Coastguard WorkerWhere `Vend. sub` is the vendor subcommand, and data field is subcommand
166*4f2df630SAndroid Build Coastguard Workerdependent. The target tells between update PDUs and encapsulated vendor
167*4f2df630SAndroid Build Coastguard Workersubcommands by looking at the `EXT_CMD` value - it is set to `0xbaccd00a` and
168*4f2df630SAndroid Build Coastguard Workeras such is guaranteed not to be a valid update PDU destination address.
169*4f2df630SAndroid Build Coastguard Worker
170*4f2df630SAndroid Build Coastguard WorkerThese commands cannot exceed the USB packet size (typically 64 bytes), as
171*4f2df630SAndroid Build Coastguard Workerno reassembly is performed for such frames.
172*4f2df630SAndroid Build Coastguard Worker
173*4f2df630SAndroid Build Coastguard WorkerThe vendor command response size is not fixed, it is subcommand dependent.
174*4f2df630SAndroid Build Coastguard Worker
175*4f2df630SAndroid Build Coastguard WorkerThe Cr50 device responds to each update PDU with a confirmation which is 4
176*4f2df630SAndroid Build Coastguard Workerbytes in size in protocol version 2, and 1 byte in size in all other
177*4f2df630SAndroid Build Coastguard Workerversions. Zero value means success, non-zero value is the error code
178*4f2df630SAndroid Build Coastguard Workerreported by Cr50.
179*4f2df630SAndroid Build Coastguard Worker
180*4f2df630SAndroid Build Coastguard WorkerAgain, vendor command responses are subcommand specific.
181*4f2df630SAndroid Build Coastguard Worker
182*4f2df630SAndroid Build Coastguard Worker### Vendor commands (common code)
183*4f2df630SAndroid Build Coastguard Worker
184*4f2df630SAndroid Build Coastguard WorkerVendor commands for command code look very similar to the TPM vendor commands
185*4f2df630SAndroid Build Coastguard Workerabove, except that we use `UPDATE_EXTRA_CMD` (`b007ab1f`) instead of `EXT_CMD`,
186*4f2df630SAndroid Build Coastguard Workerand `Vend. sub.` have a limit set of values (unless otherwise noted, commands
187*4f2df630SAndroid Build Coastguard Workertake no parameter, and reply with a single 1-byte status code):
188*4f2df630SAndroid Build Coastguard Worker
189*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_IMMEDIATE_RESET (0): Tell EC to reboot immediately.
190*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_JUMP_TO_RW (1): Tell EC (in RO) to jump to RW, if the
191*4f2df630SAndroid Build Coastguard Worker  signature verifies.
192*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_STAY_IN_RO (2): Tell EC (in RO), to stay in RO, and not
193*4f2df630SAndroid Build Coastguard Worker  jump to RW automatically. After this command is sent, a reset is necessary
194*4f2df630SAndroid Build Coastguard Worker  for the EC to accept to jump to RW again.
195*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_UNLOCK_RW (3): Tell EC to unlock RW on next reset.
196*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_UNLOCK_ROLLBACK (4): Tell EC to unlock ROLLBACK on next reset.
197*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_INJECT_ENTROPY (5): Inject entropy into the device-specific
198*4f2df630SAndroid Build Coastguard Worker  unique identifier (takes at least CONFIG_ROLLBACK_SECRET_SIZE=32 bytes of
199*4f2df630SAndroid Build Coastguard Worker  data).
200*4f2df630SAndroid Build Coastguard Worker* UPDATE_EXTRA_CMD_PAIR_CHALLENGE (6): Tell EC to answer a X25519 challenge
201*4f2df630SAndroid Build Coastguard Worker  for pairing. Takes in a `struct pair_challenge` as data, answers with a
202*4f2df630SAndroid Build Coastguard Worker  `struct pair_challenge_response`.
203