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