xref: /DADK/dadk-config/src/boot/grub.rs (revision c6f35e8aa5fda2a3004763828fb91c7762df6087)
1 use serde::Deserialize;
2 
3 /// Default time for GRUB to wait for user selection
4 const GRUB_DEFAULT_TIMEOUT: u32 = 10;
5 
6 #[derive(Debug, Clone, Deserialize)]
7 pub struct GrubConfig {
8     /// Time to wait for user selection before booting
9     #[serde(default = "default_timeout")]
10     pub timeout: u32,
11 
12     #[serde(rename = "i386-legacy")]
13     pub i386_legacy: Option<ArchConfig>,
14     #[serde(rename = "i386-efi")]
15     pub i386_efi: Option<ArchConfig>,
16     #[serde(rename = "x86_64-efi")]
17     pub x86_64_efi: Option<ArchConfig>,
18 }
19 
20 const fn default_timeout() -> u32 {
21     GRUB_DEFAULT_TIMEOUT
22 }
23 
24 #[derive(Debug, Clone, Deserialize)]
25 pub struct ArchConfig {
26     /// 指向grub-file的路径
27     #[serde(rename = "grub-file")]
28     pub grub_file: String,
29     /// 指向grub-install的路径
30     #[serde(rename = "grub-install")]
31     pub grub_install: String,
32 }
33 
34 #[cfg(test)]
35 mod tests {
36     use super::*;
37 
38     /// Test if the GRUB configuration parsing is correct for all architectures
39     #[test]
40     fn test_all_architectures() {
41         let toml = r#"
42         timeout = 15
43         [i386-legacy]
44         grub-file = "/opt/dragonos-grub/arch/i386/legacy/grub/bin/grub-file"
45         grub-install = "/opt/dragonos-grub/arch/i386/legacy/grub/sbin/grub-install"
46         [i386-efi]
47         grub-file = "/opt/dragonos-grub/arch/i386/efi/grub/bin/grub-file"
48         grub-install = "/opt/dragonos-grub/arch/i386/efi/grub/sbin/grub-install"
49         [x86_64-efi]
50         grub-file = "/opt/dragonos-grub/arch/x86_64/efi/grub/bin/grub-file"
51         grub-install = "/opt/dragonos-grub/arch/x86_64/efi/grub/sbin/grub-install"
52         "#;
53         let config: GrubConfig = toml::from_str(toml).unwrap();
54         assert_eq!(config.timeout, 15);
55         assert!(config.i386_legacy.is_some());
56         assert!(config.i386_efi.is_some());
57         assert!(config.x86_64_efi.is_some());
58     }
59 
60     #[test]
61     fn test_default_timeout() {
62         let toml = r#"
63         [i386-legacy]
64         grub-file = "grub Legacy"
65         grub-install = "/boot/grub/i386-legacy"
66         "#;
67         let config: GrubConfig = toml::from_str(toml).unwrap();
68         assert_eq!(config.timeout, GRUB_DEFAULT_TIMEOUT);
69     }
70 
71     #[test]
72     fn test_custom_timeout() {
73         let toml = r#"
74         timeout = 5
75         [i386-efi]
76         grub-file = "grub EFI"
77         grub-install = "/boot/grub/i386-efi"
78         "#;
79         let config: GrubConfig = toml::from_str(toml).unwrap();
80         assert_eq!(config.timeout, 5);
81     }
82 
83     #[test]
84     fn test_no_architectures() {
85         let toml = r#"
86         timeout = 20
87         "#;
88         let config: GrubConfig = toml::from_str(toml).unwrap();
89         assert_eq!(config.timeout, 20);
90         assert!(config.i386_legacy.is_none());
91         assert!(config.i386_efi.is_none());
92         assert!(config.x86_64_efi.is_none());
93     }
94 }
95