xref: /aosp_15_r20/external/gsc-utils/util/misc_util.h (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1 /* Copyright 2013 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 
6 #ifndef __UTIL_MISC_UTIL_H
7 #define __UTIL_MISC_UTIL_H
8 
9 /* Don't use a macro where an inline will do... */
MIN(int a,int b)10 static inline int MIN(int a, int b)
11 {
12 	return a < b ? a : b;
13 }
MAX(int a,int b)14 static inline int MAX(int a, int b)
15 {
16 	return a > b ? a : b;
17 }
18 
19 /**
20  * Write a buffer to the file.
21  *
22  * @param filename	Target filename
23  * @param buf		Buffer to write
24  * @param size		Size of buffer in bytes
25  * @return non-zero if error
26  */
27 int write_file(const char *filename, const char *buf, int size);
28 
29 /**
30  * Read a file into a newly-allocated buffer.
31  *
32  * @param filename	Source filename
33  * @param size		Size of data in bytes will be stored here on success.
34  * @return A newly allocated buffer with the data, which must be freed with
35  *         free() by the caller, or NULL if error.
36  */
37 char *read_file(const char *filename, int *size);
38 
39 /**
40  * Check if a string contains only printable characters.
41  *
42  * @param buf		Null-terminated string to check
43  * @return non-zero if buf contains only printable characters; zero if not.
44  */
45 int is_string_printable(const char *buf);
46 
47 /**
48  * Get the versions of the command supported by the EC.
49  *
50  * @param cmd		Command
51  * @param pmask		Destination for version mask; will be set to 0 on
52  *			error.
53  * @return 0 if success, <0 if error
54  */
55 int ec_get_cmd_versions(int cmd, uint32_t *pmask);
56 
57 /**
58  * Return non-zero if the EC supports the command and version
59  *
60  * @param cmd		Command to check
61  * @param ver		Version to check
62  * @return non-zero if command version supported; 0 if not.
63  */
64 int ec_cmd_version_supported(int cmd, int ver);
65 
66 /**
67  * Return 1 is the current kernel version is greater or equal to
68  * <major>.<minor>.<sublevel>
69  */
70 int kernel_version_ge(int major, int minor, int sublevel);
71 #endif
72