use bytes::Bytes;
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Stdio;
use tokio::fs::File;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::Command;
use tokio::task::JoinHandle;
use tokio::sync::mpsc::{Receiver, Sender, channel};
use tracing::info;
use crate::LOG;
use crate::installer::{
InstallProgress, InstallationFeedback, InstallerError, Rollback, RollbackStatus,
};
use crate::persistent_store::PersistentStore;
use crate::uboot::{UBootConfig, UBootEnv};
use crate::update_source::{StreamError, StreamReceiver};
use crate::{
installer::Installer,
update_source::Update,
update_workflow::{self, UpdateError, WorkflowError},
};
#[derive(Clone, Default)]
pub enum SWUpdateInstallMode {
#[default]
Stream,
TemporaryCopy {
tmp_dir: PathBuf,
bufsize: usize,
},
}
#[derive(Clone)]
pub struct SWUpdateOptions {
pub install_mode: SWUpdateInstallMode,
pub run_postupdate: bool,
pub dry_run: bool,
pub swupdate_client_bin: String,
pub swupdate_ipc_socket: String,
pub swupdate_sw_mode: Option<(String, String)>,
pub uboot_env: UBootConfig,
}
impl Default for SWUpdateOptions {
fn default() -> Self {
Self {
install_mode: SWUpdateInstallMode::default(),
run_postupdate: true,
dry_run: false,
swupdate_client_bin: "/usr/bin/swupdate-client".to_string(),
swupdate_ipc_socket: "/run/swupdate/sockinstctrl".to_string(),
swupdate_sw_mode: None,
uboot_env: UBootConfig::default(),
}
}
}
impl SWUpdateOptions {
pub fn from_config(config: &HashMap<String, String>) -> Result<Self, WorkflowError> {
let use_streaming = update_workflow::get_config_as_bool(config, "swupdate_use_streaming")?;
Ok(Self {
install_mode: if use_streaming {
SWUpdateInstallMode::Stream
} else {
SWUpdateInstallMode::TemporaryCopy {
tmp_dir: update_workflow::get_config(config, "swupdate_tmp_dir")?.into(),
bufsize: update_workflow::get_config(config, "swupdate_bufsize")?
.parse()
.map_err(|_| {
WorkflowError::InvalidConfiguration(
"failed to parse swupdate_bufsize".to_string(),
)
})?,
}
},
run_postupdate: update_workflow::get_config_as_bool(config, "swupdate_run_postupdate")?,
dry_run: update_workflow::get_config_as_bool(config, "swupdate_dry_run")?,
swupdate_client_bin: update_workflow::get_config(config, "swupdate_client_bin")?,
swupdate_ipc_socket: update_workflow::get_config(config, "swupdate_ipc_socket")?,
swupdate_sw_mode: if config.contains_key("swupdate_software_set") {
Some((
update_workflow::get_config(config, "swupdate_software_set")?,
update_workflow::get_config(config, "swupdate_running_mode")?,
))
} else {
None
},
uboot_env: UBootConfig {
setenv_bin: update_workflow::get_config(config, "swupdate_setenv_bin")?,
printenv_bin: update_workflow::get_config(config, "swupdate_printenv_bin")?,
},
})
}
}
pub struct SWUpdate {
options: SWUpdateOptions,
}
impl SWUpdate {
#[allow(dead_code)]
pub fn new(options: SWUpdateOptions) -> Self {
Self { options }
}
pub async fn auto_rollback(&self, enabled: bool) -> Result<(), InstallerError> {
let mut uboot = UBootEnv::new(self.options.uboot_env.clone());
let v = if enabled { "1" } else { "0" };
uboot.save("upgrade_available", v).await.map_err(|e| {
InstallerError::RollbackFailed(format!("Failed to save upgrade_available flag: {}", e))
})?;
uboot.save("ustate", v).await.map_err(|e| {
InstallerError::RollbackFailed(format!("Failed to save ustate flag: {}", e))
})?;
Ok(())
}
}
impl Rollback for SWUpdate {
async fn rollback(&self) -> Result<RollbackStatus, InstallerError> {
let mut uboot = UBootEnv::new(self.options.uboot_env.clone());
self.auto_rollback(true).await?;
let max_bootcount = uboot
.load("bootlimit")
.await
.map_err(|e| {
InstallerError::RollbackFailed(format!("Failed to read bootlimit: {}", e))
})?
.ok_or(InstallerError::RollbackFailed(
"bootlimit not found".to_string(),
))?;
uboot.save("bootcount", &max_bootcount).await.map_err(|e| {
InstallerError::RollbackFailed(format!("Failed to save bootcount: {}", e))
})?;
Ok(RollbackStatus::RebootRequired)
}
}
async fn get_update_stream(
options: &SWUpdateOptions,
update: &impl Update,
) -> Result<StreamReceiver, UpdateError> {
let result = match &options.install_mode {
SWUpdateInstallMode::Stream => {
update.stream()?
}
SWUpdateInstallMode::TemporaryCopy { tmp_dir, bufsize } => {
tokio::fs::create_dir_all(tmp_dir).await?;
let update_files = update.save_to_disk(Some(PathBuf::from(tmp_dir))).await?;
let (tx, rx) = channel(1);
let bufsize: usize = *bufsize;
tokio::spawn(async move {
async {
for update_file in update_files {
let file = File::open(update_file.filename).await?;
let mut reader = BufReader::with_capacity(bufsize, file);
loop {
let len = {
let buffer = reader.fill_buf().await?;
tx.send(Ok(Bytes::copy_from_slice(buffer))).await?;
buffer.len()
};
if len == 0 {
break;
}
reader.consume(len);
}
}
Ok::<(), StreamError>(())
}
.await
.map_err(|e| {
tx.try_send(Err(e)).ok();
})
.ok();
});
rx
}
};
Ok(result)
}
impl Installer for SWUpdate {
fn install(&self, update: &impl Update) -> Result<impl InstallProgress, UpdateError> {
let (feedback_sender, feedback_receiver) = channel(1);
let options = self.options.clone();
let update = update.clone();
let client_handle = tokio::spawn(async move {
let update_stream = get_update_stream(&options, &update).await?;
let mut client_builder = SwupdateClientBuilder::new(
&options.swupdate_client_bin,
&options.swupdate_ipc_socket,
)
.update_stream(update_stream, update.size())
.feedback_stream(feedback_sender);
if options.run_postupdate {
client_builder = client_builder.run_postupdate();
}
if options.dry_run {
client_builder = client_builder.dry_run();
}
if let Some((software, mode)) = options.swupdate_sw_mode.as_ref() {
client_builder = client_builder.sw_mode(software, mode);
}
let mut client = client_builder.build();
client.run().await
});
Ok(SWUpdateProgress {
feedback_receiver,
client_handle: Some(client_handle),
})
}
}
struct SWUpdateProgress {
feedback_receiver: Receiver<InstallationFeedback>,
client_handle: Option<JoinHandle<Result<(), UpdateError>>>,
}
impl InstallProgress for SWUpdateProgress {
async fn next(&mut self) -> Result<Option<InstallationFeedback>, UpdateError> {
if let Some(msg) = self.feedback_receiver.recv().await {
return Ok(Some(msg));
}
if let Some(handle) = self.client_handle.take() {
handle.await.map_err(|e| {
UpdateError::InstallerError(InstallerError::InstallationFailed(format!(
"swupdate client failed: {}",
e
)))
})??;
}
Ok(None)
}
}
pub struct SwupdateClientBuilder {
binary: PathBuf,
flags: Vec<String>,
stdin_channel: Option<StreamReceiver>,
stdout_channel: Option<Sender<InstallationFeedback>>,
update_size: Option<u64>,
}
impl SwupdateClientBuilder {
pub fn new(binary: &str, ipc_socket: &str) -> Self {
Self {
binary: PathBuf::from(binary),
flags: vec!["-s".to_string(), ipc_socket.to_string()],
stdin_channel: None,
stdout_channel: None,
update_size: None,
}
}
pub fn update_stream(mut self, update: StreamReceiver, update_size: u64) -> Self {
self.stdin_channel = Some(update);
self.update_size = Some(update_size);
self
}
pub fn feedback_stream(mut self, feedback: Sender<InstallationFeedback>) -> Self {
self.flags.push("-v".to_string());
self.stdout_channel = Some(feedback);
self
}
pub fn run_postupdate(mut self) -> Self {
self.flags.push("-p".to_string());
self
}
pub fn dry_run(mut self) -> Self {
self.flags.push("-d".to_string());
self
}
pub fn sw_mode(mut self, software: &str, mode: &str) -> Self {
self.flags.push("-e".to_string());
self.flags.push(format!("{software},{mode}"));
self
}
pub fn build(self) -> SwupdateClient {
SwupdateClient {
binary: self.binary,
flags: self.flags,
stdin_channel: self.stdin_channel,
stdout_channel: self.stdout_channel,
update_size: self.update_size,
}
}
}
pub struct SwupdateClient {
binary: PathBuf,
flags: Vec<String>,
stdin_channel: Option<StreamReceiver>,
stdout_channel: Option<Sender<InstallationFeedback>>,
update_size: Option<u64>,
}
impl SwupdateClient {
pub async fn run(&mut self) -> Result<(), UpdateError> {
let mut cmd = Command::new(&self.binary);
cmd.env_clear().kill_on_drop(true).args(&self.flags);
if self.stdin_channel.is_some() {
cmd.stdin(Stdio::piped());
}
if self.stdout_channel.is_some() {
cmd.stdout(Stdio::piped());
}
let mut child = cmd.spawn()?;
let stdout_channel = self.stdout_channel.take();
let update_size = self.update_size;
let stdin_task = if let Some(mut stdin_channel) = self.stdin_channel.take() {
let stdout_channel = stdout_channel.clone();
let mut stdin = child.stdin.take().expect("child stdin should be piped");
Some(tokio::spawn(async move {
let mut bytes_received = 0usize;
while let Some(chunk) = stdin_channel.recv().await {
let chunk = chunk?;
bytes_received += chunk.len();
if let Some(stdout_channel) = &stdout_channel {
if bytes_received / (1024 * 1024 * 10)
> (bytes_received - chunk.len()) / (1024 * 1024 * 10)
{
stdout_channel
.send(InstallationFeedback {
status: "0".to_string(),
message: format!("Received {} bytes", bytes_received),
progress: if update_size.is_some() {
Some((bytes_received as u64, update_size.unwrap_or(0)))
} else {
None
},
})
.await
.map_err(|err| {
InstallerError::InstallationFailed(format!(
"failed to report bytes received: {}",
err
))
})?;
}
}
stdin.write_all(&chunk).await?;
}
Ok::<(), UpdateError>(())
}))
} else {
None
};
let stdout_task = if let Some(stdout_channel) = stdout_channel {
let stdout = child.stdout.take().expect("child stdout should be piped");
Some(tokio::spawn(async move {
let mut reader = BufReader::new(stdout);
let mut buffer = Vec::new();
loop {
buffer.clear();
let len = reader.read_until(b'\n', &mut buffer).await?;
if len == 0 {
break;
}
let msg = String::from_utf8_lossy(&buffer).to_string();
lazy_static! {
static ref STATUS_RE: regex::Regex =
regex::Regex::new(r"Status: (\d+) message: (.*)").unwrap();
}
if let Some(captures) = STATUS_RE.captures(&msg) {
let status = captures
.get(1)
.expect("regex should have a capture group 1")
.as_str();
let message = captures
.get(2)
.expect("regex should have a capture group 2")
.as_str();
stdout_channel
.send(InstallationFeedback {
status: status.to_string(),
message: message.to_string(),
progress: None,
})
.await
.map_err(|err| {
InstallerError::InstallationFailed(format!(
"pipe to swupdate client broken: {}",
err
))
})?;
} else {
info!(LOG, "swupdate client: {}", msg);
}
}
Ok::<(), UpdateError>(())
}))
} else {
None
};
if let Some(task) = stdin_task {
task.await.map_err(|e| {
UpdateError::InstallerError(InstallerError::InstallationFailed(format!(
"stdin task failed: {}",
e
)))
})??;
}
if let Some(task) = stdout_task {
task.await.map_err(|e| {
UpdateError::InstallerError(InstallerError::InstallationFailed(format!(
"stdout task failed: {}",
e
)))
})??;
}
let status = child.wait().await?;
if !status.success() {
return Err(UpdateError::InstallerError(
InstallerError::InstallationFailed(format!(
"swupdate client failed with exit code: {}",
status
)),
));
}
Ok(())
}
}