summaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2010-06-10 17:55:02 -0700
committerRandall Spangler <rspangler@chromium.org>2010-06-10 17:55:02 -0700
commit6a97b3e2a1bee35bf3c00f2fb0faafde4aaab9e2 (patch)
tree905d6442153a9c9bcaee12a7f37ed2a476e94b83 /host
parentd55c64537245abca67a66fde5874b7f4a6cdc556 (diff)
downloadvboot-6a97b3e2a1bee35bf3c00f2fb0faafde4aaab9e2.tar.gz
Add vbutil_keyblock
Review URL: http://codereview.chromium.org/2748008
Diffstat (limited to 'host')
-rw-r--r--host/include/host_misc.h6
-rw-r--r--host/lib/host_key.c41
-rw-r--r--host/lib/host_misc.c19
-rw-r--r--host/linktest/main.c1
4 files changed, 38 insertions, 29 deletions
diff --git a/host/include/host_misc.h b/host/include/host_misc.h
index 7e66c73c..49afa299 100644
--- a/host/include/host_misc.h
+++ b/host/include/host_misc.h
@@ -22,4 +22,10 @@
uint8_t* ReadFile(const char* filename, uint64_t* size);
+/* Writes [size] bytes of [data] to [filename].
+ *
+ * Returns 0 if success, 1 if error. */
+int WriteFile(const char* filename, const void *data, uint64_t size);
+
+
#endif /* VBOOT_REFERENCE_HOST_MISC_H_ */
diff --git a/host/lib/host_key.c b/host/lib/host_key.c
index 388a2d4e..c5f49d31 100644
--- a/host/lib/host_key.c
+++ b/host/lib/host_key.c
@@ -187,37 +187,20 @@ VbPublicKey* PublicKeyRead(const char* filename) {
int PublicKeyWrite(const char* filename, const VbPublicKey* key) {
- VbPublicKey* kcopy = NULL;
- FILE* f = NULL;
- int rv = 1;
+ VbPublicKey* kcopy;
+ int rv;
- do {
- f = fopen(filename, "wb");
- if (!f) {
- debug("PublicKeyWrite() unable to open file %s\n", filename);
- break;
- }
-
- /* Copy the key, so its data is contiguous with the header */
- kcopy = PublicKeyAlloc(key->key_size, 0, 0);
- if (!kcopy || 0 != PublicKeyCopy(kcopy, key))
- break;
-
- if (1 != fwrite(kcopy, kcopy->key_offset + kcopy->key_size, 1, f))
- break;
-
- /* Success */
- rv = 0;
-
- } while(0);
-
- if (kcopy)
+ /* Copy the key, so its data is contiguous with the header */
+ kcopy = PublicKeyAlloc(key->key_size, 0, 0);
+ if (!kcopy)
+ return 1;
+ if (0 != PublicKeyCopy(kcopy, key)) {
Free(kcopy);
- if (f)
- fclose(f);
-
- if (0 != rv)
- unlink(filename); /* Delete any partial file */
+ return 1;
+ }
+ /* Write the copy, then free it */
+ rv = WriteFile(filename, kcopy, kcopy->key_offset + kcopy->key_size);
+ Free(kcopy);
return rv;
}
diff --git a/host/lib/host_misc.c b/host/lib/host_misc.c
index f24bf40d..2620df7a 100644
--- a/host/lib/host_misc.c
+++ b/host/lib/host_misc.c
@@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include "host_common.h"
@@ -47,3 +48,21 @@ uint8_t* ReadFile(const char* filename, uint64_t* size) {
fclose(f);
return buf;
}
+
+
+int WriteFile(const char* filename, const void *data, uint64_t size) {
+ FILE *f = fopen(filename, "wb");
+ if (!f) {
+ debug("Unable to open file %s\n", filename);
+ return 1;
+ }
+
+ if (1 != fwrite(data, size, 1, f)) {
+ debug("Unable to write to file %s\n", filename);
+ fclose(f);
+ unlink(filename); /* Delete any partial file */
+ }
+
+ fclose(f);
+ return 0;
+}
diff --git a/host/linktest/main.c b/host/linktest/main.c
index 767a9025..f1030ce7 100644
--- a/host/linktest/main.c
+++ b/host/linktest/main.c
@@ -16,6 +16,7 @@ int main(void)
/* host_misc.h */
ReadFile(0, 0);
+ WriteFile(0, 0, 0);
/* host_signature.h */
SignatureInit(0, 0, 0, 0);