dt.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use serde::Deserialize;
  2. use serde_device_tree::{
  3. buildin::{Node, NodeSeq, Reg, StrSeq},
  4. Dtb, DtbPtr,
  5. };
  6. use core::ops::Range;
  7. /// Root device tree structure containing system information.
  8. #[derive(Deserialize)]
  9. pub struct Tree<'a> {
  10. /// Optional model name string.
  11. pub model: Option<StrSeq<'a>>,
  12. /// Chosen node containing boot parameters.
  13. pub chosen: Chosen<'a>,
  14. /// Memory information.
  15. pub memory: NodeSeq<'a>,
  16. /// CPU information.
  17. pub cpus: Cpus<'a>,
  18. }
  19. /// Chosen node containing boot parameters.
  20. #[derive(Deserialize)]
  21. #[serde(rename_all = "kebab-case")]
  22. pub struct Chosen<'a> {
  23. /// Path to stdout device.
  24. pub stdout_path: StrSeq<'a>,
  25. }
  26. /// CPU information container.
  27. #[derive(Deserialize)]
  28. #[serde(rename_all = "kebab-case")]
  29. pub struct Cpus<'a> {
  30. /// Sequence of CPU nodes.
  31. pub cpu: NodeSeq<'a>,
  32. }
  33. /// Individual CPU node information.
  34. #[derive(Deserialize, Debug)]
  35. pub struct Cpu<'a> {
  36. /// RISC-V ISA extensions supported by this CPU.
  37. #[serde(rename = "riscv,isa-extensions")]
  38. pub isa_extensions: Option<StrSeq<'a>>,
  39. #[serde(rename = "riscv,isa")]
  40. pub isa: Option<StrSeq<'a>>,
  41. /// CPU register information.
  42. pub reg: Reg<'a>,
  43. }
  44. /// Generic device node information.
  45. #[allow(unused)]
  46. #[derive(Deserialize, Debug)]
  47. pub struct Device<'a> {
  48. /// Device register information.
  49. pub reg: Reg<'a>,
  50. }
  51. /// Memory range.
  52. #[derive(Deserialize)]
  53. #[serde(rename_all = "kebab-case")]
  54. pub struct Memory<'a> {
  55. pub reg: Reg<'a>,
  56. }
  57. /// Errors that can occur during device tree parsing.
  58. pub enum ParseDeviceTreeError {
  59. /// Invalid device tree format.
  60. Format,
  61. }
  62. pub fn parse_device_tree(opaque: usize) -> Result<Dtb, ParseDeviceTreeError> {
  63. let Ok(ptr) = DtbPtr::from_raw(opaque as *mut _) else {
  64. return Err(ParseDeviceTreeError::Format);
  65. };
  66. let dtb = Dtb::from(ptr);
  67. Ok(dtb)
  68. }
  69. pub fn get_compatible_and_range<'de>(node: &Node) -> Option<(StrSeq<'de>, Range<usize>)> {
  70. let compatible = node
  71. .get_prop("compatible")
  72. .map(|prop_item| prop_item.deserialize::<StrSeq<'de>>());
  73. let regs = node
  74. .get_prop("reg")
  75. .map(|prop_item| {
  76. let reg = prop_item.deserialize::<serde_device_tree::buildin::Reg>();
  77. if let Some(range) = reg.iter().next() {
  78. return Some(range);
  79. }
  80. None
  81. })
  82. .map_or_else(|| None, |v| v);
  83. if let Some(compatible) = compatible {
  84. if let Some(regs) = regs {
  85. Some((compatible, regs.0))
  86. } else {
  87. None
  88. }
  89. } else {
  90. None
  91. }
  92. }