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