Skip to main content

intel_crashlog/
error.rs

1// Copyright (C) 2025 Intel Corporation
2// SPDX-License-Identifier: MIT
3
4#[cfg(feature = "collateral_manager")]
5use crate::{
6    collateral::{ItemPath, PVSS},
7    header::Version,
8};
9#[cfg(not(feature = "std"))]
10use alloc::{fmt, str};
11#[cfg(not(feature = "std"))]
12use core::num;
13#[cfg(feature = "std")]
14use std::{fmt, io, num, str};
15
16/// Errors reported by the Crash Log extraction and decoding functions.
17#[derive(Debug)]
18pub enum Error {
19    InternalError,
20    InvalidCrashLog,
21    NoCrashLogFound,
22    #[cfg(feature = "collateral_manager")]
23    MissingCollateral(PVSS, ItemPath),
24    #[cfg(feature = "collateral_manager")]
25    MissingDecodeDefinitions(Version),
26    InvalidBootErrorRecordRegion,
27    InvalidHeader,
28    EmptyRegion,
29    InvalidHeaderType(u16),
30    InvalidRecordType(u8),
31    InvalidProductID(u32),
32    #[cfg(feature = "serialize")]
33    JsonError(serde_json::Error),
34    Utf8Error(str::Utf8Error),
35    ParseIntError(num::ParseIntError),
36    #[cfg(feature = "std")]
37    IOError(io::Error),
38    #[cfg(feature = "std")]
39    OsStringError(std::ffi::OsString),
40}
41
42#[cfg(feature = "std")]
43impl std::error::Error for Error {}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        match self {
48            Error::InternalError => write!(f, "Internal error in the Crash Log library"),
49            Error::NoCrashLogFound => write!(f, "No Crash Log could be found"),
50            Error::InvalidCrashLog => write!(f, "The Crash Log is invalid"),
51            #[cfg(feature = "collateral_manager")]
52            Error::MissingCollateral(pvss, item) => {
53                write!(f, "Missing {item} collateral file for {pvss}")
54            }
55            #[cfg(feature = "collateral_manager")]
56            Error::MissingDecodeDefinitions(version) => {
57                write!(f, "Missing decode definitions for {version}")
58            }
59            Error::InvalidBootErrorRecordRegion => write!(f, "Invalid Boot Error Record region"),
60            Error::InvalidHeader => write!(f, "Invalid Crash Log Header"),
61            Error::EmptyRegion => write!(f, "The Crash Log Region is not populated"),
62            Error::InvalidHeaderType(ht) => write!(f, "Invalid Crash Log Header Type: {ht}"),
63            Error::InvalidRecordType(rt) => write!(f, "Unknown Crash Log Record Type: {rt:#x}"),
64            Error::InvalidProductID(pid) => write!(f, "Unknown Crash Log Product ID: {pid:#x}"),
65            #[cfg(feature = "serialize")]
66            Error::JsonError(err) => write!(f, "Invalid JSON file: {err}"),
67            Error::Utf8Error(err) => write!(f, "UTF8 Error: {err}"),
68            Error::ParseIntError(err) => write!(f, "Error while parsing integer: {err}"),
69            #[cfg(feature = "std")]
70            Error::IOError(err) => write!(f, "Encountered IO error: {err}"),
71            #[cfg(feature = "std")]
72            Error::OsStringError(s) => write!(f, "Cannot convert OS string: {s:?}"),
73        }
74    }
75}
76
77#[cfg(feature = "std")]
78impl From<io::Error> for Error {
79    fn from(err: io::Error) -> Self {
80        Error::IOError(err)
81    }
82}
83
84#[cfg(feature = "std")]
85impl From<std::ffi::OsString> for Error {
86    fn from(err: std::ffi::OsString) -> Self {
87        Error::OsStringError(err)
88    }
89}
90
91#[cfg(feature = "serialize")]
92impl From<serde_json::Error> for Error {
93    fn from(err: serde_json::Error) -> Self {
94        Error::JsonError(err)
95    }
96}
97
98impl From<str::Utf8Error> for Error {
99    fn from(err: str::Utf8Error) -> Self {
100        Error::Utf8Error(err)
101    }
102}
103
104impl From<num::ParseIntError> for Error {
105    fn from(err: num::ParseIntError) -> Self {
106        Error::ParseIntError(err)
107    }
108}