dfly.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* dfly.c - Read DragonFly BSD disklabel64. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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/disk.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/partition.h>
  23. GRUB_MOD_LICENSE ("GPLv3+");
  24. static struct grub_partition_map grub_dfly_partition_map;
  25. #define GRUB_PARTITION_DISKLABEL64_HEADER_SIZE 200
  26. /* Full entry is 64 bytes however we really care only
  27. about offset and size which are in first 16 bytes.
  28. Avoid using too much stack. */
  29. #define GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE 64
  30. struct grub_partition_disklabel64_entry
  31. {
  32. grub_uint64_t boffset;
  33. grub_uint64_t bsize;
  34. };
  35. /* Full entry is 200 bytes however we really care only
  36. about magic and number of partitions which are in first 16 bytes.
  37. Avoid using too much stack. */
  38. struct grub_partition_disklabel64
  39. {
  40. grub_uint32_t magic;
  41. #define GRUB_DISKLABEL64_MAGIC ((grub_uint32_t)0xc4464c59)
  42. grub_uint32_t crc;
  43. grub_uint32_t unused;
  44. grub_uint32_t npartitions;
  45. };
  46. static grub_err_t
  47. dfly_partition_map_iterate (grub_disk_t disk,
  48. grub_partition_iterate_hook_t hook,
  49. void *hook_data)
  50. {
  51. struct grub_partition part;
  52. unsigned partno, pos;
  53. struct grub_partition_disklabel64 label;
  54. part.partmap = &grub_dfly_partition_map;
  55. if (grub_disk_read (disk, 1, 0, sizeof (label), &label))
  56. return grub_errno;
  57. if (label.magic != grub_cpu_to_le32_compile_time (GRUB_DISKLABEL64_MAGIC))
  58. {
  59. grub_dprintf ("partition",
  60. "bad magic (found 0x%x; wanted 0x%x)\n",
  61. (unsigned int) grub_le_to_cpu32 (label.magic),
  62. (unsigned int) GRUB_DISKLABEL64_MAGIC);
  63. return grub_error (GRUB_ERR_BAD_PART_TABLE, "disklabel64 not found");
  64. }
  65. pos = GRUB_PARTITION_DISKLABEL64_HEADER_SIZE + GRUB_DISK_SECTOR_SIZE;
  66. for (partno = 0;
  67. partno < grub_le_to_cpu32 (label.npartitions); ++partno)
  68. {
  69. grub_disk_addr_t sector = pos >> GRUB_DISK_SECTOR_BITS;
  70. grub_off_t offset = pos & (GRUB_DISK_SECTOR_SIZE - 1);
  71. struct grub_partition_disklabel64_entry dpart;
  72. pos += GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE;
  73. if (grub_disk_read (disk, sector, offset, sizeof (dpart), &dpart))
  74. return grub_errno;
  75. grub_dprintf ("partition",
  76. "partition %2d: offset 0x%llx, "
  77. "size 0x%llx\n",
  78. partno,
  79. (unsigned long long) grub_le_to_cpu64 (dpart.boffset),
  80. (unsigned long long) grub_le_to_cpu64 (dpart.bsize));
  81. /* Is partition initialized? */
  82. if (dpart.bsize == 0)
  83. continue;
  84. part.number = partno;
  85. part.start = grub_le_to_cpu64 (dpart.boffset) >> GRUB_DISK_SECTOR_BITS;
  86. part.len = grub_le_to_cpu64 (dpart.bsize) >> GRUB_DISK_SECTOR_BITS;
  87. /* This is counter-intuitive, but part.offset and sector have
  88. * the same type, and offset (NOT part.offset) is guaranteed
  89. * to fit into part.index. */
  90. part.offset = sector;
  91. part.index = offset;
  92. if (hook (disk, &part, hook_data))
  93. return grub_errno;
  94. }
  95. return GRUB_ERR_NONE;
  96. }
  97. /* Partition map type. */
  98. static struct grub_partition_map grub_dfly_partition_map =
  99. {
  100. .name = "dfly",
  101. .iterate = dfly_partition_map_iterate,
  102. };
  103. GRUB_MOD_INIT(part_dfly)
  104. {
  105. grub_partition_map_register (&grub_dfly_partition_map);
  106. }
  107. GRUB_MOD_FINI(part_dfly)
  108. {
  109. grub_partition_map_unregister (&grub_dfly_partition_map);
  110. }