xref: /linux/include/linux/kernel_read_file.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_READ_FILE_H
3 #define _LINUX_KERNEL_READ_FILE_H
4 
5 #include <linux/file.h>
6 #include <linux/types.h>
7 
8 /* This is a list of *what* is being read, not *how* nor *where*. */
9 #define __kernel_read_file_id(id) \
10 	id(UNKNOWN, unknown)		\
11 	id(FIRMWARE, firmware)		\
12 	id(MODULE, kernel-module)		\
13 	id(KEXEC_IMAGE, kexec-image)		\
14 	id(KEXEC_INITRAMFS, kexec-initramfs)	\
15 	id(POLICY, security-policy)		\
16 	id(X509_CERTIFICATE, x509-certificate)	\
17 	id(MAX_ID, )
18 
19 #define __fid_enumify(ENUM, dummy) READING_ ## ENUM,
20 #define __fid_stringify(dummy, str) #str,
21 
22 enum kernel_read_file_id {
23 	__kernel_read_file_id(__fid_enumify)
24 };
25 
26 static const char * const kernel_read_file_str[] = {
27 	__kernel_read_file_id(__fid_stringify)
28 };
29 
kernel_read_file_id_str(enum kernel_read_file_id id)30 static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id)
31 {
32 	if ((unsigned int)id >= READING_MAX_ID)
33 		return kernel_read_file_str[READING_UNKNOWN];
34 
35 	return kernel_read_file_str[id];
36 }
37 
38 ssize_t kernel_read_file(struct file *file, loff_t offset,
39 			 void **buf, size_t buf_size,
40 			 size_t *file_size,
41 			 enum kernel_read_file_id id);
42 ssize_t kernel_read_file_from_path(const char *path, loff_t offset,
43 				   void **buf, size_t buf_size,
44 				   size_t *file_size,
45 				   enum kernel_read_file_id id);
46 ssize_t kernel_read_file_from_path_initns(const char *path, loff_t offset,
47 					  void **buf, size_t buf_size,
48 					  size_t *file_size,
49 					  enum kernel_read_file_id id);
50 ssize_t kernel_read_file_from_fd(int fd, loff_t offset,
51 				 void **buf, size_t buf_size,
52 				 size_t *file_size,
53 				 enum kernel_read_file_id id);
54 
55 #endif /* _LINUX_KERNEL_READ_FILE_H */
56