OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Host functions for verified boot. | 5 * Host functions for verified boot. |
6 */ | 6 */ |
7 | 7 |
8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ | 8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ |
9 | 9 |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
14 #include "host_common.h" | 14 #include "host_common.h" |
15 | 15 |
16 #include "cryptolib.h" | 16 #include "cryptolib.h" |
17 #include "utility.h" | 17 #include "utility.h" |
18 #include "vboot_common.h" | 18 #include "vboot_common.h" |
19 | 19 |
20 | 20 |
21 uint8_t* ReadFile(const char* filename, uint64_t* size) { | 21 uint8_t* ReadFile(const char* filename, uint64_t* size) { |
22 FILE* f; | 22 FILE* f; |
23 uint8_t* buf; | 23 uint8_t* buf; |
24 | 24 |
25 f = fopen(filename, "rb"); | 25 f = fopen(filename, "rb"); |
26 if (!f) { | 26 if (!f) { |
27 debug("Unable to open file %s\n", filename); | 27 VBDEBUG(("Unable to open file %s\n", filename)); |
28 return NULL; | 28 return NULL; |
29 } | 29 } |
30 | 30 |
31 fseek(f, 0, SEEK_END); | 31 fseek(f, 0, SEEK_END); |
32 *size = ftell(f); | 32 *size = ftell(f); |
33 rewind(f); | 33 rewind(f); |
34 | 34 |
35 buf = Malloc(*size); | 35 buf = Malloc(*size); |
36 if (!buf) { | 36 if (!buf) { |
37 fclose(f); | 37 fclose(f); |
38 return NULL; | 38 return NULL; |
39 } | 39 } |
40 | 40 |
41 if(1 != fread(buf, *size, 1, f)) { | 41 if(1 != fread(buf, *size, 1, f)) { |
42 debug("Unable to read from file %s\n", filename); | 42 VBDEBUG(("Unable to read from file %s\n", filename)); |
43 fclose(f); | 43 fclose(f); |
44 Free(buf); | 44 Free(buf); |
45 return NULL; | 45 return NULL; |
46 } | 46 } |
47 | 47 |
48 fclose(f); | 48 fclose(f); |
49 return buf; | 49 return buf; |
50 } | 50 } |
51 | 51 |
52 | 52 |
53 int WriteFile(const char* filename, const void *data, uint64_t size) { | 53 int WriteFile(const char* filename, const void *data, uint64_t size) { |
54 FILE *f = fopen(filename, "wb"); | 54 FILE *f = fopen(filename, "wb"); |
55 if (!f) { | 55 if (!f) { |
56 debug("Unable to open file %s\n", filename); | 56 VBDEBUG(("Unable to open file %s\n", filename)); |
57 return 1; | 57 return 1; |
58 } | 58 } |
59 | 59 |
60 if (1 != fwrite(data, size, 1, f)) { | 60 if (1 != fwrite(data, size, 1, f)) { |
61 debug("Unable to write to file %s\n", filename); | 61 VBDEBUG(("Unable to write to file %s\n", filename)); |
62 fclose(f); | 62 fclose(f); |
63 unlink(filename); /* Delete any partial file */ | 63 unlink(filename); /* Delete any partial file */ |
64 } | 64 } |
65 | 65 |
66 fclose(f); | 66 fclose(f); |
67 return 0; | 67 return 0; |
68 } | 68 } |
OLD | NEW |