Skip to content

Commit c7b8b34

Browse files
cfrantzpamaury
authored andcommitted
[rescue] Re-work rescue calling convention
Rather than passing `bootdata` and `boot_log` along in every call, store pointers to them into the `rescue_state_t` structure. Signed-off-by: Chris Frantz <[email protected]> (cherry picked from commit 1213c98)
1 parent ceaaa7d commit c7b8b34

File tree

9 files changed

+64
-64
lines changed

9 files changed

+64
-64
lines changed

sw/device/silicon_creator/lib/rescue/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cc_library(
2727
"//hw/top:flash_ctrl_c_regs",
2828
"//sw/device/lib/base:memory",
2929
"//sw/device/silicon_creator/lib:boot_data",
30+
"//sw/device/silicon_creator/lib:boot_log",
3031
"//sw/device/silicon_creator/lib:dbg_print",
3132
"//sw/device/silicon_creator/lib:error",
3233
"//sw/device/silicon_creator/lib/boot_svc:boot_svc_msg",
@@ -90,7 +91,6 @@ cc_library(
9091
":rescue",
9192
"//sw/device/lib/base:macros",
9293
"//sw/device/lib/base:memory",
93-
"//sw/device/silicon_creator/lib:boot_data",
9494
"//sw/device/silicon_creator/lib:error",
9595
"//sw/device/silicon_creator/lib/drivers:rstmgr",
9696
"//sw/device/silicon_creator/lib/drivers:usb",

sw/device/silicon_creator/lib/rescue/dfu.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ static const rescue_mode_properties_t mode_by_altsetting[] = {
3030
//{ kRescueModeOwnerPage1, false, true },
3131
};
3232

33-
static rom_error_t validate_mode(uint32_t setting, rescue_state_t *state,
34-
boot_data_t *bootdata) {
33+
static rom_error_t validate_mode(uint32_t setting, rescue_state_t *state) {
3534
// Allow the `setting` to be either an index or a FourCC code.
3635
// The the integer value is less than the arraysize, then its clearly an
3736
// index.
@@ -58,18 +57,18 @@ static rom_error_t validate_mode(uint32_t setting, rescue_state_t *state,
5857
// rescue buffer.
5958
const rescue_mode_properties_t *mode = &mode_by_altsetting[setting];
6059
rom_error_t error2 = kErrorOk;
61-
rom_error_t error = rescue_validate_mode(mode->mode, state, bootdata);
60+
rom_error_t error = rescue_validate_mode(mode->mode, state);
6261
if (error == kErrorOk && mode->upload) {
6362
// DFU upload means send to the host. We stage the data that would
6463
// be sent to the rescue buffer.
65-
rescue_send_handler(state, bootdata);
64+
rescue_send_handler(state);
6665
}
6766
// BootSvc and OwnerPage are also recv (from the host) services. Make sure
6867
// we're set up to process a DFU download for those services.
6968
if (mode->mode == kRescueModeBootSvcRsp) {
70-
error2 = rescue_validate_mode(kRescueModeBootSvcReq, state, bootdata);
69+
error2 = rescue_validate_mode(kRescueModeBootSvcReq, state);
7170
} else if (mode->mode == kRescueModeOwnerPage0) {
72-
error2 = rescue_validate_mode(kRescueModeOwnerBlock, state, bootdata);
71+
error2 = rescue_validate_mode(kRescueModeOwnerBlock, state);
7372
}
7473

7574
if (error == kErrorOk || error2 == kErrorOk) {
@@ -180,7 +179,7 @@ static rom_error_t vendor_request(dfu_ctx_t *ctx, usb_setup_data_t *setup) {
180179
// FourCC from the value and index fields.
181180
case kDfuVendorSetMode: {
182181
uint32_t mode = ((uint32_t)setup->value << 16) | setup->index;
183-
if (validate_mode(mode, &ctx->state, ctx->bootdata) == kErrorOk) {
182+
if (validate_mode(mode, &ctx->state) == kErrorOk) {
184183
dfu_transport_data(ctx, kUsbDirIn, NULL, 0, 0);
185184
} else {
186185
return kErrorUsbBadSetup;
@@ -195,7 +194,7 @@ static rom_error_t vendor_request(dfu_ctx_t *ctx, usb_setup_data_t *setup) {
195194
static rom_error_t interface_request(dfu_ctx_t *ctx, usb_setup_data_t *setup) {
196195
switch (setup->request) {
197196
case kUsbSetupReqSetInterface:
198-
if (validate_mode(setup->value, &ctx->state, ctx->bootdata) == kErrorOk) {
197+
if (validate_mode(setup->value, &ctx->state) == kErrorOk) {
199198
ctx->interface = (uint8_t)setup->value;
200199
dfu_transport_data(ctx, kUsbDirIn, NULL, 0, 0);
201200
} else {
@@ -216,7 +215,7 @@ static rom_error_t set_configuration(dfu_ctx_t *ctx) {
216215
ctx->dfu_error = kDfuErrOk;
217216
ctx->dfu_state = kDfuStateIdle;
218217
ctx->interface = 0;
219-
validate_mode(ctx->interface, &ctx->state, ctx->bootdata);
218+
validate_mode(ctx->interface, &ctx->state);
220219
ctx->ep0.configuration = ctx->ep0.next.configuration;
221220
return kErrorOk;
222221
}
@@ -282,7 +281,7 @@ void dfu_protocol_handler(void *_ctx, uint8_t ep, usb_transfer_flags_t flags,
282281
ctx->state.data[ctx->state.offset++] = 0xFF;
283282
}
284283
// Pass the rescue buffer to the rescue receive handler.
285-
rom_error_t error = rescue_recv_handler(&ctx->state, ctx->bootdata);
284+
rom_error_t error = rescue_recv_handler(&ctx->state);
286285
switch (error) {
287286
case kErrorOk:
288287
ctx->dfu_error = kDfuErrOk;

sw/device/silicon_creator/lib/rescue/dfu.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <stdint.h>
99

10-
#include "sw/device/silicon_creator/lib/boot_data.h"
1110
#include "sw/device/silicon_creator/lib/drivers/usb.h"
1211
#include "sw/device/silicon_creator/lib/rescue/rescue.h"
1312

@@ -132,8 +131,6 @@ typedef struct dfu_ctx {
132131
usb_control_ctx_t ep0;
133132
/** Rescue state. */
134133
rescue_state_t state;
135-
/** Pointer to bootdata. */
136-
boot_data_t *bootdata;
137134
/** Expected receive length (upload) */
138135
uint32_t expected_len;
139136
/** Status buffer (used to respond to DfuReqGetStatus). */

sw/device/silicon_creator/lib/rescue/rescue.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "sw/device/lib/arch/device.h"
88
#include "sw/device/lib/base/memory.h"
99
#include "sw/device/silicon_creator/lib/boot_data.h"
10+
#include "sw/device/silicon_creator/lib/boot_log.h"
1011
#include "sw/device/silicon_creator/lib/boot_svc/boot_svc_msg.h"
1112
#include "sw/device/silicon_creator/lib/dbg_print.h"
1213
#include "sw/device/silicon_creator/lib/drivers/flash_ctrl.h"
@@ -55,11 +56,11 @@ rom_error_t flash_firmware_block(rescue_state_t *state) {
5556
return kErrorOk;
5657
}
5758

58-
rom_error_t flash_owner_block(rescue_state_t *state, boot_data_t *bootdata) {
59-
if (bootdata->ownership_state == kOwnershipStateUnlockedAny ||
60-
bootdata->ownership_state == kOwnershipStateUnlockedSelf ||
61-
bootdata->ownership_state == kOwnershipStateUnlockedEndorsed ||
62-
(bootdata->ownership_state == kOwnershipStateLockedOwner &&
59+
rom_error_t flash_owner_block(rescue_state_t *state) {
60+
if (state->bootdata->ownership_state == kOwnershipStateUnlockedAny ||
61+
state->bootdata->ownership_state == kOwnershipStateUnlockedSelf ||
62+
state->bootdata->ownership_state == kOwnershipStateUnlockedEndorsed ||
63+
(state->bootdata->ownership_state == kOwnershipStateLockedOwner &&
6364
owner_block_newversion_mode() == kHardenedBoolTrue)) {
6465
HARDENED_RETURN_IF_ERROR(flash_ctrl_info_erase(
6566
&kFlashCtrlInfoPageOwnerSlot1, kFlashCtrlEraseTypePage));
@@ -93,8 +94,7 @@ static void ownership_erase(void) {
9394
}
9495
#endif
9596

96-
rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state,
97-
boot_data_t *bootdata) {
97+
rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state) {
9898
dbg_printf("\r\nmode: %C\r\n", bitfield_byteswap32(mode));
9999
rom_error_t result = kErrorOk;
100100

@@ -131,10 +131,11 @@ rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state,
131131
dbg_printf("ok: send boot_svc request\r\n");
132132
break;
133133
case kRescueModeOwnerBlock:
134-
if (bootdata->ownership_state == kOwnershipStateUnlockedAny ||
135-
bootdata->ownership_state == kOwnershipStateUnlockedSelf ||
136-
bootdata->ownership_state == kOwnershipStateUnlockedEndorsed ||
137-
(bootdata->ownership_state == kOwnershipStateLockedOwner &&
134+
if (state->bootdata->ownership_state == kOwnershipStateUnlockedAny ||
135+
state->bootdata->ownership_state == kOwnershipStateUnlockedSelf ||
136+
state->bootdata->ownership_state ==
137+
kOwnershipStateUnlockedEndorsed ||
138+
(state->bootdata->ownership_state == kOwnershipStateLockedOwner &&
138139
owner_block_newversion_mode() == kHardenedBoolTrue)) {
139140
dbg_printf("ok: send owner_block\r\n");
140141
} else {
@@ -170,7 +171,7 @@ rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state,
170171
return result;
171172
}
172173

173-
rom_error_t rescue_send_handler(rescue_state_t *state, boot_data_t *bootdata) {
174+
rom_error_t rescue_send_handler(rescue_state_t *state) {
174175
hardened_bool_t allow =
175176
owner_rescue_command_allowed(state->config, state->mode);
176177
if (allow != kHardenedBoolTrue) {
@@ -219,7 +220,7 @@ rom_error_t rescue_send_handler(rescue_state_t *state, boot_data_t *bootdata) {
219220
return kErrorRescueSendStart;
220221
}
221222

222-
rom_error_t rescue_recv_handler(rescue_state_t *state, boot_data_t *bootdata) {
223+
rom_error_t rescue_recv_handler(rescue_state_t *state) {
223224
hardened_bool_t allow =
224225
owner_rescue_command_allowed(state->config, state->mode);
225226
if (allow != kHardenedBoolTrue) {
@@ -249,7 +250,7 @@ rom_error_t rescue_recv_handler(rescue_state_t *state, boot_data_t *bootdata) {
249250
break;
250251
case kRescueModeOwnerBlock:
251252
if (state->offset == sizeof(state->data)) {
252-
HARDENED_RETURN_IF_ERROR(flash_owner_block(state, bootdata));
253+
HARDENED_RETURN_IF_ERROR(flash_owner_block(state));
253254
state->offset = 0;
254255
}
255256
break;
@@ -268,8 +269,11 @@ rom_error_t rescue_recv_handler(rescue_state_t *state, boot_data_t *bootdata) {
268269
return kErrorOk;
269270
}
270271

271-
void rescue_state_init(rescue_state_t *state,
272+
void rescue_state_init(rescue_state_t *state, boot_data_t *bootdata,
273+
boot_log_t *boot_log,
272274
const owner_rescue_config_t *config) {
275+
state->boot_log = boot_log;
276+
state->bootdata = bootdata;
273277
state->config = config;
274278
if ((hardened_bool_t)config == kHardenedBoolFalse) {
275279
HARDENED_CHECK_EQ((hardened_bool_t)config, kHardenedBoolFalse);

sw/device/silicon_creator/lib/rescue/rescue.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdint.h>
1010

1111
#include "sw/device/silicon_creator/lib/boot_data.h"
12+
#include "sw/device/silicon_creator/lib/boot_log.h"
1213
#include "sw/device/silicon_creator/lib/boot_svc/boot_svc_msg.h"
1314
#include "sw/device/silicon_creator/lib/dbg_print.h"
1415
#include "sw/device/silicon_creator/lib/error.h"
@@ -80,6 +81,10 @@ typedef struct RescueState {
8081
// Range to erase and write for firmware rescue (inclusive).
8182
uint32_t flash_start;
8283
uint32_t flash_limit;
84+
// Pointer to the current bootdata record.
85+
boot_data_t *bootdata;
86+
// Pointer to the boot log.
87+
boot_log_t *boot_log;
8388
// Rescue configuration.
8489
const owner_rescue_config_t *config;
8590
// Data buffer to hold xmodem upload data.
@@ -90,42 +95,41 @@ typedef struct RescueState {
9095
* Handle rescue modes that involve sending data to the host.
9196
*
9297
* @param state Rescue state
93-
* @param bootdata Boot data
9498
* @return kErrorOk if nothing to do, kErrorRescueSendStart if the state->data
9599
* buffer is ready to send, or an error.
96100
*/
97-
rom_error_t rescue_send_handler(rescue_state_t *state, boot_data_t *bootdata);
101+
rom_error_t rescue_send_handler(rescue_state_t *state);
98102

99103
/**
100104
* Handle rescue movdes that involve receiving data into the device.
101105
*
102106
* @param state Rescue state
103-
* @param bootdata Boot data
104107
* @return kErrorOk if no error or an error code indicating a problem with
105108
* the received data.
106109
*/
107-
rom_error_t rescue_recv_handler(rescue_state_t *state, boot_data_t *bootdata);
110+
rom_error_t rescue_recv_handler(rescue_state_t *state);
108111

109112
/**
110113
* Validate a new rescue mode.
111114
*
112115
* @param mode The new mode.
113116
* @param state Rescue state
114-
* @param bootdata Boot data
115117
* @return kErrorOk if the new mode was accepted, kErrorBadMode otherwise.
116118
*
117119
* The rescue state is updated: mode, offset and flash_offset.
118120
*/
119-
rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state,
120-
boot_data_t *bootdata);
121+
rom_error_t rescue_validate_mode(uint32_t mode, rescue_state_t *state);
121122

122123
/**
123124
* Initialize the rescue state.
124125
*
125126
* @param state Rescue state
127+
* @param bootdata Boot data
128+
* @param boot_log The boot_log
126129
* @param config The ownership rescue config (if any).
127130
*/
128-
void rescue_state_init(rescue_state_t *state,
131+
void rescue_state_init(rescue_state_t *state, boot_data_t *bootdata,
132+
boot_log_t *boot_log,
129133
const owner_rescue_config_t *config);
130134

131135
/**
@@ -140,10 +144,11 @@ rom_error_t rescue_enter_handler(boot_svc_msg_t *msg);
140144
* Perform the rescue protocol.
141145
*
142146
* @param bootdata Boot data
147+
* @param boot_log The boot_log
143148
* @param config The ownership rescue config (if any).
144149
* @return Any error in processing the rescue protocol.
145150
*/
146-
rom_error_t rescue_protocol(boot_data_t *bootdata,
151+
rom_error_t rescue_protocol(boot_data_t *bootdata, boot_log_t *boot_log,
147152
const owner_rescue_config_t *config);
148153

149154
/**

sw/device/silicon_creator/lib/rescue/rescue_spi.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ void dfu_transport_result(dfu_ctx_t *ctx, rom_error_t result) {
7474
spi_device_flash_status_clear();
7575
}
7676

77-
rom_error_t rescue_protocol(boot_data_t *bootdata,
77+
rom_error_t rescue_protocol(boot_data_t *bootdata, boot_log_t *boot_log,
7878
const owner_rescue_config_t *config) {
7979
dfu_ctx_t ctx = {
80-
.bootdata = bootdata,
8180
.dfu_state = kDfuStateIdle,
8281
.dfu_error = kDfuErrOk,
8382
};
8483
dbg_printf("SPI-DFU rescue ready\r\n");
85-
rescue_state_init(&ctx.state, config);
84+
rescue_state_init(&ctx.state, bootdata, boot_log, config);
8685
spi_device_init(
8786
/*log2_density=*/kRescueDensity, &kRescueSfdpTable,
8887
sizeof(kRescueSfdpTable));

sw/device/silicon_creator/lib/rescue/rescue_usb.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ void dfu_transport_result(dfu_ctx_t *ctx, rom_error_t result) {
151151
}
152152
}
153153

154-
rom_error_t rescue_protocol(boot_data_t *bootdata,
154+
rom_error_t rescue_protocol(boot_data_t *bootdata, boot_log_t *boot_log,
155155
const owner_rescue_config_t *config) {
156156
set_serialnumber();
157157
dfu_ctx_t ctx = {
158-
.bootdata = bootdata,
159158
.ep0 =
160159
{
161160
.device_desc = &device_desc,
@@ -166,7 +165,7 @@ rom_error_t rescue_protocol(boot_data_t *bootdata,
166165
.dfu_error = kDfuErrOk,
167166
};
168167
dbg_printf("USB-DFU rescue ready\r\n");
169-
rescue_state_init(&ctx.state, config);
168+
rescue_state_init(&ctx.state, bootdata, boot_log, config);
170169
pinmux_init_usb();
171170
usb_init();
172171
usb_ep_init(0, kUsbEpTypeControl, 0x40, dfu_protocol_handler, &ctx);

0 commit comments

Comments
 (0)