dmraid_nvidia.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/raid.h>
  25. #define NV_SIGNATURES 4
  26. #define NV_IDLE 0
  27. #define NV_SCDB_INIT_RAID 2
  28. #define NV_SCDB_REBUILD_RAID 3
  29. #define NV_SCDB_UPGRADE_RAID 4
  30. #define NV_SCDB_SYNC_RAID 5
  31. #define NV_LEVEL_UNKNOWN 0x00
  32. #define NV_LEVEL_JBOD 0xFF
  33. #define NV_LEVEL_0 0x80
  34. #define NV_LEVEL_1 0x81
  35. #define NV_LEVEL_3 0x83
  36. #define NV_LEVEL_5 0x85
  37. #define NV_LEVEL_10 0x8a
  38. #define NV_LEVEL_1_0 0x8180
  39. #define NV_ARRAY_FLAG_BOOT 1 /* BIOS use only. */
  40. #define NV_ARRAY_FLAG_ERROR 2 /* Degraded or offline. */
  41. #define NV_ARRAY_FLAG_PARITY_VALID 4 /* RAID-3/5 parity valid. */
  42. struct grub_nv_array
  43. {
  44. grub_uint32_t version;
  45. grub_uint32_t signature[NV_SIGNATURES];
  46. grub_uint8_t raid_job_code;
  47. grub_uint8_t stripe_width;
  48. grub_uint8_t total_volumes;
  49. grub_uint8_t original_width;
  50. grub_uint32_t raid_level;
  51. grub_uint32_t stripe_block_size;
  52. grub_uint32_t stripe_block_size_bytes;
  53. grub_uint32_t stripe_block_size_log2;
  54. grub_uint32_t stripe_mask;
  55. grub_uint32_t stripe_size;
  56. grub_uint32_t stripe_size_bytes;
  57. grub_uint32_t raid_job_mask;
  58. grub_uint32_t original_capacity;
  59. grub_uint32_t flags;
  60. };
  61. #define NV_ID_LEN 8
  62. #define NV_ID_STRING "NVIDIA"
  63. #define NV_VERSION 100
  64. #define NV_PRODID_LEN 16
  65. #define NV_PRODREV_LEN 4
  66. struct grub_nv_super
  67. {
  68. char vendor[NV_ID_LEN]; /* 0x00 - 0x07 ID string. */
  69. grub_uint32_t size; /* 0x08 - 0x0B Size of metadata in dwords. */
  70. grub_uint32_t chksum; /* 0x0C - 0x0F Checksum of this struct. */
  71. grub_uint16_t version; /* 0x10 - 0x11 NV version. */
  72. grub_uint8_t unit_number; /* 0x12 Disk index in array. */
  73. grub_uint8_t reserved; /* 0x13. */
  74. grub_uint32_t capacity; /* 0x14 - 0x17 Array capacity in sectors. */
  75. grub_uint32_t sector_size; /* 0x18 - 0x1B Sector size. */
  76. char prodid[NV_PRODID_LEN]; /* 0x1C - 0x2B Array product ID. */
  77. char prodrev[NV_PRODREV_LEN]; /* 0x2C - 0x2F Array product revision */
  78. grub_uint32_t unit_flags; /* 0x30 - 0x33 Flags for this disk */
  79. struct grub_nv_array array; /* Array information */
  80. } __attribute__ ((packed));
  81. static grub_err_t
  82. grub_dmraid_nv_detect (grub_disk_t disk, struct grub_raid_array *array)
  83. {
  84. grub_disk_addr_t sector;
  85. struct grub_nv_super sb;
  86. if (disk->partition)
  87. return grub_error (GRUB_ERR_OUT_OF_RANGE, "skip partition");
  88. sector = grub_disk_get_size (disk) - 2;
  89. if (grub_disk_read (disk, sector, 0, sizeof (sb), &sb))
  90. return grub_errno;
  91. if (grub_memcmp (sb.vendor, NV_ID_STRING, 6))
  92. return grub_error (GRUB_ERR_OUT_OF_RANGE, "not raid");
  93. if (sb.version != NV_VERSION)
  94. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  95. "unknown version: %d.%d", sb.version);
  96. switch (sb.array.raid_level)
  97. {
  98. case NV_LEVEL_0:
  99. array->level = 0;
  100. array->disk_size = sb.capacity / sb.array.total_volumes;
  101. break;
  102. case NV_LEVEL_1:
  103. array->level = 1;
  104. array->disk_size = sb.capacity;
  105. break;
  106. case NV_LEVEL_5:
  107. array->level = 5;
  108. array->layout = GRUB_RAID_LAYOUT_LEFT_ASYMMETRIC;
  109. array->disk_size = sb.capacity / (sb.array.total_volumes - 1);
  110. break;
  111. default:
  112. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  113. "unsupported RAID level: %d", sb.array.raid_level);
  114. }
  115. array->number = 0;
  116. array->total_devs = sb.array.total_volumes;
  117. array->chunk_size = sb.array.stripe_block_size;
  118. array->disk_offset = 0;
  119. array->index = sb.unit_number;
  120. array->uuid_len = sizeof (sb.array.signature);
  121. array->uuid = grub_malloc (sizeof (sb.array.signature));
  122. if (! array->uuid)
  123. return grub_errno;
  124. grub_memcpy (array->uuid, (char *) &sb.array.signature,
  125. sizeof (sb.array.signature));
  126. return 0;
  127. }
  128. static struct grub_raid grub_dmraid_nv_dev =
  129. {
  130. .name = "dmraid_nv",
  131. .detect = grub_dmraid_nv_detect,
  132. .next = 0
  133. };
  134. GRUB_MOD_INIT(dm_nv)
  135. {
  136. grub_raid_register (&grub_dmraid_nv_dev);
  137. }
  138. GRUB_MOD_FINI(dm_nv)
  139. {
  140. grub_raid_unregister (&grub_dmraid_nv_dev);
  141. }