注意:此内容最初发表于 blog.t8012.dev,与 h0meus3r、mrarm 和 aunali1 合作发布。
介绍

在我们的团队成功将 checkm8 漏洞移植到 AppleSilicon T2 芯片后,我们开始探索封闭机箱硬件调试(CCD)的方法,这在其他 iDevice 上也有实现。在这些设备上,串行线调试(Serial Wire Debug)协议可以通过 Lightning 接口复用,我们推测同样可以通过 USB Type‑C 接口实现。借助我们 MacBook 型号的易得原理图,我们确定复用是由 Apple/TI 共同设计的 USB Type‑C 端口控制器完成的,俗称 “ACE”。以下内容详细说明了我们的发现以及厂商定义的协议,称为 AppleVDM,Apple 在 USB Power Delivery 标准之上实现了该协议,用于复用各种内部外设。
我们团队成员 @h0m3us3r 通过在 MacBook Pro 主板的暴露测试点上连接 SWD 探针,转储了 ACE 固件。得益于此,我们获得了 ROM 与其上应用的固件负载补丁,并发现 ACE 芯片内置 Cortex‑M0 r0p1 ARM 核心。后续的大部分静态分析由 @mrarm 完成。
Connecting to target via SWD
Found SW-DP with ID 0x0BC11477
DPIDR: 0x0BC11477
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x04770031)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410CC601. Implementer code: 0x41 (ARM)
Found Cortex-M0 r0p1, Little endian.
FPUnit: 4 code (BP) slots and 0 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB008 SCS
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 000BB00A DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 000BB00B FPB
Cortex-M0 identified.
ACE 概览
为了更好地了解 ACE 的内部工作原理,我们在转储的固件中寻找标识信息。我们发现字符串 CD3217 HW0022 FW002.032.00 ZACE2-J213,在网上搜索 CD3217 后得到类似的 TI TPS65986。鉴于上述 TI USB Type‑C 控制器的功能描述与 ACE 十分相似,并且 TI 与 Apple 共同设计了 ACE,合理推断大部分技术信息同样适用于 ACE。
TI 的 USB Type‑C 控制器为主机管理提供 I²C 接口,我们找到了出色的主机 Interface Technical Reference Manual,其中记录了大量公开的 I²C 寄存器和命令。

