mac.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/partitions/mac.c
  4. *
  5. * Code extracted from drivers/block/genhd.c
  6. * Copyright (C) 1991-1998 Linus Torvalds
  7. * Re-organised Feb 1998 Russell King
  8. */
  9. #include <linux/ctype.h>
  10. #include "check.h"
  11. #include "mac.h"
  12. #ifdef CONFIG_PPC_PMAC
  13. #include <asm/machdep.h>
  14. extern void note_bootable_part(dev_t dev, int part, int goodness);
  15. #endif
  16. /*
  17. * Code to understand MacOS partition tables.
  18. */
  19. #ifdef CONFIG_PPC_PMAC
  20. static inline void mac_fix_string(char *stg, int len)
  21. {
  22. int i;
  23. for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
  24. stg[i] = 0;
  25. }
  26. #endif
  27. int mac_partition(struct parsed_partitions *state)
  28. {
  29. Sector sect;
  30. unsigned char *data;
  31. int slot, blocks_in_map;
  32. unsigned secsize, datasize, partoffset;
  33. #ifdef CONFIG_PPC_PMAC
  34. int found_root = 0;
  35. int found_root_goodness = 0;
  36. #endif
  37. struct mac_partition *part;
  38. struct mac_driver_desc *md;
  39. /* Get 0th block and look at the first partition map entry. */
  40. md = read_part_sector(state, 0, &sect);
  41. if (!md)
  42. return -1;
  43. if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
  44. put_dev_sector(sect);
  45. return 0;
  46. }
  47. secsize = be16_to_cpu(md->block_size);
  48. put_dev_sector(sect);
  49. /*
  50. * If the "block size" is not a power of 2, things get weird - we might
  51. * end up with a partition straddling a sector boundary, so we wouldn't
  52. * be able to read a partition entry with read_part_sector().
  53. * Real block sizes are probably (?) powers of two, so just require
  54. * that.
  55. */
  56. if (!is_power_of_2(secsize))
  57. return -1;
  58. datasize = round_down(secsize, 512);
  59. data = read_part_sector(state, datasize / 512, &sect);
  60. if (!data)
  61. return -1;
  62. partoffset = secsize % 512;
  63. if (partoffset + sizeof(*part) > datasize) {
  64. put_dev_sector(sect);
  65. return -1;
  66. }
  67. part = (struct mac_partition *) (data + partoffset);
  68. if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
  69. put_dev_sector(sect);
  70. return 0; /* not a MacOS disk */
  71. }
  72. blocks_in_map = be32_to_cpu(part->map_count);
  73. if (blocks_in_map < 0 || blocks_in_map >= DISK_MAX_PARTS) {
  74. put_dev_sector(sect);
  75. return 0;
  76. }
  77. if (blocks_in_map >= state->limit)
  78. blocks_in_map = state->limit - 1;
  79. strlcat(state->pp_buf, " [mac]", PAGE_SIZE);
  80. for (slot = 1; slot <= blocks_in_map; ++slot) {
  81. int pos = slot * secsize;
  82. put_dev_sector(sect);
  83. data = read_part_sector(state, pos/512, &sect);
  84. if (!data)
  85. return -1;
  86. part = (struct mac_partition *) (data + pos%512);
  87. if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
  88. break;
  89. put_partition(state, slot,
  90. be32_to_cpu(part->start_block) * (secsize/512),
  91. be32_to_cpu(part->block_count) * (secsize/512));
  92. if (!strncasecmp(part->type, "Linux_RAID", 10))
  93. state->parts[slot].flags = ADDPART_FLAG_RAID;
  94. #ifdef CONFIG_PPC_PMAC
  95. /*
  96. * If this is the first bootable partition, tell the
  97. * setup code, in case it wants to make this the root.
  98. */
  99. if (machine_is(powermac)) {
  100. int goodness = 0;
  101. mac_fix_string(part->processor, 16);
  102. mac_fix_string(part->name, 32);
  103. mac_fix_string(part->type, 32);
  104. if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
  105. && strcasecmp(part->processor, "powerpc") == 0)
  106. goodness++;
  107. if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
  108. || (strncasecmp(part->type, "Linux", 5) == 0
  109. && strcasecmp(part->type, "Linux_swap") != 0)) {
  110. int i, l;
  111. goodness++;
  112. l = strnlen(part->name, sizeof(part->name));
  113. if (strncmp(part->name, "/", sizeof(part->name)) == 0)
  114. goodness++;
  115. for (i = 0; i <= l - 4; ++i) {
  116. if (strncasecmp(part->name + i, "root",
  117. 4) == 0) {
  118. goodness += 2;
  119. break;
  120. }
  121. }
  122. if (strncasecmp(part->name, "swap", 4) == 0)
  123. goodness--;
  124. }
  125. if (goodness > found_root_goodness) {
  126. found_root = slot;
  127. found_root_goodness = goodness;
  128. }
  129. }
  130. #endif /* CONFIG_PPC_PMAC */
  131. }
  132. #ifdef CONFIG_PPC_PMAC
  133. if (found_root_goodness)
  134. note_bootable_part(state->disk->part0->bd_dev, found_root,
  135. found_root_goodness);
  136. #endif
  137. put_dev_sector(sect);
  138. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  139. return 1;
  140. }