blob: d8f52970202ff83e4a9e920453c96d6ddc98fe61 [file] [log] [blame]
Randall Spanglerd55c6452010-06-10 12:43:51 -07001/* 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
3 * found in the LICENSE file.
4 *
5 * Host functions for verified boot.
6 */
7
8/* TODO: change all 'return 0', 'return 1' into meaningful return codes */
9
10#include <stdio.h>
11#include <stdlib.h>
Randall Spangler6a97b3e2010-06-10 17:55:02 -070012#include <unistd.h>
Randall Spanglerd55c6452010-06-10 12:43:51 -070013
14#include "host_common.h"
15
16#include "cryptolib.h"
17#include "utility.h"
18#include "vboot_common.h"
19
20
21uint8_t* ReadFile(const char* filename, uint64_t* size) {
22 FILE* f;
23 uint8_t* buf;
24
25 f = fopen(filename, "rb");
26 if (!f) {
Bill Richardsonabf05502010-07-01 10:22:06 -070027 VBDEBUG(("Unable to open file %s\n", filename));
Randall Spanglerd55c6452010-06-10 12:43:51 -070028 return NULL;
29 }
30
31 fseek(f, 0, SEEK_END);
32 *size = ftell(f);
33 rewind(f);
34
35 buf = Malloc(*size);
36 if (!buf) {
37 fclose(f);
38 return NULL;
39 }
40
41 if(1 != fread(buf, *size, 1, f)) {
Bill Richardsonabf05502010-07-01 10:22:06 -070042 VBDEBUG(("Unable to read from file %s\n", filename));
Randall Spanglerd55c6452010-06-10 12:43:51 -070043 fclose(f);
44 Free(buf);
45 return NULL;
46 }
47
48 fclose(f);
49 return buf;
50}
Randall Spangler6a97b3e2010-06-10 17:55:02 -070051
52
53int WriteFile(const char* filename, const void *data, uint64_t size) {
54 FILE *f = fopen(filename, "wb");
55 if (!f) {
Bill Richardsonabf05502010-07-01 10:22:06 -070056 VBDEBUG(("Unable to open file %s\n", filename));
Randall Spangler6a97b3e2010-06-10 17:55:02 -070057 return 1;
58 }
59
60 if (1 != fwrite(data, size, 1, f)) {
Bill Richardsonabf05502010-07-01 10:22:06 -070061 VBDEBUG(("Unable to write to file %s\n", filename));
Randall Spangler6a97b3e2010-06-10 17:55:02 -070062 fclose(f);
63 unlink(filename); /* Delete any partial file */
64 }
65
66 fclose(f);
67 return 0;
68}