1 use serde::Deserialize;
2
3 use crate::common::target_arch::TargetArch;
4
5 #[derive(Debug, Clone, Deserialize)]
6 pub struct UbootConfig {
7 /// URL to download U-Boot binary file
8 ///
9 /// If the URL is `https://mirrors.dragonos.org.cn/pub/third_party/u-boot`,
10 /// then the final download URL will be `https://mirrors.dragonos.org.cn/pub/third_party/u-boot/u-boot-{version}-{arch}.tar.xz`
11 #[serde(rename = "download-url", default = "default_download_url")]
12 pub download_url: String,
13
14 /// Version of U-Boot
15 #[serde(rename = "version", default = "default_version")]
16 pub version: String,
17
18 /// Prefix directory for U-Boot binary file
19 ///
20 /// Example:
21 /// If the current architecture is `riscv64` and the version is `v2023.10`,
22 /// `path_prefix` is `bin/uboot/`,
23 /// then the path to locate the U-Boot binary file would be: `bin/uboot/riscv64/v2023.10/uboot.bin`
24 #[serde(rename = "path-prefix", default = "default_path_prefix")]
25 pub path_prefix: String,
26 }
27
28 impl Default for UbootConfig {
default() -> Self29 fn default() -> Self {
30 Self {
31 download_url: Self::DEFAULT_DOWNLOAD_URL.to_string(),
32 version: Self::DEFAULT_VERSION.to_string(),
33 path_prefix: Self::DEFAULT_PATH_PREFIX.to_string(),
34 }
35 }
36 }
37
38 impl UbootConfig {
39 const DEFAULT_DOWNLOAD_URL: &'static str =
40 "https://mirrors.dragonos.org.cn/pub/third_party/u-boot";
41
42 const DEFAULT_VERSION: &'static str = "v2023.10";
43
44 const DEFAULT_PATH_PREFIX: &'static str = "bin/uboot/";
45 /// Get the full download URL for the U-Boot binary file archive
full_download_url(&self, target_arch: TargetArch) -> String46 pub fn full_download_url(&self, target_arch: TargetArch) -> String {
47 let arch_str: &str = target_arch.into();
48 format!(
49 "{}/u-boot-{}-{}.tar.xz",
50 self.download_url, self.version, arch_str
51 )
52 }
53 }
54
default_download_url() -> String55 fn default_download_url() -> String {
56 UbootConfig::DEFAULT_DOWNLOAD_URL.to_string()
57 }
58
default_version() -> String59 fn default_version() -> String {
60 UbootConfig::DEFAULT_VERSION.to_string()
61 }
62
default_path_prefix() -> String63 fn default_path_prefix() -> String {
64 UbootConfig::DEFAULT_PATH_PREFIX.to_string()
65 }
66
67 #[cfg(test)]
68 mod tests {
69 use super::*;
70
71 #[test]
test_default_uboot_config()72 fn test_default_uboot_config() {
73 let config = UbootConfig::default();
74 assert_eq!(config.download_url, UbootConfig::DEFAULT_DOWNLOAD_URL);
75 assert_eq!(config.version, "v2023.10");
76 assert_eq!(config.path_prefix, "bin/uboot/");
77 }
78
79 #[test]
test_full_download_url_riscv64()80 fn test_full_download_url_riscv64() {
81 let config = UbootConfig::default();
82 let url = config.full_download_url(TargetArch::RiscV64);
83 assert_eq!(
84 url,
85 "https://mirrors.dragonos.org.cn/pub/third_party/u-boot/u-boot-v2023.10-riscv64.tar.xz"
86 );
87 }
88
89 #[test]
test_empty_toml_deserialization()90 fn test_empty_toml_deserialization() {
91 let toml_content = "";
92 let config: UbootConfig = toml::from_str(toml_content).unwrap();
93
94 // Verify that the default values are set
95 assert_eq!(config.download_url, UbootConfig::DEFAULT_DOWNLOAD_URL);
96 assert_eq!(config.version, "v2023.10");
97 assert_eq!(config.path_prefix, "bin/uboot/");
98 }
99 }
100