acorn.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* acorn.c - Read Linux/ADFS partition tables. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,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/acorn_filecore.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. #define LINUX_NATIVE_MAGIC grub_cpu_to_le32_compile_time (0xdeafa1de)
  26. #define LINUX_SWAP_MAGIC grub_cpu_to_le32_compile_time (0xdeafab1e)
  27. #define LINUX_MAP_ENTRIES (512 / 12)
  28. #define NONADFS_PARTITION_TYPE_LINUX 9
  29. #define NONADFS_PARTITION_TYPE_MASK 15
  30. struct grub_acorn_boot_block
  31. {
  32. union
  33. {
  34. struct
  35. {
  36. grub_uint8_t misc[0x1C0];
  37. struct grub_filecore_disc_record disc_record;
  38. grub_uint8_t flags;
  39. grub_uint16_t start_cylinder;
  40. grub_uint8_t checksum;
  41. } GRUB_PACKED;
  42. grub_uint8_t bin[0x200];
  43. };
  44. } GRUB_PACKED;
  45. struct linux_part
  46. {
  47. grub_uint32_t magic;
  48. grub_uint32_t start;
  49. grub_uint32_t size;
  50. };
  51. static struct grub_partition_map grub_acorn_partition_map;
  52. static grub_err_t
  53. acorn_partition_map_find (grub_disk_t disk, struct linux_part *m,
  54. grub_disk_addr_t *sector)
  55. {
  56. struct grub_acorn_boot_block boot;
  57. grub_err_t err;
  58. unsigned int checksum = 0;
  59. unsigned int heads;
  60. unsigned int sectors_per_cylinder;
  61. int i;
  62. err = grub_disk_read (disk, 0xC00 / GRUB_DISK_SECTOR_SIZE, 0,
  63. sizeof (struct grub_acorn_boot_block),
  64. &boot);
  65. if (err)
  66. return err;
  67. if ((boot.flags & NONADFS_PARTITION_TYPE_MASK) != NONADFS_PARTITION_TYPE_LINUX)
  68. goto fail;
  69. for (i = 0; i != 0x1ff; ++i)
  70. checksum = ((checksum & 0xff) + (checksum >> 8) + boot.bin[i]);
  71. if ((grub_uint8_t) checksum != boot.checksum)
  72. goto fail;
  73. heads = (boot.disc_record.heads
  74. + ((boot.disc_record.lowsector >> 6) & 1));
  75. sectors_per_cylinder = boot.disc_record.secspertrack * heads;
  76. *sector = grub_le_to_cpu16 (boot.start_cylinder) * sectors_per_cylinder;
  77. return grub_disk_read (disk, *sector, 0,
  78. sizeof (struct linux_part) * LINUX_MAP_ENTRIES,
  79. m);
  80. fail:
  81. return grub_error (GRUB_ERR_BAD_PART_TABLE,
  82. "Linux/ADFS partition map not found");
  83. }
  84. static grub_err_t
  85. acorn_partition_map_iterate (grub_disk_t disk,
  86. grub_partition_iterate_hook_t hook,
  87. void *hook_data)
  88. {
  89. struct grub_partition part;
  90. struct linux_part map[LINUX_MAP_ENTRIES];
  91. int i;
  92. grub_disk_addr_t sector = 0;
  93. grub_err_t err;
  94. err = acorn_partition_map_find (disk, map, &sector);
  95. if (err)
  96. return err;
  97. part.partmap = &grub_acorn_partition_map;
  98. for (i = 0; i != LINUX_MAP_ENTRIES; ++i)
  99. {
  100. if (map[i].magic != LINUX_NATIVE_MAGIC
  101. && map[i].magic != LINUX_SWAP_MAGIC)
  102. return GRUB_ERR_NONE;
  103. part.start = sector + map[i].start;
  104. part.len = map[i].size;
  105. part.offset = 6;
  106. part.number = part.index = i;
  107. if (hook (disk, &part, hook_data))
  108. return grub_errno;
  109. }
  110. return GRUB_ERR_NONE;
  111. }
  112. /* Partition map type. */
  113. static struct grub_partition_map grub_acorn_partition_map =
  114. {
  115. .name = "acorn",
  116. .iterate = acorn_partition_map_iterate,
  117. };
  118. GRUB_MOD_INIT(part_acorn)
  119. {
  120. grub_partition_map_register (&grub_acorn_partition_map);
  121. }
  122. GRUB_MOD_FINI(part_acorn)
  123. {
  124. grub_partition_map_unregister (&grub_acorn_partition_map);
  125. }