1 /* SPDX-License-Identifier: BSD-3-Clause */
6 #include <commonlib/bsd/cb_err.h>
7 #include <commonlib/bsd/cbfs_mdata.h>
12 /**********************************************************************************************
13 * CBFS FILE ACCESS APIs *
14 **********************************************************************************************/
16 /* For documentation look in src/include/cbfs.h file in the main coreboot source tree. */
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
,
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
,
28 void cbfs_unmap(void *mapping
);
30 static inline size_t cbfs_get_size(const char *name
);
31 static inline size_t cbfs_ro_get_size(const char *name
);
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
);
36 static inline bool cbfs_file_exists(const char *name
);
37 static inline bool cbfs_ro_file_exists(const char *name
);
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
);
44 void *_cbfs_load(const char *name
, void *buf
, size_t *size_inout
, bool force_ro
);
46 void *_cbfs_unverified_area_load(const char *area
, const char *name
, void *buf
,
49 /**********************************************************************************************
50 * INLINE IMPLEMENTATIONS *
51 **********************************************************************************************/
53 static inline void *cbfs_map(const char *name
, size_t *size_out
)
55 return _cbfs_load(name
, NULL
, size_out
, false);
58 static inline void *cbfs_ro_map(const char *name
, size_t *size_out
)
60 return _cbfs_load(name
, NULL
, size_out
, true);
63 static inline void *cbfs_unverified_area_map(const char *area
, const char *name
,
66 return _cbfs_unverified_area_load(area
, name
, NULL
, size_out
);
69 static inline size_t cbfs_load(const char *name
, void *buf
, size_t size
)
71 if (_cbfs_load(name
, buf
, &size
, false))
77 static inline size_t cbfs_ro_load(const char *name
, void *buf
, size_t size
)
79 if (_cbfs_load(name
, buf
, &size
, true))
85 static inline size_t cbfs_unverified_area_load(const char *area
, const char *name
, void *buf
,
88 if (_cbfs_unverified_area_load(area
, name
, buf
, &size
))
94 static inline size_t cbfs_get_size(const char *name
)
96 union cbfs_mdata mdata
;
97 if (_cbfs_boot_lookup(name
, false, &mdata
) < 0)
100 return be32toh(mdata
.h
.len
);
103 static inline size_t cbfs_ro_get_size(const char *name
)
105 union cbfs_mdata mdata
;
106 if (_cbfs_boot_lookup(name
, true, &mdata
) < 0)
109 return be32toh(mdata
.h
.len
);
112 static inline enum cbfs_type
cbfs_get_type(const char *name
)
114 union cbfs_mdata mdata
;
115 if (_cbfs_boot_lookup(name
, false, &mdata
) < 0)
116 return CBFS_TYPE_NULL
;
118 return be32toh(mdata
.h
.type
);
121 static inline enum cbfs_type
cbfs_ro_get_type(const char *name
)
123 union cbfs_mdata mdata
;
124 if (_cbfs_boot_lookup(name
, true, &mdata
) < 0)
125 return CBFS_TYPE_NULL
;
127 return be32toh(mdata
.h
.type
);
130 static inline bool cbfs_file_exists(const char *name
)
132 union cbfs_mdata mdata
;
133 return _cbfs_boot_lookup(name
, false, &mdata
) >= 0;
136 static inline bool cbfs_ro_file_exists(const char *name
)
138 union cbfs_mdata mdata
;
139 return _cbfs_boot_lookup(name
, true, &mdata
) >= 0;