Skip to content
flags.rs 5.44 KiB
Newer Older
Luker's avatar
Luker committed
/*
Luker's avatar
Luker committed
 * Copyright (c) 2021-2022, Luca Fulchir <luker@fenrirproject.org>
Luker's avatar
Luker committed
 *
 * This file is part of dfim.
 *
 * dfim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dfim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dfim.  If not, see <https://www.gnu.org/licenses/>.
 */
Luker's avatar
Luker committed
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

#[derive(
Luker's avatar
Luker committed
    ::serde::Deserialize,
    ::serde::Serialize,
    Clone,
    Copy,
    FromPrimitive,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
Luker's avatar
Luker committed
)]
Luker's avatar
Luker committed
#[serde(deny_unknown_fields)]
pub enum Flag {
    Bit0 = 0,
    Bit1 = 1,
    Bit2,
    Bit3,
    Bit4,
    Bit5,
    Bit6,
    Bit7,
    Bit8,
    Bit9,
    Bit10,
    Bit11,
    Bit12,
    Bit13,
    Bit14,
    Bit15,
    Bit16,
    Bit17,
    Bit18,
    Bit19,
    Bit20,
    Bit21,
    Bit22,
    Bit23,
    Bit24,
    Bit25,
    Bit26,
    Bit27,
    Bit28,
    Bit29,
    Bit30,
    Bit31,
    Bit32,
    Bit33,
    Bit34,
    Bit35,
    Bit36,
    Bit37,
    Bit38,
    Bit39,
    Bit40,
    Bit41,
    Bit42,
    Bit43,
    Bit44,
    Bit45,
    Bit46,
    Bit47,
    Bit48,
    Bit49,
    Bit50,
    Bit51,
    Bit52,
    Bit53,
    Bit54,
    Bit55,
    Bit56,
    Bit57,
    Bit58,
    Bit59,
    Bit60,
    Bit61,
    Bit62,
    Bit63,
    // These next are user-friendly names
    // for the above bits
    // common
    PlatformRequired,
    EfiIgnore,
    LegacyBiosBootable,
    // ChromeOS
    // little endian? big endian?
    ChromeOsPriorityBit0,
    ChromeOsPriorityBit1,
    ChromeOsPriorityBit2,
    ChromeOsPriorityBit3,
    // little endian? big endian?
    ChromeOsRetriesBit0,
    ChromeOsRetriesBit1,
    ChromeOsRetriesBit2,
    ChromeOsRetriesBit3,
    ChromeOsSuccessfulBoot,
    // Microsoft
    MSReadOnly,
    MSShadowCopy,
    MSHidden,
    MSNoDriveLetter,
    // Linux Systemd
    SystemdGrowPartition,
    SystemdReadOnly,
    SystemdNoAuto,
}

impl Flag {
Luker's avatar
Luker committed
    pub fn from_bits(bits: u64) -> Vec<Flag> {
        let mut ret = Vec::with_capacity(8);
        for idx in 0..63 {
Luker's avatar
Luker committed
            let raw_flag = bits & (1 << idx);
            if raw_flag != 0 {
                if let Some(flag) = FromPrimitive::from_u64(idx) {
                    ret.push(flag);
                }
Luker's avatar
Luker committed
        ret.sort();
Luker's avatar
Luker committed
        ret
    }
Luker's avatar
Luker committed
    pub const fn bits(&self) -> u64 {
Luker's avatar
Luker committed
        match self {
            Flag::Bit0
            | Flag::Bit1
            | Flag::Bit2
            | Flag::Bit3
            | Flag::Bit4
            | Flag::Bit5
            | Flag::Bit6
            | Flag::Bit7
            | Flag::Bit8
            | Flag::Bit9
            | Flag::Bit10
            | Flag::Bit11
            | Flag::Bit12
            | Flag::Bit13
            | Flag::Bit14
            | Flag::Bit15
            | Flag::Bit16
            | Flag::Bit17
            | Flag::Bit18
            | Flag::Bit19
            | Flag::Bit20
            | Flag::Bit21
            | Flag::Bit22
            | Flag::Bit23
            | Flag::Bit24
            | Flag::Bit25
            | Flag::Bit26
            | Flag::Bit27
            | Flag::Bit28
            | Flag::Bit29
            | Flag::Bit30
            | Flag::Bit31
            | Flag::Bit32
            | Flag::Bit33
            | Flag::Bit34
            | Flag::Bit35
            | Flag::Bit36
            | Flag::Bit37
            | Flag::Bit38
            | Flag::Bit39
            | Flag::Bit40
            | Flag::Bit41
            | Flag::Bit42
            | Flag::Bit43
            | Flag::Bit44
            | Flag::Bit45
            | Flag::Bit46
            | Flag::Bit47
            | Flag::Bit48
            | Flag::Bit49
            | Flag::Bit50
            | Flag::Bit51
            | Flag::Bit52
            | Flag::Bit53
            | Flag::Bit54
            | Flag::Bit55
            | Flag::Bit56
            | Flag::Bit57
            | Flag::Bit58
            | Flag::Bit59
            | Flag::Bit60
            | Flag::Bit61
            | Flag::Bit62
Luker's avatar
Luker committed
            | Flag::Bit63 => 1 << (*self as u64),
Luker's avatar
Luker committed
            Flag::PlatformRequired => 0b1 as u64,
            Flag::EfiIgnore => 0b10 as u64,
            Flag::LegacyBiosBootable => 0b100 as u64,
            // FIXME: Endianness???
            Flag::ChromeOsPriorityBit0 => (1 as u64) << 47,
            Flag::ChromeOsPriorityBit1 => (1 as u64) << 48,
            Flag::ChromeOsPriorityBit2 => (1 as u64) << 49,
            Flag::ChromeOsPriorityBit3 => (1 as u64) << 50,
            Flag::ChromeOsRetriesBit0 => (1 as u64) << 51,
            Flag::ChromeOsRetriesBit1 => (1 as u64) << 52,
            Flag::ChromeOsRetriesBit2 => (1 as u64) << 53,
            Flag::ChromeOsRetriesBit3 => (1 as u64) << 54,
            Flag::ChromeOsSuccessfulBoot => (1 as u64) << 55,
            Flag::MSReadOnly => (1 as u64) << 59,
            Flag::MSShadowCopy => (1 as u64) << 60,
            Flag::MSHidden => (1 as u64) << 61,
            Flag::MSNoDriveLetter => (1 as u64) << 62,
            Flag::SystemdGrowPartition => (1 as u64) << 58,
            Flag::SystemdReadOnly => (1 as u64) << 59,
            Flag::SystemdNoAuto => (1 as u64) << 62,
        }
    }
}