ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / partitions / amiga.c
1 /*
2  *  fs/partitions/amiga.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/types.h>
11 #include <linux/affs_hardblocks.h>
12
13 #include "check.h"
14 #include "amiga.h"
15
16 static __inline__ u32
17 checksum_block(u32 *m, int size)
18 {
19         u32 sum = 0;
20
21         while (size--)
22                 sum += be32_to_cpu(*m++);
23         return sum;
24 }
25
26 int
27 amiga_partition(struct parsed_partitions *state, struct block_device *bdev)
28 {
29         Sector sect;
30         unsigned char *data;
31         struct RigidDiskBlock *rdb;
32         struct PartitionBlock *pb;
33         int start_sect, nr_sects, blk, part, res = 0;
34         int slot = 1;
35         char b[BDEVNAME_SIZE];
36
37         for (blk = 0; ; blk++, put_dev_sector(sect)) {
38                 if (blk == RDB_ALLOCATION_LIMIT)
39                         goto rdb_done;
40                 data = read_dev_sector(bdev, blk, &sect);
41                 if (!data) {
42                         if (warn_no_part)
43                                 printk("Dev %s: unable to read RDB block %d\n",
44                                        bdevname(bdev, b), blk);
45                         goto rdb_done;
46                 }
47                 if (*(u32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
48                         continue;
49
50                 rdb = (struct RigidDiskBlock *)data;
51                 if (checksum_block((u32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
52                         break;
53                 /* Try again with 0xdc..0xdf zeroed, Windows might have
54                  * trashed it.
55                  */
56                 *(u32 *)(data+0xdc) = 0;
57                 if (checksum_block((u32 *)data,
58                                 be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
59                         printk("Warning: Trashed word at 0xd0 in block %d "
60                                 "ignored in checksum calculation\n",blk);
61                         break;
62                 }
63
64                 printk("Dev %s: RDB in block %d has bad checksum\n",
65                                bdevname(bdev, b), blk);
66         }
67
68         printk(" RDSK");
69         blk = be32_to_cpu(rdb->rdb_PartitionList);
70         put_dev_sector(sect);
71         for (part = 1; blk>0 && part<=16; part++, put_dev_sector(sect)) {
72                 data = read_dev_sector(bdev, blk, &sect);
73                 if (!data) {
74                         if (warn_no_part)
75                                 printk("Dev %s: unable to read partition block %d\n",
76                                        bdevname(bdev, b), blk);
77                         goto rdb_done;
78                 }
79                 pb  = (struct PartitionBlock *)data;
80                 blk = be32_to_cpu(pb->pb_Next);
81                 if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
82                         continue;
83                 if (checksum_block((u32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
84                         continue;
85
86                 /* Tell Kernel about it */
87
88                 nr_sects = (be32_to_cpu(pb->pb_Environment[10]) + 1 -
89                             be32_to_cpu(pb->pb_Environment[9])) *
90                            be32_to_cpu(pb->pb_Environment[3]) *
91                            be32_to_cpu(pb->pb_Environment[5]);
92                 if (!nr_sects)
93                         continue;
94                 start_sect = be32_to_cpu(pb->pb_Environment[9]) *
95                              be32_to_cpu(pb->pb_Environment[3]) *
96                              be32_to_cpu(pb->pb_Environment[5]);
97                 put_partition(state,slot++,start_sect,nr_sects);
98                 res = 1;
99         }
100         printk("\n");
101
102 rdb_done:
103         return res;
104 }