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