blob: 621317ac4d59f5fd75e433718a194bad7b1c2a56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/ctype.h>
10#include "check.h"
11#include "mac.h"
12
13#ifdef CONFIG_PPC_PMAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110014#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015extern void note_bootable_part(dev_t dev, int part, int goodness);
16#endif
17
18/*
19 * Code to understand MacOS partition tables.
20 */
21
22static inline void mac_fix_string(char *stg, int len)
23{
24 int i;
25
26 for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
27 stg[i] = 0;
28}
29
Tejun Heo1493bf22010-05-15 20:09:30 +020030int mac_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 Sector sect;
33 unsigned char *data;
Timo Warnsfa7ea872011-02-17 22:27:40 +010034 int slot, blocks_in_map;
Kees Cook02e2a5b2015-11-19 17:18:54 -080035 unsigned secsize, datasize, partoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#ifdef CONFIG_PPC_PMAC
37 int found_root = 0;
38 int found_root_goodness = 0;
39#endif
40 struct mac_partition *part;
41 struct mac_driver_desc *md;
42
43 /* Get 0th block and look at the first partition map entry. */
Tejun Heo1493bf22010-05-15 20:09:30 +020044 md = read_part_sector(state, 0, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!md)
46 return -1;
47 if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
48 put_dev_sector(sect);
49 return 0;
50 }
51 secsize = be16_to_cpu(md->block_size);
52 put_dev_sector(sect);
Kees Cook02e2a5b2015-11-19 17:18:54 -080053 datasize = round_down(secsize, 512);
54 data = read_part_sector(state, datasize / 512, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!data)
56 return -1;
Kees Cook02e2a5b2015-11-19 17:18:54 -080057 partoffset = secsize % 512;
58 if (partoffset + sizeof(*part) > datasize)
59 return -1;
60 part = (struct mac_partition *) (data + partoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
62 put_dev_sector(sect);
63 return 0; /* not a MacOS disk */
64 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 blocks_in_map = be32_to_cpu(part->map_count);
Timo Warnsfa7ea872011-02-17 22:27:40 +010066 if (blocks_in_map < 0 || blocks_in_map >= DISK_MAX_PARTS) {
67 put_dev_sector(sect);
68 return 0;
69 }
Ming Lei06004e62013-02-27 17:05:18 -080070
71 if (blocks_in_map >= state->limit)
72 blocks_in_map = state->limit - 1;
73
Timo Warnsfa7ea872011-02-17 22:27:40 +010074 strlcat(state->pp_buf, " [mac]", PAGE_SIZE);
75 for (slot = 1; slot <= blocks_in_map; ++slot) {
76 int pos = slot * secsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 put_dev_sector(sect);
Tejun Heo1493bf22010-05-15 20:09:30 +020078 data = read_part_sector(state, pos/512, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!data)
80 return -1;
81 part = (struct mac_partition *) (data + pos%512);
82 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
83 break;
84 put_partition(state, slot,
85 be32_to_cpu(part->start_block) * (secsize/512),
86 be32_to_cpu(part->block_count) * (secsize/512));
87
Rasmus Villemoes58294052014-09-16 22:51:16 +020088 if (!strncasecmp(part->type, "Linux_RAID", 10))
Cesar Eduardo Barroscc910622010-04-17 19:28:09 -030089 state->parts[slot].flags = ADDPART_FLAG_RAID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#ifdef CONFIG_PPC_PMAC
91 /*
92 * If this is the first bootable partition, tell the
93 * setup code, in case it wants to make this the root.
94 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110095 if (machine_is(powermac)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int goodness = 0;
97
98 mac_fix_string(part->processor, 16);
99 mac_fix_string(part->name, 32);
100 mac_fix_string(part->type, 32);
101
102 if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
103 && strcasecmp(part->processor, "powerpc") == 0)
104 goodness++;
105
106 if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
Rasmus Villemoes58294052014-09-16 22:51:16 +0200107 || (strncasecmp(part->type, "Linux", 5) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 && strcasecmp(part->type, "Linux_swap") != 0)) {
109 int i, l;
110
111 goodness++;
112 l = strlen(part->name);
113 if (strcmp(part->name, "/") == 0)
114 goodness++;
115 for (i = 0; i <= l - 4; ++i) {
Rasmus Villemoes58294052014-09-16 22:51:16 +0200116 if (strncasecmp(part->name + i, "root",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 4) == 0) {
118 goodness += 2;
119 break;
120 }
121 }
Rasmus Villemoes58294052014-09-16 22:51:16 +0200122 if (strncasecmp(part->name, "swap", 4) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 goodness--;
124 }
125
126 if (goodness > found_root_goodness) {
Timo Warnsfa7ea872011-02-17 22:27:40 +0100127 found_root = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 found_root_goodness = goodness;
129 }
130 }
131#endif /* CONFIG_PPC_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133#ifdef CONFIG_PPC_PMAC
134 if (found_root_goodness)
Tejun Heo1493bf22010-05-15 20:09:30 +0200135 note_bootable_part(state->bdev->bd_dev, found_root,
136 found_root_goodness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
139 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700140 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return 1;
142}