apple.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* apple.c - Read macintosh 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/disk.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/partition.h>
  23. #define GRUB_APPLE_HEADER_MAGIC 0x4552
  24. #define GRUB_APPLE_PART_MAGIC 0x504D
  25. struct grub_apple_header
  26. {
  27. /* The magic number to identify the partition map, it should have
  28. the value `0x4552'. */
  29. grub_uint16_t magic;
  30. grub_uint16_t blocksize;
  31. };
  32. struct grub_apple_part
  33. {
  34. /* The magic number to identify this as a partition, it should have
  35. the value `0x504D'. */
  36. grub_uint16_t magic;
  37. /* Reserved. */
  38. grub_uint16_t reserved;
  39. /* The size of the partition map in blocks. */
  40. grub_uint32_t partmap_size;
  41. /* The first physical block of the partition. */
  42. grub_uint32_t first_phys_block;
  43. /* The amount of blocks. */
  44. grub_uint32_t blockcnt;
  45. /* The partition name. */
  46. char partname[32];
  47. /* The partition type. */
  48. char parttype[32];
  49. /* The first datablock of the partition. */
  50. grub_uint32_t datablocks_first;
  51. /* The amount datablocks. */
  52. grub_uint32_t datablocks_count;
  53. /* The status of the partition. (???) */
  54. grub_uint32_t status;
  55. /* The first block on which the bootcode can be found. */
  56. grub_uint32_t bootcode_pos;
  57. /* The size of the bootcode in bytes. */
  58. grub_uint32_t bootcode_size;
  59. /* The load address of the bootcode. */
  60. grub_uint32_t bootcode_loadaddr;
  61. /* Reserved. */
  62. grub_uint32_t reserved2;
  63. /* The entry point of the bootcode. */
  64. grub_uint32_t bootcode_entrypoint;
  65. /* Reserved. */
  66. grub_uint32_t reserved3;
  67. /* A checksum of the bootcode. */
  68. grub_uint32_t bootcode_checksum;
  69. /* The processor type. */
  70. char processor[16];
  71. /* Padding. */
  72. grub_uint16_t pad[187];
  73. };
  74. static struct grub_partition_map grub_apple_partition_map;
  75. static grub_err_t
  76. apple_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. struct grub_partition part;
  83. struct grub_apple_header aheader;
  84. struct grub_apple_part apart;
  85. int partno = 0, partnum = 0;
  86. unsigned pos;
  87. part.partmap = &grub_apple_partition_map;
  88. if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
  89. return grub_errno;
  90. if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
  91. {
  92. grub_dprintf ("partition",
  93. "bad magic (found 0x%x; wanted 0x%x\n",
  94. grub_be_to_cpu16 (aheader.magic),
  95. GRUB_APPLE_HEADER_MAGIC);
  96. goto fail;
  97. }
  98. pos = grub_be_to_cpu16 (aheader.blocksize);
  99. do
  100. {
  101. part.offset = pos / GRUB_DISK_SECTOR_SIZE;
  102. part.index = pos % GRUB_DISK_SECTOR_SIZE;
  103. if (grub_disk_read (disk, part.offset, part.index,
  104. sizeof (struct grub_apple_part), &apart))
  105. return grub_errno;
  106. if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
  107. {
  108. grub_dprintf ("partition",
  109. "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
  110. partno, grub_be_to_cpu16 (apart.magic),
  111. GRUB_APPLE_PART_MAGIC);
  112. break;
  113. }
  114. if (partnum == 0)
  115. partnum = grub_be_to_cpu32 (apart.partmap_size);
  116. part.start = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.first_phys_block)
  117. * grub_be_to_cpu16 (aheader.blocksize))
  118. / GRUB_DISK_SECTOR_SIZE;
  119. part.len = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.blockcnt)
  120. * grub_be_to_cpu16 (aheader.blocksize))
  121. / GRUB_DISK_SECTOR_SIZE;
  122. part.offset = pos;
  123. part.index = partno;
  124. part.number = partno;
  125. grub_dprintf ("partition",
  126. "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
  127. partno, apart.partname, apart.parttype,
  128. grub_be_to_cpu32 (apart.first_phys_block),
  129. grub_be_to_cpu32 (apart.blockcnt));
  130. if (hook (disk, &part, closure))
  131. return grub_errno;
  132. pos += grub_be_to_cpu16 (aheader.blocksize);
  133. partno++;
  134. }
  135. while (partno < partnum);
  136. if (partno != 0)
  137. return 0;
  138. fail:
  139. return grub_error (GRUB_ERR_BAD_PART_TABLE,
  140. "Apple partition map not found");
  141. }
  142. /* Partition map type. */
  143. static struct grub_partition_map grub_apple_partition_map =
  144. {
  145. .name = "apple",
  146. .iterate = apple_partition_map_iterate,
  147. };
  148. GRUB_MOD_INIT(part_apple)
  149. {
  150. grub_partition_map_register (&grub_apple_partition_map);
  151. }
  152. GRUB_MOD_FINI(part_apple)
  153. {
  154. grub_partition_map_unregister (&grub_apple_partition_map);
  155. }