Skip to content

Commit

Permalink
Update prelude after #347 (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Dec 29, 2023
1 parent 4099392 commit 3fba94f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
1 change: 1 addition & 0 deletions crates/prelude/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Major

- Migrate `platform::update` to use `Error` instead of `usize`
- Remove custom error types and use `wasefire-error` instead
- Rename `native` to `test` and use `native` for native applets

Expand Down
30 changes: 10 additions & 20 deletions crates/prelude/src/platform/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use alloc::boxed::Box;

use wasefire_applet_api::platform::update as api;

use crate::{convert_unit, Error};

/// Returns whether platform update is supported.
pub fn is_supported() -> bool {
let api::is_supported::Results { supported } = unsafe { api::is_supported() };
Expand All @@ -27,49 +29,37 @@ pub fn is_supported() -> bool {
/// Returns the metadata of the platform.
///
/// This typically contains the version and side (A or B) of the running platform.
pub fn metadata() -> Result<Box<[u8]>, usize> {
pub fn metadata() -> Result<Box<[u8]>, Error> {
let mut ptr = core::ptr::null_mut();
let mut len = 0;
let params = api::metadata::Params { ptr: &mut ptr, len: &mut len };
let api::metadata::Results { res } = unsafe { api::metadata(params) };
assert_eq!(convert(res)?, 0);
convert_unit(res)?;
let ptr = unsafe { core::slice::from_raw_parts_mut(ptr, len) };
Ok(unsafe { Box::from_raw(ptr) })
}

/// Starts a platform update process.
///
/// During a dry-run, any mutable operation is skipped and only checks are performed.
pub fn initialize(dry_run: bool) -> Result<(), usize> {
pub fn initialize(dry_run: bool) -> Result<(), Error> {
let params = api::initialize::Params { dry_run: dry_run as usize };
let api::initialize::Results { res } = unsafe { api::initialize(params) };
assert_eq!(convert(res)?, 0);
Ok(())
convert_unit(res)
}

/// Processes the next chunk of a platform update.
pub fn process(chunk: &[u8]) -> Result<(), usize> {
pub fn process(chunk: &[u8]) -> Result<(), Error> {
let params = api::process::Params { ptr: chunk.as_ptr(), len: chunk.len() };
let api::process::Results { res } = unsafe { api::process(params) };
assert_eq!(convert(res)?, 0);
Ok(())
convert_unit(res)
}

/// Finalizes a platform update process.
///
/// This function will reboot when the update is successful and thus only returns in case of errors
/// or in dry-run mode.
pub fn finalize() -> Result<(), usize> {
pub fn finalize() -> Result<(), Error> {
let api::finalize::Results { res } = unsafe { api::finalize() };
assert_eq!(convert(res)?, 0);
Ok(())
}

fn convert(res: isize) -> Result<usize, usize> {
let val = res as usize;
if res < 0 {
Err(!val)
} else {
Ok(val)
}
convert_unit(res)
}

0 comments on commit 3fba94f

Please sign in to comment.