• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2011 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>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include "cryptolib.h"
16 #include "host_common.h"
17 #include "vboot_common.h"
18 
19 
StrCopy(char * dest,const char * src,int dest_size)20 char* StrCopy(char* dest, const char* src, int dest_size) {
21   strncpy(dest, src, dest_size);
22   dest[dest_size - 1] = '\0';
23   return dest;
24 }
25 
26 
ReadFile(const char * filename,uint64_t * sizeptr)27 uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) {
28   FILE* f;
29   uint8_t* buf;
30   uint64_t size;
31 
32   f = fopen(filename, "rb");
33   if (!f) {
34     VBDEBUG(("Unable to open file %s\n", filename));
35     return NULL;
36   }
37 
38   fseek(f, 0, SEEK_END);
39   size = ftell(f);
40   rewind(f);
41 
42   buf = malloc(size);
43   if (!buf) {
44     fclose(f);
45     return NULL;
46   }
47 
48   if(1 != fread(buf, size, 1, f)) {
49     VBDEBUG(("Unable to read from file %s\n", filename));
50     fclose(f);
51     free(buf);
52     return NULL;
53   }
54 
55   fclose(f);
56   if (sizeptr)
57     *sizeptr = size;
58   return buf;
59 }
60 
61 
ReadFileString(char * dest,int size,const char * filename)62 char* ReadFileString(char* dest, int size, const char* filename) {
63   char* got;
64   FILE* f;
65 
66   f = fopen(filename, "rt");
67   if (!f)
68     return NULL;
69 
70   got = fgets(dest, size, f);
71   fclose(f);
72   return got;
73 }
74 
75 
ReadFileInt(const char * filename,unsigned * value)76 int ReadFileInt(const char* filename, unsigned* value) {
77   char buf[64];
78   char* e = NULL;
79 
80   if (!ReadFileString(buf, sizeof(buf), filename))
81     return -1;
82 
83   /* Convert to integer.  Allow characters after the int ("123 blah"). */
84   *value = (unsigned)strtoul(buf, &e, 0);
85   if (e == buf)
86     return -1;  /* No characters consumed, so conversion failed */
87 
88   return 0;
89 }
90 
91 
ReadFileBit(const char * filename,int bitmask)92 int ReadFileBit(const char* filename, int bitmask) {
93   unsigned value;
94   if (ReadFileInt(filename, &value) < 0)
95     return -1;
96   else return (value & bitmask ? 1 : 0);
97 }
98 
99 
WriteFile(const char * filename,const void * data,uint64_t size)100 int WriteFile(const char* filename, const void *data, uint64_t size) {
101   FILE *f = fopen(filename, "wb");
102   if (!f) {
103     VBDEBUG(("Unable to open file %s\n", filename));
104     return 1;
105   }
106 
107   if (1 != fwrite(data, size, 1, f)) {
108     VBDEBUG(("Unable to write to file %s\n", filename));
109     fclose(f);
110     unlink(filename);  /* Delete any partial file */
111     return 1;
112   }
113 
114   fclose(f);
115   return 0;
116 }
117