blob: 611253b91a4d899e21e3d810cfc7b8d856c89dde [file] [log] [blame]
Randall Spangler32a65262011-06-27 10:49:11 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Randall Spanglerd55c6452010-06-10 12:43:51 -07002 * 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 Spanglereb591952011-04-07 10:02:00 -070012#include <string.h>
Randall Spangler6a97b3e2010-06-10 17:55:02 -070013#include <unistd.h>
Randall Spanglerd55c6452010-06-10 12:43:51 -070014
Randall Spangler32a65262011-06-27 10:49:11 -070015#include "host_common.h"
Randall Spanglerd55c6452010-06-10 12:43:51 -070016#include "vboot_common.h"
17
Randall Spanglerfb267152016-10-11 15:28:16 -070018char* StrCopy(char* dest, const char* src, int dest_size)
19{
20 strncpy(dest, src, dest_size);
21 dest[dest_size - 1] = '\0';
22 return dest;
Randall Spanglereb591952011-04-07 10:02:00 -070023}
24
Randall Spanglerfb267152016-10-11 15:28:16 -070025uint8_t* ReadFile(const char* filename, uint64_t* sizeptr)
26{
27 FILE* f;
28 uint8_t* buf;
29 long size;
Randall Spanglereb591952011-04-07 10:02:00 -070030
Randall Spanglerfb267152016-10-11 15:28:16 -070031 f = fopen(filename, "rb");
32 if (!f) {
33 VBDEBUG(("Unable to open file %s\n", filename));
34 return NULL;
35 }
Randall Spanglerd55c6452010-06-10 12:43:51 -070036
Randall Spanglerfb267152016-10-11 15:28:16 -070037 fseek(f, 0, SEEK_END);
38 size = ftell(f);
39 if (size < 0) {
40 fclose(f);
41 return NULL;
42 }
43 rewind(f);
Randall Spanglerd55c6452010-06-10 12:43:51 -070044
Randall Spanglerfb267152016-10-11 15:28:16 -070045 buf = malloc(size);
46 if (!buf) {
47 fclose(f);
48 return NULL;
49 }
Randall Spanglerd55c6452010-06-10 12:43:51 -070050
Randall Spanglerfb267152016-10-11 15:28:16 -070051 if(1 != fread(buf, size, 1, f)) {
52 VBDEBUG(("Unable to read from file %s\n", filename));
53 fclose(f);
54 free(buf);
55 return NULL;
56 }
Randall Spanglerd55c6452010-06-10 12:43:51 -070057
Randall Spanglerfb267152016-10-11 15:28:16 -070058 fclose(f);
59 if (sizeptr)
60 *sizeptr = size;
61 return buf;
Randall Spanglerd55c6452010-06-10 12:43:51 -070062}
Randall Spangler6a97b3e2010-06-10 17:55:02 -070063
Randall Spanglerfb267152016-10-11 15:28:16 -070064char* ReadFileString(char* dest, int size, const char* filename)
65{
66 char* got;
67 FILE* f;
Randall Spangler6a97b3e2010-06-10 17:55:02 -070068
Randall Spanglerfb267152016-10-11 15:28:16 -070069 f = fopen(filename, "rt");
70 if (!f)
71 return NULL;
Randall Spanglereb591952011-04-07 10:02:00 -070072
Randall Spanglerfb267152016-10-11 15:28:16 -070073 got = fgets(dest, size, f);
74 fclose(f);
75 return got;
Randall Spanglereb591952011-04-07 10:02:00 -070076}
77
Randall Spanglerfb267152016-10-11 15:28:16 -070078int ReadFileInt(const char* filename, unsigned* value)
79{
80 char buf[64];
81 char* e = NULL;
Randall Spanglereb591952011-04-07 10:02:00 -070082
Randall Spanglerfb267152016-10-11 15:28:16 -070083 if (!ReadFileString(buf, sizeof(buf), filename))
84 return -1;
Randall Spanglereb591952011-04-07 10:02:00 -070085
Randall Spanglerfb267152016-10-11 15:28:16 -070086 /* Convert to integer. Allow characters after the int ("123 blah"). */
87 *value = (unsigned)strtoul(buf, &e, 0);
88 if (e == buf)
89 return -1; /* No characters consumed, so conversion failed */
Randall Spanglereb591952011-04-07 10:02:00 -070090
Randall Spanglerfb267152016-10-11 15:28:16 -070091 return 0;
Randall Spanglereb591952011-04-07 10:02:00 -070092}
93
Randall Spanglerfb267152016-10-11 15:28:16 -070094int ReadFileBit(const char* filename, int bitmask)
95{
96 unsigned value;
97 if (ReadFileInt(filename, &value) < 0)
98 return -1;
99 else return (value & bitmask ? 1 : 0);
Randall Spanglereb591952011-04-07 10:02:00 -0700100}
101
Randall Spanglerfb267152016-10-11 15:28:16 -0700102int WriteFile(const char* filename, const void *data, uint64_t size)
103{
104 FILE *f = fopen(filename, "wb");
105 if (!f) {
106 VBDEBUG(("Unable to open file %s\n", filename));
107 return 1;
108 }
Randall Spanglereb591952011-04-07 10:02:00 -0700109
Randall Spanglerfb267152016-10-11 15:28:16 -0700110 if (1 != fwrite(data, size, 1, f)) {
111 VBDEBUG(("Unable to write to file %s\n", filename));
112 fclose(f);
113 unlink(filename); /* Delete any partial file */
114 return 1;
115 }
Randall Spangler6a97b3e2010-06-10 17:55:02 -0700116
Randall Spanglerfb267152016-10-11 15:28:16 -0700117 fclose(f);
118 return 0;
Randall Spangler6a97b3e2010-06-10 17:55:02 -0700119}