use super::platform::{inspect,Limits,Workspace}; pub const MAX_CPUS:usize=8; pub const AFFINITY_MASK:u64=0x0000_00ff_00ff_ffff; #[repr(C)]#[derive(Clone,Copy,Debug,PartialEq,Eq)]pub struct Cpu{pub affinity:u64,pub status:u64,pub method:u64,pub node_offset:u64} impl Cpu{pub const EMPTY:Self=Self{affinity:0,status:0,method:0,node_offset:0};} #[repr(C)]#[derive(Clone,Copy,Debug,PartialEq,Eq)]pub struct Topology{pub count:u64,pub enabled:u64,pub disabled:u64,pub boot_index:u64,pub address_cells:u64,pub source_bytes:u64,pub inspected_nodes:u64,pub cpus:[Cpu;MAX_CPUS]} impl Topology{pub const EMPTY:Self=Self{count:0,enabled:0,disabled:0,boot_index:0,address_cells:0,source_bytes:0,inspected_nodes:0,cpus:[Cpu::EMPTY;MAX_CPUS]};pub fn entries(&self)->&[Cpu]{&self.cpus[..self.count as usize]}} #[derive(Clone,Copy,Debug,PartialEq,Eq)]pub enum Error{Structure,MissingCpus,DuplicateCpus,Cells,Capacity,CpuNode,Reg,DuplicateAffinity,Status,Method,BootCpu} fn word(blob:&[u8],at:usize)->Result{Ok(u32::from_be_bytes(blob.get(at..at.checked_add(4).ok_or(Error::Structure)?).ok_or(Error::Structure)?.try_into().map_err(|_|Error::Structure)?))} fn string(bytes:&[u8])->Result<&[u8],Error>{let p=bytes.iter().position(|b|*b==0).ok_or(Error::Structure)?;Ok(&bytes[..p])} fn single(bytes:&[u8])->Result<&[u8],Error>{let s=string(bytes)?;if s.len()+1!=bytes.len(){return Err(Error::Structure)}Ok(s)} fn align(n:usize)->Result{Ok(n.checked_add(3).ok_or(Error::Structure)?&!3)} #[derive(Clone,Copy)]struct Pending{offset:u64,device:bool,reg:Option,status:u64,method:u64} impl Pending{const EMPTY:Self=Self{offset:0,device:false,reg:None,status:1,method:0};} /// Parse the validated immutable FDT and publish only a complete, unique CPU set. /// The MPIDR input is read by the boot CPU; only architecture affinity bits select it. /// Parsing does not turn on another CPU or declare it online. pub fn parse_into(output:&mut Topology,blob:&[u8],boot_mpidr:u64,scratch:&mut Workspace)->Result<(),Error>{ let info=inspect(blob,Limits::DEFAULT,scratch).map_err(|_|Error::Structure)?;let blob=&blob[..info.blob_bytes];let strings_at=word(blob,12)?as usize;let strings_end=strings_at+word(blob,32)?as usize;let strings=blob.get(strings_at..strings_end).ok_or(Error::Structure)?;let mut cursor=word(blob,8)?as usize;let end=cursor+word(blob,36)?as usize;let mut depth=0;let mut in_cpus=false;let mut seen_cpus=false;let mut cells=None;let mut sizes=None;let mut pending=Pending::EMPTY;let mut current_cpu=false;let mut result=Topology::EMPTY;result.source_bytes=info.blob_bytes as u64;result.inspected_nodes=info.nodes as u64; while cursor{let name=string(blob.get(cursor..end).ok_or(Error::Structure)?)?;cursor=align(cursor+name.len()+1)?;depth+=1;if depth==2&&name==b"cpus"{if seen_cpus{return Err(Error::DuplicateCpus)}seen_cpus=true;in_cpus=true;}else if in_cpus&&depth==3{if !(name==b"cpu"||name.starts_with(b"cpu@")){current_cpu=false;}else{if !matches!(cells,Some(1)|Some(2))||sizes!=Some(0){return Err(Error::Cells)}pending=Pending{offset:offset as u64,..Pending::EMPTY};current_cpu=true;}}}, 2=>{if in_cpus&&depth==3&¤t_cpu{if !pending.device{return Err(Error::CpuNode)}let affinity=pending.reg.ok_or(Error::Reg)?;if result.entries().iter().any(|c|c.affinity==affinity){return Err(Error::DuplicateAffinity)}if result.count as usize==MAX_CPUS{return Err(Error::Capacity)}if pending.status==2&&pending.method!=1{return Err(Error::Method)}result.cpus[result.count as usize]=Cpu{affinity,status:pending.status,method:pending.method,node_offset:pending.offset};result.count+=1;current_cpu=false;}if in_cpus&&depth==2{in_cpus=false;}depth-=1;}, 3=>{let length=word(blob,cursor)?as usize;let name=word(blob,cursor+4)?as usize;cursor+=8;let value=blob.get(cursor..cursor.checked_add(length).ok_or(Error::Structure)?).ok_or(Error::Structure)?;cursor=align(cursor+length)?;let name=string(strings.get(name..).ok_or(Error::Structure)?)?;if in_cpus&&depth==2{if name==b"#address-cells"{if value.len()!=4{return Err(Error::Cells)}cells=Some(word(value,0)?);}else if name==b"#size-cells"{if value.len()!=4{return Err(Error::Cells)}sizes=Some(word(value,0)?);}} if in_cpus&&depth==3&¤t_cpu{match name{b"device_type"=>{if single(value)?!=b"cpu"{return Err(Error::CpuNode)}pending.device=true;},b"reg"=>{let n=cells.ok_or(Error::Cells)?;if value.len()!=n as usize*4{return Err(Error::Reg)}let affinity=if n==2{((word(value,0)?as u64)<<32)|word(value,4)?as u64}else{word(value,0)?as u64};if affinity&!AFFINITY_MASK!=0{return Err(Error::Reg)}pending.reg=Some(affinity);},b"status"=>{let status=single(value)?;pending.status=match status{b"okay"|b"ok"=>1,b"disabled"=>2,b"fail"=>3,_ if status.starts_with(b"fail-")&&status.len()>5=>3,_=>return Err(Error::Status)};},b"enable-method"=>{if single(value)?!=b"psci"{return Err(Error::Method)}pending.method=1;},_=>{}}} },4=>{},9=>break,_=>return Err(Error::Structure) }} if !seen_cpus||result.count==0{return Err(Error::MissingCpus)}if !matches!(cells,Some(1)|Some(2))||sizes!=Some(0){return Err(Error::Cells)}result.address_cells=cells.unwrap()as u64;if result.count>1&&result.entries().iter().any(|c|c.method!=1){return Err(Error::Method)}result.cpus[..result.count as usize].sort_unstable_by_key(|c|c.affinity);let boot=boot_mpidr&AFFINITY_MASK;let boot_index=result.entries().iter().position(|c|c.affinity==boot&&c.status==1).ok_or(Error::BootCpu)?;result.boot_index=boot_index as u64;result.enabled=result.entries().iter().filter(|c|c.status==1).count()as u64;result.disabled=result.entries().iter().filter(|c|c.status==2).count()as u64;*output=result;Ok(()) } impl Topology{pub const fn os_k2005_physical_layout()->[u64;15]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Self,count)as u64,offset_of!(Self,enabled)as u64,offset_of!(Self,disabled)as u64,offset_of!(Self,boot_index)as u64,offset_of!(Self,address_cells)as u64,offset_of!(Self,source_bytes)as u64,offset_of!(Self,inspected_nodes)as u64,offset_of!(Self,cpus)as u64,size_of::()as u64,offset_of!(Cpu,affinity)as u64,offset_of!(Cpu,status)as u64,offset_of!(Cpu,method)as u64,offset_of!(Cpu,node_offset)as u64,MAX_CPUS as u64]}}