libfdt: Replace helper functions with FdtRawResult

Move all FFI interfaces to the newly-introduced FdtRawResult,
implementing TryFrom<FdtRawResult> as needed, and remove the
now-obsolete helper functions.

Note that all low-level libfdt wrappers now simply return

    FdtRawResult::from(ret).try_into()

Test: m pvmfw
Test: atest liblibfdt.integration_test
Change-Id: I54ee0330556533a7a8d0ce07f15fed50849260eb
diff --git a/libs/libfdt/src/ctypes.rs b/libs/libfdt/src/ctypes.rs
index 640d447..a65f8e7 100644
--- a/libs/libfdt/src/ctypes.rs
+++ b/libs/libfdt/src/ctypes.rs
@@ -14,6 +14,7 @@
 
 //! Safe zero-cost wrappers around integer values used by libfdt.
 
+use crate::result::FdtRawResult;
 use crate::{FdtError, Result};
 
 /// Wrapper guaranteed to contain a valid phandle.
@@ -50,3 +51,11 @@
         Self::new(value).ok_or(FdtError::BadPhandle)
     }
 }
+
+impl TryFrom<FdtRawResult> for Phandle {
+    type Error = FdtError;
+
+    fn try_from(res: FdtRawResult) -> Result<Self> {
+        Self::new(res.try_into()?).ok_or(FdtError::BadPhandle)
+    }
+}
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index 925c473..86d4abd 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -33,7 +33,6 @@
 use core::ops::Range;
 use cstr::cstr;
 use libfdt::get_slice_at_ptr;
-use result::{fdt_err, fdt_err_expect_zero, fdt_err_or_option};
 use zerocopy::AsBytes as _;
 
 use crate::libfdt::{Libfdt, LibfdtMut};
diff --git a/libs/libfdt/src/libfdt.rs b/libs/libfdt/src/libfdt.rs
index 9c8d2b1..1e82c9f 100644
--- a/libs/libfdt/src/libfdt.rs
+++ b/libs/libfdt/src/libfdt.rs
@@ -22,7 +22,8 @@
 use core::mem;
 use core::ptr;
 
-use crate::{fdt_err, fdt_err_expect_zero, fdt_err_or_option, FdtError, Phandle, Result};
+use crate::result::FdtRawResult;
+use crate::{FdtError, Phandle, Result};
 
 // Function names are the C function names without the `fdt_` prefix.
 
@@ -35,7 +36,7 @@
     //          There will be no memory write outside of the given fdt.
     let ret = unsafe { libfdt_bindgen::fdt_create_empty_tree(fdt, len) };
 
-    fdt_err_expect_zero(ret)
+    FdtRawResult::from(ret).try_into()
 }
 
 /// Safe wrapper around `fdt_check_full()` (C function).
@@ -49,7 +50,7 @@
     // calls as it expects the client code to keep track of the objects (DT, nodes, ...).
     let ret = unsafe { libfdt_bindgen::fdt_check_full(fdt, len) };
 
-    fdt_err_expect_zero(ret)
+    FdtRawResult::from(ret).try_into()
 }
 
 /// Wrapper for the read-only libfdt.h functions.
