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