blob: 560936617d9c146b84d980de6137a3d241da2986 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/partitions/amiga.c
4 *
5 * Code extracted from drivers/block/genhd.c
6 *
7 * Copyright (C) 1991-1998 Linus Torvalds
8 * Re-organised Feb 1998 Russell King
9 */
10
Fabian Frederick600ffc52014-06-12 20:04:52 +020011#define pr_fmt(fmt) fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/affs_hardblocks.h>
15
16#include "check.h"
17#include "amiga.h"
18
19static __inline__ u32
20checksum_block(__be32 *m, int size)
21{
22 u32 sum = 0;
23
24 while (size--)
25 sum += be32_to_cpu(*m++);
26 return sum;
27}
28
Tejun Heo1493bf22010-05-15 20:09:30 +020029int amiga_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 Sector sect;
32 unsigned char *data;
33 struct RigidDiskBlock *rdb;
34 struct PartitionBlock *pb;
35 int start_sect, nr_sects, blk, part, res = 0;
36 int blksize = 1; /* Multiplier for disk block size */
37 int slot = 1;
38 char b[BDEVNAME_SIZE];
39
40 for (blk = 0; ; blk++, put_dev_sector(sect)) {
41 if (blk == RDB_ALLOCATION_LIMIT)
42 goto rdb_done;
Tejun Heo1493bf22010-05-15 20:09:30 +020043 data = read_part_sector(state, blk, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (!data) {
45 if (warn_no_part)
Fabian Frederick600ffc52014-06-12 20:04:52 +020046 pr_err("Dev %s: unable to read RDB block %d\n",
Tejun Heo1493bf22010-05-15 20:09:30 +020047 bdevname(state->bdev, b), blk);
Suzuki K P57881dd2006-12-06 20:35:16 -080048 res = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 goto rdb_done;
50 }
51 if (*(__be32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
52 continue;
53
54 rdb = (struct RigidDiskBlock *)data;
55 if (checksum_block((__be32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
56 break;
57 /* Try again with 0xdc..0xdf zeroed, Windows might have
58 * trashed it.
59 */
60 *(__be32 *)(data+0xdc) = 0;
61 if (checksum_block((__be32 *)data,
62 be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
Fabian Frederick600ffc52014-06-12 20:04:52 +020063 pr_err("Trashed word at 0xd0 in block %d ignored in checksum calculation\n",
64 blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
66 }
67
Fabian Frederick600ffc52014-06-12 20:04:52 +020068 pr_err("Dev %s: RDB in block %d has bad checksum\n",
Tejun Heo1493bf22010-05-15 20:09:30 +020069 bdevname(state->bdev, b), blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 /* blksize is blocks per 512 byte standard block */
73 blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
74
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -070075 {
76 char tmp[7 + 10 + 1 + 1];
77
78 /* Be more informative */
79 snprintf(tmp, sizeof(tmp), " RDSK (%d)", blksize * 512);
80 strlcat(state->pp_buf, tmp, PAGE_SIZE);
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 blk = be32_to_cpu(rdb->rdb_PartitionList);
83 put_dev_sector(sect);
84 for (part = 1; blk>0 && part<=16; part++, put_dev_sector(sect)) {
85 blk *= blksize; /* Read in terms partition table understands */
Tejun Heo1493bf22010-05-15 20:09:30 +020086 data = read_part_sector(state, blk, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!data) {
88 if (warn_no_part)
Fabian Frederick600ffc52014-06-12 20:04:52 +020089 pr_err("Dev %s: unable to read partition block %d\n",
Tejun Heo1493bf22010-05-15 20:09:30 +020090 bdevname(state->bdev, b), blk);
Suzuki K P57881dd2006-12-06 20:35:16 -080091 res = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 goto rdb_done;
93 }
94 pb = (struct PartitionBlock *)data;
95 blk = be32_to_cpu(pb->pb_Next);
96 if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
97 continue;
98 if (checksum_block((__be32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
99 continue;
100
101 /* Tell Kernel about it */
102
103 nr_sects = (be32_to_cpu(pb->pb_Environment[10]) + 1 -
104 be32_to_cpu(pb->pb_Environment[9])) *
105 be32_to_cpu(pb->pb_Environment[3]) *
106 be32_to_cpu(pb->pb_Environment[5]) *
107 blksize;
108 if (!nr_sects)
109 continue;
110 start_sect = be32_to_cpu(pb->pb_Environment[9]) *
111 be32_to_cpu(pb->pb_Environment[3]) *
112 be32_to_cpu(pb->pb_Environment[5]) *
113 blksize;
114 put_partition(state,slot++,start_sect,nr_sects);
115 {
116 /* Be even more informative to aid mounting */
117 char dostype[4];
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700118 char tmp[42];
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 __be32 *dt = (__be32 *)dostype;
121 *dt = pb->pb_Environment[16];
122 if (dostype[3] < ' ')
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700123 snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dostype[0], dostype[1],
125 dostype[2], dostype[3] + '@' );
126 else
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700127 snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dostype[0], dostype[1],
129 dostype[2], dostype[3]);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700130 strlcat(state->pp_buf, tmp, PAGE_SIZE);
131 snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 be32_to_cpu(pb->pb_Environment[6]),
133 be32_to_cpu(pb->pb_Environment[4]));
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700134 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 res = 1;
137 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700138 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140rdb_done:
141 return res;
142}