blob: 9643e1e6580bb331f1c3039f68dbb1215026833e [file] [log] [blame]
Pierre-Clément Tosi99a76902023-12-21 10:30:37 +00001// Copyright 2024, The Android Open Source Project
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//! Rust types related to the libfdt C integer results.
16
17use core::ffi::c_int;
18use core::fmt;
19use core::result;
20
21/// Error type corresponding to libfdt error codes.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub enum FdtError {
24 /// FDT_ERR_NOTFOUND
25 NotFound,
26 /// FDT_ERR_EXISTS
27 Exists,
28 /// FDT_ERR_NOSPACE
29 NoSpace,
30 /// FDT_ERR_BADOFFSET
31 BadOffset,
32 /// FDT_ERR_BADPATH
33 BadPath,
34 /// FDT_ERR_BADPHANDLE
35 BadPhandle,
36 /// FDT_ERR_BADSTATE
37 BadState,
38 /// FDT_ERR_TRUNCATED
39 Truncated,
40 /// FDT_ERR_BADMAGIC
41 BadMagic,
42 /// FDT_ERR_BADVERSION
43 BadVersion,
44 /// FDT_ERR_BADSTRUCTURE
45 BadStructure,
46 /// FDT_ERR_BADLAYOUT
47 BadLayout,
48 /// FDT_ERR_INTERNAL
49 Internal,
50 /// FDT_ERR_BADNCELLS
51 BadNCells,
52 /// FDT_ERR_BADVALUE
53 BadValue,
54 /// FDT_ERR_BADOVERLAY
55 BadOverlay,
56 /// FDT_ERR_NOPHANDLES
57 NoPhandles,
58 /// FDT_ERR_BADFLAGS
59 BadFlags,
60 /// FDT_ERR_ALIGNMENT
61 Alignment,
62 /// Unexpected error code
63 Unknown(i32),
64}
65
66impl fmt::Display for FdtError {
67 /// Prints error messages from libfdt.h documentation.
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 match self {
70 Self::NotFound => write!(f, "The requested node or property does not exist"),
71 Self::Exists => write!(f, "Attempted to create an existing node or property"),
72 Self::NoSpace => write!(f, "Insufficient buffer space to contain the expanded tree"),
73 Self::BadOffset => write!(f, "Structure block offset is out-of-bounds or invalid"),
74 Self::BadPath => write!(f, "Badly formatted path"),
75 Self::BadPhandle => write!(f, "Invalid phandle length or value"),
76 Self::BadState => write!(f, "Received incomplete device tree"),
77 Self::Truncated => write!(f, "Device tree or sub-block is improperly terminated"),
78 Self::BadMagic => write!(f, "Device tree header missing its magic number"),
79 Self::BadVersion => write!(f, "Device tree has a version which can't be handled"),
80 Self::BadStructure => write!(f, "Device tree has a corrupt structure block"),
81 Self::BadLayout => write!(f, "Device tree sub-blocks in unsupported order"),
82 Self::Internal => write!(f, "libfdt has failed an internal assertion"),
83 Self::BadNCells => write!(f, "Bad format or value of #address-cells or #size-cells"),
84 Self::BadValue => write!(f, "Unexpected property value"),
85 Self::BadOverlay => write!(f, "Overlay cannot be applied"),
86 Self::NoPhandles => write!(f, "Device tree doesn't have any phandle available anymore"),
87 Self::BadFlags => write!(f, "Invalid flag or invalid combination of flags"),
88 Self::Alignment => write!(f, "Device tree base address is not 8-byte aligned"),
89 Self::Unknown(e) => write!(f, "Unknown libfdt error '{e}'"),
90 }
91 }
92}
93
94/// Result type with FdtError enum.
95pub type Result<T> = result::Result<T, FdtError>;
96
97pub(crate) fn fdt_err(val: c_int) -> Result<c_int> {
98 if val >= 0 {
99 Ok(val)
100 } else {
101 Err(match -val as _ {
102 libfdt_bindgen::FDT_ERR_NOTFOUND => FdtError::NotFound,
103 libfdt_bindgen::FDT_ERR_EXISTS => FdtError::Exists,
104 libfdt_bindgen::FDT_ERR_NOSPACE => FdtError::NoSpace,
105 libfdt_bindgen::FDT_ERR_BADOFFSET => FdtError::BadOffset,
106 libfdt_bindgen::FDT_ERR_BADPATH => FdtError::BadPath,
107 libfdt_bindgen::FDT_ERR_BADPHANDLE => FdtError::BadPhandle,
108 libfdt_bindgen::FDT_ERR_BADSTATE => FdtError::BadState,
109 libfdt_bindgen::FDT_ERR_TRUNCATED => FdtError::Truncated,
110 libfdt_bindgen::FDT_ERR_BADMAGIC => FdtError::BadMagic,
111 libfdt_bindgen::FDT_ERR_BADVERSION => FdtError::BadVersion,
112 libfdt_bindgen::FDT_ERR_BADSTRUCTURE => FdtError::BadStructure,
113 libfdt_bindgen::FDT_ERR_BADLAYOUT => FdtError::BadLayout,
114 libfdt_bindgen::FDT_ERR_INTERNAL => FdtError::Internal,
115 libfdt_bindgen::FDT_ERR_BADNCELLS => FdtError::BadNCells,
116 libfdt_bindgen::FDT_ERR_BADVALUE => FdtError::BadValue,
117 libfdt_bindgen::FDT_ERR_BADOVERLAY => FdtError::BadOverlay,
118 libfdt_bindgen::FDT_ERR_NOPHANDLES => FdtError::NoPhandles,
119 libfdt_bindgen::FDT_ERR_BADFLAGS => FdtError::BadFlags,
120 libfdt_bindgen::FDT_ERR_ALIGNMENT => FdtError::Alignment,
121 _ => FdtError::Unknown(val),
122 })
123 }
124}
125
126pub(crate) fn fdt_err_expect_zero(val: c_int) -> Result<()> {
127 match fdt_err(val)? {
128 0 => Ok(()),
129 _ => Err(FdtError::Unknown(val)),
130 }
131}
132
133pub(crate) fn fdt_err_or_option(val: c_int) -> Result<Option<c_int>> {
134 match fdt_err(val) {
135 Ok(val) => Ok(Some(val)),
136 Err(FdtError::NotFound) => Ok(None),
137 Err(e) => Err(e),
138 }
139}