@@ -76,7 +77,7 @@
         // function respects the passed number of characters.
         let ret = unsafe { libfdt_bindgen::fdt_path_offset_namelen(fdt, path, len) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_node_offset_by_phandle()` (C function).
@@ -86,7 +87,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_phandle(fdt, phandle) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_node_offset_by_compatible()` (C function).
@@ -96,7 +97,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_node_offset_by_compatible(fdt, prev, compatible) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_next_node()` (C function).
@@ -106,7 +107,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_next_node(fdt, node, &mut depth) };
 
-        match fdt_err_or_option(ret)? {
+        match FdtRawResult::from(ret).try_into()? {
             Some(offset) if depth >= 0 => {
                 let depth = depth.try_into().unwrap();
                 Ok(Some((offset, depth)))
@@ -123,7 +124,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_parent_offset(fdt, node) };
 
-        fdt_err(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_supernode_atdepth_offset()` (C function).
@@ -138,7 +139,7 @@
             // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
             unsafe { libfdt_bindgen::fdt_supernode_atdepth_offset(fdt, node, depth, nodedepth) };
 
-        fdt_err(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_subnode_offset_namelen()` (C function).
@@ -149,7 +150,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_subnode_offset_namelen(fdt, parent, name, namelen) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
     /// Safe wrapper around `fdt_first_subnode()` (C function).
     fn first_subnode(&self, node: c_int) -> Result<Option<c_int>> {
@@ -157,7 +158,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_first_subnode(fdt, node) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_next_subnode()` (C function).
@@ -166,7 +167,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_next_subnode(fdt, node) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_address_cells()` (C function).
@@ -175,7 +176,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_address_cells(fdt, node) };
 
-        Ok(fdt_err(ret)?.try_into().unwrap())
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_size_cells()` (C function).
@@ -184,7 +185,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_size_cells(fdt, node) };
 
-        Ok(fdt_err(ret)?.try_into().unwrap())
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_get_name()` (C function).
@@ -194,7 +195,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor). On success, the
         // function returns valid null terminating string and otherwise returned values are dropped.
         let name = unsafe { libfdt_bindgen::fdt_get_name(fdt, node, &mut len) };
-        let len = usize::try_from(fdt_err(len)?).unwrap().checked_add(1).unwrap();
+        let len = usize::try_from(FdtRawResult::from(len))?.checked_add(1).unwrap();
 
         get_slice_at_ptr(self.as_fdt_slice(), name.cast(), len).ok_or(FdtError::Internal)
     }
@@ -210,8 +211,7 @@
             // function respects the passed number of characters.
             unsafe { libfdt_bindgen::fdt_getprop_namelen(fdt, node, name, namelen, &mut len) };
 
-        if let Some(len) = fdt_err_or_option(len)? {
-            let len = usize::try_from(len).unwrap();
+        if let Some(len) = FdtRawResult::from(len).try_into()? {
             let bytes = get_slice_at_ptr(self.as_fdt_slice(), prop.cast(), len);
 
             Ok(Some(bytes.ok_or(FdtError::Internal)?))
@@ -227,7 +227,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let prop = unsafe { libfdt_bindgen::fdt_get_property_by_offset(fdt, offset, &mut len) };
 
-        let data_len = fdt_err(len)?.try_into().unwrap();
+        let data_len = FdtRawResult::from(len).try_into()?;
         // TODO(stable_feature(offset_of)): mem::offset_of!(fdt_property, data).
         let data_offset = memoffset::offset_of!(libfdt_bindgen::fdt_property, data);
         let len = data_offset.checked_add(data_len).ok_or(FdtError::Internal)?;
@@ -252,7 +252,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_first_property_offset(fdt, node) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_next_property_offset()` (C function).
@@ -261,7 +261,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_next_property_offset(fdt, prev) };
 
-        fdt_err_or_option(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_find_max_phandle()` (C function).
@@ -271,7 +271,7 @@
         // SAFETY: Accesses (read-only) are constrained to the DT totalsize.
         let ret = unsafe { libfdt_bindgen::fdt_find_max_phandle(fdt, &mut phandle) };
 
-        fdt_err_expect_zero(ret)?;
+        FdtRawResult::from(ret).try_into()?;
 
         phandle.try_into()
     }
@@ -321,7 +321,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_nop_node(fdt, node) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_add_subnode_namelen()` (C function).
@@ -332,7 +332,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_add_subnode_namelen(fdt, node, name, namelen) };
 
-        fdt_err(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_setprop()` (C function).
@@ -345,7 +345,7 @@
         //          (validated by underlying libfdt).
         let ret = unsafe { libfdt_bindgen::fdt_setprop(fdt, node, name, value, len) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_setprop_placeholder()` (C function).
@@ -358,7 +358,7 @@
             // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
             unsafe { libfdt_bindgen::fdt_setprop_placeholder(fdt, node, name, len, &mut data) };
 
-        fdt_err_expect_zero(ret)?;
+        FdtRawResult::from(ret).try_into()?;
 
         get_mut_slice_at_ptr(self.as_fdt_slice_mut(), data.cast(), size).ok_or(FdtError::Internal)
     }