基于上述框图,主要关注的组件是数字核心,它直接与主机通信并运行我们之前转储的固件。如前所述,该核心基于 Cortex‑M0 r0p1。遗憾的是,由于 TI 并未公开核心本身的资料,我们无法完整绘制包含外设的内存映射。不过,我们仍然得到了如下的基本内存布局:
由于我们不确定复用功能的代码是位于 ACE 本身还是其他芯片(因为有用于接收厂商消息并在另一芯片上处理的寄存器),我们同样调查了能够直接与 ACE 通信的芯片固件——SMC(T2 SoC 的一部分)以及 Thunderbolt 控制器。我们获取并分析了 SMC 固件中的 I2C 使用情况,未发现可疑代码;但在分析 Thunderbolt 控制器固件时遇到困难,最终决定仅在必要时才对其进行逆向工程。最初我们认为只能通过修改 SMC 固件让其转发命令给 ACE,才能在 macOS(Intel)上与 ACE 通信。虽然我们成功编辑了 SMC 固件并实现了与 ACE 的通信,但事实证明这一步并非必需。随后我们发现了以下项目:osy/ThunderboltPatcher,它使用 AppleHPM kext 与 ACE 通信。
AppleHPM KEXT 允许以 root 身份运行的用户空间程序读写 ACE 寄存器。它还提供了若干看似用于通过 USB Type‑C 线缆连接到 DUT 的 ACE 进行寄存器读写的方法。遗憾的是,我们尝试调用这些方法时未能成功,表明这些方法可能在量产硬件上被禁用,或需要额外的准备工作才能使用。我们还发现了一个用于 ACE 的 EFI 模式固件更新程序 HPMUtil.efi,并对其进行了部分逆向。根据硬件版本的不同,ACE 的更新方式有多种;EFI 程序似乎还有一个参数可以更新外接的 ACE,但在最近的 ACE 上该功能似乎已失效;根据我们从 Mac 获得的固件转储(以及可用命令),唯一可行的更新路径似乎是硬编码在少数几处,仅针对本地 ACE 执行命令。
已知至少有三种 ACE 芯片版本。根据我们手头的硬件,第一种变体 ACE1 见于 2018 年及更早的 T2 机型,第二种 ACE2 出现在 2019 年及之后的 T2 机型,第三种则用于 2020 年起的搭载 M1 的笔记本。软件更新方式在 ACE1 与 ACE2 之间差异显著。撰写本文时,我们仅成功转储了单个 ACE 固件(ACE2),本文的所有分析均基于该版本。
USB PD
告诉Mac从芯片上复用除USB之外的其他信号的逻辑通道是USB PD,因为ACE与外部设备的主要通信向量是USB PD,并且使用USB PD控制数据线是一种广泛的做法(参见:MIPI调试接口以及这篇关于从三星手机获取UART的文章:https://ntnuopen.ntnu.no/ntnu-xmlui/bitstream/handle/11250/2632162/bookchapter.pdf)。这个源自TI的控制器可以默认配置为支持某种变体的MIPI调试模式(而MIPI调试ID也出现在Apple ACE固件中),然而正如我们后面发现的,它在Apple的配置中被禁用,无法使用。因此,我们决定寻找ACE能够理解的其他Vendor Defined Messages。
在USB PD协议中,进入厂商模式首先需要发送一个SVID(Standard ID或Vendor ID,通常是厂商的USB VID)以及对象位置(可以把它看作厂商可以定义的子模式)。通过分析ACE固件,我们发现Apple SVID下至少有3个可能的对象位置,但它们的可用性是动态的,默认情况下只提供单一的SVID和对象位置。该主模式例如在Apple充电器连接时使用。我们尚不清楚其他模式何时被激活,计划进一步研究这些模式的用途。
因为如果有针对进入模式的命令的回复会更合乎逻辑,而且在没有任何上下文的情况下逆向嵌入式固件非常困难,我们决定研究VDM命令,它可以用来发送VDM PD消息。这让我们获取了固件内部工作的重要信息,并找到了几个发送带有Apple厂商ID的消息的函数。我们调查了其中的若干函数,有的确实与充电器使用的协议相关(我们不确定其具体用途;不过看起来只能用于读取外部设备的某些内存区段),但我们也发现了另一个包含三种情况的过程,其中两种会以类似Discover SVIDs VDM的方式返回由特定内存区域信息生成的16位值,另一种则更为复杂并处理额外逻辑。我们进一步研究后最终利用它将控制器中的信号复用出来。
VDM命令的另一种可能用法可以在AppleHPM KEXT和HPLUtil.efi程序中找到,它们用于读取外部连接设备的寄存器。由于这不是我们的目标且实验失败,我们在本文中不作描述。
请注意,既然可以从ACE复用SWD,进一步逆向ACE模式的价值就不大了;你应该能够使用ACE的SWD触发任意你想要的复用选择,并直接与ACE能够访问的所有外设通信。
深入代码
在@h0m3us3r提供的ROM和RAM转储的帮助下,我们首先在IDA中加载了ACE固件。复位处理函数的地址位于0x00000004,可用于获取入口点地址。
可以通过搜索四字符代码轻松找到命令表。寄存器表需要稍多一点工作,但也不难找到(因为有一个寄存器是字符串,你可以查找引用它的地方)。与公开的TI文档相比,命令和寄存器的数量显著增多,其中一些是Apple特有的扩展。由于固件几乎没有字符串或调试信息,命令列表非常有帮助。
在最初浏览固件时,我们很快碰到了一些看起来被诅咒的跳板,例如下面的代码:
ROM:000266C2 _Command_HRST ; DATA XREF: ROM:commands↓o
ROM:000266C2
ROM:000266C2
ROM:000266C2 PUSH {R4-R6,LR}
ROM:000266C4 MOV R4, R1
ROM:000266C6 MOV R5, R0
ROM:000266C8 BL sub_E4
ROM:000266CC LDR R0, =dword_20041040
ROM:000266CE MOV R1, R4
ROM:000266D0 ADDS R0, #0x40 ; '@'
ROM:000266D2 LDR R2, [R0]
ROM:000266D4 MOV R0, R5
ROM:000266D6 BLX R2
ROM:000266D8 MOV R4, R0
ROM:000266DA BL sub_E8
ROM:000266DE MOV R0, R4
ROM:000266E0 B loc_260E2
ROM:000266E0 ; End of function _Command_HRST
由于IDA对它们的处理不佳,我们编写了一个简单脚本为BLX调用添加注释。虽然这与IDA的反编译器不完全兼容,但已足以继续固件研究。这些跳板的作用是允许对固件中的函数进行选择性打补丁,因为主固件似乎驻留在ROM中。
在分析VDMs(VDM发送)命令处理程序后,我们迅速找到了负责发送任意PD消息的函数。随后我们查看了引用该函数的列表,发现了一些有趣的潜在调查对象,其中一个正是我们要找的目标。此外,它还被一个检查SOP来源是否为SOP'DBG或SOP''DBG的函数调用。

图中名为AppleVDM_0x12的函数尤其值得关注,因为它也被DVEn命令引用。遗憾的是,仅凭四个字符我们无法仅凭名称推断其功能。我们决定从0x10开始分析所有被该函数调用的函数。
unsigned char AppleVDM_0x10_StartNo, AppleVDM_0x10_NextStartNo, AppleVDM_0x10_IterA, AppleVDM_0x10_IterB;
unsigned char AppleVDM_Data[];
unsigned short AppleVDM_Type1_0x10_Values[];
void __fastcall AppleVDM_0x10(int sop)
{
// Prepare the reply VDM
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x50) | 0x5AC8000;
if ( !AppleVDM_0x10_StartNo )
AppleVDM_0x10_StartIter();
unsigned char i = 1;
int j = AppleVDM_0x10_StartNo;
bool even = true;
while ( i != 7 )
{
unsigned int value = AppleVDM_0x10_GetValue(j);
AppleVDM_0x10_NextIter(&j);
if ( even )
SendMessageDataAfterHeader[i] = 0;
if ( !value && ( !AppleVDM_0x10_IterA || AppleVDM_0x10_IterB == 8 ) )
break;
if ( !value && AppleVDM_0x10_IterB < 8 )
continue;
SendMessageDataAfterHeader[i] |= value << ( even ? 16 : 0 );
if ( !even )
++i;
even = !even;
AppleVDM_0x10_NextStartNo = j;
}
if ( i != 7 ) {
AppleVDM_0x10_NextStartNo = 0;
AppleVDM_0x10_StartIter();
++i;
}
AppleVDM_0x10_ReplyMsgId = NextSendMessageId[sop];
SendMessage(sop, 15, i);
}
void AppleVDM_0x10_StartIter() {
AppleVDM_0x10_IterA = 0;
AppleVDM_0x10_IterB = 0;
}
bool AppleVDM_0x10_NextIter(int *i)
{
while ( *i < 0x40 )
{
// the d variable is composed of two parts - the end position (bits 0:5) and the entry type (bits 6:7)
unsigned char d = AppleVDM_Data[*i];
if ( ( d >> 6 ) == 1 && AppleVDM_0x10_IterB < 8 )
{
++AppleVDM_0x10_IterB;
return 0;
}
// endpos is the position where this data entry ends (relative to the start of the AppleVDM_Data blob)
int endpos = d & 0x3F;
if ( !endpos || endpos == 0x3F )
return true;
*i = endpos + 1;
// if the type is not zero, return this index, otherwise skip
if ( ( AppleVDM_Data[*i] >> 6 ) != 0 )
return false;
}
return true;
}
unsigned int AppleVDM_0x10_GetValue(int i)
{
if ( i + 4 >= 0x40 )
return 0;
int type = AppleVDM_Data[i] >> 6;
switch (type) {
case 1:
AppleVDM_0x10_IterA = 1;
if ( (AppleVDM_0x10_IterB || !sub_27B30()) && AppleVDM_Data[i + 1] & (1 << AppleVDM_0x10_IterB) )
return AppleVDM_Type1_0x10_Values[AppleVDM_0x10_IterB];
case 2:
return 0x100 + AppleVDM_Data[i + 1];
case 3:
return (AppleVDM_Data[i + 1] << 8) | AppleVDM_Data[i + 2];
}
return 0;
}
这个AppleVDM_0x10函数处理0x10消息。函数遍历可能的操作列表,并以类似Discover SVIDs的格式(短整数后跟0终止符)呈现它们。
该函数本身可能并不特别有趣,但它足以让我们对整体流程有一个粗略的了解,并且相对独立(除了一些后续交叉验证的类型外)。
继续到0x11:
unsigned char AppleVDM_0x11_StartNo, AppleVDM_0x11_NextStartNo;
unsigned short AppleVDM_0x11_LastArgVal;
int Apple_0x11_ArgValIdx;
unsigned short AppleVDM_Type1_0x11_Values[];
void __fastcall AppleVDM_0x11(int sop, unsigned int *data)
{
unsigned int argVal = data[1] & 0xffff;
if ( argVal != AppleVDM_0x11_LastArgVal )
{
AppleVDM_0x11_LastArgVal = argVal;
AppleVDM_0x11_StartNo = 0;
AppleVDM_0x11_NextStartNo = 0;
}
AppleVDM_0x10_StartIter();
Apple_0x11_ArgValIdx = AppleVDM_0x10_FindValue(argVal);
if ( Apple_0x11_ArgValIdx == -1 )
{
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x91) | 0x5AC8000;
SendMessage(sop, 15, 1);
return;
}
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x51) | 0x5AC8000;
unsigned char i = 1;
int j = (unsigned __int8)AppleVDM_0x11_StartNo;
bool even = true;
while ( i != 7 )
{
if ( even )
SendMessageDataAfterHeader[i] = 0;
unsigned int data = AppleVDM_0x11_GetReplyData(Apple_0x11_ArgValIdx, j++);
SendMessageDataAfterHeader[i] |= data << (even ? 0x10 : 0);
AppleVDM_0x11_NextStartNo = j;
if ( !even )
++i;
even = !even;
if ( !data )
break;
}
if ( i != 7 ) {
AppleVDM_0x11_NextStartNo = 0;
AppleVDM_0x10_StartIter();
}
if ( j & 1 )
{
SendMessageDataAfterHeader[++i] = 0;
}
AppleVDM_0x11_ReplyMsgId = NextSendMessageId[sop];
SendMessage(sop, 15, i);
}
int __fastcall AppleVDM_0x11_GetReplyData(int argIndex, int num)
{
if ( argIndex == -2 )
return AppleVDM_0x11_GetReplyData_Type1(argIndex, num);
unsigned int endposAndType = AppleVDM_Data[argIndex];
unsigned int endpos = endposAndType & 0x3F;
if ( !endpos )
return 0;
unsigned int type = endposAndType >> 6;
if (type == 2)
{
int j = argIndex + 2;
if ( j > endpos )
return 0;
for ( int i = 0; i != num; ++i )
{
if ( j > endpos )
return 0;
j += 3;
}
return (AppleVDM_Data[j] << 8) | AppleVDM_Data[j + 1];
}
else if (type == 3)
{
int idx_b5_6, idx_remaining, idx_b1, idx_221;
unsigned int t = ~AppleVDM_Data[j + 3];
AppleVDM_0x11_GetReplyIndexes_Type3(((t >> 6) & 1) == 0, ((t >> 5) & 1) == 0, &idx_b5_6, &idx_remaining, &idx_b1, &idx_221);
if ( idx_b1 == num )
return 0x100 + AppleVDM_Data[j + 4];
if ( idx_221 == num )
return 0x221;
unsigned int j = num == idx_b5_6 ? (argIndex + 5) : (argIndex + 7 + 2 * (num - idx_remaining));
if (j + 1 <= endpos)
return (AppleVDM_Data[j] << 8) | AppleVDM_Data[j + 1];
}
return 0;
}
unsigned int AppleVDM_0x11_GetReplyData_Type1(int argIndex, int num)
{
if ( a2 < 8 )
result = AppleVDM_Type1_0x11_Values[8 * AppleVDM_0x10_IterB + num];
return 0;
}
void AppleVDM_0x11_GetReplyIndexes_Type3(int a1, int a2, int *idx_b5_6, int *idx_remaining, int *idx_b1, int *idx_221)
{
if ( a1 )
{
*idx_b5_6 = 0;
*idx_b1 = 1;
if ( a2 )
{
*idx_221 = 2;
*idx_remaining = 3;
return;
}
*idx_remaining = 2;
*idx_221 = -1;
}
else
{
*idx_b1 = 0;
if ( a2 )
{
*idx_221 = 1;
*idx_b5_6 = 2;
*idx_remaining = 3;
return;
}
*idx_b5_6 = 1;
*idx_remaining = 2;
*idx_221 = -1;
}
}
unsigned int AppleVDM_0x10_FindValue(int value)
{
int i = 0;
AppleVDM_0x10_StartIter();
while ( AppleVDM_0x10_GetValue(i) != value )
{
if ( AppleVDM_0x10_NextIter(&i) )
return -1;
}
if ( AppleVDM_0x10_IterA )
return -2;
else
return i;
}
这似乎为每个参数列出了一些额外信息(这些信息可以从0x10 VDM中获取)。目前我们无法对这些信息作出解释,但对于类型2来说,一旦我们分析了0x12的基本信息,这些信息就会变得清晰。需要注意的是,由于复杂性,0x12并未完全分析,部分细节被跳过并通过实验进行验证。
bool __fastcall AppleVDM_0x12(int sop, unsigned int *data, unsigned int dataCount, int internalNonVDM)
{
int replyVdoCount = 0;
if ( !internalNonVDM )
{
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x52) | 0x5AC8000;
replyVdoCount = 1;
}
unsigned int argVal = data[1] & 0xffff;
unsigned char argLines = (data[1] >> 16) & 0x7F;
bool argTryToExitPrevious = ((data[1] >> (16 + 7)) & 1) != 0;
bool argPersistThroughReset = ((data[1] >> 24) & 1) != 0;
bool argExit = ((data[1] >> 24) & 2) != 0;
if ( !argVal )
{
if ( argTryToExitPrevious )
DisableConflictingMuxModes(argLines);
goto succcess;
}
bool hasNoCollidingModes = true;
for ( int i = 0; i < 7; i++)
{
if ( (1 << i) & argLines && LineModeInfo[i].type >= 2u )
hasNoCollidingModes = false; // has active colliding mode we should exit first
}
if ( !hasNoCollidingModes && argTryToExitPrevious ) {
// note: this method sends a reply if it fails - 0x5AC80D2 if it exits an altmode
// and you need to wait or 0x5AC8092 if it fails
TryToExitCollidingModes(sop, argLines, replyVdoCount, &hasNoCollidingModes);
}
if ( !hasNoCollidingModes && argTryToExitPrevious )
return false;
if ( !argTryToExitPrevious && !hasNoCollidingModes )
{
if ( !internalNonVDM )
{
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x92) | 0x5AC8000;
SendMessage(sop_, 15, replyVdoCount);
}
return false;
}
unsigned short args[10];
memset(args, 0, 20);
int j = 0;
for ( int i = 2; i < dataCount; ++i )
{
args[j++] = data[i] >> 16;
args[j++] = data[i];
}
if ( !AppleVDM_0x12_Perform(argValue, !argExit, argLines, argPersistThroughReset, args) )
{
if ( !internalNonVDM )
{
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x92) | 0x5AC8000;
SendMessage(sop_, 15, replyVdoCount);
}
return false;
}
succcess:
AppleVDM_0x12_CollectStateForReply((int)&AppleVDM_0x12_CurrentStateForReply);
if ( !internalNonVDM )
{
AppleVDM_0x12_SOPForReply = sop;
ScheduleWork(14, 52, AppleVDM_0x12_SendSuccessWithCurrentState);
}
sub_25B66(56);
return true;
}
int __fastcall AppleVDM_0x12_CollectStateForReply(unsigned int *data)
{
unsigned short *sdata = (unsigned short *) data;
sdata[1] = 0;
sdata[0] = GetCurrentActiveModeInfo(0, &type[0]);
sdata[3] = GetCurrentActiveModeInfo(1, &type[1]);
sdata[2] = GetCurrentActiveModeInfo(2, &type[2]);
sdata[5] = GetCurrentActiveModeInfo(3, &type[3]);
sdata[4] = GetCurrentActiveModeInfo(4, &type[4]);
sdata[7] = GetCurrentActiveModeInfo(5, &type[5]);
sdata[6] = GetCurrentActiveModeInfo(6, &type[6]);
unsigned int connState = SystemStatus.ConnState;
if ( connState == 2 || connState == 3 ) // Audio connection (Ra/Ra), Debug connection (Rd/Rd)
{
v4 = 3;
}
else if ( connState == 6 || connState == 7 ) // Connection present
{
if ( IsPlugUpsideUp() )
v4 = 1;
else
v4 = 2;
}
else // No connection
{
v4 = 0;
}
data[0] |= (type[0] << 16) | (type[1] << 18) | (type[2] << 20) | (type[3] << 22) |
(type[4] << 24) | (type[5] << 26) | (type[6] << 28) | (v4 << 30);
}
void AppleVDM_0x12_SendSuccessWithCurrentState()
{
SendMessageDataAfterHeader[0] = ((byte_20044394 << 13) + 0x52) | 0x5AC8000;
memcpy(&SendMessageDataAfterHeader[1], &AppleVDM_0x12_CurrentStateForReply, 16);
SendMessage(AppleVDM_0x12_SOPForReply, 15, 5);
}
bool AppleVDM_0x12_Perform(int argValue, bool activate, int targetLines, bool persistThroughReset, unsigned short *args)
{
int argIndex = AppleVDM_0x10_FindValue(argValue);
if ( argIndex == -1 )
return false;
int type;
if ( argIndex == -2 )
{
if (AppleVDM_0x10_IterB >= 7u )
type = 2;
else
type = 3;
}
else
{
type = AppleVDM_Data[argIndex] >> 6;
}
if ( type == 2 )
{
if ( !args )
return false;
unsigned char extraVal;
bool result = AppleVDM_0x12_Find_Type2_ExtraValue(valNo, args[0], &extraVal);
if ( result )
result = AppleVDM_0x12_Perform_Type2(extraVal);
return result;
}
else if (type == 3)
{
if ( valNo == -2 )
v10 = AppleVDM_Type1_0x12_Values[AppleVDM_0x10_IterB];
else
v10 = AppleVDM_Data[argIndex + 3] & 0x1F;
return AppleVDM_0x12_Perform_Type3(v10, targetLines, activate, argValue, flag1, args);
}
return 0;
}
int AppleVDM_0x12_Perform_Type2(int internalId)
{
if ( internalId == 0 )
Command_GAID_Gaid(0);
if ( internalId == 1 || internalId == 2 ) {
if ( internalId == 2 ) {
if ( FindGPIOBit(10) != 0xFF )
GPIOAction(10, 4);
MEMORY[0x4009004C] &= 0xFEFFFFFF;
if ( RegAppleVIDConfig[5] & 0x20 )
sub_28B22();
}
GPIOAction(8, 4);
GPIOAction(9, 7);
byte_200424F2 = 1;
ScheduleWork(35, dword_200406A0, (int)sub_289EE); // resets the GPIO
return 1;
}
if ( internalId == 3 ) {
GPIOAction(15, 0);
byte_20042FC8 |= 0x10u;
sub_25B66(0x3A);
return 1;
}
return 0;
}
int AppleVDM_0x12_Perform_Type3(int internalId, int targetLines, bool activate, int argValue,
bool persistThroughReset, unsigned short *params);
我们在分析并尝试使用 0x12 处理程序时首先发现,Type 2 用于复位芯片并脉冲 GPIO 引脚。Type 2 的 Action 0 复位 ACE,Action 1 似乎会复位整机,Action 2 强制进入 DFU 并复位设备。我们不确定 Action 3 的作用,因为在我们的 ACE 上它似乎没有被暴露。
对于 Type 1 操作,AppleVDM_0x10_FindValue 函数返回 -2,因此 Type 1 操作会被映射为 Type 1 或 Type 2(最后一种会成为 Type 2)。
对于 Type 3,情况更为有趣,我们尚未完全逆向所有相关代码。不过,通过一些假设,我们找到了重置模式的代码,从而推测出 argTryToExitPrevious、argPersistThroughReset 和 argExit 位的含义。总体来看,params 参数似乎仅被保存而未被使用,唯一例外是内部操作 ID 0x15(外部 ID 0x303?),在该情况下它会选择以下参数中的第一个:0x306、0x30C、0x809E,并据此更改 IO 寄存器写入。
在 ACE2 上解码 0x10 值
随后,我们根据反编译的代码对固件中可用的操作进行了解码。如果想进一步分析固件,这将非常有帮助。
操作类型
Type 1
从我们的 ACE 固件转储中可以看到以下 Type 1 操作是可能的(如果它们实际被启用的话,但目前并未启用)。该列表可能会因 ACE 固件和变体的不同而有所差异。
[0] 0x203 - internal ID 0x5
[1] 0x303 - internal ID 0x15
[2] 0x803 - internal ID 0xB
[3] 0x809 - internal ID 0xC
(4-6) are not defined
[7] 0x103 - Resolves to a General Action with an internal id of 0.
请注意,在我们分析的固件中,这一特定操作作为原生 Type 2 操作存在,而该 Type 1 操作并未启用。
Type 2
在我们的固件转储中,以下通用操作及其对应的 VDO 参数映射到相应的内部通用操作 ID:
0x106,arg =0x8001映射到 ID 2(PMU 复位 + DFU 持续)0x105,arg =0x8000映射到 ID 1(PMU 复位)0x103,arg =0x8000映射到 ID 0(ACE GAID 复位)
向已知操作发送其他参数应不会产生任何影响。
Type 3
映射到内部 ID(同样来源于 ACE2):
0x606- 内部 ID 20x206- 内部 ID 80x304- 内部 ID 90x203- 内部 ID 5
在你的设备上转储原始表格
可以在任何 ACE 上转储 AppleVDM 表,并使用上述代码手动解码。只需读取 0x57 寄存器即可完成。
协议概述
有了上述代码后,终于可以总结出该协议的工作方式。所有消息必须来自 SOP'DBG 或 SOP''DBG。
AppleVDM 0x10:获取操作列表
输入:{ 0x5AC8010 } 回复:使用 VDM 编码的 Shorts(高 16 位为第一个 short,低 16 位为第二个 short),以零结尾。
示例回复 VDO:05020702 06060206 01060105 02030103 00000000
AppleVDM 0x11:获取操作信息
参数:
- uint16_t ActionId - 指定操作,从 0x10 的回复中获取
输入:{
0x5AC8011, ActionId } 回复:使用 VDM 编码的 Shorts(高 16 位为第一个 short,低 16 位为第二个 short),以零结尾。 示例回复 VDO:05010103 04090305 00000000
AppleVDM 0x12:执行操作
参数:
- uint16_t ActionId - 指定操作,从 0x10 的回复中获取
- uint8_t Lines - 要复用该操作的线路位掩码
- bool TryToExitPrevious - ACE 将尝试退出任何冲突的模式
- bool PersistSoftReset - ACE 将尝试在软复位后保持该模式激活
- bool Exit - 与其进入该模式,不如退出它
- 输入:
- 0x5AC8012
(Exit << 25) | (PersistSoftReset << 24) | (TryToExitPrevious << 23) | ((Lines & 0x7F) << 16) | ActionId }- 输出:当前激活线路的状态,使用 8 个 16 位 short 编码(高 16 位为第一个 short,低 16 位为第二个 short):首先是一个打包值 (ConnectionState << 14) | (LineState[i] << (2 * i))(i 为 0 到 7,左闭右开),随后是 7 个 short,指示对应线路上复用的操作 ID。ConnectionState 为 0 表示未连接,1 或 2 表示标准连接设备(取决于方向),3 表示音频和调试连接。LineState 为 2 位值,其具体含义目前尚不清楚。
结束语
我们详细阐述了 Apple 专有的 USB Type‑C 控制器(称为 ACE)的内部工作原理,并揭示了在 USB‑PD 协议上用于外部控制这些控制器的厂商定义消息(称为 AppleVDM)。我们认为在该领域进行进一步研究对于揭示现代 Apple 设备中存在的硬件安全机制至关重要。