xref: /aosp_15_r20/external/vboot_reference/host/lib/host_misc.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2011 The ChromiumOS Authors
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 "host_common.h"
16 
StrCopy(char * dest,const char * src,int dest_size)17 char* StrCopy(char* dest, const char* src, int dest_size)
18 {
19 	strncpy(dest, src, dest_size);
20 	dest[dest_size - 1] = '\0';
21 	return dest;
22 }
23 
ReadFile(const char * filename,uint64_t * sizeptr)24 uint8_t* ReadFile(const char* filename, uint64_t* sizeptr)
25 {
26 	FILE* f;
27 	uint8_t* buf;
28 	long size;
29 
30 	f = fopen(filename, "rb");
31 	if (!f) {
32 		fprintf(stderr, "Unable to open file %s\n", filename);
33 		return NULL;
34 	}
35 
36 	fseek(f, 0, SEEK_END);
37 	size = ftell(f);
38 	if (size < 0) {
39 		fclose(f);
40 		return NULL;
41 	}
42 	rewind(f);
43 
44 	buf = malloc(size);
45 	if (!buf) {
46 		fclose(f);
47 		return NULL;
48 	}
49 
50 	if (1 != fread(buf, size, 1, f)) {
51 		fprintf(stderr, "Unable to read from file %s\n", filename);
52 		fclose(f);
53 		free(buf);
54 		return NULL;
55 	}
56 
57 	fclose(f);
58 	if (sizeptr)
59 		*sizeptr = size;
60 	return buf;
61 }
62 
ReadFileFirstLine(char * dest,int size,const char * filename)63 char* ReadFileFirstLine(char* dest, int size, const char* filename)
64 {
65 	char* got;
66 	FILE* f;
67 
68 	f = fopen(filename, "rt");
69 	if (!f)
70 		return NULL;
71 
72 	got = fgets(dest, size, f);
73 	fclose(f);
74 
75 	/* chomp the trailing newline if any */
76 	if (got)
77 		dest[strcspn(dest, "\n")] = 0;
78 	return got;
79 }
80 
ReadFileInt(const char * filename,unsigned * value)81 int ReadFileInt(const char* filename, unsigned* value)
82 {
83 	char buf[64];
84 	char* e = NULL;
85 
86 	if (!ReadFileFirstLine(buf, sizeof(buf), filename))
87 		return -1;
88 
89 	/* Convert to integer.  Allow characters after the int ("123 blah"). */
90 	*value = (unsigned)strtoul(buf, &e, 0);
91 	if (e == buf)
92 		return -1;  /* No characters consumed, so conversion failed */
93 
94 	return 0;
95 }
96 
ReadFileBit(const char * filename,int bitmask)97 int ReadFileBit(const char* filename, int bitmask)
98 {
99 	unsigned value;
100 	if (ReadFileInt(filename, &value) < 0)
101 		return -1;
102 	else return (value & bitmask ? 1 : 0);
103 }
104 
WriteFile(const char * filename,const void * data,uint64_t size)105 vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size)
106 {
107 	FILE *f = fopen(filename, "wb");
108 	if (!f) {
109 		fprintf(stderr, "Unable to open file %s\n", filename);
110 		return 1;
111 	}
112 
113 	if (1 != fwrite(data, size, 1, f)) {
114 		fprintf(stderr, "Unable to write to file %s\n", filename);
115 		fclose(f);
116 		unlink(filename);  /* Delete any partial file */
117 		return 1;
118 	}
119 
120 	fclose(f);
121 	return 0;
122 }
123 
parse_hex(uint8_t * val,const char * str)124 bool parse_hex(uint8_t *val, const char *str)
125 {
126 	uint8_t v = 0;
127 	char c;
128 	int digit;
129 
130 	for (digit = 0; digit < 2; digit++) {
131 		c = *str;
132 		if (!c)
133 			return false;
134 		if (!isxdigit(c))
135 			return false;
136 		c = tolower(c);
137 		if (c >= '0' && c <= '9')
138 			v += c - '0';
139 		else
140 			v += 10 + c - 'a';
141 		if (!digit)
142 			v <<= 4;
143 		str++;
144 	}
145 
146 	*val = v;
147 	return true;
148 }
149 
parse_hash(uint8_t * buf,size_t len,const char * str)150 bool parse_hash(uint8_t *buf, size_t len, const char *str)
151 {
152 	const char *s = str;
153 	int i;
154 
155 	for (i = 0; i < len; i++) {
156 		/* skip whitespace */
157 		while (*s && isspace(*s))
158 			s++;
159 		if (!*s)
160 			break;
161 		if (!parse_hex(buf, s))
162 			break;
163 
164 		/* on to the next byte */
165 		s += 2;
166 		buf++;
167 	}
168 
169 	if (i != len || *s)
170 		return false;
171 	return true;
172 }
173