cbfs.h
  1  /* SPDX-License-Identifier: BSD-3-Clause */
  2  
  3  #ifndef _CBFS_H_
  4  #define _CBFS_H_
  5  
  6  #include <commonlib/bsd/cb_err.h>
  7  #include <commonlib/bsd/cbfs_mdata.h>
  8  #include <endian.h>
  9  #include <stdbool.h>
 10  
 11  
 12  /**********************************************************************************************
 13   *                                  CBFS FILE ACCESS APIs                                     *
 14   **********************************************************************************************/
 15  
 16  /* For documentation look in src/include/cbfs.h file in the main coreboot source tree. */
 17  
 18  static inline size_t cbfs_load(const char *name, void *buf, size_t size);
 19  static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size);
 20  static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf,
 21  					       size_t size);
 22  
 23  static inline void *cbfs_map(const char *name, size_t *size_out);
 24  static inline void *cbfs_ro_map(const char *name, size_t *size_out);
 25  static inline void *cbfs_unverified_area_map(const char *area, const char *name,
 26  					     size_t *size_out);
 27  
 28  void cbfs_unmap(void *mapping);
 29  
 30  static inline size_t cbfs_get_size(const char *name);
 31  static inline size_t cbfs_ro_get_size(const char *name);
 32  
 33  static inline enum cbfs_type cbfs_get_type(const char *name);
 34  static inline enum cbfs_type cbfs_ro_get_type(const char *name);
 35  
 36  static inline bool cbfs_file_exists(const char *name);
 37  static inline bool cbfs_ro_file_exists(const char *name);
 38  
 39  /**********************************************************************************************
 40   *                         INTERNAL HELPERS FOR INLINES, DO NOT USE.                          *
 41   **********************************************************************************************/
 42  ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mdata);
 43  
 44  void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro);
 45  
 46  void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf,
 47  				 size_t *size_inout);
 48  
 49  /**********************************************************************************************
 50   *                                  INLINE IMPLEMENTATIONS                                    *
 51   **********************************************************************************************/
 52  
 53  static inline void *cbfs_map(const char *name, size_t *size_out)
 54  {
 55  	return _cbfs_load(name, NULL, size_out, false);
 56  }
 57  
 58  static inline void *cbfs_ro_map(const char *name, size_t *size_out)
 59  {
 60  	return _cbfs_load(name, NULL, size_out, true);
 61  }
 62  
 63  static inline void *cbfs_unverified_area_map(const char *area, const char *name,
 64  					     size_t *size_out)
 65  {
 66  	return _cbfs_unverified_area_load(area, name, NULL, size_out);
 67  }
 68  
 69  static inline size_t cbfs_load(const char *name, void *buf, size_t size)
 70  {
 71  	if (_cbfs_load(name, buf, &size, false))
 72  		return size;
 73  	else
 74  		return 0;
 75  }
 76  
 77  static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size)
 78  {
 79  	if (_cbfs_load(name, buf, &size, true))
 80  		return size;
 81  	else
 82  		return 0;
 83  }
 84  
 85  static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf,
 86  					       size_t size)
 87  {
 88  	if (_cbfs_unverified_area_load(area, name, buf, &size))
 89  		return size;
 90  	else
 91  		return 0;
 92  }
 93  
 94  static inline size_t cbfs_get_size(const char *name)
 95  {
 96  	union cbfs_mdata mdata;
 97  	if (_cbfs_boot_lookup(name, false, &mdata) < 0)
 98  		return 0;
 99  	else
100  		return be32toh(mdata.h.len);
101  }
102  
103  static inline size_t cbfs_ro_get_size(const char *name)
104  {
105  	union cbfs_mdata mdata;
106  	if (_cbfs_boot_lookup(name, true, &mdata) < 0)
107  		return 0;
108  	else
109  		return be32toh(mdata.h.len);
110  }
111  
112  static inline enum cbfs_type cbfs_get_type(const char *name)
113  {
114  	union cbfs_mdata mdata;
115  	if (_cbfs_boot_lookup(name, false, &mdata) < 0)
116  		return CBFS_TYPE_NULL;
117  	else
118  		return be32toh(mdata.h.type);
119  }
120  
121  static inline enum cbfs_type cbfs_ro_get_type(const char *name)
122  {
123  	union cbfs_mdata mdata;
124  	if (_cbfs_boot_lookup(name, true, &mdata) < 0)
125  		return CBFS_TYPE_NULL;
126  	else
127  		return be32toh(mdata.h.type);
128  }
129  
130  static inline bool cbfs_file_exists(const char *name)
131  {
132  	union cbfs_mdata mdata;
133  	return _cbfs_boot_lookup(name, false, &mdata) >= 0;
134  }
135  
136  static inline bool cbfs_ro_file_exists(const char *name)
137  {
138  	union cbfs_mdata mdata;
139  	return _cbfs_boot_lookup(name, true, &mdata) >= 0;
140  }
141  
142  #endif