mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-27 15:12:36 +02:00
flashrom.c: Move do_*() helpers into cli_classic.c
These helpers are only used by the CLI logic and so we localise them here to move towards cli_classic being a pure libflashrom user. BUG=b:208132085 TEST=`make` Change-Id: If1112155e2421e0178fd73f847cbb80868387433 Signed-off-by: Edward O'Callaghan <quasisec@google.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/60070 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
This commit is contained in:
parent
103b139cf7
commit
55aa056c74
@ -154,6 +154,103 @@ static int parse_wp_range(unsigned int *start, unsigned int *len)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int do_read(struct flashctx *const flash, const char *const filename)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
unsigned long size = flash->chip->total_size * 1024;
|
||||||
|
unsigned char *buf = calloc(size, sizeof(unsigned char));
|
||||||
|
if (!buf) {
|
||||||
|
msg_gerr("Memory allocation failed!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = flashrom_image_read(flash, buf, size);
|
||||||
|
if (ret > 0)
|
||||||
|
goto free_out;
|
||||||
|
|
||||||
|
if (write_buf_to_include_args(flash, buf)) {
|
||||||
|
ret = 1;
|
||||||
|
goto free_out;
|
||||||
|
}
|
||||||
|
if (filename)
|
||||||
|
ret = write_buf_to_file(buf, size, filename);
|
||||||
|
|
||||||
|
free_out:
|
||||||
|
free(buf);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int do_extract(struct flashctx *const flash)
|
||||||
|
{
|
||||||
|
prepare_layout_for_extraction(flash);
|
||||||
|
return do_read(flash, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
|
||||||
|
{
|
||||||
|
const size_t flash_size = flash->chip->total_size * 1024;
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
|
uint8_t *const newcontents = malloc(flash_size);
|
||||||
|
uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
|
||||||
|
|
||||||
|
if (!newcontents || (referencefile && !refcontents)) {
|
||||||
|
msg_gerr("Out of memory!\n");
|
||||||
|
goto _free_ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read '-w' argument first... */
|
||||||
|
if (read_buf_from_file(newcontents, flash_size, filename))
|
||||||
|
goto _free_ret;
|
||||||
|
/*
|
||||||
|
* ... then update newcontents with contents from files provided to '-i'
|
||||||
|
* args if needed.
|
||||||
|
*/
|
||||||
|
if (read_buf_from_include_args(flash, newcontents))
|
||||||
|
goto _free_ret;
|
||||||
|
|
||||||
|
if (referencefile) {
|
||||||
|
if (read_buf_from_file(refcontents, flash_size, referencefile))
|
||||||
|
goto _free_ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
|
||||||
|
|
||||||
|
_free_ret:
|
||||||
|
free(refcontents);
|
||||||
|
free(newcontents);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int do_verify(struct flashctx *const flash, const char *const filename)
|
||||||
|
{
|
||||||
|
const size_t flash_size = flash->chip->total_size * 1024;
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
|
uint8_t *const newcontents = malloc(flash_size);
|
||||||
|
if (!newcontents) {
|
||||||
|
msg_gerr("Out of memory!\n");
|
||||||
|
goto _free_ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read '-v' argument first... */
|
||||||
|
if (read_buf_from_file(newcontents, flash_size, filename))
|
||||||
|
goto _free_ret;
|
||||||
|
/*
|
||||||
|
* ... then update newcontents with contents from files provided to '-i'
|
||||||
|
* args if needed.
|
||||||
|
*/
|
||||||
|
if (read_buf_from_include_args(flash, newcontents))
|
||||||
|
goto _free_ret;
|
||||||
|
|
||||||
|
ret = flashrom_image_verify(flash, newcontents, flash_size);
|
||||||
|
|
||||||
|
_free_ret:
|
||||||
|
free(newcontents);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const struct flashchip *chip = NULL;
|
const struct flashchip *chip = NULL;
|
||||||
|
4
flash.h
4
flash.h
@ -356,10 +356,6 @@ int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *
|
|||||||
int write_buf_to_include_args(const struct flashctx *const flash, unsigned char *buf);
|
int write_buf_to_include_args(const struct flashctx *const flash, unsigned char *buf);
|
||||||
int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it);
|
int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it);
|
||||||
void finalize_flash_access(struct flashctx *);
|
void finalize_flash_access(struct flashctx *);
|
||||||
int do_read(struct flashctx *, const char *filename);
|
|
||||||
int do_extract(struct flashctx *);
|
|
||||||
int do_write(struct flashctx *, const char *const filename, const char *const referencefile);
|
|
||||||
int do_verify(struct flashctx *, const char *const filename);
|
|
||||||
int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);
|
int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);
|
||||||
|
|
||||||
/* Something happened that shouldn't happen, but we can go on. */
|
/* Something happened that shouldn't happen, but we can go on. */
|
||||||
|
97
flashrom.c
97
flashrom.c
@ -2189,100 +2189,3 @@ _free_ret:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @} */ /* end flashrom-ops */
|
/** @} */ /* end flashrom-ops */
|
||||||
|
|
||||||
int do_read(struct flashctx *const flash, const char *const filename)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
unsigned long size = flash->chip->total_size * 1024;
|
|
||||||
unsigned char *buf = calloc(size, sizeof(unsigned char));
|
|
||||||
if (!buf) {
|
|
||||||
msg_gerr("Memory allocation failed!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = flashrom_image_read(flash, buf, size);
|
|
||||||
if (ret > 0)
|
|
||||||
goto free_out;
|
|
||||||
|
|
||||||
if (write_buf_to_include_args(flash, buf)) {
|
|
||||||
ret = 1;
|
|
||||||
goto free_out;
|
|
||||||
}
|
|
||||||
if (filename)
|
|
||||||
ret = write_buf_to_file(buf, size, filename);
|
|
||||||
|
|
||||||
free_out:
|
|
||||||
free(buf);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int do_extract(struct flashctx *const flash)
|
|
||||||
{
|
|
||||||
prepare_layout_for_extraction(flash);
|
|
||||||
return do_read(flash, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
|
|
||||||
{
|
|
||||||
const size_t flash_size = flash->chip->total_size * 1024;
|
|
||||||
int ret = 1;
|
|
||||||
|
|
||||||
uint8_t *const newcontents = malloc(flash_size);
|
|
||||||
uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
|
|
||||||
|
|
||||||
if (!newcontents || (referencefile && !refcontents)) {
|
|
||||||
msg_gerr("Out of memory!\n");
|
|
||||||
goto _free_ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read '-w' argument first... */
|
|
||||||
if (read_buf_from_file(newcontents, flash_size, filename))
|
|
||||||
goto _free_ret;
|
|
||||||
/*
|
|
||||||
* ... then update newcontents with contents from files provided to '-i'
|
|
||||||
* args if needed.
|
|
||||||
*/
|
|
||||||
if (read_buf_from_include_args(flash, newcontents))
|
|
||||||
goto _free_ret;
|
|
||||||
|
|
||||||
if (referencefile) {
|
|
||||||
if (read_buf_from_file(refcontents, flash_size, referencefile))
|
|
||||||
goto _free_ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
|
|
||||||
|
|
||||||
_free_ret:
|
|
||||||
free(refcontents);
|
|
||||||
free(newcontents);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int do_verify(struct flashctx *const flash, const char *const filename)
|
|
||||||
{
|
|
||||||
const size_t flash_size = flash->chip->total_size * 1024;
|
|
||||||
int ret = 1;
|
|
||||||
|
|
||||||
uint8_t *const newcontents = malloc(flash_size);
|
|
||||||
if (!newcontents) {
|
|
||||||
msg_gerr("Out of memory!\n");
|
|
||||||
goto _free_ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read '-v' argument first... */
|
|
||||||
if (read_buf_from_file(newcontents, flash_size, filename))
|
|
||||||
goto _free_ret;
|
|
||||||
/*
|
|
||||||
* ... then update newcontents with contents from files provided to '-i'
|
|
||||||
* args if needed.
|
|
||||||
*/
|
|
||||||
if (read_buf_from_include_args(flash, newcontents))
|
|
||||||
goto _free_ret;
|
|
||||||
|
|
||||||
ret = flashrom_image_verify(flash, newcontents, flash_size);
|
|
||||||
|
|
||||||
_free_ret:
|
|
||||||
free(newcontents);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user