Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ publish = false # TODO
maintenance = { status = "actively-developed" }

[dependencies]
android-sparse-image = "0.1.3"
anyhow = "1.0.89"
clap = { version = "4.5.18", features = ["derive"] }
clap-num = "1.1.1"
Expand Down
148 changes: 147 additions & 1 deletion cli/src/programfile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
use anyhow::bail;
use anyhow::{Ok, bail};
use indexmap::IndexMap;
use std::{
fs,
Expand All @@ -14,6 +14,10 @@ use qdl::{
types::QdlChan,
};

use android_sparse_image::{
ChunkHeader, ChunkHeaderBytes, ChunkType, FILE_HEADER_BYTES_LEN, FileHeader, FileHeaderBytes,
};

fn parse_read_cmd<T: Read + Write + QdlChan>(
channel: &mut T,
out_dir: &Path,
Expand Down Expand Up @@ -141,7 +145,149 @@ fn parse_program_cmd<T: Read + Write + QdlChan>(
}
}

let sparse = attrs
.get("sparse")
.unwrap_or(&"false".to_owned())
.parse::<bool>()
.unwrap_or(false);

let mut buf = fs::File::open(file_path)?;

if sparse {
let mut header_bytes: FileHeaderBytes = [0; FILE_HEADER_BYTES_LEN];
buf.read_exact(&mut header_bytes)?;
let header = FileHeader::from_bytes(&header_bytes)?;

let mut offset: usize = 0;
let start_sector_base = start_sector.parse::<usize>()?;

let mut agg_data = Vec::new();
let mut agg_start_sector = 0;
let mut agg_num_sectors = 0;

for index in 0..header.chunks {
let mut chunk_bytes = ChunkHeaderBytes::default();
buf.read_exact(&mut chunk_bytes)?;
let chunk = ChunkHeader::from_bytes(&chunk_bytes)?;

let out_size = chunk.out_size(&header);
let num_sectors = out_size / sector_size;
let current_start_sector = start_sector_base + offset;

match chunk.chunk_type {
ChunkType::Raw | ChunkType::Fill => {
let is_large = out_size > channel.fh_config().send_buffer_size;
let is_contiguous = agg_num_sectors > 0
&& (agg_start_sector + agg_num_sectors == current_start_sector);
let would_overflow =
agg_data.len() + out_size > channel.fh_config().send_buffer_size;

if !is_contiguous || would_overflow || is_large {
if agg_num_sectors > 0 {
firehose_program_storage(
channel,
&mut &agg_data[..],
&format!("{label}_merged"),
agg_num_sectors,
phys_part_idx,
agg_start_sector.to_string().as_str(),
)?;
agg_data.clear();
agg_num_sectors = 0;
}
}

if is_large {
if chunk.chunk_type == ChunkType::Raw {
firehose_program_storage(
channel,
&mut buf,
&format!("{label}_{index}"),
num_sectors,
phys_part_idx,
current_start_sector.to_string().as_str(),
)?;
} else {
let mut fill_value = [0u8; 4];
buf.read_exact(&mut fill_value)?;

let mut fill_vec = Vec::<u8>::with_capacity(out_size);
for _ in 0..out_size / 4 {
fill_vec.extend_from_slice(&fill_value[..]);
}

firehose_program_storage(
channel,
&mut &fill_vec[..],
&format!("{label}_{index}"),
num_sectors,
phys_part_idx,
current_start_sector.to_string().as_str(),
)?;
}
} else {
if agg_num_sectors == 0 {
agg_start_sector = current_start_sector;
}
if chunk.chunk_type == ChunkType::Raw {
let mut tmp = vec![0u8; out_size];
buf.read_exact(&mut tmp)?;
agg_data.extend(tmp);
} else {
let mut fill_value = [0u8; 4];
buf.read_exact(&mut fill_value)?;
for _ in 0..out_size / 4 {
agg_data.extend_from_slice(&fill_value[..]);
}
}
agg_num_sectors += num_sectors;
}
}
ChunkType::DontCare => {
// Fill gaps up to 256KB
let is_small_gap = out_size <= 256 * 1024;
let would_overflow =
agg_data.len() + out_size > channel.fh_config().send_buffer_size;

if agg_num_sectors > 0 && is_small_gap && !would_overflow {
// Fill gap with zeros to keep aggregation going
agg_data.resize(agg_data.len() + out_size, 0);
agg_num_sectors += num_sectors;
} else if agg_num_sectors > 0 {
firehose_program_storage(
channel,
&mut &agg_data[..],
&format!("{label}_merged"),
agg_num_sectors,
phys_part_idx,
agg_start_sector.to_string().as_str(),
)?;
agg_data.clear();
agg_num_sectors = 0;
}
}
ChunkType::Crc32 => {
buf.seek_relative(4)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is the CRC valid on your images?

}
}

offset += num_sectors;
}

if agg_num_sectors > 0 {
firehose_program_storage(
channel,
&mut &agg_data[..],
&format!("{label}_merged"),
agg_num_sectors,
phys_part_idx,
agg_start_sector.to_string().as_str(),
)?;
}

return Ok(());
}

buf.seek(SeekFrom::Current(
sector_size as i64 * file_sector_offset as i64,
))?;
Expand Down