summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;