mac.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. static inline void mac_fix_string(char *stg, int len)
  20. {
  21. int i;
  22. for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
  23. stg[i] = 0;
  24. }
  25. int mac_partition(struct parsed_partitions *state)
  26. {
  27. Sector sect;
  28. unsigned char *data;
  29. int slot, blocks_in_map;
  30. unsigned secsize, datasize, partoffset;
  31. #ifdef CONFIG_PPC_PMAC
  32. int found_root = 0;
  33. int found_root_goodness = 0;
  34. #endif
  35. struct mac_partition *part;
  36. struct mac_driver_desc *md;
  37. /* Get 0th block and look at the first partition map entry. */
  38. md = read_part_sector(state, 0, &sect);
  39. if (!md)
  40. return -1;
  41. if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
  42. put_dev_sector(sect);
  43. return 0;
  44. }
  45. secsize = be16_to_cpu(md->block_size);
  46. put_dev_sector(sect);
  47. datasize = round_down(secsize, 512);
  48. data = read_part_sector(state, datasize / 512, &sect);
  49. if (!data)
  50. return -1;
  51. partoffset = secsize % 512;
  52. if (partoffset + sizeof(*part) > datasize)
  53. return -1;
  54. part = (struct mac_partition *) (data + partoffset);
  55. if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
  56. put_dev_sector(sect);
  57. return 0; /* not a MacOS disk */
  58. }
  59. blocks_in_map = be32_to_cpu(part->map_count);
  60. if (blocks_in_map < 0 || blocks_in_map >= DISK_MAX_PARTS) {
  61. put_dev_sector(sect);
  62. return 0;
  63. }
  64. if (blocks_in_map >= state->limit)
  65. blocks_in_map = state->limit - 1;
  66. strlcat(state->pp_buf, " [mac]", PAGE_SIZE);
  67. for (slot = 1; slot <= blocks_in_map; ++slot) {
  68. int pos = slot * secsize;
  69. put_dev_sector(sect);
  70. data = read_part_sector(state, pos/512, &sect);
  71. if (!data)
  72. return -1;
  73. part = (struct mac_partition *) (data + pos%512);
  74. if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
  75. break;
  76. put_partition(state, slot,
  77. be32_to_cpu(part->start_block) * (secsize/512),
  78. be32_to_cpu(part->block_count) * (secsize/512));
  79. if (!strncasecmp(part->type, "Linux_RAID", 10))
  80. state->parts[slot].flags = ADDPART_FLAG_RAID;
  81. #ifdef CONFIG_PPC_PMAC
  82. /*
  83. * If this is the first bootable partition, tell the
  84. * setup code, in case it wants to make this the root.
  85. */
  86. if (machine_is(powermac)) {
  87. int goodness = 0;
  88. mac_fix_string(part->processor, 16);
  89. mac_fix_string(part->name, 32);
  90. mac_fix_string(part->type, 32);
  91. if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
  92. && strcasecmp(part->processor, "powerpc") == 0)
  93. goodness++;
  94. if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
  95. || (strncasecmp(part->type, "Linux", 5) == 0
  96. && strcasecmp(part->type, "Linux_swap") != 0)) {
  97. int i, l;
  98. goodness++;
  99. l = strlen(part->name);
  100. if (strcmp(part->name, "/") == 0)
  101. goodness++;
  102. for (i = 0; i <= l - 4; ++i) {
  103. if (strncasecmp(part->name + i, "root",
  104. 4) == 0) {
  105. goodness += 2;
  106. break;
  107. }
  108. }
  109. if (strncasecmp(part->name, "swap", 4) == 0)
  110. goodness--;
  111. }
  112. if (goodness > found_root_goodness) {
  113. found_root = slot;
  114. found_root_goodness = goodness;
  115. }
  116. }
  117. #endif /* CONFIG_PPC_PMAC */
  118. }
  119. #ifdef CONFIG_PPC_PMAC
  120. if (found_root_goodness)
  121. note_bootable_part(state->bdev->bd_dev, found_root,
  122. found_root_goodness);
  123. #endif
  124. put_dev_sector(sect);
  125. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  126. return 1;
  127. }