disk.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 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_DISK_HEADER
  19. #define GRUB_DISK_HEADER 1
  20. #include <config.h>
  21. #include <grub/symbol.h>
  22. #include <grub/err.h>
  23. #include <grub/types.h>
  24. #include <grub/device.h>
  25. /* For NULL. */
  26. #include <grub/mm.h>
  27. /* For ALIGN_UP. */
  28. #include <grub/misc.h>
  29. /* These are used to set a device id. When you add a new disk device,
  30. you must define a new id for it here. */
  31. enum grub_disk_dev_id
  32. {
  33. GRUB_DISK_DEVICE_BIOSDISK_ID,
  34. GRUB_DISK_DEVICE_OFDISK_ID,
  35. GRUB_DISK_DEVICE_LOOPBACK_ID,
  36. GRUB_DISK_DEVICE_EFIDISK_ID,
  37. GRUB_DISK_DEVICE_DISKFILTER_ID,
  38. GRUB_DISK_DEVICE_HOST_ID,
  39. GRUB_DISK_DEVICE_ATA_ID,
  40. GRUB_DISK_DEVICE_MEMDISK_ID,
  41. GRUB_DISK_DEVICE_NAND_ID,
  42. GRUB_DISK_DEVICE_SCSI_ID,
  43. GRUB_DISK_DEVICE_CRYPTODISK_ID,
  44. GRUB_DISK_DEVICE_ARCDISK_ID,
  45. GRUB_DISK_DEVICE_HOSTDISK_ID,
  46. GRUB_DISK_DEVICE_PROCFS_ID,
  47. GRUB_DISK_DEVICE_CBFSDISK_ID,
  48. GRUB_DISK_DEVICE_UBOOTDISK_ID,
  49. GRUB_DISK_DEVICE_XEN,
  50. GRUB_DISK_DEVICE_OBDISK_ID,
  51. };
  52. struct grub_disk;
  53. #ifdef GRUB_UTIL
  54. struct grub_disk_memberlist;
  55. #endif
  56. typedef enum
  57. {
  58. GRUB_DISK_PULL_NONE,
  59. GRUB_DISK_PULL_REMOVABLE,
  60. GRUB_DISK_PULL_RESCAN,
  61. GRUB_DISK_PULL_MAX
  62. } grub_disk_pull_t;
  63. typedef int (*grub_disk_dev_iterate_hook_t) (const char *name, void *data);
  64. /* Disk device. */
  65. struct grub_disk_dev
  66. {
  67. /* The device name. */
  68. const char *name;
  69. /* The device id used by the cache manager. */
  70. enum grub_disk_dev_id id;
  71. /* Call HOOK with each device name, until HOOK returns non-zero. */
  72. int (*disk_iterate) (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  73. grub_disk_pull_t pull);
  74. /* Open the device named NAME, and set up DISK. */
  75. grub_err_t (*disk_open) (const char *name, struct grub_disk *disk);
  76. /* Close the disk DISK. */
  77. void (*disk_close) (struct grub_disk *disk);
  78. /* Read SIZE sectors from the sector SECTOR of the disk DISK into BUF. */
  79. grub_err_t (*disk_read) (struct grub_disk *disk, grub_disk_addr_t sector,
  80. grub_size_t size, char *buf);
  81. /* Write SIZE sectors from BUF into the sector SECTOR of the disk DISK. */
  82. grub_err_t (*disk_write) (struct grub_disk *disk, grub_disk_addr_t sector,
  83. grub_size_t size, const char *buf);
  84. #ifdef GRUB_UTIL
  85. struct grub_disk_memberlist *(*disk_memberlist) (struct grub_disk *disk);
  86. const char * (*disk_raidname) (struct grub_disk *disk);
  87. #endif
  88. /* The next disk device. */
  89. struct grub_disk_dev *next;
  90. };
  91. typedef struct grub_disk_dev *grub_disk_dev_t;
  92. extern grub_disk_dev_t EXPORT_VAR (grub_disk_dev_list);
  93. struct grub_partition;
  94. typedef grub_err_t (*grub_disk_read_hook_t) (grub_disk_addr_t sector,
  95. unsigned offset, unsigned length,
  96. char *buf, void *data);
  97. /* Disk. */
  98. struct grub_disk
  99. {
  100. /* The disk name. */
  101. const char *name;
  102. /* The underlying disk device. */
  103. grub_disk_dev_t dev;
  104. /* The total number of sectors. */
  105. grub_uint64_t total_sectors;
  106. /* Logarithm of sector size. */
  107. unsigned int log_sector_size;
  108. /* Maximum number of sectors read divided by GRUB_DISK_CACHE_SIZE. */
  109. unsigned int max_agglomerate;
  110. /* The id used by the disk cache manager. */
  111. unsigned long id;
  112. /* The partition information. This is machine-specific. */
  113. struct grub_partition *partition;
  114. /* Called when a sector was read. OFFSET is between 0 and
  115. the sector size minus 1, and LENGTH is between 0 and the sector size. */
  116. grub_disk_read_hook_t read_hook;
  117. /* Caller-specific data passed to the read hook. */
  118. void *read_hook_data;
  119. /* Device-specific data. */
  120. void *data;
  121. };
  122. typedef struct grub_disk *grub_disk_t;
  123. #ifdef GRUB_UTIL
  124. struct grub_disk_memberlist
  125. {
  126. grub_disk_t disk;
  127. struct grub_disk_memberlist *next;
  128. };
  129. typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
  130. #endif
  131. /* The sector size. */
  132. #define GRUB_DISK_SECTOR_SIZE 0x200
  133. #define GRUB_DISK_SECTOR_BITS 9
  134. /*
  135. * Some drivers have problems with disks above reasonable sizes.
  136. * Set max disk size at 1 EiB.
  137. */
  138. #define GRUB_DISK_MAX_SECTORS (1ULL << (60 - GRUB_DISK_SECTOR_BITS))
  139. /* The maximum number of disk caches. */
  140. #define GRUB_DISK_CACHE_NUM 1021
  141. /*
  142. * The maximum number of disks in an mdraid device.
  143. *
  144. * GET_DISK_INFO nr_disks (total count) does not map to disk.number,
  145. * which is an internal kernel index. Instead, do what mdadm does
  146. * and keep scanning until we find enough valid disks. The limit is
  147. * copied from there, which notes that it is sufficiently high given
  148. * that the on-disk metadata for v1.x can only support 1920.
  149. */
  150. #define GRUB_MDRAID_MAX_DISKS 4096
  151. /* The size of a disk cache in 512B units. Must be at least as big as the
  152. largest supported sector size, currently 16K. */
  153. #define GRUB_DISK_CACHE_BITS 6
  154. #define GRUB_DISK_CACHE_SIZE (1 << GRUB_DISK_CACHE_BITS)
  155. #define GRUB_DISK_MAX_MAX_AGGLOMERATE ((1 << (30 - GRUB_DISK_CACHE_BITS - GRUB_DISK_SECTOR_BITS)) - 1)
  156. /* Maximum number of sectors to read in LBA mode at once. */
  157. #define GRUB_DISK_MAX_LBA_SECTORS 63
  158. /* Return value of grub_disk_native_sectors() in case disk size is unknown. */
  159. #define GRUB_DISK_SIZE_UNKNOWN 0xffffffffffffffffULL
  160. #define GRUB_DISK_KiB_TO_SECTORS(x) ((x) << (10 - GRUB_DISK_SECTOR_BITS))
  161. /* Convert sector number from one sector size to another. */
  162. static inline grub_disk_addr_t
  163. grub_convert_sector (grub_disk_addr_t sector,
  164. grub_size_t log_sector_size_from,
  165. grub_size_t log_sector_size_to)
  166. {
  167. if (log_sector_size_from == log_sector_size_to)
  168. return sector;
  169. else if (log_sector_size_from < log_sector_size_to)
  170. {
  171. sector = ALIGN_UP (sector, 1 << (log_sector_size_to - log_sector_size_from));
  172. return sector >> (log_sector_size_to - log_sector_size_from);
  173. }
  174. else
  175. return sector << (log_sector_size_from - log_sector_size_to);
  176. }
  177. /* Convert to GRUB native disk sized sector from disk sized sector. */
  178. static inline grub_disk_addr_t
  179. grub_disk_from_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
  180. {
  181. return sector << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  182. }
  183. /* Convert from GRUB native disk sized sector to disk sized sector. */
  184. static inline grub_disk_addr_t
  185. grub_disk_to_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
  186. {
  187. return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  188. }
  189. /* This is called from the memory manager. */
  190. void grub_disk_cache_invalidate_all (void);
  191. void EXPORT_FUNC(grub_disk_dev_register) (grub_disk_dev_t dev);
  192. void EXPORT_FUNC(grub_disk_dev_unregister) (grub_disk_dev_t dev);
  193. static inline int
  194. grub_disk_dev_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data)
  195. {
  196. grub_disk_dev_t p;
  197. grub_disk_pull_t pull;
  198. for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
  199. for (p = grub_disk_dev_list; p; p = p->next)
  200. if (p->disk_iterate && (p->disk_iterate) (hook, hook_data, pull))
  201. return 1;
  202. return 0;
  203. }
  204. grub_disk_t EXPORT_FUNC(grub_disk_open) (const char *name);
  205. void EXPORT_FUNC(grub_disk_close) (grub_disk_t disk);
  206. grub_err_t EXPORT_FUNC(grub_disk_read) (grub_disk_t disk,
  207. grub_disk_addr_t sector,
  208. grub_off_t offset,
  209. grub_size_t size,
  210. void *buf);
  211. grub_err_t grub_disk_write (grub_disk_t disk,
  212. grub_disk_addr_t sector,
  213. grub_off_t offset,
  214. grub_size_t size,
  215. const void *buf);
  216. extern grub_err_t (*EXPORT_VAR(grub_disk_write_weak)) (grub_disk_t disk,
  217. grub_disk_addr_t sector,
  218. grub_off_t offset,
  219. grub_size_t size,
  220. const void *buf);
  221. grub_uint64_t EXPORT_FUNC(grub_disk_native_sectors) (grub_disk_t disk);
  222. #if DISK_CACHE_STATS
  223. void
  224. EXPORT_FUNC(grub_disk_cache_get_performance) (unsigned long *hits, unsigned long *misses);
  225. #endif
  226. extern void (* EXPORT_VAR(grub_disk_firmware_fini)) (void);
  227. extern int EXPORT_VAR(grub_disk_firmware_is_tainted);
  228. static inline void
  229. grub_stop_disk_firmware (void)
  230. {
  231. /* To prevent two drivers operating on the same disks. */
  232. grub_disk_firmware_is_tainted = 1;
  233. if (grub_disk_firmware_fini)
  234. {
  235. grub_disk_firmware_fini ();
  236. grub_disk_firmware_fini = NULL;
  237. }
  238. }
  239. /* Disk cache. */
  240. struct grub_disk_cache
  241. {
  242. enum grub_disk_dev_id dev_id;
  243. unsigned long disk_id;
  244. grub_disk_addr_t sector;
  245. char *data;
  246. int lock;
  247. };
  248. extern struct grub_disk_cache EXPORT_VAR(grub_disk_cache_table)[GRUB_DISK_CACHE_NUM];
  249. #if defined (GRUB_UTIL)
  250. void grub_lvm_init (void);
  251. void grub_ldm_init (void);
  252. void grub_mdraid09_init (void);
  253. void grub_mdraid1x_init (void);
  254. void grub_diskfilter_init (void);
  255. void grub_lvm_fini (void);
  256. void grub_ldm_fini (void);
  257. void grub_mdraid09_fini (void);
  258. void grub_mdraid1x_fini (void);
  259. void grub_diskfilter_fini (void);
  260. #endif
  261. #endif /* ! GRUB_DISK_HEADER */