Skip to content

Commit 0239344

Browse files
LeilaCY-Linquinchou77
authored andcommitted
fwk: modified the UCSI port swap logic to avoid EC crash
This is the side effect of commit 5b85f16. HLK UCSI test item will send the invalid indicate port to EC, it will cause the array out of index and crash. Add the condition to avoid the EC crash when received the invalid indicate port. BRANCH=fwk-main BUG=https://app.clickup.com/t/86er6kvqt TEST=run the HLK UCSI test, will not cause EC crash Signed-off-by: LeilaCY-Lin <LeilaCY_Lin@compal.com> (cherry picked from commit ce4f52f)
1 parent ddb55ab commit 0239344

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

  • zephyr/program/framework/src

zephyr/program/framework/src/ucsi.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int ucsi_write_tunnel(void)
8686
uint8_t *message_out = host_get_memmap(EC_CUSTOMIZED_MEMMAP_UCSI_MESSAGE_OUT);
8787
uint8_t *command = host_get_memmap(EC_CUSTOMIZED_MEMMAP_UCSI_COMMAND);
8888
uint8_t change_connector_indicator = 0;
89-
int controller;
89+
int controller = 0;
9090
int offset = 0;
9191
int rv = EC_SUCCESS;
9292

@@ -132,12 +132,17 @@ int ucsi_write_tunnel(void)
132132
change_connector_indicator =
133133
*host_get_memmap(EC_CUSTOMIZED_MEMMAP_UCSI_CTR_SPECIFIC + offset) & 0x7f;
134134

135-
if (change_connector_indicator > 0 && change_connector_indicator !=
136-
ucsi_pd_port_map[change_connector_indicator-1].pd_controller_port) {
135+
if (change_connector_indicator > PD_PORT_COUNT ||
136+
change_connector_indicator == 0) {
137+
/* Print the invalid port for debugging */
138+
if (ucsi_debug_enable && change_connector_indicator > PD_PORT_COUNT)
139+
CPRINTS("UCSI write invalid type-c port:%d",
140+
change_connector_indicator);
141+
} else {
137142
*host_get_memmap(EC_CUSTOMIZED_MEMMAP_UCSI_CTR_SPECIFIC + offset) =
138-
ucsi_pd_port_map[change_connector_indicator-1].pd_controller_port;
143+
ucsi_pd_port_map[change_connector_indicator-1].pd_controller_port;
144+
controller = ucsi_pd_port_map[change_connector_indicator-1].pd_controller;
139145
}
140-
controller = ucsi_pd_port_map[change_connector_indicator-1].pd_controller;
141146

142147
pd_chip_ucsi_info[controller].wait_ack = 1;
143148
rv = cypd_write_reg_block(controller, CCG_MESSAGE_OUT_REG, message_out, 16);
@@ -297,7 +302,7 @@ static int ucsi_check_all_pd_status(int operator)
297302

298303
int ucsi_read_tunnel(int controller)
299304
{
300-
int rv;
305+
int rv, port_indicator;
301306

302307
if (ucsi_debug_enable && pd_chip_ucsi_info[controller].read_tunnel_complete == 1 &&
303308
(pd_chip_ucsi_info[controller].cci & CCI_BUSY_FLAG) == 0) {
@@ -310,10 +315,14 @@ int ucsi_read_tunnel(int controller)
310315
if (rv != EC_SUCCESS)
311316
CPRINTS("CCI_REG failed");
312317
/* we need to offset the pd connector number to correct number */
313-
if (pd_chip_ucsi_info[controller].cci & 0xFE) {
318+
port_indicator = (pd_chip_ucsi_info[controller].cci & 0xFE)>>1;
319+
if (port_indicator <= 0 || port_indicator > PORTS_PER_CONTROLLER) {
320+
/* Print the invalid port for debugging */
321+
if (ucsi_debug_enable && port_indicator > PORTS_PER_CONTROLLER)
322+
CPRINTS("UCSI read invalid type-c port:%d", port_indicator);
323+
} else {
314324
pd_chip_ucsi_info[controller].cci = (pd_chip_ucsi_info[controller].cci & 0xFFFFFF01)
315-
| (pd_ucsi_port_map[controller*2+((pd_chip_ucsi_info[controller].cci & 0xFE)>>1)-1]
316-
<< 1);
325+
| (pd_ucsi_port_map[controller*2+port_indicator-1] << 1);
317326
}
318327

319328
/* If data length is non zero, then get data */

0 commit comments

Comments
 (0)