dmraid_nvidia.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* dmraid_nvidia.c - module to handle Nvidia fakeraid. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/disk.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/diskfilter.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define NV_SIGNATURES 4
  27. #define NV_IDLE 0
  28. #define NV_SCDB_INIT_RAID 2
  29. #define NV_SCDB_REBUILD_RAID 3
  30. #define NV_SCDB_UPGRADE_RAID 4
  31. #define NV_SCDB_SYNC_RAID 5
  32. #define NV_LEVEL_UNKNOWN 0x00
  33. #define NV_LEVEL_JBOD 0xFF
  34. #define NV_LEVEL_0 0x80
  35. #define NV_LEVEL_1 0x81
  36. #define NV_LEVEL_3 0x83
  37. #define NV_LEVEL_5 0x85
  38. #define NV_LEVEL_10 0x8a
  39. #define NV_LEVEL_1_0 0x8180
  40. #define NV_ARRAY_FLAG_BOOT 1 /* BIOS use only. */
  41. #define NV_ARRAY_FLAG_ERROR 2 /* Degraded or offline. */
  42. #define NV_ARRAY_FLAG_PARITY_VALID 4 /* RAID-3/5 parity valid. */
  43. struct grub_nv_array
  44. {
  45. grub_uint32_t version;
  46. grub_uint32_t signature[NV_SIGNATURES];
  47. grub_uint8_t raid_job_code;
  48. grub_uint8_t stripe_width;
  49. grub_uint8_t total_volumes;
  50. grub_uint8_t original_width;
  51. grub_uint32_t raid_level;
  52. grub_uint32_t stripe_block_size;
  53. grub_uint32_t stripe_block_size_bytes;
  54. grub_uint32_t stripe_block_size_log2;
  55. grub_uint32_t stripe_mask;
  56. grub_uint32_t stripe_size;
  57. grub_uint32_t stripe_size_bytes;
  58. grub_uint32_t raid_job_mask;
  59. grub_uint32_t original_capacity;
  60. grub_uint32_t flags;
  61. };
  62. #define NV_ID_LEN 8
  63. #define NV_ID_STRING "NVIDIA"
  64. #define NV_VERSION 100
  65. #define NV_PRODID_LEN 16
  66. #define NV_PRODREV_LEN 4
  67. struct grub_nv_super
  68. {
  69. char vendor[NV_ID_LEN]; /* 0x00 - 0x07 ID string. */
  70. grub_uint32_t size; /* 0x08 - 0x0B Size of metadata in dwords. */
  71. grub_uint32_t chksum; /* 0x0C - 0x0F Checksum of this struct. */
  72. grub_uint16_t version; /* 0x10 - 0x11 NV version. */
  73. grub_uint8_t unit_number; /* 0x12 Disk index in array. */
  74. grub_uint8_t reserved; /* 0x13. */
  75. grub_uint32_t capacity; /* 0x14 - 0x17 Array capacity in sectors. */
  76. grub_uint32_t sector_size; /* 0x18 - 0x1B Sector size. */
  77. char prodid[NV_PRODID_LEN]; /* 0x1C - 0x2B Array product ID. */
  78. char prodrev[NV_PRODREV_LEN]; /* 0x2C - 0x2F Array product revision */
  79. grub_uint32_t unit_flags; /* 0x30 - 0x33 Flags for this disk */
  80. struct grub_nv_array array; /* Array information */
  81. } GRUB_PACKED;
  82. static struct grub_diskfilter_vg *
  83. grub_dmraid_nv_detect (grub_disk_t disk,
  84. struct grub_diskfilter_pv_id *id,
  85. grub_disk_addr_t *start_sector)
  86. {
  87. grub_disk_addr_t sector;
  88. struct grub_nv_super sb;
  89. int level;
  90. grub_uint64_t disk_size;
  91. grub_uint32_t capacity;
  92. grub_uint8_t total_volumes;
  93. char *uuid;
  94. if (disk->partition)
  95. /* Skip partition. */
  96. return NULL;
  97. sector = grub_disk_native_sectors (disk);
  98. if (sector == GRUB_DISK_SIZE_UNKNOWN)
  99. /* Not raid. */
  100. return NULL;
  101. sector -= 2;
  102. if (grub_disk_read (disk, sector, 0, sizeof (sb), &sb))
  103. return NULL;
  104. if (grub_memcmp (sb.vendor, NV_ID_STRING, 6))
  105. /* Not raid. */
  106. return NULL;
  107. if (sb.version != NV_VERSION)
  108. {
  109. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  110. "unknown version: %d.%d", sb.version >> 8, sb.version & 0xFF);
  111. return NULL;
  112. }
  113. capacity = grub_le_to_cpu32 (sb.capacity);
  114. total_volumes = sb.array.total_volumes;
  115. switch (sb.array.raid_level)
  116. {
  117. case NV_LEVEL_0:
  118. level = 0;
  119. if (total_volumes == 0)
  120. /* Not RAID. */
  121. return NULL;
  122. disk_size = capacity / total_volumes;
  123. break;
  124. case NV_LEVEL_1:
  125. level = 1;
  126. disk_size = sb.capacity;
  127. break;
  128. case NV_LEVEL_5:
  129. level = 5;
  130. if (total_volumes == 0 || total_volumes == 1)
  131. /* Not RAID. */
  132. return NULL;
  133. disk_size = capacity / (total_volumes - 1);
  134. break;
  135. default:
  136. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  137. "unsupported RAID level: %d", sb.array.raid_level);
  138. return NULL;
  139. }
  140. uuid = grub_malloc (sizeof (sb.array.signature));
  141. if (! uuid)
  142. return NULL;
  143. grub_memcpy (uuid, (char *) &sb.array.signature,
  144. sizeof (sb.array.signature));
  145. id->uuidlen = 0;
  146. id->id = sb.unit_number;
  147. *start_sector = 0;
  148. return grub_diskfilter_make_raid (sizeof (sb.array.signature),
  149. uuid, sb.array.total_volumes,
  150. "nv", disk_size,
  151. sb.array.stripe_block_size,
  152. GRUB_RAID_LAYOUT_LEFT_ASYMMETRIC,
  153. level);
  154. }
  155. static struct grub_diskfilter grub_dmraid_nv_dev =
  156. {
  157. .name = "dmraid_nv",
  158. .detect = grub_dmraid_nv_detect,
  159. .next = 0
  160. };
  161. GRUB_MOD_INIT(dm_nv)
  162. {
  163. grub_diskfilter_register_front (&grub_dmraid_nv_dev);
  164. }
  165. GRUB_MOD_FINI(dm_nv)
  166. {
  167. grub_diskfilter_unregister (&grub_dmraid_nv_dev);
  168. }