File: /usr/src/linux/fs/partitions/mac.c

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/config.h>
10     #include <linux/fs.h>
11     #include <linux/genhd.h>
12     #include <linux/kernel.h>
13     #include <linux/major.h>
14     #include <linux/string.h>
15     #include <linux/blk.h>
16     #include <linux/ctype.h>
17     
18     #include <asm/system.h>
19     
20     #include "check.h"
21     #include "mac.h"
22     
23     #ifdef CONFIG_PPC
24     extern void note_bootable_part(kdev_t dev, int part, int goodness);
25     #endif
26     
27     /*
28      * Code to understand MacOS partition tables.
29      */
30     
31     static inline void mac_fix_string(char *stg, int len)
32     {
33     	int i;
34     
35     	for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
36     		stg[i] = 0;
37     }
38     
39     int mac_partition(struct gendisk *hd, kdev_t dev, unsigned long fsec, int first_part_minor)
40     {
41     	struct buffer_head *bh;
42     	int blk, blocks_in_map;
43     	int dev_bsize, dev_pos, pos;
44     	unsigned secsize;
45     #ifdef CONFIG_PPC
46     	int found_root = 0;
47     	int found_root_goodness = 0;
48     #endif
49     	struct mac_partition *part;
50     	struct mac_driver_desc *md;
51     
52     	dev_bsize = get_ptable_blocksize(dev);
53     	dev_pos = 0;
54     	/* Get 0th block and look at the first partition map entry. */
55     	if ((bh = bread(dev, 0, dev_bsize)) == 0) {
56     	    printk("%s: error reading partition table\n",
57     		   kdevname(dev));
58     	    return -1;
59     	}
60     	md = (struct mac_driver_desc *) bh->b_data;
61     	if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
62     		brelse(bh);
63     		return 0;
64     	}
65     	secsize = be16_to_cpu(md->block_size);
66     	if (secsize >= dev_bsize) {
67     		brelse(bh);
68     		dev_pos = secsize;
69     		if ((bh = bread(dev, secsize/dev_bsize, dev_bsize)) == 0) {
70     			printk("%s: error reading Mac partition table\n",
71     			       kdevname(dev));
72     			return -1;
73     		}
74     	}
75     	part = (struct mac_partition *) (bh->b_data + secsize - dev_pos);
76     	if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
77     		brelse(bh);
78     		return 0;		/* not a MacOS disk */
79     	}
80     	printk(" [mac]");
81     	blocks_in_map = be32_to_cpu(part->map_count);
82     	for (blk = 1; blk <= blocks_in_map; ++blk) {
83     		pos = blk * secsize;
84     		if (pos >= dev_pos + dev_bsize) {
85     			brelse(bh);
86     			dev_pos = pos;
87     			if ((bh = bread(dev, pos/dev_bsize, dev_bsize)) == 0) {
88     				printk("%s: error reading partition table\n",
89     				       kdevname(dev));
90     				return -1;
91     			}
92     		}
93     		part = (struct mac_partition *) (bh->b_data + pos - dev_pos);
94     		if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
95     			break;
96     		blocks_in_map = be32_to_cpu(part->map_count);
97     		add_gd_partition(hd, first_part_minor,
98     			fsec + be32_to_cpu(part->start_block) * (secsize/512),
99     			be32_to_cpu(part->block_count) * (secsize/512));
100     
101     #ifdef CONFIG_PPC
102     		/*
103     		 * If this is the first bootable partition, tell the
104     		 * setup code, in case it wants to make this the root.
105     		 */
106     		if (_machine == _MACH_Pmac) {
107     			int goodness = 0;
108     
109     			mac_fix_string(part->processor, 16);
110     			mac_fix_string(part->name, 32);
111     			mac_fix_string(part->type, 32);					
112     		    
113     			if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
114     			    && strcasecmp(part->processor, "powerpc") == 0)
115     				goodness++;
116     
117     			if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
118     			    || (strnicmp(part->type, "Linux", 5) == 0
119     			        && strcasecmp(part->type, "Linux_swap") != 0)) {
120     				int i, l;
121     
122     				goodness++;
123     				l = strlen(part->name);
124     				if (strcmp(part->name, "/") == 0)
125     					goodness++;
126     				for (i = 0; i <= l - 4; ++i) {
127     					if (strnicmp(part->name + i, "root",
128     						     4) == 0) {
129     						goodness += 2;
130     						break;
131     					}
132     				}
133     				if (strnicmp(part->name, "swap", 4) == 0)
134     					goodness--;
135     			}
136     
137     			if (goodness > found_root_goodness) {
138     				found_root = blk;
139     				found_root_goodness = goodness;
140     			}
141     		}
142     #endif /* CONFIG_PPC */
143     
144     		++first_part_minor;
145     	}
146     #ifdef CONFIG_PPC
147     	if (found_root_goodness)
148     		note_bootable_part(dev, found_root, found_root_goodness);
149     #endif
150     	brelse(bh);
151     	printk("\n");
152     	return 1;
153     }
154     
155