From 6a97b3e2a1bee35bf3c00f2fb0faafde4aaab9e2 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Thu, 10 Jun 2010 17:55:02 -0700 Subject: Add vbutil_keyblock Review URL: http://codereview.chromium.org/2748008 --- host/include/host_misc.h | 6 ++++++ host/lib/host_key.c | 41 ++++++++++++----------------------------- host/lib/host_misc.c | 19 +++++++++++++++++++ host/linktest/main.c | 1 + 4 files changed, 38 insertions(+), 29 deletions(-) (limited to 'host') 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 #include +#include #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); -- cgit v1.2.1