Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / partitions / acorn.c
1 /*
2  *  linux/fs/partitions/acorn.c
3  *
4  *  Copyright (c) 1996-2000 Russell King.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Scan ADFS partitions on hard disk drives.  Unfortunately, there
11  *  isn't a standard for partitioning drives on Acorn machines, so
12  *  every single manufacturer of SCSI and IDE cards created their own
13  *  method.
14  */
15 #include <linux/buffer_head.h>
16 #include <linux/adfs_fs.h>
17
18 #include "check.h"
19 #include "acorn.h"
20
21 /*
22  * Partition types. (Oh for reusability)
23  */
24 #define PARTITION_RISCIX_MFM    1
25 #define PARTITION_RISCIX_SCSI   2
26 #define PARTITION_LINUX         9
27
28 static struct adfs_discrecord *
29 adfs_partition(struct parsed_partitions *state, char *name, char *data,
30                unsigned long first_sector, int slot)
31 {
32         struct adfs_discrecord *dr;
33         unsigned int nr_sects;
34
35         if (adfs_checkbblk(data))
36                 return NULL;
37
38         dr = (struct adfs_discrecord *)(data + 0x1c0);
39
40         if (dr->disc_size == 0 && dr->disc_size_high == 0)
41                 return NULL;
42
43         nr_sects = (le32_to_cpu(dr->disc_size_high) << 23) |
44                    (le32_to_cpu(dr->disc_size) >> 9);
45
46         if (name)
47                 printk(" [%s]", name);
48         put_partition(state, slot, first_sector, nr_sects);
49         return dr;
50 }
51
52 #ifdef CONFIG_ACORN_PARTITION_RISCIX
53
54 struct riscix_part {
55         __le32  start;
56         __le32  length;
57         __le32  one;
58         char    name[16];
59 };
60
61 struct riscix_record {
62         __le32  magic;
63 #define RISCIX_MAGIC    cpu_to_le32(0x4a657320)
64         __le32  date;
65         struct riscix_part part[8];
66 };
67
68 static int
69 riscix_partition(struct parsed_partitions *state, struct block_device *bdev,
70                 unsigned long first_sect, int slot, unsigned long nr_sects)
71 {
72         Sector sect;
73         struct riscix_record *rr;
74         
75         rr = (struct riscix_record *)read_dev_sector(bdev, first_sect, &sect);
76         if (!rr)
77                 return -1;
78
79         printk(" [RISCiX]");
80
81
82         if (rr->magic == RISCIX_MAGIC) {
83                 unsigned long size = nr_sects > 2 ? 2 : nr_sects;
84                 int part;
85
86                 printk(" <");
87
88                 put_partition(state, slot++, first_sect, size);
89                 for (part = 0; part < 8; part++) {
90                         if (rr->part[part].one &&
91                             memcmp(rr->part[part].name, "All\0", 4)) {
92                                 put_partition(state, slot++,
93                                         le32_to_cpu(rr->part[part].start),
94                                         le32_to_cpu(rr->part[part].length));
95                                 printk("(%s)", rr->part[part].name);
96                         }
97                 }
98
99                 printk(" >\n");
100         } else {
101                 put_partition(state, slot++, first_sect, nr_sects);
102         }
103
104         put_dev_sector(sect);
105         return slot;
106 }
107 #endif
108
109 #define LINUX_NATIVE_MAGIC 0xdeafa1de
110 #define LINUX_SWAP_MAGIC   0xdeafab1e
111
112 struct linux_part {
113         __le32 magic;
114         __le32 start_sect;
115         __le32 nr_sects;
116 };
117
118 static int
119 linux_partition(struct parsed_partitions *state, struct block_device *bdev,
120                 unsigned long first_sect, int slot, unsigned long nr_sects)
121 {
122         Sector sect;
123         struct linux_part *linuxp;
124         unsigned long size = nr_sects > 2 ? 2 : nr_sects;
125
126         printk(" [Linux]");
127
128         put_partition(state, slot++, first_sect, size);
129
130         linuxp = (struct linux_part *)read_dev_sector(bdev, first_sect, &sect);
131         if (!linuxp)
132                 return -1;
133
134         printk(" <");
135         while (linuxp->magic == cpu_to_le32(LINUX_NATIVE_MAGIC) ||
136                linuxp->magic == cpu_to_le32(LINUX_SWAP_MAGIC)) {
137                 if (slot == state->limit)
138                         break;
139                 put_partition(state, slot++, first_sect +
140                                  le32_to_cpu(linuxp->start_sect),
141                                  le32_to_cpu(linuxp->nr_sects));
142                 linuxp ++;
143         }
144         printk(" >");
145
146         put_dev_sector(sect);
147         return slot;
148 }
149
150 #ifdef CONFIG_ACORN_PARTITION_CUMANA
151 int
152 adfspart_check_CUMANA(struct parsed_partitions *state, struct block_device *bdev)
153 {
154         unsigned long first_sector = 0;
155         unsigned int start_blk = 0;
156         Sector sect;
157         unsigned char *data;
158         char *name = "CUMANA/ADFS";
159         int first = 1;
160         int slot = 1;
161
162         /*
163          * Try Cumana style partitions - sector 6 contains ADFS boot block
164          * with pointer to next 'drive'.
165          *
166          * There are unknowns in this code - is the 'cylinder number' of the
167          * next partition relative to the start of this one - I'm assuming
168          * it is.
169          *
170          * Also, which ID did Cumana use?
171          *
172          * This is totally unfinished, and will require more work to get it
173          * going. Hence it is totally untested.
174          */
175         do {
176                 struct adfs_discrecord *dr;
177                 unsigned int nr_sects;
178
179                 data = read_dev_sector(bdev, start_blk * 2 + 6, &sect);
180                 if (!data)
181                         return -1;
182
183                 if (slot == state->limit)
184                         break;
185
186                 dr = adfs_partition(state, name, data, first_sector, slot++);
187                 if (!dr)
188                         break;
189
190                 name = NULL;
191
192                 nr_sects = (data[0x1fd] + (data[0x1fe] << 8)) *
193                            (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) *
194                            dr->secspertrack;
195
196                 if (!nr_sects)
197                         break;
198
199                 first = 0;
200                 first_sector += nr_sects;
201                 start_blk += nr_sects >> (BLOCK_SIZE_BITS - 9);
202                 nr_sects = 0; /* hmm - should be partition size */
203
204                 switch (data[0x1fc] & 15) {
205                 case 0: /* No partition / ADFS? */
206                         break;
207
208 #ifdef CONFIG_ACORN_PARTITION_RISCIX
209                 case PARTITION_RISCIX_SCSI:
210                         /* RISCiX - we don't know how to find the next one. */
211                         slot = riscix_partition(state, bdev, first_sector,
212                                                  slot, nr_sects);
213                         break;
214 #endif
215
216                 case PARTITION_LINUX:
217                         slot = linux_partition(state, bdev, first_sector,
218                                                 slot, nr_sects);
219                         break;
220                 }
221                 put_dev_sector(sect);
222                 if (slot == -1)
223                         return -1;
224         } while (1);
225         put_dev_sector(sect);
226         return first ? 0 : 1;
227 }
228 #endif
229
230 #ifdef CONFIG_ACORN_PARTITION_ADFS
231 /*
232  * Purpose: allocate ADFS partitions.
233  *
234  * Params : hd          - pointer to gendisk structure to store partition info.
235  *          dev         - device number to access.
236  *
237  * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
238  *
239  * Alloc  : hda  = whole drive
240  *          hda1 = ADFS partition on first drive.
241  *          hda2 = non-ADFS partition.
242  */
243 int
244 adfspart_check_ADFS(struct parsed_partitions *state, struct block_device *bdev)
245 {
246         unsigned long start_sect, nr_sects, sectscyl, heads;
247         Sector sect;
248         unsigned char *data;
249         struct adfs_discrecord *dr;
250         unsigned char id;
251         int slot = 1;
252
253         data = read_dev_sector(bdev, 6, &sect);
254         if (!data)
255                 return -1;
256
257         dr = adfs_partition(state, "ADFS", data, 0, slot++);
258         if (!dr) {
259                 put_dev_sector(sect);
260                 return 0;
261         }
262
263         heads = dr->heads + ((dr->lowsector >> 6) & 1);
264         sectscyl = dr->secspertrack * heads;
265         start_sect = ((data[0x1fe] << 8) + data[0x1fd]) * sectscyl;
266         id = data[0x1fc] & 15;
267         put_dev_sector(sect);
268
269 #ifdef CONFIG_BLK_DEV_MFM
270         if (MAJOR(bdev->bd_dev) == MFM_ACORN_MAJOR) {
271                 extern void xd_set_geometry(struct block_device *,
272                         unsigned char, unsigned char, unsigned int);
273                 xd_set_geometry(bdev, dr->secspertrack, heads, 1);
274                 invalidate_bdev(bdev, 1);
275                 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
276         }
277 #endif
278
279         /*
280          * Work out start of non-adfs partition.
281          */
282         nr_sects = (bdev->bd_inode->i_size >> 9) - start_sect;
283
284         if (start_sect) {
285                 switch (id) {
286 #ifdef CONFIG_ACORN_PARTITION_RISCIX
287                 case PARTITION_RISCIX_SCSI:
288                 case PARTITION_RISCIX_MFM:
289                         slot = riscix_partition(state, bdev, start_sect,
290                                                  slot, nr_sects);
291                         break;
292 #endif
293
294                 case PARTITION_LINUX:
295                         slot = linux_partition(state, bdev, start_sect,
296                                                 slot, nr_sects);
297                         break;
298                 }
299         }
300         printk("\n");
301         return 1;
302 }
303 #endif
304
305 #ifdef CONFIG_ACORN_PARTITION_ICS
306
307 struct ics_part {
308         __le32 start;
309         __le32 size;
310 };
311
312 static int adfspart_check_ICSLinux(struct block_device *bdev, unsigned long block)
313 {
314         Sector sect;
315         unsigned char *data = read_dev_sector(bdev, block, &sect);
316         int result = 0;
317
318         if (data) {
319                 if (memcmp(data, "LinuxPart", 9) == 0)
320                         result = 1;
321                 put_dev_sector(sect);
322         }
323
324         return result;
325 }
326
327 /*
328  * Check for a valid ICS partition using the checksum.
329  */
330 static inline int valid_ics_sector(const unsigned char *data)
331 {
332         unsigned long sum;
333         int i;
334
335         for (i = 0, sum = 0x50617274; i < 508; i++)
336                 sum += data[i];
337
338         sum -= le32_to_cpu(*(__le32 *)(&data[508]));
339
340         return sum == 0;
341 }
342
343 /*
344  * Purpose: allocate ICS partitions.
345  * Params : hd          - pointer to gendisk structure to store partition info.
346  *          dev         - device number to access.
347  * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
348  * Alloc  : hda  = whole drive
349  *          hda1 = ADFS partition 0 on first drive.
350  *          hda2 = ADFS partition 1 on first drive.
351  *              ..etc..
352  */
353 int
354 adfspart_check_ICS(struct parsed_partitions *state, struct block_device *bdev)
355 {
356         const unsigned char *data;
357         const struct ics_part *p;
358         int slot;
359         Sector sect;
360
361         /*
362          * Try ICS style partitions - sector 0 contains partition info.
363          */
364         data = read_dev_sector(bdev, 0, &sect);
365         if (!data)
366                 return -1;
367
368         if (!valid_ics_sector(data)) {
369                 put_dev_sector(sect);
370                 return 0;
371         }
372
373         printk(" [ICS]");
374
375         for (slot = 1, p = (const struct ics_part *)data; p->size; p++) {
376                 u32 start = le32_to_cpu(p->start);
377                 s32 size = le32_to_cpu(p->size); /* yes, it's signed. */
378
379                 if (slot == state->limit)
380                         break;
381
382                 /*
383                  * Negative sizes tell the RISC OS ICS driver to ignore
384                  * this partition - in effect it says that this does not
385                  * contain an ADFS filesystem.
386                  */
387                 if (size < 0) {
388                         size = -size;
389
390                         /*
391                          * Our own extension - We use the first sector
392                          * of the partition to identify what type this
393                          * partition is.  We must not make this visible
394                          * to the filesystem.
395                          */
396                         if (size > 1 && adfspart_check_ICSLinux(bdev, start)) {
397                                 start += 1;
398                                 size -= 1;
399                         }
400                 }
401
402                 if (size)
403                         put_partition(state, slot++, start, size);
404         }
405
406         put_dev_sector(sect);
407         printk("\n");
408         return 1;
409 }
410 #endif
411
412 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
413 struct ptec_part {
414         __le32 unused1;
415         __le32 unused2;
416         __le32 start;
417         __le32 size;
418         __le32 unused5;
419         char type[8];
420 };
421
422 static inline int valid_ptec_sector(const unsigned char *data)
423 {
424         unsigned char checksum = 0x2a;
425         int i;
426
427         /*
428          * If it looks like a PC/BIOS partition, then it
429          * probably isn't PowerTec.
430          */
431         if (data[510] == 0x55 && data[511] == 0xaa)
432                 return 0;
433
434         for (i = 0; i < 511; i++)
435                 checksum += data[i];
436
437         return checksum == data[511];
438 }
439
440 /*
441  * Purpose: allocate ICS partitions.
442  * Params : hd          - pointer to gendisk structure to store partition info.
443  *          dev         - device number to access.
444  * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
445  * Alloc  : hda  = whole drive
446  *          hda1 = ADFS partition 0 on first drive.
447  *          hda2 = ADFS partition 1 on first drive.
448  *              ..etc..
449  */
450 int
451 adfspart_check_POWERTEC(struct parsed_partitions *state, struct block_device *bdev)
452 {
453         Sector sect;
454         const unsigned char *data;
455         const struct ptec_part *p;
456         int slot = 1;
457         int i;
458
459         data = read_dev_sector(bdev, 0, &sect);
460         if (!data)
461                 return -1;
462
463         if (!valid_ptec_sector(data)) {
464                 put_dev_sector(sect);
465                 return 0;
466         }
467
468         printk(" [POWERTEC]");
469
470         for (i = 0, p = (const struct ptec_part *)data; i < 12; i++, p++) {
471                 u32 start = le32_to_cpu(p->start);
472                 u32 size = le32_to_cpu(p->size);
473
474                 if (size)
475                         put_partition(state, slot++, start, size);
476         }
477
478         put_dev_sector(sect);
479         printk("\n");
480         return 1;
481 }
482 #endif
483
484 #ifdef CONFIG_ACORN_PARTITION_EESOX
485 struct eesox_part {
486         char    magic[6];
487         char    name[10];
488         __le32  start;
489         __le32  unused6;
490         __le32  unused7;
491         __le32  unused8;
492 };
493
494 /*
495  * Guess who created this format?
496  */
497 static const char eesox_name[] = {
498         'N', 'e', 'i', 'l', ' ',
499         'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
500 };
501
502 /*
503  * EESOX SCSI partition format.
504  *
505  * This is a goddamned awful partition format.  We don't seem to store
506  * the size of the partition in this table, only the start addresses.
507  *
508  * There are two possibilities where the size comes from:
509  *  1. The individual ADFS boot block entries that are placed on the disk.
510  *  2. The start address of the next entry.
511  */
512 int
513 adfspart_check_EESOX(struct parsed_partitions *state, struct block_device *bdev)
514 {
515         Sector sect;
516         const unsigned char *data;
517         unsigned char buffer[256];
518         struct eesox_part *p;
519         sector_t start = 0;
520         int i, slot = 1;
521
522         data = read_dev_sector(bdev, 7, &sect);
523         if (!data)
524                 return -1;
525
526         /*
527          * "Decrypt" the partition table.  God knows why...
528          */
529         for (i = 0; i < 256; i++)
530                 buffer[i] = data[i] ^ eesox_name[i & 15];
531
532         put_dev_sector(sect);
533
534         for (i = 0, p = (struct eesox_part *)buffer; i < 8; i++, p++) {
535                 sector_t next;
536
537                 if (memcmp(p->magic, "Eesox", 6))
538                         break;
539
540                 next = le32_to_cpu(p->start);
541                 if (i)
542                         put_partition(state, slot++, start, next - start);
543                 start = next;
544         }
545
546         if (i != 0) {
547                 sector_t size;
548
549                 size = get_capacity(bdev->bd_disk);
550                 put_partition(state, slot++, start, size - start);
551                 printk("\n");
552         }
553
554         return i ? 1 : 0;
555 }
556 #endif