dvh.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2005,2006,2007,2011 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. #include <grub/partition.h>
  19. #include <grub/disk.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/dl.h>
  23. #include <grub/symbol.h>
  24. #include <grub/types.h>
  25. #include <grub/err.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. #define DVH_MAGIC 0x0be5a941
  28. struct grub_dvh_partition_descriptor
  29. {
  30. grub_uint32_t length;
  31. grub_uint32_t start;
  32. grub_uint32_t type;
  33. } GRUB_PACKED;
  34. struct grub_dvh_block
  35. {
  36. grub_uint32_t magic;
  37. grub_uint8_t unused[308];
  38. struct grub_dvh_partition_descriptor parts[16];
  39. grub_uint32_t checksum;
  40. grub_uint32_t unused2;
  41. } GRUB_PACKED;
  42. static struct grub_partition_map grub_dvh_partition_map;
  43. /* Verify checksum (true=ok). */
  44. static int
  45. grub_dvh_is_valid (grub_uint32_t *label)
  46. {
  47. grub_uint32_t *pos;
  48. grub_uint32_t sum = 0;
  49. for (pos = label;
  50. pos < (label + sizeof (struct grub_dvh_block) / 4);
  51. pos++)
  52. sum += grub_be_to_cpu32 (*pos);
  53. return ! sum;
  54. }
  55. static grub_err_t
  56. dvh_partition_map_iterate (grub_disk_t disk,
  57. grub_partition_iterate_hook_t hook, void *hook_data)
  58. {
  59. struct grub_partition p;
  60. union
  61. {
  62. struct grub_dvh_block dvh;
  63. grub_uint32_t raw[0];
  64. } block;
  65. unsigned partnum;
  66. grub_err_t err;
  67. p.partmap = &grub_dvh_partition_map;
  68. err = grub_disk_read (disk, 0, 0, sizeof (struct grub_dvh_block),
  69. &block);
  70. if (err)
  71. return err;
  72. if (DVH_MAGIC != grub_be_to_cpu32 (block.dvh.magic))
  73. return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a dvh partition table");
  74. if (! grub_dvh_is_valid (block.raw))
  75. return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
  76. /* Maybe another error value would be better, because partition
  77. table _is_ recognized but invalid. */
  78. for (partnum = 0; partnum < ARRAY_SIZE (block.dvh.parts); partnum++)
  79. {
  80. if (block.dvh.parts[partnum].length == 0)
  81. continue;
  82. if (partnum == 10)
  83. continue;
  84. p.start = grub_be_to_cpu32 (block.dvh.parts[partnum].start);
  85. p.len = grub_be_to_cpu32 (block.dvh.parts[partnum].length);
  86. p.number = p.index = partnum;
  87. if (hook (disk, &p, hook_data))
  88. break;
  89. }
  90. return grub_errno;
  91. }
  92. /* Partition map type. */
  93. static struct grub_partition_map grub_dvh_partition_map =
  94. {
  95. .name = "dvh",
  96. .iterate = dvh_partition_map_iterate,
  97. };
  98. GRUB_MOD_INIT(part_dvh)
  99. {
  100. grub_partition_map_register (&grub_dvh_partition_map);
  101. }
  102. GRUB_MOD_FINI(part_dvh)
  103. {
  104. grub_partition_map_unregister (&grub_dvh_partition_map);
  105. }