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
72 changes: 71 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,73 @@ 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 = start_sector.parse::<usize>()?;
for index in 0..header.chunks {
let label_sparse = format!("{label}_{index}");
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 start_offset = start_sector + offset;
match chunk.chunk_type {
ChunkType::Raw => {
firehose_program_storage(
channel,
&mut buf,
&label_sparse,
num_sectors,
phys_part_idx,
start_offset.to_string().as_str(),
)?;
}
ChunkType::Fill => {
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[..]);
Copy link
Contributor

Choose a reason for hiding this comment

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

We do something roughly equivalent inside program_storage already, so all this can simplify this branch to passing a &mut &[0u8][..] as an argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not the same because the fill value is a 4 bytes that you get from the sparse file and you repeat for N times. If you set &mut &[0u8][..] you will get zero repeated for N times that not respect the sparse format.

Giving a let mut fill_value = [1, 2, 3, 4]; read from files and an let out_size = 25 ( N=5 ) you shall get something like that

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

It can sure be improved but I decided to keep it simple and not modify also the firehose_program_storage function.

If you have some hint on how to handle it better I can sure improve it.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

fill_vec.iter().repeat(fill_value).take(out_size/4);

since the value will likely be the same, we can perhaps cache fill_value and check if it differs on each found Fill chunk, and only re-assign the vector if we need to, taking away some overhead

}

firehose_program_storage(
channel,
&mut &fill_vec[..],
&label_sparse,
num_sectors,
phys_part_idx,
start_offset.to_string().as_str(),
)?;
}
ChunkType::DontCare => {
// Don't Care, skip
}
ChunkType::Crc32 => {
// Not supported, on qcom tools is ignored, seek if present
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 += out_size;
}
return Ok(());
}

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