sunpc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* sunpc.c - Read SUN PC style partition tables. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2006,2007,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/disk.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/dl.h>
  24. #include <grub/symbol.h>
  25. #include <grub/types.h>
  26. #include <grub/err.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #define GRUB_PARTMAP_SUN_PC_MAGIC 0xDABE
  29. #define GRUB_PARTMAP_SUN_PC_MAX_PARTS 16
  30. #define GRUB_PARTMAP_SUN_PC_WHOLE_DISK_ID 0x05
  31. struct grub_sun_pc_partition_descriptor
  32. {
  33. grub_uint16_t id;
  34. grub_uint16_t unused;
  35. grub_uint32_t start_sector;
  36. grub_uint32_t num_sectors;
  37. } GRUB_PACKED;
  38. struct grub_sun_pc_block
  39. {
  40. grub_uint8_t unused[72];
  41. struct grub_sun_pc_partition_descriptor partitions[GRUB_PARTMAP_SUN_PC_MAX_PARTS];
  42. grub_uint8_t unused2[244];
  43. grub_uint16_t magic; /* Magic number. */
  44. grub_uint16_t csum; /* Label xor'd checksum. */
  45. } GRUB_PACKED;
  46. static struct grub_partition_map grub_sun_pc_partition_map;
  47. /* Verify checksum (true=ok). */
  48. static int
  49. grub_sun_is_valid (grub_uint16_t *label)
  50. {
  51. grub_uint16_t *pos;
  52. grub_uint16_t sum = 0;
  53. for (pos = label;
  54. pos < (label + sizeof (struct grub_sun_pc_block) / 2);
  55. pos++)
  56. sum ^= *pos;
  57. return ! sum;
  58. }
  59. static grub_err_t
  60. sun_pc_partition_map_iterate (grub_disk_t disk,
  61. grub_partition_iterate_hook_t hook,
  62. void *hook_data)
  63. {
  64. grub_partition_t p;
  65. union
  66. {
  67. struct grub_sun_pc_block sun_block;
  68. grub_uint16_t raw[0];
  69. } block;
  70. int partnum;
  71. grub_err_t err;
  72. p = (grub_partition_t) grub_zalloc (sizeof (struct grub_partition));
  73. if (! p)
  74. return grub_errno;
  75. p->partmap = &grub_sun_pc_partition_map;
  76. err = grub_disk_read (disk, 1, 0, sizeof (struct grub_sun_pc_block), &block);
  77. if (err)
  78. {
  79. grub_free (p);
  80. return err;
  81. }
  82. if (GRUB_PARTMAP_SUN_PC_MAGIC != grub_le_to_cpu16 (block.sun_block.magic))
  83. {
  84. grub_free (p);
  85. return grub_error (GRUB_ERR_BAD_PART_TABLE,
  86. "not a sun_pc partition table");
  87. }
  88. if (! grub_sun_is_valid (block.raw))
  89. {
  90. grub_free (p);
  91. return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
  92. }
  93. /* Maybe another error value would be better, because partition
  94. table _is_ recognized but invalid. */
  95. for (partnum = 0; partnum < GRUB_PARTMAP_SUN_PC_MAX_PARTS; partnum++)
  96. {
  97. struct grub_sun_pc_partition_descriptor *desc;
  98. if (block.sun_block.partitions[partnum].id == 0
  99. || block.sun_block.partitions[partnum].id
  100. == GRUB_PARTMAP_SUN_PC_WHOLE_DISK_ID)
  101. continue;
  102. desc = &block.sun_block.partitions[partnum];
  103. p->start = grub_le_to_cpu32 (desc->start_sector);
  104. p->len = grub_le_to_cpu32 (desc->num_sectors);
  105. p->number = partnum;
  106. if (p->len)
  107. {
  108. if (hook (disk, p, hook_data))
  109. partnum = GRUB_PARTMAP_SUN_PC_MAX_PARTS;
  110. }
  111. }
  112. grub_free (p);
  113. return grub_errno;
  114. }
  115. /* Partition map type. */
  116. static struct grub_partition_map grub_sun_pc_partition_map =
  117. {
  118. .name = "sunpc",
  119. .iterate = sun_pc_partition_map_iterate,
  120. };
  121. GRUB_MOD_INIT(part_sunpc)
  122. {
  123. grub_partition_map_register (&grub_sun_pc_partition_map);
  124. }
  125. GRUB_MOD_FINI(part_sunpc)
  126. {
  127. grub_partition_map_unregister (&grub_sun_pc_partition_map);
  128. }