amiga.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* amiga.c - Read amiga partition tables (RDB). */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,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/disk.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/partition.h>
  23. #include <grub/dl.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. #define AMIGA_CHECKSUM_WORDS 128
  26. struct grub_amiga_rdsk
  27. {
  28. /* "RDSK". */
  29. grub_uint8_t magic[4];
  30. #define GRUB_AMIGA_RDSK_MAGIC "RDSK"
  31. grub_uint32_t size;
  32. grub_int32_t checksum;
  33. grub_uint32_t scsihost;
  34. grub_uint32_t blksz;
  35. grub_uint32_t flags;
  36. grub_uint32_t badblcklst;
  37. grub_uint32_t partitionlst;
  38. grub_uint32_t fslst;
  39. grub_uint32_t unused[AMIGA_CHECKSUM_WORDS - 9];
  40. } GRUB_PACKED;
  41. struct grub_amiga_partition
  42. {
  43. /* "PART". */
  44. grub_uint8_t magic[4];
  45. #define GRUB_AMIGA_PART_MAGIC "PART"
  46. grub_uint32_t size;
  47. grub_int32_t checksum;
  48. grub_uint32_t scsihost;
  49. grub_uint32_t next;
  50. grub_uint32_t flags;
  51. grub_uint32_t unused1[2];
  52. grub_uint32_t devflags;
  53. grub_uint8_t namelen;
  54. grub_uint8_t name[31];
  55. grub_uint32_t unused2[15];
  56. grub_uint32_t unused3[3];
  57. grub_uint32_t heads;
  58. grub_uint32_t unused4;
  59. grub_uint32_t block_per_track;
  60. grub_uint32_t unused5[3];
  61. grub_uint32_t lowcyl;
  62. grub_uint32_t highcyl;
  63. grub_uint32_t firstcyl;
  64. grub_uint32_t unused[AMIGA_CHECKSUM_WORDS - 44];
  65. } GRUB_PACKED;
  66. static struct grub_partition_map grub_amiga_partition_map;
  67. static grub_uint32_t
  68. amiga_partition_map_checksum (void *buf)
  69. {
  70. grub_uint32_t *ptr = buf;
  71. grub_uint32_t r = 0;
  72. grub_size_t sz;
  73. /* Fancy and quick way of checking sz >= 512 / 4 = 128. */
  74. if (ptr[1] & ~grub_cpu_to_be32_compile_time (AMIGA_CHECKSUM_WORDS - 1))
  75. sz = AMIGA_CHECKSUM_WORDS;
  76. else
  77. sz = grub_be_to_cpu32 (ptr[1]);
  78. for (; sz; sz--, ptr++)
  79. r += grub_be_to_cpu32 (*ptr);
  80. return r;
  81. }
  82. static grub_err_t
  83. amiga_partition_map_iterate (grub_disk_t disk,
  84. grub_partition_iterate_hook_t hook,
  85. void *hook_data)
  86. {
  87. struct grub_partition part;
  88. struct grub_amiga_rdsk rdsk;
  89. int partno = 0;
  90. int next = -1;
  91. unsigned pos;
  92. /* The RDSK block is one of the first 15 blocks. */
  93. for (pos = 0; pos < 15; pos++)
  94. {
  95. /* Read the RDSK block which is a descriptor for the entire disk. */
  96. if (grub_disk_read (disk, pos, 0, sizeof (rdsk), &rdsk))
  97. return grub_errno;
  98. if (grub_memcmp (rdsk.magic, GRUB_AMIGA_RDSK_MAGIC,
  99. sizeof (rdsk.magic)) == 0
  100. && amiga_partition_map_checksum (&rdsk) == 0)
  101. {
  102. /* Found the first PART block. */
  103. next = grub_be_to_cpu32 (rdsk.partitionlst);
  104. break;
  105. }
  106. }
  107. if (next == -1)
  108. return grub_error (GRUB_ERR_BAD_PART_TABLE,
  109. "Amiga partition map not found");
  110. /* The end of the partition list is marked using "-1". */
  111. while (next != -1)
  112. {
  113. struct grub_amiga_partition apart;
  114. /* Read the RDSK block which is a descriptor for the entire disk. */
  115. if (grub_disk_read (disk, next, 0, sizeof (apart), &apart))
  116. return grub_errno;
  117. if (grub_memcmp (apart.magic, GRUB_AMIGA_PART_MAGIC,
  118. sizeof (apart.magic)) != 0
  119. || amiga_partition_map_checksum (&apart) != 0)
  120. return grub_error (GRUB_ERR_BAD_PART_TABLE,
  121. "invalid Amiga partition map");
  122. /* Calculate the first block and the size of the partition. */
  123. part.start = (grub_be_to_cpu32 (apart.lowcyl)
  124. * grub_be_to_cpu32 (apart.heads)
  125. * grub_be_to_cpu32 (apart.block_per_track));
  126. part.len = ((grub_be_to_cpu32 (apart.highcyl)
  127. - grub_be_to_cpu32 (apart.lowcyl) + 1)
  128. * grub_be_to_cpu32 (apart.heads)
  129. * grub_be_to_cpu32 (apart.block_per_track));
  130. part.offset = next;
  131. part.number = partno;
  132. part.index = 0;
  133. part.partmap = &grub_amiga_partition_map;
  134. if (hook (disk, &part, hook_data))
  135. return grub_errno;
  136. next = grub_be_to_cpu32 (apart.next);
  137. partno++;
  138. }
  139. return 0;
  140. }
  141. /* Partition map type. */
  142. static struct grub_partition_map grub_amiga_partition_map =
  143. {
  144. .name = "amiga",
  145. .iterate = amiga_partition_map_iterate,
  146. };
  147. GRUB_MOD_INIT(part_amiga)
  148. {
  149. grub_partition_map_register (&grub_amiga_partition_map);
  150. }
  151. GRUB_MOD_FINI(part_amiga)
  152. {
  153. grub_partition_map_unregister (&grub_amiga_partition_map);
  154. }