bsdlabel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* bsdlabel.c - Read BSD style partition tables. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2005,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/partition.h>
  20. #include <grub/bsdlabel.h>
  21. #include <grub/disk.h>
  22. #include <grub/mm.h>
  23. #include <grub/misc.h>
  24. #include <grub/dl.h>
  25. static struct grub_partition_map grub_bsdlabel_partition_map;
  26. static grub_err_t
  27. bsdlabel_partition_map_iterate (grub_disk_t disk,
  28. int (*hook) (grub_disk_t disk,
  29. const grub_partition_t partition,
  30. void *closure),
  31. void *closure)
  32. {
  33. struct grub_partition_bsd_disk_label label;
  34. struct grub_partition p;
  35. grub_disk_addr_t delta = 0;
  36. unsigned pos;
  37. /* BSDLabel offsets are absolute even when it's embed inside partition. */
  38. delta = grub_partition_get_start (disk->partition);
  39. /* Read the BSD label. */
  40. if (grub_disk_read (disk, GRUB_PC_PARTITION_BSD_LABEL_SECTOR,
  41. 0, sizeof (label), &label))
  42. return grub_errno;
  43. /* Check if it is valid. */
  44. if (label.magic != grub_cpu_to_le32 (GRUB_PC_PARTITION_BSD_LABEL_MAGIC))
  45. return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
  46. pos = sizeof (label) + GRUB_PC_PARTITION_BSD_LABEL_SECTOR
  47. * GRUB_DISK_SECTOR_SIZE;
  48. for (p.number = 0;
  49. p.number < grub_cpu_to_le16 (label.num_partitions);
  50. p.number++)
  51. {
  52. struct grub_partition_bsd_entry be;
  53. p.offset = pos / GRUB_DISK_SECTOR_SIZE;
  54. p.index = pos % GRUB_DISK_SECTOR_SIZE;
  55. if (grub_disk_read (disk, p.offset, p.index, sizeof (be), &be))
  56. return grub_errno;
  57. p.start = grub_le_to_cpu32 (be.offset) - delta;
  58. p.len = grub_le_to_cpu32 (be.size);
  59. p.partmap = &grub_bsdlabel_partition_map;
  60. if (be.fs_type != GRUB_PC_PARTITION_BSD_TYPE_UNUSED)
  61. if (hook (disk, &p, closure))
  62. return grub_errno;
  63. pos += sizeof (struct grub_partition_bsd_entry);
  64. }
  65. return GRUB_ERR_NONE;
  66. }
  67. /* Partition map type. */
  68. static struct grub_partition_map grub_bsdlabel_partition_map =
  69. {
  70. .name = "bsd",
  71. .iterate = bsdlabel_partition_map_iterate,
  72. };
  73. GRUB_MOD_INIT(part_bsd)
  74. {
  75. grub_partition_map_register (&grub_bsdlabel_partition_map);
  76. }
  77. GRUB_MOD_FINI(part_bsd)
  78. {
  79. grub_partition_map_unregister (&grub_bsdlabel_partition_map);
  80. }