1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 
11 #define pr_fmt(fmt) fmt
12 
13 #include <linux/types.h>
14 #include <linux/affs_hardblocks.h>
15 
16 #include "check.h"
17 #include "amiga.h"
18 
19 static __inline__ u32
checksum_block(__be32 * m,int size)20 checksum_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 
amiga_partition(struct parsed_partitions * state)29 int amiga_partition(struct parsed_partitions *state)
30 {
31 	Sector sect;
32 	unsigned char *data;
33 	struct RigidDiskBlock *rdb;
34 	struct PartitionBlock *pb;
35 	sector_t start_sect, nr_sects;
36 	int blk, part, res = 0;
37 	int blksize = 1;	/* Multiplier for disk block size */
38 	int slot = 1;
39 	char b[BDEVNAME_SIZE];
40 
41 	for (blk = 0; ; blk++, put_dev_sector(sect)) {
42 		if (blk == RDB_ALLOCATION_LIMIT)
43 			goto rdb_done;
44 		data = read_part_sector(state, blk, &sect);
45 		if (!data) {
46 			if (warn_no_part)
47 				pr_err("Dev %s: unable to read RDB block %d\n",
48 				       bdevname(state->bdev, b), blk);
49 			res = -1;
50 			goto rdb_done;
51 		}
52 		if (*(__be32 *)data != cpu_to_be32(IDNAME_RIGIDDISK))
53 			continue;
54 
55 		rdb = (struct RigidDiskBlock *)data;
56 		if (checksum_block((__be32 *)data, be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F) == 0)
57 			break;
58 		/* Try again with 0xdc..0xdf zeroed, Windows might have
59 		 * trashed it.
60 		 */
61 		*(__be32 *)(data+0xdc) = 0;
62 		if (checksum_block((__be32 *)data,
63 				be32_to_cpu(rdb->rdb_SummedLongs) & 0x7F)==0) {
64 			pr_err("Trashed word at 0xd0 in block %d ignored in checksum calculation\n",
65 			       blk);
66 			break;
67 		}
68 
69 		pr_err("Dev %s: RDB in block %d has bad checksum\n",
70 		       bdevname(state->bdev, b), blk);
71 	}
72 
73 	/* blksize is blocks per 512 byte standard block */
74 	blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
75 
76 	{
77 		char tmp[7 + 10 + 1 + 1];
78 
79 		/* Be more informative */
80 		snprintf(tmp, sizeof(tmp), " RDSK (%d)", blksize * 512);
81 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
82 	}
83 	blk = be32_to_cpu(rdb->rdb_PartitionList);
84 	put_dev_sector(sect);
85 	for (part = 1; blk>0 && part<=16; part++, put_dev_sector(sect)) {
86 		blk *= blksize;	/* Read in terms partition table understands */
87 		data = read_part_sector(state, blk, &sect);
88 		if (!data) {
89 			if (warn_no_part)
90 				pr_err("Dev %s: unable to read partition block %d\n",
91 				       bdevname(state->bdev, b), blk);
92 			res = -1;
93 			goto rdb_done;
94 		}
95 		pb  = (struct PartitionBlock *)data;
96 		blk = be32_to_cpu(pb->pb_Next);
97 		if (pb->pb_ID != cpu_to_be32(IDNAME_PARTITION))
98 			continue;
99 		if (checksum_block((__be32 *)pb, be32_to_cpu(pb->pb_SummedLongs) & 0x7F) != 0 )
100 			continue;
101 
102 		/* Tell Kernel about it */
103 
104 		nr_sects = ((sector_t)be32_to_cpu(pb->pb_Environment[10]) + 1 -
105 			   be32_to_cpu(pb->pb_Environment[9])) *
106 			   be32_to_cpu(pb->pb_Environment[3]) *
107 			   be32_to_cpu(pb->pb_Environment[5]) *
108 			   blksize;
109 		if (!nr_sects)
110 			continue;
111 		start_sect = (sector_t)be32_to_cpu(pb->pb_Environment[9]) *
112 			     be32_to_cpu(pb->pb_Environment[3]) *
113 			     be32_to_cpu(pb->pb_Environment[5]) *
114 			     blksize;
115 		put_partition(state,slot++,start_sect,nr_sects);
116 		{
117 			/* Be even more informative to aid mounting */
118 			char dostype[4];
119 			char tmp[42];
120 
121 			__be32 *dt = (__be32 *)dostype;
122 			*dt = pb->pb_Environment[16];
123 			if (dostype[3] < ' ')
124 				snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
125 					dostype[0], dostype[1],
126 					dostype[2], dostype[3] + '@' );
127 			else
128 				snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
129 					dostype[0], dostype[1],
130 					dostype[2], dostype[3]);
131 			strlcat(state->pp_buf, tmp, PAGE_SIZE);
132 			snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
133 				be32_to_cpu(pb->pb_Environment[6]),
134 				be32_to_cpu(pb->pb_Environment[4]));
135 			strlcat(state->pp_buf, tmp, PAGE_SIZE);
136 		}
137 		res = 1;
138 	}
139 	strlcat(state->pp_buf, "\n", PAGE_SIZE);
140 
141 rdb_done:
142 	return res;
143 }
144