sun.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* sun.c - Read SUN style partition tables. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2006,2007 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_MAGIC 0xDABE
  28. #define GRUB_PARTMAP_SUN_MAX_PARTS 8
  29. #define GRUB_PARTMAP_SUN_WHOLE_DISK_ID 0x05
  30. struct grub_sun_partition_info
  31. {
  32. grub_uint8_t spare1;
  33. grub_uint8_t id;
  34. grub_uint8_t spare2;
  35. grub_uint8_t flags;
  36. } __attribute__ ((packed));
  37. struct grub_sun_partition_descriptor
  38. {
  39. grub_uint32_t start_cylinder;
  40. grub_uint32_t num_sectors;
  41. } __attribute__ ((packed));
  42. struct grub_sun_block
  43. {
  44. grub_uint8_t info[128]; /* Informative text string. */
  45. grub_uint8_t spare0[14];
  46. struct grub_sun_partition_info infos[8];
  47. grub_uint8_t spare1[246]; /* Boot information etc. */
  48. grub_uint16_t rspeed; /* Disk rotational speed. */
  49. grub_uint16_t pcylcount; /* Physical cylinder count. */
  50. grub_uint16_t sparecyl; /* extra sects per cylinder. */
  51. grub_uint8_t spare2[4]; /* More magic... */
  52. grub_uint16_t ilfact; /* Interleave factor. */
  53. grub_uint16_t ncyl; /* Data cylinder count. */
  54. grub_uint16_t nacyl; /* Alt. cylinder count. */
  55. grub_uint16_t ntrks; /* Tracks per cylinder. */
  56. grub_uint16_t nsect; /* Sectors per track. */
  57. grub_uint8_t spare3[4]; /* Even more magic... */
  58. struct grub_sun_partition_descriptor partitions[8];
  59. grub_uint16_t magic; /* Magic number. */
  60. grub_uint16_t csum; /* Label xor'd checksum. */
  61. } __attribute__ ((packed));
  62. static struct grub_partition_map grub_sun_partition_map;
  63. /* Verify checksum (true=ok). */
  64. static int
  65. grub_sun_is_valid (struct grub_sun_block *label)
  66. {
  67. grub_uint16_t *pos;
  68. grub_uint16_t sum = 0;
  69. for (pos = (grub_uint16_t *) label;
  70. pos < (grub_uint16_t *) (label + 1);
  71. pos++)
  72. sum ^= *pos;
  73. return ! sum;
  74. }
  75. static grub_err_t
  76. sun_partition_map_iterate (grub_disk_t disk,
  77. int (*hook) (grub_disk_t disk,
  78. const grub_partition_t partition,
  79. void *closure),
  80. void *closure)
  81. {
  82. grub_partition_t p;
  83. struct grub_sun_block block;
  84. int partnum;
  85. grub_err_t err;
  86. p = (grub_partition_t) grub_zalloc (sizeof (struct grub_partition));
  87. if (! p)
  88. return grub_errno;
  89. p->partmap = &grub_sun_partition_map;
  90. err = grub_disk_read (disk, 0, 0, sizeof (struct grub_sun_block),
  91. &block);
  92. if (err)
  93. {
  94. grub_free (p);
  95. return err;
  96. }
  97. if (GRUB_PARTMAP_SUN_MAGIC != grub_be_to_cpu16 (block.magic))
  98. {
  99. grub_free (p);
  100. return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a sun partition table");
  101. }
  102. if (! grub_sun_is_valid (&block))
  103. {
  104. grub_free (p);
  105. return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
  106. }
  107. /* Maybe another error value would be better, because partition
  108. table _is_ recognized but invalid. */
  109. for (partnum = 0; partnum < GRUB_PARTMAP_SUN_MAX_PARTS; partnum++)
  110. {
  111. struct grub_sun_partition_descriptor *desc;
  112. if (block.infos[partnum].id == 0
  113. || block.infos[partnum].id == GRUB_PARTMAP_SUN_WHOLE_DISK_ID)
  114. continue;
  115. desc = &block.partitions[partnum];
  116. p->start = ((grub_uint64_t) grub_be_to_cpu32 (desc->start_cylinder)
  117. * grub_be_to_cpu16 (block.ntrks)
  118. * grub_be_to_cpu16 (block.nsect));
  119. p->len = grub_be_to_cpu32 (desc->num_sectors);
  120. p->number = p->index = partnum;
  121. if (p->len)
  122. {
  123. if (hook (disk, p, closure))
  124. partnum = GRUB_PARTMAP_SUN_MAX_PARTS;
  125. }
  126. }
  127. grub_free (p);
  128. return grub_errno;
  129. }
  130. /* Partition map type. */
  131. static struct grub_partition_map grub_sun_partition_map =
  132. {
  133. .name = "sun",
  134. .iterate = sun_partition_map_iterate,
  135. };
  136. GRUB_MOD_INIT(part_sun)
  137. {
  138. grub_partition_map_register (&grub_sun_partition_map);
  139. }
  140. GRUB_MOD_FINI(part_sun)
  141. {
  142. grub_partition_map_unregister (&grub_sun_partition_map);
  143. }