Skip to content

Commit a463ee6

Browse files
committed
api: port reboot api endpoint to rust
1 parent f1d41a8 commit a463ee6

File tree

8 files changed

+94
-36
lines changed

8 files changed

+94
-36
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ add_custom_target(rust-bindgen
423423
--whitelist-function app_eth_erc20_params_get
424424
--whitelist-function app_eth_sighash
425425
--whitelist-function app_btc_address_simple
426+
--whitelist-function reboot
426427
${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-sys/wrapper.h --
427428
-DPB_NO_PACKED_STRUCTS=1 -DPB_FIELD_16BIT=1 -fshort-enums ${RUST_BINDGEN_FLAGS} ${RUST_INCLUDES}
428429
COMMAND

src/commander/commander.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <util.h>
3434
#include <version.h>
3535

36-
#include <workflow/reboot.h>
3736
#include <workflow/restore.h>
3837
#include <workflow/workflow.h>
3938

@@ -105,14 +104,6 @@ static commander_error_t _api_restore_backup(const RestoreBackupRequest* request
105104
}
106105
#endif
107106

108-
static commander_error_t _api_reboot(void)
109-
{
110-
if (!workflow_reboot()) {
111-
return COMMANDER_ERR_GENERIC;
112-
}
113-
return COMMANDER_OK;
114-
}
115-
116107
// ------------------------------------ Process ------------------------------------- //
117108

118109
/**
@@ -156,9 +147,6 @@ static commander_error_t _api_process(const Request* request, Response* response
156147
response->which_response = Response_success_tag;
157148
return commander_bitboxbase(&(request->request.bitboxbase));
158149
#endif
159-
case Request_reboot_tag:
160-
response->which_response = Response_success_tag;
161-
return _api_reboot();
162150
default:
163151
screen_print_debug("command unknown", 1000);
164152
return COMMANDER_ERR_INVALID_INPUT;

src/rust/bitbox02-rust/src/hww/api.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod set_device_name;
3333
mod set_mnemonic_passphrase_enabled;
3434
mod set_password;
3535
mod show_mnemonic;
36+
mod system;
3637

3738
use alloc::vec::Vec;
3839

@@ -120,6 +121,7 @@ async fn process_api_btc(_request: &Request) -> Option<Result<Response, Error>>
120121
/// the C commander.
121122
async fn process_api(request: &Request) -> Option<Result<Response, Error>> {
122123
match request {
124+
Request::Reboot(ref request) => Some(system::reboot(request).await),
123125
Request::DeviceInfo(_) => Some(device_info::process()),
124126
Request::DeviceName(ref request) => Some(set_device_name::process(request).await),
125127
Request::SetPassword(ref request) => Some(set_password::process(request).await),
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2021 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use super::Error;
16+
use crate::pb;
17+
18+
use pb::response::Response;
19+
20+
use crate::workflow::confirm;
21+
22+
pub async fn reboot(&pb::RebootRequest {}: &pb::RebootRequest) -> Result<Response, Error> {
23+
confirm::confirm(&confirm::Params {
24+
title: "",
25+
body: "Proceed to upgrade?",
26+
..Default::default()
27+
})
28+
.await?;
29+
bitbox02::reboot()
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
extern crate std;
35+
use super::*;
36+
37+
use crate::bb02_async::block_on;
38+
use bitbox02::testing::{mock, Data, MUTEX};
39+
use std::boxed::Box;
40+
41+
#[test]
42+
pub fn test_reboot() {
43+
let _guard = MUTEX.lock().unwrap();
44+
45+
mock(Data {
46+
ui_confirm_create: Some(Box::new(|_| true)),
47+
..Default::default()
48+
});
49+
let reboot_called = std::panic::catch_unwind(|| {
50+
block_on(reboot(&pb::RebootRequest {
51+
purpose: Purpose::Upgrade as _,
52+
}))
53+
.unwrap();
54+
});
55+
match reboot_called {
56+
Ok(()) => panic!("reboot was not called"),
57+
Err(msg) => assert_eq!(msg.downcast_ref::<&str>(), Some(&"reboot called")),
58+
}
59+
}
60+
61+
#[test]
62+
pub fn test_reboot_aborted() {
63+
let _guard = MUTEX.lock().unwrap();
64+
65+
mock(Data {
66+
ui_confirm_create: Some(Box::new(|_| false)),
67+
..Default::default()
68+
});
69+
assert_eq!(
70+
block_on(reboot(&pb::RebootRequest {
71+
purpose: Purpose::Upgrade as _
72+
})),
73+
Err(Error::UserAbort),
74+
);
75+
}
76+
}

src/rust/bitbox02-sys/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <wally_core.h>
5252
#include <wally_crypto.h>
5353
#include <workflow/confirm.h>
54+
#include <workflow/reboot.h>
5455

5556
#if !defined(TESTING)
5657
#include <hal_delay.h>

src/rust/bitbox02/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,14 @@ pub fn version_short() -> &'static str {
280280
pub fn version_short() -> &'static str {
281281
"9.2.0-testing"
282282
}
283+
284+
#[cfg(not(feature = "testing"))]
285+
pub fn reboot() -> ! {
286+
unsafe { bitbox02_sys::reboot() }
287+
loop {}
288+
}
289+
290+
#[cfg(feature = "testing")]
291+
pub fn reboot() -> ! {
292+
panic!("reboot called")
293+
}

src/workflow/reboot.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// limitations under the License.
1414

1515
#include "reboot.h"
16-
#include "confirm.h"
1716
#include <memory/memory.h>
1817
#include <screen.h>
1918
#ifndef TESTING
2019
#include <driver_init.h>
2120
#endif
2221

23-
static void _reboot(void)
22+
void reboot(void)
2423
{
2524
auto_enter_t auto_enter = {
2625
.value = sectrue_u8,
@@ -42,19 +41,3 @@ static void _reboot(void)
4241
_reset_mcu();
4342
#endif
4443
}
45-
46-
bool workflow_reboot(void)
47-
{
48-
#if PLATFORM_BITBOX02 == 1
49-
// Only ask on the bitbox02 platform, bitboxbase will always reboot
50-
const confirm_params_t params = {
51-
.title = "",
52-
.body = "Proceed to upgrade?",
53-
};
54-
if (!workflow_confirm_blocking(&params)) {
55-
return false;
56-
}
57-
#endif
58-
_reboot();
59-
return true;
60-
}

src/workflow/reboot.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
#ifndef _WORKFLOW_REBOOT_H_
1616
#define _WORKFLOW_REBOOT_H_
1717

18-
#include <stdbool.h>
19-
2018
/**
21-
* Shows a confirmation dialog to the user to confirm/reject rebooting the
22-
* device into bootoader mode to upgrade the firmware.
23-
* @return true if the user confirms, false if the user rejects.
19+
* Reboots the device.
2420
*/
25-
bool workflow_reboot(void);
21+
void reboot(void);
2622

2723
#endif

0 commit comments

Comments
 (0)