Skip to main content

nv_redfish/computer_system/
boot_option.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//! Boot options
16//!
17
18use crate::computer_system::BootOptionReference;
19use crate::schema::redfish::boot_option::BootOption as BootOptionSchema;
20use crate::schema::redfish::boot_option_collection::BootOptionCollection as BootOptionCollectionSchema;
21use crate::Error;
22use crate::NvBmc;
23use crate::Resource;
24use crate::ResourceSchema;
25use nv_redfish_core::Bmc;
26use nv_redfish_core::NavProperty;
27use std::convert::identity;
28use std::marker::PhantomData;
29use std::sync::Arc;
30use tagged_types::TaggedType;
31
32/// Boot options collection.
33///
34/// Provides functions to access collection members.
35pub struct BootOptionCollection<B: Bmc> {
36    bmc: NvBmc<B>,
37    collection: Arc<BootOptionCollectionSchema>,
38}
39
40impl<B: Bmc> BootOptionCollection<B> {
41    /// Create a new manager collection handle.
42    pub(crate) async fn new(
43        bmc: &NvBmc<B>,
44        nav: &NavProperty<BootOptionCollectionSchema>,
45    ) -> Result<Self, Error<B>> {
46        let collection = bmc.expand_property(nav).await?;
47        Ok(Self {
48            bmc: bmc.clone(),
49            collection,
50        })
51    }
52
53    /// List all managers available in this BMC.
54    ///
55    /// # Errors
56    ///
57    /// Returns an error if fetching manager data fails.
58    pub async fn members(&self) -> Result<Vec<BootOption<B>>, Error<B>> {
59        let mut members = Vec::new();
60        for m in &self.collection.members {
61            members.push(BootOption::new(&self.bmc, m).await?);
62        }
63        Ok(members)
64    }
65}
66
67/// An indication of whether the boot option is enabled.
68pub type Enabled = TaggedType<bool, EnabledTag>;
69#[doc(hidden)]
70#[derive(tagged_types::Tag)]
71#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
72#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
73#[capability(inner_access)]
74pub enum EnabledTag {}
75
76/// The UEFI device path to access this UEFI boot option.
77///
78/// Nv-redfish keeps open underlying type for `UefiDevicePath` because it
79/// can really be represented by any implementation of UEFI's device path.
80pub type UefiDevicePath<T> = TaggedType<T, UefiDevicePathTag>;
81#[doc(hidden)]
82#[derive(tagged_types::Tag)]
83#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
84#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
85#[capability(inner_access, cloned)]
86pub enum UefiDevicePathTag {}
87
88/// The user-readable display name of the boot option that appears in
89/// the boot order list in the user interface.
90pub type DisplayName<T> = TaggedType<T, DisplayNameTag>;
91#[doc(hidden)]
92#[derive(tagged_types::Tag)]
93#[implement(Clone, Copy)]
94#[transparent(Debug, Display, Serialize, Deserialize)]
95#[capability(inner_access, cloned)]
96pub enum DisplayNameTag {}
97
98/// Boot option.
99///
100/// Provides functions to access boot option.
101pub struct BootOption<B: Bmc> {
102    data: Arc<BootOptionSchema>,
103    _marker: PhantomData<B>,
104}
105
106impl<B: Bmc> BootOption<B> {
107    /// Create a new log service handle.
108    pub(crate) async fn new(
109        bmc: &NvBmc<B>,
110        nav: &NavProperty<BootOptionSchema>,
111    ) -> Result<Self, Error<B>> {
112        nav.get(bmc.as_ref())
113            .await
114            .map_err(crate::Error::Bmc)
115            .map(|data| Self {
116                data,
117                _marker: PhantomData,
118            })
119    }
120
121    /// Get the raw schema data for this boot option.
122    #[must_use]
123    pub fn raw(&self) -> Arc<BootOptionSchema> {
124        self.data.clone()
125    }
126
127    ///
128    /// Boot option reference.
129    #[must_use]
130    pub fn boot_reference(&self) -> BootOptionReference<&String> {
131        BootOptionReference::new(self.id().inner())
132    }
133
134    /// An indication of whether the boot option is enabled.
135    #[must_use]
136    pub fn enabled(&self) -> Option<Enabled> {
137        self.data
138            .boot_option_enabled
139            .and_then(identity)
140            .map(Enabled::new)
141    }
142
143    /// The user-readable display name of the boot option that appears
144    /// in the boot order list in the user interface.
145    #[must_use]
146    pub fn display_name(&self) -> Option<DisplayName<&String>> {
147        self.data
148            .display_name
149            .as_ref()
150            .and_then(Option::as_ref)
151            .map(DisplayName::new)
152    }
153
154    /// The UEFI device path to access this UEFI boot option.
155    #[must_use]
156    pub fn uefi_device_path(&self) -> Option<UefiDevicePath<&String>> {
157        self.data
158            .uefi_device_path
159            .as_ref()
160            .and_then(Option::as_ref)
161            .map(UefiDevicePath::new)
162    }
163}
164
165impl<B: Bmc> Resource for BootOption<B> {
166    fn resource_ref(&self) -> &ResourceSchema {
167        &self.data.as_ref().base
168    }
169}