-
Notifications
You must be signed in to change notification settings - Fork 89
Implement sparse image support #30
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
Draft
obbardc
wants to merge
1
commit into
linux-msm:master
Choose a base branch
from
obbardc:wip/obbardc/sparse
base: master
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.
Draft
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
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
#include <unistd.h> | ||
#include <libxml/parser.h> | ||
#include <libxml/tree.h> | ||
#include <sparse/sparse.h> | ||
#include "qdl.h" | ||
#include "ufs.h" | ||
|
||
|
@@ -323,6 +324,68 @@ static int firehose_erase(struct qdl_device *qdl, struct program *program) | |
return ret; | ||
} | ||
|
||
|
||
struct sparse_cb_info_t { | ||
struct qdl_device *qdl; | ||
struct program *program; | ||
void *payload; | ||
size_t payload_len; | ||
size_t write_count; | ||
size_t total_write_count; | ||
}; | ||
|
||
static int firehose_program_sparse(void *priv, const void *data, size_t len, unsigned int block, unsigned int nr_blocks) | ||
{ | ||
int left; | ||
struct sparse_cb_info_t *priv_info = (struct sparse_cb_info_t *)priv; | ||
size_t chunk_size; | ||
size_t data_write_count = 0; | ||
int n; | ||
char *p_data = (char *)data; | ||
int ret = 0; | ||
|
||
// TODO progress report using block / nr_blocks ? | ||
|
||
left = (priv_info->payload_len + len) / priv_info->program->sector_size; | ||
while (left > 0) { | ||
chunk_size = MIN(max_payload_size / priv_info->program->sector_size, left); | ||
|
||
n = chunk_size * priv_info->program->sector_size - priv_info->payload_len; | ||
memcpy(priv_info->payload + priv_info->payload_len, p_data + data_write_count, n); | ||
data_write_count += n; | ||
|
||
priv_info->payload_len += n; | ||
if (priv_info->payload_len < max_payload_size && (priv_info->write_count + priv_info->payload_len) < priv_info->total_write_count) { | ||
if (chunk_size < left) | ||
continue; | ||
else | ||
break; | ||
} | ||
|
||
if (priv_info->payload_len < max_payload_size) | ||
memset(priv_info->payload + n, 0, max_payload_size - n); | ||
|
||
n = qdl_write(priv_info->qdl, priv_info->payload, chunk_size * priv_info->program->sector_size); | ||
if (n < 0) { | ||
err(1, "failed to write"); | ||
ret = -1; | ||
} | ||
|
||
if (n != chunk_size * priv_info->program->sector_size) { | ||
err(1, "failed to write full sector"); | ||
ret = -1; | ||
} | ||
|
||
priv_info->write_count += n; | ||
|
||
priv_info->payload_len = 0U; | ||
|
||
left -= chunk_size; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
static int firehose_program(struct qdl_device *qdl, struct program *program, int fd) | ||
{ | ||
unsigned num_sectors; | ||
|
@@ -337,14 +400,28 @@ static int firehose_program(struct qdl_device *qdl, struct program *program, int | |
int left; | ||
int ret; | ||
int n; | ||
struct sparse_file *s_file = NULL; | ||
struct sparse_cb_info_t sparse_cb_info; | ||
|
||
num_sectors = program->num_sectors; | ||
|
||
ret = fstat(fd, &sb); | ||
if (ret < 0) | ||
err(1, "failed to stat \"%s\"\n", program->filename); | ||
|
||
num_sectors = (sb.st_size + program->sector_size - 1) / program->sector_size; | ||
lseek(fd, program->file_offset * program->sector_size, SEEK_SET); | ||
|
||
if (program->is_sparse) { | ||
// TODO check last parameter | ||
s_file = sparse_file_import(fd, true, false); | ||
|
||
// TODO check last two parameters | ||
sparse_cb_info.total_write_count = sparse_file_len(s_file, true, false); | ||
|
||
num_sectors = (sparse_cb_info.total_write_count + program->sector_size - 1) / program->sector_size; | ||
} else { | ||
num_sectors = (sb.st_size + program->sector_size - 1) / program->sector_size; | ||
} | ||
|
||
if (program->num_sectors && num_sectors > program->num_sectors) { | ||
fprintf(stderr, "[PROGRAM] %s truncated to %d\n", | ||
|
@@ -388,26 +465,39 @@ static int firehose_program(struct qdl_device *qdl, struct program *program, int | |
|
||
t0 = time(NULL); | ||
|
||
lseek(fd, (off_t) program->file_offset * program->sector_size, SEEK_SET); | ||
left = num_sectors; | ||
while (left > 0) { | ||
chunk_size = MIN(max_payload_size / program->sector_size, left); | ||
if (program->is_sparse) { | ||
sparse_cb_info.payload = buf; | ||
sparse_cb_info.payload_len = 0U; | ||
sparse_cb_info.write_count = 0U; | ||
sparse_cb_info.program = program; | ||
sparse_cb_info.qdl = qdl; | ||
|
||
n = read(fd, buf, chunk_size * program->sector_size); | ||
if (n < 0) | ||
err(1, "failed to read"); | ||
// TODO originally used sparse_stream_callback | ||
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. Currently it times out. |
||
//ret = sparse_stream_callback(s_file, firehose_program_sparse, &sparse_cb_info); | ||
ret = sparse_file_foreach_chunk(s_file, true, false, firehose_program_sparse, &sparse_cb_info); | ||
|
||
if (n < max_payload_size) | ||
memset(buf + n, 0, max_payload_size - n); | ||
sparse_file_destroy(s_file); | ||
} else { | ||
left = num_sectors; | ||
while (left > 0) { | ||
chunk_size = MIN(max_payload_size / program->sector_size, left); | ||
|
||
n = qdl_write(qdl, buf, chunk_size * program->sector_size); | ||
if (n < 0) | ||
err(1, "failed to write"); | ||
n = read(fd, buf, chunk_size * program->sector_size); | ||
if (n < 0) | ||
err(1, "failed to read"); | ||
|
||
if (n != chunk_size * program->sector_size) | ||
err(1, "failed to write full sector"); | ||
if (n < max_payload_size) | ||
memset(buf + n, 0, max_payload_size - n); | ||
|
||
left -= chunk_size; | ||
n = qdl_write(qdl, buf, chunk_size * program->sector_size); | ||
if (n < 0) | ||
err(1, "failed to write"); | ||
|
||
if (n != chunk_size * program->sector_size) | ||
err(1, "failed to write full sector"); | ||
|
||
left -= chunk_size; | ||
} | ||
} | ||
|
||
t = time(NULL) - t0; | ||
|
@@ -427,6 +517,8 @@ static int firehose_program(struct qdl_device *qdl, struct program *program, int | |
|
||
out: | ||
xmlFreeDoc(doc); | ||
// Not present before. | ||
//free(buf); | ||
return ret; | ||
} | ||
|
||
|
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
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.
Is this part of a commonly available package in most distributions?
What can be done about the "x86_64"-part, I'm exclusively running qdl on my ARM64 laptop.
I was under the impression that the sparse format essentially is a form of run-length-encoding to avoid encoding the empty regions in the image. Would it be possible to implement this in qdl, instead of relying on a 3rd party library?
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.
@andersson I don't have the bandwidth to make this modification right now.
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.
@lumag I tried to add sparse support to qdl, but lost bandwidth. Are you interested in sparse support ?