import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / fs / partitions / atari.c
1 /*
2  *  fs/partitions/atari.c
3  *
4  *  Code extracted from drivers/block/genhd.c
5  *
6  *  Copyright (C) 1991-1998  Linus Torvalds
7  *  Re-organised Feb 1998 Russell King
8  */
9
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/byteorder.h>
19 #include <asm/system.h>
20
21 #include "check.h"
22 #include "atari.h"
23
24 /* ++guenther: this should be settable by the user ("make config")?.
25  */
26 #define ICD_PARTS
27
28 /* check if a partition entry looks valid -- Atari format is assumed if at
29    least one of the primary entries is ok this way */
30 #define VALID_PARTITION(pi,hdsiz)                                            \
31     (((pi)->flg & 1) &&                                                      \
32      isalnum((pi)->id[0]) && isalnum((pi)->id[1]) && isalnum((pi)->id[2]) && \
33      be32_to_cpu((pi)->st) <= (hdsiz) &&                                     \
34      be32_to_cpu((pi)->st) + be32_to_cpu((pi)->siz) <= (hdsiz))
35
36 static inline int OK_id(char *s)
37 {
38         return  memcmp (s, "GEM", 3) == 0 || memcmp (s, "BGM", 3) == 0 ||
39                 memcmp (s, "LNX", 3) == 0 || memcmp (s, "SWP", 3) == 0 ||
40                 memcmp (s, "RAW", 3) == 0 ;
41 }
42
43 int atari_partition (struct gendisk *hd, struct block_device *bdev,
44                      unsigned long first_sector, int minor)
45 {
46         int m_lim = minor + hd->max_p;
47         Sector sect;
48         struct rootsector *rs;
49         struct partition_info *pi;
50         u32 extensect;
51         u32 hd_size;
52 #ifdef ICD_PARTS
53         int part_fmt = 0; /* 0:unknown, 1:AHDI, 2:ICD/Supra */
54 #endif
55
56         rs = (struct rootsector *) read_dev_sector(bdev, 0, &sect);
57         if (!rs)
58                 return -1;
59
60         /* Verify this is an Atari rootsector: */
61         hd_size = hd->part[minor - 1].nr_sects;
62         if (!VALID_PARTITION(&rs->part[0], hd_size) &&
63             !VALID_PARTITION(&rs->part[1], hd_size) &&
64             !VALID_PARTITION(&rs->part[2], hd_size) &&
65             !VALID_PARTITION(&rs->part[3], hd_size)) {
66                 /*
67                  * if there's no valid primary partition, assume that no Atari
68                  * format partition table (there's no reliable magic or the like
69                  * :-()
70                  */
71                 put_dev_sector(sect);
72                 return 0;
73         }
74
75         pi = &rs->part[0];
76         printk (" AHDI");
77         for (; pi < &rs->part[4] && minor < m_lim; minor++, pi++) {
78                 struct rootsector *xrs;
79                 Sector sect2;
80                 ulong partsect;
81
82                 if ( !(pi->flg & 1) )
83                         continue;
84                 /* active partition */
85                 if (memcmp (pi->id, "XGM", 3) != 0) {
86                         /* we don't care about other id's */
87                         add_gd_partition (hd, minor, be32_to_cpu(pi->st),
88                                         be32_to_cpu(pi->siz));
89                         continue;
90                 }
91                 /* extension partition */
92 #ifdef ICD_PARTS
93                 part_fmt = 1;
94 #endif
95                 printk(" XGM<");
96                 partsect = extensect = be32_to_cpu(pi->st);
97                 while (1) {
98                         xrs = (struct rootsector *)read_dev_sector(bdev, partsect, &sect2);
99                         if (!xrs) {
100                                 printk (" block %ld read failed\n", partsect);
101                                 put_dev_sector(sect);
102                                 return 0;
103                         }
104
105                         /* ++roman: sanity check: bit 0 of flg field must be set */
106                         if (!(xrs->part[0].flg & 1)) {
107                                 printk( "\nFirst sub-partition in extended partition is not valid!\n" );
108                                 put_dev_sector(sect2);
109                                 break;
110                         }
111
112                         add_gd_partition(hd, minor,
113                                    partsect + be32_to_cpu(xrs->part[0].st),
114                                    be32_to_cpu(xrs->part[0].siz));
115
116                         if (!(xrs->part[1].flg & 1)) {
117                                 /* end of linked partition list */
118                                 put_dev_sector(sect2);
119                                 break;
120                         }
121                         if (memcmp( xrs->part[1].id, "XGM", 3 ) != 0) {
122                                 printk("\nID of extended partition is not XGM!\n");
123                                 put_dev_sector(sect2);
124                                 break;
125                         }
126
127                         partsect = be32_to_cpu(xrs->part[1].st) + extensect;
128                         put_dev_sector(sect2);
129                         minor++;
130                         if (minor >= m_lim) {
131                                 printk( "\nMaximum number of partitions reached!\n" );
132                                 break;
133                         }
134                 }
135                 printk(" >");
136         }
137 #ifdef ICD_PARTS
138         if ( part_fmt!=1 ) { /* no extended partitions -> test ICD-format */
139                 pi = &rs->icdpart[0];
140                 /* sanity check: no ICD format if first partition invalid */
141                 if (OK_id(pi->id)) {
142                         printk(" ICD<");
143                         for (; pi < &rs->icdpart[8] && minor < m_lim; minor++, pi++) {
144                                 /* accept only GEM,BGM,RAW,LNX,SWP partitions */
145                                 if (!((pi->flg & 1) && OK_id(pi->id)))
146                                         continue;
147                                 part_fmt = 2;
148                                 add_gd_partition (hd, minor,
149                                                 be32_to_cpu(pi->st),
150                                                 be32_to_cpu(pi->siz));
151                         }
152                         printk(" >");
153                 }
154         }
155 #endif
156         put_dev_sector(sect);
157
158         printk ("\n");
159
160         return 1;
161 }
162