part_tbl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * linux/fs/hfs/part_tbl.c
  3. *
  4. * Copyright (C) 1996-1997 Paul H. Hargrove
  5. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6. * This file may be distributed under the terms of the GNU General Public License.
  7. *
  8. * Original code to handle the new style Mac partition table based on
  9. * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
  10. */
  11. #include "hfs_fs.h"
  12. /*
  13. * The new style Mac partition map
  14. *
  15. * For each partition on the media there is a physical block (512-byte
  16. * block) containing one of these structures. These blocks are
  17. * contiguous starting at block 1.
  18. */
  19. struct new_pmap {
  20. __be16 pmSig; /* signature */
  21. __be16 reSigPad; /* padding */
  22. __be32 pmMapBlkCnt; /* partition blocks count */
  23. __be32 pmPyPartStart; /* physical block start of partition */
  24. __be32 pmPartBlkCnt; /* physical block count of partition */
  25. u8 pmPartName[32]; /* (null terminated?) string
  26. giving the name of this
  27. partition */
  28. u8 pmPartType[32]; /* (null terminated?) string
  29. giving the type of this
  30. partition */
  31. /* a bunch more stuff we don't need */
  32. } __packed;
  33. /*
  34. * The old style Mac partition map
  35. *
  36. * The partition map consists for a 2-byte signature followed by an
  37. * array of these structures. The map is terminated with an all-zero
  38. * one of these.
  39. */
  40. struct old_pmap {
  41. __be16 pdSig; /* Signature bytes */
  42. struct old_pmap_entry {
  43. __be32 pdStart;
  44. __be32 pdSize;
  45. __be32 pdFSID;
  46. } pdEntry[42];
  47. } __packed;
  48. /*
  49. * hfs_part_find()
  50. *
  51. * Parse the partition map looking for the
  52. * start and length of the 'part'th HFS partition.
  53. */
  54. int hfs_part_find(struct super_block *sb,
  55. sector_t *part_start, sector_t *part_size)
  56. {
  57. struct buffer_head *bh;
  58. __be16 *data;
  59. int i, size, res;
  60. res = -ENOENT;
  61. bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK, data);
  62. if (!bh)
  63. return -EIO;
  64. switch (be16_to_cpu(*data)) {
  65. case HFS_OLD_PMAP_MAGIC:
  66. {
  67. struct old_pmap *pm;
  68. struct old_pmap_entry *p;
  69. pm = (struct old_pmap *)bh->b_data;
  70. p = pm->pdEntry;
  71. size = 42;
  72. for (i = 0; i < size; p++, i++) {
  73. if (p->pdStart && p->pdSize &&
  74. p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
  75. (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
  76. *part_start += be32_to_cpu(p->pdStart);
  77. *part_size = be32_to_cpu(p->pdSize);
  78. res = 0;
  79. }
  80. }
  81. break;
  82. }
  83. case HFS_NEW_PMAP_MAGIC:
  84. {
  85. struct new_pmap *pm;
  86. pm = (struct new_pmap *)bh->b_data;
  87. size = be32_to_cpu(pm->pmMapBlkCnt);
  88. for (i = 0; i < size;) {
  89. if (!memcmp(pm->pmPartType,"Apple_HFS", 9) &&
  90. (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
  91. *part_start += be32_to_cpu(pm->pmPyPartStart);
  92. *part_size = be32_to_cpu(pm->pmPartBlkCnt);
  93. res = 0;
  94. break;
  95. }
  96. brelse(bh);
  97. bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK + ++i, pm);
  98. if (!bh)
  99. return -EIO;
  100. if (pm->pmSig != cpu_to_be16(HFS_NEW_PMAP_MAGIC))
  101. break;
  102. }
  103. break;
  104. }
  105. }
  106. brelse(bh);
  107. return res;
  108. }