-
Notifications
You must be signed in to change notification settings - Fork 9
cli: added support to sparse images flashing #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gfabiano
wants to merge
1
commit into
qualcomm:main
Choose a base branch
from
gfabiano:sparse_image_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
|
|
@@ -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, | ||
|
|
@@ -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[..]); | ||
| } | ||
|
|
||
| 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)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| ))?; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 argumentThere was a problem hiding this comment.
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 anlet 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_storagefunction.If you have some hint on how to handle it better I can sure improve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
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