Skip to content
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

Add basic CBFS command to list and read files #3

Merged
merged 3 commits into from
Apr 19, 2018

Conversation

flammit
Copy link
Contributor

@flammit flammit commented Mar 23, 2018

Tested on qemu and x230.

@@ -34,4 +34,11 @@ memcpy_width(
mem_op_t op
);

extern void
copy_physical(
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you remap /dev/mem each time rather than mapping the flash rom region once and then memcpy from the mapped region?

This isn't a performance critical piece, so this doesn't really matter...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry just laziness - will fix this up on the next commit along with @kakaroto's comments

util.c Outdated

memcpy(dest, buf, len);

munmap((uint8_t *)buf, len);
Copy link
Owner

Choose a reason for hiding this comment

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

This isn't guaranteed to unmap all of the mapped memory. If phys_addr isn't page aligned or len is not a multiple of page length, map_physical() will fix them up and map a larger amount of memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch - I'll fix this up.

cbfs.c Outdated
"\n"
"-h | -? | --help This help\n"
"-v | --verbose Increase verbosity\n"
"-r | --read file Read the CBFS filed and dump to stdout\n"

Choose a reason for hiding this comment

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

typo s/filed/file/

cbfs.c Outdated
"-v | --verbose Increase verbosity\n"
"-r | --read file Read the CBFS filed and dump to stdout\n"
"-l | --list List the files of specified type in the CBFS\n"
"-t | --type 50 Filter to CBFS file type (hex, default is raw=50)\n"

Choose a reason for hiding this comment

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

All types are accepted if -t is not specified (!do_type || (do_type && file.type == type)) so there is technically no default value here.

{
fprintf(stderr, "%s", usage);
return EXIT_FAILURE;
}

Choose a reason for hiding this comment

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

I don't know what coding style you prefer, but I would say keep the variable declarations grouped, and have this if (argc <= 1) after the filename declaration... or maybe it's not even needed actually, but instead you need to after the while getopt :
if (!do_list && !do_read) { usage }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

{
fprintf(stderr, "%s: Excess arguments?\n", prog_name);
return EXIT_FAILURE;
}

Choose a reason for hiding this comment

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

As commented above, this might be a good place to ensure that at least a -l or a -r was passed as argument, otherwise you read the cbfs file with no action to perform.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

cbfs.c Outdated

uint64_t header_off;
if (header_delta < 0) {
header_off = end - ((uint64_t)(-header_delta));

Choose a reason for hiding this comment

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

wouldn't this just be : header_off = header_delta ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cbfs.c Outdated
if (file_data == NULL) {
fprintf(stderr, "Failed to allocate memory for file data: length=%d\n",
file.len);
return EXIT_FAILURE;

Choose a reason for hiding this comment

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

Not particularly critical since you exit right here, but I'd add free(name)

if (rc <= 0) {
fprintf(stderr, "Failed to write file to stdout: %s\n",
strerror(errno));
return EXIT_FAILURE;

Choose a reason for hiding this comment

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

Not particularly critical since you exit right here, but I'd add free(name); free(flie_data)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore

cbfs.c Outdated
copy_physical(file_off, file.len, file_data);

for (size_t offset = 0 ; offset < file.len ; ) {
const ssize_t rc = write(

Choose a reason for hiding this comment

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

I'm not entirely sure here why you would loop and write the file to stdout unless you expect the write to get interrupted by a signal. I would just do a write and make sure that rc == file.len.
Also, I agree with what @osresearch said, it's better to mmap the entire (header_off <-> end) region once and work on it rather than do here a malloc, mmap, memcpy, munmap, write, free, instead of just write. You'd need to check that 'file_off + file.len < end' (that's a sanity check I would add even if you keep the copy_physical stuff).

Copy link
Owner

Choose a reason for hiding this comment

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

Pipes often cause this problem with writes (and reads), so looping is unfortunately necessary.

@flammit
Copy link
Contributor Author

flammit commented Apr 10, 2018

Any other comments here? @osresearch would you like me to change the map_physical interface to make it easier to munmap later and clean up all usages?

@tlaurion
Copy link

tlaurion commented Apr 17, 2018

Nope! Can this be merged so linuxboot/heads#357 and linuxboot/heads#376 can move forward? :)

@osresearch osresearch merged commit 998a146 into osresearch:master Apr 19, 2018
@osresearch
Copy link
Owner

Testing with cbfs to read payload on qemu and it looks like it works.

flashtool still goes into an infinite loop, so we can't test the writing of the cbfs on qemu yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants