sun.c 4.4 KB

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