1 /* 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-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 "utility.h"
12 #include "vboot_struct.h"
13 
14 /* Copy up to dest_size-1 characters from src to dest, ensuring null
15    termination (which strncpy() doesn't do).  Returns the destination
16    string. */
17 char* StrCopy(char* dest, const char* src, int dest_size);
18 
19 /* Read data from [filename].  Store the size of returned data in [size].
20  *
21  * Returns the data buffer, which the caller must Free(), or NULL if
22  * error. */
23 uint8_t* ReadFile(const char* filename, uint64_t* size);
24 
25 /* Read a string from a file.  Passed the destination, dest size, and
26  * filename to read.
27  *
28  * Returns the destination, or NULL if error. */
29 char* ReadFileString(char* dest, int size, const char* filename);
30 
31 /* Read an unsigned integer from a file and save into passed pointer.
32  *
33  * Returns 0 if success, -1 if error. */
34 int ReadFileInt(const char* filename, unsigned* value);
35 
36 /* Check if a bit is set in a file which contains an integer.
37  *
38  * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
39 int ReadFileBit(const char* filename, int bitmask);
40 
41 /* Writes [size] bytes of [data] to [filename].
42  *
43  * Returns 0 if success, 1 if error. */
44 int WriteFile(const char* filename, const void *data, uint64_t size);
45 
46 /**
47  * Read data from a file into a newly allocated buffer.
48  *
49  * @param filename	Name of file to read from
50  * @param data_ptr	On exit, pointer to newly allocated buffer with data
51  *			will be stored here.  Caller must free() the buffer
52  *			when done with it.
53  * @param size_ptr	On exit, size of data will be stored here.
54  * @return VB2_SUCCESS, or non-zero if error.
55  */
56 int vb2_read_file(const char *filename, uint8_t **data_ptr, uint32_t *size_ptr);
57 
58 /**
59  * Write data to a file from a buffer.
60  *
61  * @param filename	Name of file to write to
62  * @param buf		Buffer to write
63  * @param size		Number of bytes of data to write
64  * @return VB2_SUCCESS, or non-zero if error.
65  */
66 int vb2_write_file(const char *filename, const void *buf, uint32_t size);
67 
68 /**
69  * Write a buffer which starts with a standard vb2_struct_common header.
70  *
71  * Determines the buffer size from the common header total size field.
72  *
73  * @param filename	Name of file to write to
74  * @param buf		Buffer to write
75  * @return VB2_SUCCESS, or non-zero if error.
76  */
77 int vb2_write_object(const char *filename, const void *buf);
78 
79 /**
80  * Round up a size to a multiple of 32 bits (4 bytes).
81  */
roundup32(uint32_t v)82 static __inline const uint32_t roundup32(uint32_t v)
83 {
84 	return (v + 3) & ~3;
85 }
86 
87 /**
88  * Return the buffer size required to hold a description string.
89  *
90  * If the string is NULL or empty, the size is zero.  Otherwise, it is the
91  * size of a buffer which can hold the string and its null terminator,
92  * rounded up to the nerest multiple of 32 bits.
93  *
94  * @param desc		Description string
95  * @return The buffer size in bytes.
96  */
97 uint32_t vb2_desc_size(const char *desc);
98 
99 #endif  /* VBOOT_REFERENCE_HOST_MISC_H_ */
100