Linux v2.6.13
[linux-2.6/next.git] / fs / partitions / acorn.c
blobc05085710fce5a32c689620c915dd3f9bf519712
1 /*
2 * linux/fs/partitions/acorn.c
4 * Copyright (c) 1996-2000 Russell King.
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.
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.
15 #include <linux/config.h>
16 #include <linux/buffer_head.h>
17 #include <linux/adfs_fs.h>
19 #include "check.h"
20 #include "acorn.h"
23 * Partition types. (Oh for reusability)
25 #define PARTITION_RISCIX_MFM 1
26 #define PARTITION_RISCIX_SCSI 2
27 #define PARTITION_LINUX 9
29 static struct adfs_discrecord *
30 adfs_partition(struct parsed_partitions *state, char *name, char *data,
31 unsigned long first_sector, int slot)
33 struct adfs_discrecord *dr;
34 unsigned int nr_sects;
36 if (adfs_checkbblk(data))
37 return NULL;
39 dr = (struct adfs_discrecord *)(data + 0x1c0);
41 if (dr->disc_size == 0 && dr->disc_size_high == 0)
42 return NULL;
44 nr_sects = (le32_to_cpu(dr->disc_size_high) << 23) |
45 (le32_to_cpu(dr->disc_size) >> 9);
47 if (name)
48 printk(" [%s]", name);
49 put_partition(state, slot, first_sector, nr_sects);
50 return dr;
53 #ifdef CONFIG_ACORN_PARTITION_RISCIX
55 struct riscix_part {
56 __le32 start;
57 __le32 length;
58 __le32 one;
59 char name[16];
62 struct riscix_record {
63 __le32 magic;
64 #define RISCIX_MAGIC cpu_to_le32(0x4a657320)
65 __le32 date;
66 struct riscix_part part[8];
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)
73 Sector sect;
74 struct riscix_record *rr;
76 rr = (struct riscix_record *)read_dev_sector(bdev, first_sect, &sect);
77 if (!rr)
78 return -1;
80 printk(" [RISCiX]");
83 if (rr->magic == RISCIX_MAGIC) {
84 unsigned long size = nr_sects > 2 ? 2 : nr_sects;
85 int part;
87 printk(" <");
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);
100 printk(" >\n");
101 } else {
102 put_partition(state, slot++, first_sect, nr_sects);
105 put_dev_sector(sect);
106 return slot;
108 #endif
110 #define LINUX_NATIVE_MAGIC 0xdeafa1de
111 #define LINUX_SWAP_MAGIC 0xdeafab1e
113 struct linux_part {
114 __le32 magic;
115 __le32 start_sect;
116 __le32 nr_sects;
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)
123 Sector sect;
124 struct linux_part *linuxp;
125 unsigned long size = nr_sects > 2 ? 2 : nr_sects;
127 printk(" [Linux]");
129 put_partition(state, slot++, first_sect, size);
131 linuxp = (struct linux_part *)read_dev_sector(bdev, first_sect, &sect);
132 if (!linuxp)
133 return -1;
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 ++;
145 printk(" >");
147 put_dev_sector(sect);
148 return slot;
151 #ifdef CONFIG_ACORN_PARTITION_CUMANA
153 adfspart_check_CUMANA(struct parsed_partitions *state, struct block_device *bdev)
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;
164 * Try Cumana style partitions - sector 6 contains ADFS boot block
165 * with pointer to next 'drive'.
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.
171 * Also, which ID did Cumana use?
173 * This is totally unfinished, and will require more work to get it
174 * going. Hence it is totally untested.
176 do {
177 struct adfs_discrecord *dr;
178 unsigned int nr_sects;
180 data = read_dev_sector(bdev, start_blk * 2 + 6, &sect);
181 if (!data)
182 return -1;
184 if (slot == state->limit)
185 break;
187 dr = adfs_partition(state, name, data, first_sector, slot++);
188 if (!dr)
189 break;
191 name = NULL;
193 nr_sects = (data[0x1fd] + (data[0x1fe] << 8)) *
194 (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) *
195 dr->secspertrack;
197 if (!nr_sects)
198 break;
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 */
205 switch (data[0x1fc] & 15) {
206 case 0: /* No partition / ADFS? */
207 break;
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
217 case PARTITION_LINUX:
218 slot = linux_partition(state, bdev, first_sector,
219 slot, nr_sects);
220 break;
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;
229 #endif
231 #ifdef CONFIG_ACORN_PARTITION_ADFS
233 * Purpose: allocate ADFS partitions.
235 * Params : hd - pointer to gendisk structure to store partition info.
236 * dev - device number to access.
238 * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
240 * Alloc : hda = whole drive
241 * hda1 = ADFS partition on first drive.
242 * hda2 = non-ADFS partition.
245 adfspart_check_ADFS(struct parsed_partitions *state, struct block_device *bdev)
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;
254 data = read_dev_sector(bdev, 6, &sect);
255 if (!data)
256 return -1;
258 dr = adfs_partition(state, "ADFS", data, 0, slot++);
259 if (!dr) {
260 put_dev_sector(sect);
261 return 0;
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);
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);
278 #endif
281 * Work out start of non-adfs partition.
283 nr_sects = (bdev->bd_inode->i_size >> 9) - start_sect;
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
295 case PARTITION_LINUX:
296 slot = linux_partition(state, bdev, start_sect,
297 slot, nr_sects);
298 break;
301 printk("\n");
302 return 1;
304 #endif
306 #ifdef CONFIG_ACORN_PARTITION_ICS
308 struct ics_part {
309 __le32 start;
310 __le32 size;
313 static int adfspart_check_ICSLinux(struct block_device *bdev, unsigned long block)
315 Sector sect;
316 unsigned char *data = read_dev_sector(bdev, block, &sect);
317 int result = 0;
319 if (data) {
320 if (memcmp(data, "LinuxPart", 9) == 0)
321 result = 1;
322 put_dev_sector(sect);
325 return result;
329 * Check for a valid ICS partition using the checksum.
331 static inline int valid_ics_sector(const unsigned char *data)
333 unsigned long sum;
334 int i;
336 for (i = 0, sum = 0x50617274; i < 508; i++)
337 sum += data[i];
339 sum -= le32_to_cpu(*(__le32 *)(&data[508]));
341 return sum == 0;
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..
355 adfspart_check_ICS(struct parsed_partitions *state, struct block_device *bdev)
357 const unsigned char *data;
358 const struct ics_part *p;
359 int slot;
360 Sector sect;
363 * Try ICS style partitions - sector 0 contains partition info.
365 data = read_dev_sector(bdev, 0, &sect);
366 if (!data)
367 return -1;
369 if (!valid_ics_sector(data)) {
370 put_dev_sector(sect);
371 return 0;
374 printk(" [ICS]");
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. */
380 if (slot == state->limit)
381 break;
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.
388 if (size < 0) {
389 size = -size;
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.
397 if (size > 1 && adfspart_check_ICSLinux(bdev, start)) {
398 start += 1;
399 size -= 1;
403 if (size)
404 put_partition(state, slot++, start, size);
407 put_dev_sector(sect);
408 printk("\n");
409 return 1;
411 #endif
413 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
414 struct ptec_part {
415 __le32 unused1;
416 __le32 unused2;
417 __le32 start;
418 __le32 size;
419 __le32 unused5;
420 char type[8];
423 static inline int valid_ptec_sector(const unsigned char *data)
425 unsigned char checksum = 0x2a;
426 int i;
429 * If it looks like a PC/BIOS partition, then it
430 * probably isn't PowerTec.
432 if (data[510] == 0x55 && data[511] == 0xaa)
433 return 0;
435 for (i = 0; i < 511; i++)
436 checksum += data[i];
438 return checksum == data[511];
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..
452 adfspart_check_POWERTEC(struct parsed_partitions *state, struct block_device *bdev)
454 Sector sect;
455 const unsigned char *data;
456 const struct ptec_part *p;
457 int slot = 1;
458 int i;
460 data = read_dev_sector(bdev, 0, &sect);
461 if (!data)
462 return -1;
464 if (!valid_ptec_sector(data)) {
465 put_dev_sector(sect);
466 return 0;
469 printk(" [POWERTEC]");
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);
475 if (size)
476 put_partition(state, slot++, start, size);
479 put_dev_sector(sect);
480 printk("\n");
481 return 1;
483 #endif
485 #ifdef CONFIG_ACORN_PARTITION_EESOX
486 struct eesox_part {
487 char magic[6];
488 char name[10];
489 __le32 start;
490 __le32 unused6;
491 __le32 unused7;
492 __le32 unused8;
496 * Guess who created this format?
498 static const char eesox_name[] = {
499 'N', 'e', 'i', 'l', ' ',
500 'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
504 * EESOX SCSI partition format.
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.
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.
514 adfspart_check_EESOX(struct parsed_partitions *state, struct block_device *bdev)
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;
523 data = read_dev_sector(bdev, 7, &sect);
524 if (!data)
525 return -1;
528 * "Decrypt" the partition table. God knows why...
530 for (i = 0; i < 256; i++)
531 buffer[i] = data[i] ^ eesox_name[i & 15];
533 put_dev_sector(sect);
535 for (i = 0, p = (struct eesox_part *)buffer; i < 8; i++, p++) {
536 sector_t next;
538 if (memcmp(p->magic, "Eesox", 6))
539 break;
541 next = le32_to_cpu(p->start);
542 if (i)
543 put_partition(state, slot++, start, next - start);
544 start = next;
547 if (i != 0) {
548 sector_t size;
550 size = get_capacity(bdev->bd_disk);
551 put_partition(state, slot++, start, size - start);
552 printk("\n");
555 return i ? 1 : 0;
557 #endif