xref: /aosp_15_r20/external/vboot_reference/host/lib/include/host_misc.h (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2010 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-side misc functions for verified boot.
6  */
7 
8 #ifndef VBOOT_REFERENCE_HOST_MISC_H_
9 #define VBOOT_REFERENCE_HOST_MISC_H_
10 
11 #include <stdbool.h>
12 
13 #include "vboot_struct.h"
14 #include "vboot_api.h"
15 
16 /* Copy up to dest_size-1 characters from src to dest, ensuring null
17  * termination (which strncpy() doesn't do).  Returns the destination
18  * string. */
19 char* StrCopy(char* dest, const char* src, int dest_size);
20 
21 /* Read data from [filename].  Store the size of returned data in [size].
22  *
23  * Returns the data buffer, which the caller must Free(), or NULL if
24  * error. */
25 uint8_t* ReadFile(const char* filename, uint64_t* size);
26 
27 /* Read the first line from a file.  Passed the destination, dest size,
28  * and filename to read.
29  *
30  * Trailing newlines will be chomped from dest.
31  *
32  * Returns the destination, or NULL if error. */
33 char* ReadFileFirstLine(char* dest, int size, const char* filename);
34 
35 /* Read an unsigned integer from a file and save into passed pointer.
36  *
37  * Returns 0 if success, -1 if error. */
38 int ReadFileInt(const char* filename, unsigned* value);
39 
40 /* Check if a bit is set in a file which contains an integer.
41  *
42  * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
43 int ReadFileBit(const char* filename, int bitmask);
44 
45 /* Writes [size] bytes of [data] to [filename].
46  *
47  * Returns 0 if success, 1 if error. */
48 vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size);
49 
50 /**
51  * Read data from a file into a newly allocated buffer.
52  * The buffer will end with an extra null byte ('\0', not counted in size).
53  *
54  * @param filename	Name of file to read from
55  * @param data_ptr	On exit, pointer to newly allocated buffer with data
56  *			will be stored here.  Caller must free() the buffer
57  *			when done with it.
58  * @param size_ptr	On exit, size of data will be stored here.
59  * @return VB2_SUCCESS, or non-zero if error.
60  */
61 vb2_error_t vb2_read_file(const char *filename, uint8_t **data_ptr,
62 			  uint32_t *size_ptr);
63 
64 /**
65  * Write data to a file from a buffer.
66  *
67  * @param filename	Name of file to write to
68  * @param buf		Buffer to write
69  * @param size		Number of bytes of data to write
70  * @return VB2_SUCCESS, or non-zero if error.
71  */
72 vb2_error_t vb2_write_file(const char *filename, const void *buf,
73 			   uint32_t size);
74 
75 /**
76  * Write a buffer which starts with a standard vb21_struct_common header.
77  *
78  * Determines the buffer size from the common header total size field.
79  *
80  * @param filename	Name of file to write to
81  * @param buf		Buffer to write
82  * @return VB2_SUCCESS, or non-zero if error.
83  */
84 vb2_error_t vb21_write_object(const char *filename, const void *buf);
85 
86 /**
87  * Round up a size to a multiple of 32 bits (4 bytes).
88  */
roundup32(uint32_t v)89 static inline const uint32_t roundup32(uint32_t v)
90 {
91 	return (v + 3) & ~3;
92 }
93 
94 /**
95  * Return the buffer size required to hold a description string.
96  *
97  * If the string is NULL or empty, the size is zero.  Otherwise, it is the
98  * size of a buffer which can hold the string and its null terminator,
99  * rounded up to the nerest multiple of 32 bits.
100  *
101  * @param desc		Description string
102  * @return The buffer size in bytes.
103  */
104 uint32_t vb2_desc_size(const char *desc);
105 
106 /**
107  * Parse byte from hex string.
108  *
109  * @param val		Pointer to the value buffer
110  * @param str		String to parse
111  * @return true on success, false otherwise.
112  * */
113 bool parse_hex(uint8_t *val, const char *str);
114 
115 
116 /**
117  * Parse hash from string.
118  *
119  * @param buf		Output buffer. Has to be at least `len` bytes long
120  * @param len		Hash length in bytes
121  * @param str		Hash string form. Has to be at least 2 * `len` long
122  *			Whitespaces are ignored
123  * @return true on success, false otherwise.
124  */
125 bool parse_hash(uint8_t *buf, size_t len, const char *str);
126 
127 #endif  /* VBOOT_REFERENCE_HOST_MISC_H_ */
128