mac.c 3.5 KB

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