config: refactor and share

This commit is contained in:
Sebastien Bourdeauducq 2020-07-08 19:24:26 +08:00
parent e263814546
commit 6454315cd2
4 changed files with 43 additions and 32 deletions

View File

@ -290,8 +290,8 @@ async fn handle_connection(stream: &TcpStream, control: Rc<RefCell<kernel::Contr
} }
} }
pub fn main(timer: GlobalTimer) { pub fn main(timer: GlobalTimer, cfg: &config::Config) {
let net_addresses = net_settings::get_adresses(); let net_addresses = net_settings::get_adresses(cfg);
info!("network addresses: {}", net_addresses); info!("network addresses: {}", net_addresses);
let eth = zynq::eth::Eth::default(net_addresses.hardware_addr.0.clone()); let eth = zynq::eth::Eth::default(net_addresses.hardware_addr.0.clone());
@ -329,14 +329,10 @@ pub fn main(timer: GlobalTimer) {
} }
}; };
let startup_kernel: Option<Vec<u8>>;
let cfg = config::Config::new();
startup_kernel = cfg.as_ref().ok().and_then(|cfg| cfg.read("startup").ok());
Sockets::init(32); Sockets::init(32);
let control: Rc<RefCell<kernel::Control>> = Rc::new(RefCell::new(kernel::Control::start())); let control: Rc<RefCell<kernel::Control>> = Rc::new(RefCell::new(kernel::Control::start()));
if let Some(buffer) = startup_kernel { if let Ok(buffer) = cfg.read("startup") {
info!("Loading startup kernel..."); info!("Loading startup kernel...");
if let Ok(()) = task::block_on(load_kernel(buffer, &control, None)) { if let Ok(()) = task::block_on(load_kernel(buffer, &control, None)) {
info!("Starting startup kernel..."); info!("Starting startup kernel...");

View File

@ -11,6 +11,7 @@ pub enum Error<'a> {
IoError(io::Error), IoError(io::Error),
Utf8Error(FromUtf8Error), Utf8Error(FromUtf8Error),
KeyNotFoundError(&'a str), KeyNotFoundError(&'a str),
NoConfig,
} }
pub type Result<'a, T> = core::result::Result<T, Error<'a>>; pub type Result<'a, T> = core::result::Result<T, Error<'a>>;
@ -22,6 +23,7 @@ impl<'a> fmt::Display for Error<'a> {
Error::IoError(error) => write!(f, "I/O error: {}", error), Error::IoError(error) => write!(f, "I/O error: {}", error),
Error::Utf8Error(error) => write!(f, "UTF-8 error: {}", error), Error::Utf8Error(error) => write!(f, "UTF-8 error: {}", error),
Error::KeyNotFoundError(name) => write!(f, "Configuration key `{}` not found", name), Error::KeyNotFoundError(name) => write!(f, "Configuration key `{}` not found", name),
Error::NoConfig => write!(f, "Configuration not present"),
} }
} }
} }
@ -61,7 +63,7 @@ fn parse_config<'a>(
} }
pub struct Config { pub struct Config {
fs: fatfs::FileSystem<sd_reader::SdReader>, fs: Option<fatfs::FileSystem<sd_reader::SdReader>>,
} }
impl Config { impl Config {
@ -74,11 +76,16 @@ impl Config {
let reader = sd_reader::SdReader::new(sd); let reader = sd_reader::SdReader::new(sd);
let fs = reader.mount_fatfs(sd_reader::PartitionEntry::Entry1)?; let fs = reader.mount_fatfs(sd_reader::PartitionEntry::Entry1)?;
Ok(Config { fs }) Ok(Config { fs: Some(fs) })
}
pub fn new_dummy() -> Self {
Config { fs: None }
} }
pub fn read<'b>(&self, key: &'b str) -> Result<'b, Vec<u8>> { pub fn read<'b>(&self, key: &'b str) -> Result<'b, Vec<u8>> {
let root_dir = self.fs.root_dir(); if let Some(fs) = &self.fs {
let root_dir = fs.root_dir();
let mut buffer: Vec<u8> = Vec::new(); let mut buffer: Vec<u8> = Vec::new();
match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) { match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) {
Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?, Ok(mut f) => f.read_to_end(&mut buffer).map(|_| ())?,
@ -88,6 +95,9 @@ impl Config {
}, },
}; };
Ok(buffer) Ok(buffer)
} else {
Err(Error::NoConfig)
}
} }
pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> { pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> {

View File

@ -7,7 +7,7 @@
extern crate alloc; extern crate alloc;
use core::{cmp, str}; use core::{cmp, str};
use log::info; use log::{info, warn};
use libboard_zynq::{timer::GlobalTimer, logger, devc, slcr}; use libboard_zynq::{timer::GlobalTimer, logger, devc, slcr};
use libsupport_zynq::ram; use libsupport_zynq::ram;
@ -93,5 +93,12 @@ pub fn main_core0() {
pl::csr::rtio_core::reset_phy_write(1); pl::csr::rtio_core::reset_phy_write(1);
} }
comms::main(timer); let cfg = match config::Config::new() {
Ok(cfg) => cfg,
Err(err) => {
warn!("config initialization failed: {}", err);
config::Config::new_dummy()
}
};
comms::main(timer, &cfg);
} }

View File

@ -23,12 +23,11 @@ impl fmt::Display for NetAddresses {
} }
} }
pub fn get_adresses() -> NetAddresses { pub fn get_adresses(cfg: &config::Config) -> NetAddresses {
let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x52]); let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x52]);
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 52); let mut ipv4_addr = IpAddress::v4(192, 168, 1, 52);
let mut ipv6_addr = None; let mut ipv6_addr = None;
if let Ok(cfg) = config::Config::new() {
if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) { if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) {
hardware_addr = addr; hardware_addr = addr;
} }
@ -38,7 +37,6 @@ pub fn get_adresses() -> NetAddresses {
if let Ok(Ok(addr)) = cfg.read_str("ip6").map(|s| s.parse()) { if let Ok(Ok(addr)) = cfg.read_str("ip6").map(|s| s.parse()) {
ipv6_addr = Some(addr); ipv6_addr = Some(addr);
} }
}
let ipv6_ll_addr = IpAddress::v6( let ipv6_ll_addr = IpAddress::v6(
0xfe80, 0x0000, 0x0000, 0x0000, 0xfe80, 0x0000, 0x0000, 0x0000,