sunpc.c 3.8 KB

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