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/// The UEFI device path to access this UEFI boot option.
68///
69/// Nv-redfish keeps open underlying type for `UefiDevicePath` because it
70/// can really be represented by any implementation of UEFI's device path.
71pub type UefiDevicePath<T> = TaggedType<T, UefiDevicePathTag>;
72#[doc(hidden)]
73#[derive(tagged_types::Tag)]
74#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
75#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
76#[capability(inner_access, cloned)]
77pub enum UefiDevicePathTag {}
78
79/// The user-readable display name of the boot option that appears in
80/// the boot order list in the user interface.
81pub type DisplayName<T> = TaggedType<T, DisplayNameTag>;
82#[doc(hidden)]
83#[derive(tagged_types::Tag)]
84#[implement(Clone, Copy)]
85#[transparent(Debug, Display, Serialize, Deserialize)]
86#[capability(inner_access, cloned)]
87pub enum DisplayNameTag {}
88
89/// Boot option.
90///
91/// Provides functions to access boot option.
92pub struct BootOption<B: Bmc> {
93    data: Arc<BootOptionSchema>,
94    _marker: PhantomData<B>,
95}
96
97impl<B: Bmc> BootOption<B> {
98    /// Create a new log service handle.
99    pub(crate) async fn new(
100        bmc: &NvBmc<B>,
101        nav: &NavProperty<BootOptionSchema>,
102    ) -> Result<Self, Error<B>> {
103        nav.get(bmc.as_ref())
104            .await
105            .map_err(crate::Error::Bmc)
106            .map(|data| Self {
107                data,
108                _marker: PhantomData,
109            })
110    }
111
112    /// Get the raw schema data for this boot option.
113    #[must_use]
114    pub fn raw(&self) -> Arc<BootOptionSchema> {
115        self.data.clone()
116    }
117
118    ///
119    /// Boot option reference.
120    #[must_use]
121    pub fn boot_reference(&self) -> BootOptionReference<&str> {
122        self.data.boot_option_reference.as_deref().map_or_else(
123            || BootOptionReference::new(self.id().inner()),
124            BootOptionReference::new,
125        )
126    }
127
128    /// An indication of whether the boot option is enabled.
129    #[must_use]
130    pub fn enabled(&self) -> Option<bool> {
131        self.data.boot_option_enabled.and_then(identity)
132    }
133
134    /// The user-readable display name of the boot option that appears
135    /// in the boot order list in the user interface.
136    #[must_use]
137    pub fn display_name(&self) -> Option<DisplayName<&str>> {
138        self.data
139            .display_name
140            .as_ref()
141            .and_then(Option::as_ref)
142            .map(String::as_str)
143            .map(DisplayName::new)
144    }
145
146    /// The UEFI device path to access this UEFI boot option.
147    #[must_use]
148    pub fn uefi_device_path(&self) -> Option<UefiDevicePath<&str>> {
149        self.data
150            .uefi_device_path
151            .as_ref()
152            .and_then(Option::as_ref)
153            .map(String::as_str)
154            .map(UefiDevicePath::new)
155    }
156}
157
158impl<B: Bmc> Resource for BootOption<B> {
159    fn resource_ref(&self) -> &ResourceSchema {
160        &self.data.as_ref().base
161    }
162}