summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward O'Callaghan <quasisec@google.com>2022-02-17 11:26:43 +1100
committerCommit Bot <commit-bot@chromium.org>2022-03-03 12:33:53 +0000
commit065008678e3cd2567c3dc44ecaddf0bcad226c81 (patch)
tree548ec51a60a381101966b870bc676fa3d68cf4b7
parentd0e5d8bc0ba6a5ac0ddff70f38adf31bfc082e49 (diff)
downloadvboot-065008678e3cd2567c3dc44ecaddf0bcad226c81.tar.gz
vboot_ref/cgpt: Move mkdir out of flashrom worker func
Small refactor to make it easier to more towards libflashrom. This makes the call site responsible for setting up the path for ReadNorFlash(). BUG=b:207808292,b:220079643 BRANCH=none TEST=`make cgpt` Signed-off-by: Edward O'Callaghan <quasisec@google.com> Change-Id: I15fc57e5afca6206b60df92616fd4cdce63b03e3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3469738 Tested-by: Edward O'Callaghan <quasisec@chromium.org> Auto-Submit: Edward O'Callaghan <quasisec@chromium.org> Reviewed-by: Sam McNally <sammc@chromium.org> Commit-Queue: Edward O'Callaghan <quasisec@chromium.org>
-rw-r--r--cgpt/cgpt_find.c6
-rw-r--r--cgpt/cgpt_nor.c16
-rw-r--r--cgpt/cgpt_nor.h6
-rw-r--r--cgpt/cgpt_wrapper.c6
4 files changed, 16 insertions, 18 deletions
diff --git a/cgpt/cgpt_find.c b/cgpt/cgpt_find.c
index 8046aad3..f50908fe 100644
--- a/cgpt/cgpt_find.c
+++ b/cgpt/cgpt_find.c
@@ -243,8 +243,14 @@ static int scan_spi_gpt(CgptFindParams *params) {
goto cleanup;
}
}
+ // Create a temp dir to work in.
+ if (mkdtemp(temp_dir) == NULL) {
+ perror("Cannot create a temporary directory.\n");
+ goto cleanup;
+ }
if (ReadNorFlash(temp_dir) != 0) {
perror("ReadNorFlash");
+ RemoveDir(temp_dir);
goto cleanup;
}
char nor_file[64];
diff --git a/cgpt/cgpt_nor.c b/cgpt/cgpt_nor.c
index 76fefe1e..e96ee274 100644
--- a/cgpt/cgpt_nor.c
+++ b/cgpt/cgpt_nor.c
@@ -203,20 +203,11 @@ int RemoveDir(const char *dir) {
#define FLASHROM_RW_GPT_SEC "RW_GPT_SECONDARY:rw_gpt_2"
#define FLASHROM_RW_GPT "RW_GPT:rw_gpt"
-// Read RW_GPT from NOR flash to "rw_gpt" in a temp dir |temp_dir_template|.
-// |temp_dir_template| is passed to mkdtemp() so it must satisfy all
-// requirements by mkdtemp.
+// Read RW_GPT from NOR flash to "rw_gpt" in a dir.
// TODO(b:184812319): Replace this function with flashrom_read.
-int ReadNorFlash(char *temp_dir_template) {
+int ReadNorFlash(const char *dir) {
int ret = 0;
- // Create a temp dir to work in.
- ret++;
- if (mkdtemp(temp_dir_template) == NULL) {
- Error("Cannot create a temporary directory.\n");
- return ret;
- }
-
// Read RW_GPT section from NOR flash to "rw_gpt".
ret++;
@@ -225,7 +216,7 @@ int ReadNorFlash(char *temp_dir_template) {
Error("Cannot get current directory.\n");
return ret;
}
- if (chdir(temp_dir_template) < 0) {
+ if (chdir(dir) < 0) {
Error("Cannot change directory.\n");
goto out_free;
}
@@ -234,7 +225,6 @@ int ReadNorFlash(char *temp_dir_template) {
// output.
if (subprocess_run(argv, &subprocess_null, &subprocess_null, NULL) != 0) {
Error("Cannot exec flashrom to read from RW_GPT section.\n");
- RemoveDir(temp_dir_template);
} else {
ret = 0;
}
diff --git a/cgpt/cgpt_nor.h b/cgpt/cgpt_nor.h
index 47460895..93e9ddee 100644
--- a/cgpt/cgpt_nor.h
+++ b/cgpt/cgpt_nor.h
@@ -23,10 +23,8 @@ int ForkExecL(const char *cwd, const char *cmd, ...);
// Exec "rm" to remove |dir|.
int RemoveDir(const char *dir);
-// Read RW_GPT from NOR flash to "rw_gpt" in a temp dir |temp_dir_template|.
-// |temp_dir_template| is passed to mkdtemp() so it must satisfy all
-// requirements by mkdtemp().
-int ReadNorFlash(char *temp_dir_template);
+// Read RW_GPT from NOR flash to "rw_gpt" in dir.
+int ReadNorFlash(const char *dir);
// Write "rw_gpt" back to NOR flash. We write the file in two parts for safety.
int WriteNorFlash(const char *dir);
diff --git a/cgpt/cgpt_wrapper.c b/cgpt/cgpt_wrapper.c
index d26682dd..17336c15 100644
--- a/cgpt/cgpt_wrapper.c
+++ b/cgpt/cgpt_wrapper.c
@@ -82,9 +82,13 @@ static int wrap_cgpt(int argc,
// Create a temp dir to work in.
ret++;
char temp_dir[] = "/tmp/cgpt_wrapper.XXXXXX";
- if (ReadNorFlash(temp_dir) != 0) {
+ if (mkdtemp(temp_dir_template) == NULL) {
+ Error("Cannot create a temporary directory.\n");
return ret;
}
+ if (ReadNorFlash(temp_dir) != 0) {
+ goto cleanup;
+ }
char rw_gpt_path[PATH_MAX];
if (snprintf(rw_gpt_path, sizeof(rw_gpt_path), "%s/rw_gpt", temp_dir) < 0) {
goto cleanup;