file.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2007 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_FILE_HEADER
  19. #define GRUB_FILE_HEADER 1
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. #include <grub/device.h>
  23. #include <grub/fs.h>
  24. #include <grub/disk.h>
  25. enum grub_file_type
  26. {
  27. GRUB_FILE_TYPE_NONE = 0,
  28. /* GRUB module to be loaded. */
  29. GRUB_FILE_TYPE_GRUB_MODULE,
  30. /* Loopback file to be represented as disk. */
  31. GRUB_FILE_TYPE_LOOPBACK,
  32. /* Linux kernel to be loaded. */
  33. GRUB_FILE_TYPE_LINUX_KERNEL,
  34. /* Linux initrd. */
  35. GRUB_FILE_TYPE_LINUX_INITRD,
  36. /* Multiboot kernel. */
  37. GRUB_FILE_TYPE_MULTIBOOT_KERNEL,
  38. /* Multiboot module. */
  39. GRUB_FILE_TYPE_MULTIBOOT_MODULE,
  40. /* Xen hypervisor - used on ARM only. */
  41. GRUB_FILE_TYPE_XEN_HYPERVISOR,
  42. /* Xen module - used on ARM only. */
  43. GRUB_FILE_TYPE_XEN_MODULE,
  44. GRUB_FILE_TYPE_BSD_KERNEL,
  45. GRUB_FILE_TYPE_FREEBSD_ENV,
  46. GRUB_FILE_TYPE_FREEBSD_MODULE,
  47. GRUB_FILE_TYPE_FREEBSD_MODULE_ELF,
  48. GRUB_FILE_TYPE_NETBSD_MODULE,
  49. GRUB_FILE_TYPE_OPENBSD_RAMDISK,
  50. GRUB_FILE_TYPE_XNU_INFO_PLIST,
  51. GRUB_FILE_TYPE_XNU_MKEXT,
  52. GRUB_FILE_TYPE_XNU_KEXT,
  53. GRUB_FILE_TYPE_XNU_KERNEL,
  54. GRUB_FILE_TYPE_XNU_RAMDISK,
  55. GRUB_FILE_TYPE_XNU_HIBERNATE_IMAGE,
  56. GRUB_FILE_XNU_DEVPROP,
  57. GRUB_FILE_TYPE_PLAN9_KERNEL,
  58. GRUB_FILE_TYPE_NTLDR,
  59. GRUB_FILE_TYPE_TRUECRYPT,
  60. GRUB_FILE_TYPE_FREEDOS,
  61. GRUB_FILE_TYPE_PXECHAINLOADER,
  62. GRUB_FILE_TYPE_PCCHAINLOADER,
  63. GRUB_FILE_TYPE_COREBOOT_CHAINLOADER,
  64. GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE,
  65. /* File holding signature. */
  66. GRUB_FILE_TYPE_SIGNATURE,
  67. /* File holding public key to verify signature once. */
  68. GRUB_FILE_TYPE_PUBLIC_KEY,
  69. /* File holding public key to add to trused keys. */
  70. GRUB_FILE_TYPE_PUBLIC_KEY_TRUST,
  71. /* File of which we intend to print a blocklist to the user. */
  72. GRUB_FILE_TYPE_PRINT_BLOCKLIST,
  73. /* File we intend to use for test loading or testing speed. */
  74. GRUB_FILE_TYPE_TESTLOAD,
  75. /* File we open only to get its size. E.g. in ls output. */
  76. GRUB_FILE_TYPE_GET_SIZE,
  77. /* Font file. */
  78. GRUB_FILE_TYPE_FONT,
  79. /* File holding encryption key for encrypted ZFS. */
  80. GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY,
  81. /* File holding the encryption key. */
  82. GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY,
  83. /* File holding the encryption metadata header */
  84. GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER,
  85. /* File we open n grub-fstest. */
  86. GRUB_FILE_TYPE_FSTEST,
  87. /* File we open n grub-mount. */
  88. GRUB_FILE_TYPE_MOUNT,
  89. /* File which we attempt to identify the type of. */
  90. GRUB_FILE_TYPE_FILE_ID,
  91. /* File holding ACPI table. */
  92. GRUB_FILE_TYPE_ACPI_TABLE,
  93. /* File holding Device Tree. */
  94. GRUB_FILE_TYPE_DEVICE_TREE_IMAGE,
  95. /* File we intend show to user. */
  96. GRUB_FILE_TYPE_CAT,
  97. GRUB_FILE_TYPE_HEXCAT,
  98. /* One of pair of files we intend to compare. */
  99. GRUB_FILE_TYPE_CMP,
  100. /* List of hashes for hashsum. */
  101. GRUB_FILE_TYPE_HASHLIST,
  102. /* File hashed by hashsum. */
  103. GRUB_FILE_TYPE_TO_HASH,
  104. /* Keyboard layout. */
  105. GRUB_FILE_TYPE_KEYBOARD_LAYOUT,
  106. /* Picture file. */
  107. GRUB_FILE_TYPE_PIXMAP,
  108. /* *.lst shipped by GRUB. */
  109. GRUB_FILE_TYPE_GRUB_MODULE_LIST,
  110. /* config file. */
  111. GRUB_FILE_TYPE_CONFIG,
  112. GRUB_FILE_TYPE_THEME,
  113. GRUB_FILE_TYPE_GETTEXT_CATALOG,
  114. GRUB_FILE_TYPE_FS_SEARCH,
  115. GRUB_FILE_TYPE_AUDIO,
  116. GRUB_FILE_TYPE_VBE_DUMP,
  117. GRUB_FILE_TYPE_LOADENV,
  118. GRUB_FILE_TYPE_SAVEENV,
  119. GRUB_FILE_TYPE_VERIFY_SIGNATURE,
  120. GRUB_FILE_TYPE_MASK = 0xffff,
  121. /* --skip-sig is specified. */
  122. GRUB_FILE_TYPE_SKIP_SIGNATURE = 0x10000,
  123. GRUB_FILE_TYPE_NO_DECOMPRESS = 0x20000
  124. };
  125. /* File description. */
  126. struct grub_file
  127. {
  128. /* File name. */
  129. char *name;
  130. /* The underlying device. */
  131. grub_device_t device;
  132. /* The underlying filesystem. */
  133. grub_fs_t fs;
  134. /* The current offset. */
  135. grub_off_t offset;
  136. grub_off_t progress_offset;
  137. /* Progress info. */
  138. grub_uint64_t last_progress_time;
  139. grub_off_t last_progress_offset;
  140. grub_uint64_t estimated_speed;
  141. /* The file size. */
  142. grub_off_t size;
  143. /* If file is not easily seekable. Should be set by underlying layer. */
  144. int not_easily_seekable;
  145. /* Filesystem-specific data. */
  146. void *data;
  147. /* This is called when a sector is read. Used only for a disk device. */
  148. grub_disk_read_hook_t read_hook;
  149. /* Caller-specific data passed to the read hook. */
  150. void *read_hook_data;
  151. };
  152. typedef struct grub_file *grub_file_t;
  153. extern grub_disk_read_hook_t EXPORT_VAR(grub_file_progress_hook);
  154. /* Filters with lower ID are executed first. */
  155. typedef enum grub_file_filter_id
  156. {
  157. GRUB_FILE_FILTER_VERIFY,
  158. GRUB_FILE_FILTER_GZIO,
  159. GRUB_FILE_FILTER_XZIO,
  160. GRUB_FILE_FILTER_LZOPIO,
  161. GRUB_FILE_FILTER_MAX,
  162. GRUB_FILE_FILTER_COMPRESSION_FIRST = GRUB_FILE_FILTER_GZIO,
  163. GRUB_FILE_FILTER_COMPRESSION_LAST = GRUB_FILE_FILTER_LZOPIO,
  164. } grub_file_filter_id_t;
  165. typedef grub_file_t (*grub_file_filter_t) (grub_file_t in, enum grub_file_type type);
  166. extern grub_file_filter_t EXPORT_VAR(grub_file_filters)[GRUB_FILE_FILTER_MAX];
  167. static inline void
  168. grub_file_filter_register (grub_file_filter_id_t id, grub_file_filter_t filter)
  169. {
  170. grub_file_filters[id] = filter;
  171. }
  172. static inline void
  173. grub_file_filter_unregister (grub_file_filter_id_t id)
  174. {
  175. grub_file_filters[id] = 0;
  176. }
  177. /* Get a device name from NAME. */
  178. char *EXPORT_FUNC(grub_file_get_device_name) (const char *name);
  179. grub_file_t EXPORT_FUNC(grub_file_open) (const char *name, enum grub_file_type type);
  180. grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf,
  181. grub_size_t len);
  182. grub_off_t EXPORT_FUNC(grub_file_seek) (grub_file_t file, grub_off_t offset);
  183. grub_err_t EXPORT_FUNC(grub_file_close) (grub_file_t file);
  184. /* Return value of grub_file_size() in case file size is unknown. */
  185. #define GRUB_FILE_SIZE_UNKNOWN 0xffffffffffffffffULL
  186. static inline grub_off_t
  187. grub_file_size (const grub_file_t file)
  188. {
  189. return file->size;
  190. }
  191. static inline grub_off_t
  192. grub_file_tell (const grub_file_t file)
  193. {
  194. return file->offset;
  195. }
  196. static inline int
  197. grub_file_seekable (const grub_file_t file)
  198. {
  199. return !file->not_easily_seekable;
  200. }
  201. grub_file_t
  202. grub_file_offset_open (grub_file_t parent, enum grub_file_type type,
  203. grub_off_t start, grub_off_t size);
  204. void
  205. grub_file_offset_close (grub_file_t file);
  206. #endif /* ! GRUB_FILE_HEADER */