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