Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / block / partitions / mac.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
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
1da177e4
LT
10#include <linux/ctype.h>
11#include "check.h"
12#include "mac.h"
13
14#ifdef CONFIG_PPC_PMAC
e8222502 15#include <asm/machdep.h>
1da177e4
LT
16extern void note_bootable_part(dev_t dev, int part, int goodness);
17#endif
18
19/*
20 * Code to understand MacOS partition tables.
21 */
22
5205a4aa 23#ifdef CONFIG_PPC_PMAC
1da177e4
LT
24static inline void mac_fix_string(char *stg, int len)
25{
26 int i;
27
28 for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
29 stg[i] = 0;
30}
5205a4aa 31#endif
1da177e4 32
1493bf21 33int mac_partition(struct parsed_partitions *state)
1da177e4 34{
1da177e4
LT
35 Sector sect;
36 unsigned char *data;
fa7ea87a 37 int slot, blocks_in_map;
02e2a5bf 38 unsigned secsize, datasize, partoffset;
1da177e4
LT
39#ifdef CONFIG_PPC_PMAC
40 int found_root = 0;
41 int found_root_goodness = 0;
42#endif
43 struct mac_partition *part;
44 struct mac_driver_desc *md;
45
46 /* Get 0th block and look at the first partition map entry. */
1493bf21 47 md = read_part_sector(state, 0, &sect);
1da177e4
LT
48 if (!md)
49 return -1;
50 if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
51 put_dev_sector(sect);
52 return 0;
53 }
54 secsize = be16_to_cpu(md->block_size);
55 put_dev_sector(sect);
02e2a5bf
KC
56 datasize = round_down(secsize, 512);
57 data = read_part_sector(state, datasize / 512, &sect);
1da177e4
LT
58 if (!data)
59 return -1;
02e2a5bf
KC
60 partoffset = secsize % 512;
61 if (partoffset + sizeof(*part) > datasize)
62 return -1;
63 part = (struct mac_partition *) (data + partoffset);
1da177e4
LT
64 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
65 put_dev_sector(sect);
66 return 0; /* not a MacOS disk */
67 }
1da177e4 68 blocks_in_map = be32_to_cpu(part->map_count);
fa7ea87a
TW
69 if (blocks_in_map < 0 || blocks_in_map >= DISK_MAX_PARTS) {
70 put_dev_sector(sect);
71 return 0;
72 }
06004e6e
ML
73
74 if (blocks_in_map >= state->limit)
75 blocks_in_map = state->limit - 1;
76
fa7ea87a
TW
77 strlcat(state->pp_buf, " [mac]", PAGE_SIZE);
78 for (slot = 1; slot <= blocks_in_map; ++slot) {
79 int pos = slot * secsize;
1da177e4 80 put_dev_sector(sect);
1493bf21 81 data = read_part_sector(state, pos/512, &sect);
1da177e4
LT
82 if (!data)
83 return -1;
84 part = (struct mac_partition *) (data + pos%512);
85 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
86 break;
87 put_partition(state, slot,
88 be32_to_cpu(part->start_block) * (secsize/512),
89 be32_to_cpu(part->block_count) * (secsize/512));
90
58294050 91 if (!strncasecmp(part->type, "Linux_RAID", 10))
cc910624 92 state->parts[slot].flags = ADDPART_FLAG_RAID;
1da177e4
LT
93#ifdef CONFIG_PPC_PMAC
94 /*
95 * If this is the first bootable partition, tell the
96 * setup code, in case it wants to make this the root.
97 */
e8222502 98 if (machine_is(powermac)) {
1da177e4
LT
99 int goodness = 0;
100
101 mac_fix_string(part->processor, 16);
102 mac_fix_string(part->name, 32);
103 mac_fix_string(part->type, 32);
104
105 if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
106 && strcasecmp(part->processor, "powerpc") == 0)
107 goodness++;
108
109 if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
58294050 110 || (strncasecmp(part->type, "Linux", 5) == 0
1da177e4
LT
111 && strcasecmp(part->type, "Linux_swap") != 0)) {
112 int i, l;
113
114 goodness++;
115 l = strlen(part->name);
116 if (strcmp(part->name, "/") == 0)
117 goodness++;
118 for (i = 0; i <= l - 4; ++i) {
58294050 119 if (strncasecmp(part->name + i, "root",
1da177e4
LT
120 4) == 0) {
121 goodness += 2;
122 break;
123 }
124 }
58294050 125 if (strncasecmp(part->name, "swap", 4) == 0)
1da177e4
LT
126 goodness--;
127 }
128
129 if (goodness > found_root_goodness) {
fa7ea87a 130 found_root = slot;
1da177e4
LT
131 found_root_goodness = goodness;
132 }
133 }
134#endif /* CONFIG_PPC_PMAC */
1da177e4
LT
135 }
136#ifdef CONFIG_PPC_PMAC
137 if (found_root_goodness)
a08aa9bc 138 note_bootable_part(state->disk->part0->bd_dev, found_root,
1493bf21 139 found_root_goodness);
1da177e4
LT
140#endif
141
142 put_dev_sector(sect);
9c867fbe 143 strlcat(state->pp_buf, "\n", PAGE_SIZE);
1da177e4
LT
144 return 1;
145}