@@ -373,7 +373,7 @@
         //          (validated by underlying libfdt).
         let ret = unsafe { libfdt_bindgen::fdt_setprop_inplace(fdt, node, name, value, len) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_appendprop()` (C function).
@@ -385,7 +385,7 @@
         // SAFETY: Accesses are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_appendprop(fdt, node, name, value, len) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_appendprop_addrrange()` (C function).
@@ -404,7 +404,7 @@
             libfdt_bindgen::fdt_appendprop_addrrange(fdt, parent, node, name, addr, size)
         };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_delprop()` (C function).
@@ -417,7 +417,7 @@
         // being called when FdtNode instances are in use.
         let ret = unsafe { libfdt_bindgen::fdt_delprop(fdt, node, name) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe wrapper around `fdt_nop_property()` (C function).
@@ -428,7 +428,7 @@
         // library locates the node's property.
         let ret = unsafe { libfdt_bindgen::fdt_nop_property(fdt, node, name) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Safe and aliasing-compatible wrapper around `fdt_open_into()` (C function).
@@ -451,7 +451,7 @@
         // SAFETY: Accesses (R/W) are constrained to the DT totalsize (validated by ctor).
         let ret = unsafe { libfdt_bindgen::fdt_pack(fdt) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 
     /// Wrapper around `fdt_overlay_apply()` (C function).
@@ -470,7 +470,7 @@
         // but that's our caller's responsibility.
         let ret = unsafe { libfdt_bindgen::fdt_overlay_apply(fdt, overlay) };
 
-        fdt_err_expect_zero(ret)
+        FdtRawResult::from(ret).try_into()
     }
 }
 
@@ -508,7 +508,7 @@
     // size, reflected in its fdt_header::totalsize.
     let ret = unsafe { libfdt_bindgen::fdt_open_into(fdt, dest, len) };
 
-    fdt_err_expect_zero(ret)
+    FdtRawResult::from(ret).try_into()
 }
 
 // TODO(stable_feature(pointer_is_aligned)): p.is_aligned()
diff --git a/libs/libfdt/src/result.rs b/libs/libfdt/src/result.rs
index 65ef55d..52291ca 100644
--- a/libs/libfdt/src/result.rs
+++ b/libs/libfdt/src/result.rs
@@ -14,7 +14,7 @@
 
 //! Rust types related to the libfdt C integer results.
 
-use core::ffi::c_int;
+use core::ffi::{c_int, c_uint};
 use core::fmt;
 use core::result;
 
@@ -94,18 +94,6 @@
 /// Result type with FdtError enum.
 pub type Result<T> = result::Result<T, FdtError>;
 
-pub(crate) fn fdt_err(val: c_int) -> Result<c_int> {
-    FdtRawResult::from(val).try_into()
-}
-
-pub(crate) fn fdt_err_expect_zero(val: c_int) -> Result<()> {
-    FdtRawResult::from(val).try_into()
-}
-
-pub(crate) fn fdt_err_or_option(val: c_int) -> Result<Option<c_int>> {
-    FdtRawResult::from(val).try_into()
-}
-
 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
 pub(crate) struct FdtRawResult(c_int);
 
@@ -164,6 +152,34 @@
     }
 }
 
+impl TryFrom<FdtRawResult> for c_uint {
+    type Error = FdtError;
+
+    fn try_from(res: FdtRawResult) -> Result<Self> {
+        Ok(c_int::try_from(res)?.try_into().unwrap())
+    }
+}
+
+impl TryFrom<FdtRawResult> for usize {
+    type Error = FdtError;
+
+    fn try_from(res: FdtRawResult) -> Result<Self> {
+        Ok(c_int::try_from(res)?.try_into().unwrap())
+    }
+}
+
+impl TryFrom<FdtRawResult> for Option<usize> {
+    type Error = FdtError;
+
+    fn try_from(res: FdtRawResult) -> Result<Self> {
+        match res.try_into() {
+            Ok(n) => Ok(Some(n)),
+            Err(FdtError::NotFound) => Ok(None),
+            Err(e) => Err(e),
+        }
+    }
+}
+
 impl TryFrom<FdtRawResult> for () {
     type Error = FdtError;