xref: /openbmc/linux/block/partitions/amiga.c (revision de528723)
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
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 
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 	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;
43 		data = read_part_sector(state, blk, &sect);
44 		if (!data) {
45 			if (warn_no_part)
46 				pr_err("Dev %s: unable to read RDB block %d\n",
47 				       bdevname(state->bdev, b), blk);
48 			res = -1;
49 			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) {
63 			pr_err("Trashed word at 0xd0 in block %d ignored in checksum calculation\n",
64 			       blk);
65 			break;
66 		}
67 
68 		pr_err("Dev %s: RDB in block %d has bad checksum\n",
69 		       bdevname(state->bdev, b), blk);
70 	}
71 
72 	/* blksize is blocks per 512 byte standard block */
73 	blksize = be32_to_cpu( rdb->rdb_BlockBytes ) / 512;
74 
75 	{
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 	}
82 	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 */
86 		data = read_part_sector(state, blk, &sect);
87 		if (!data) {
88 			if (warn_no_part)
89 				pr_err("Dev %s: unable to read partition block %d\n",
90 				       bdevname(state->bdev, b), blk);
91 			res = -1;
92 			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];
118 			char tmp[42];
119 
120 			__be32 *dt = (__be32 *)dostype;
121 			*dt = pb->pb_Environment[16];
122 			if (dostype[3] < ' ')
123 				snprintf(tmp, sizeof(tmp), " (%c%c%c^%c)",
124 					dostype[0], dostype[1],
125 					dostype[2], dostype[3] + '@' );
126 			else
127 				snprintf(tmp, sizeof(tmp), " (%c%c%c%c)",
128 					dostype[0], dostype[1],
129 					dostype[2], dostype[3]);
130 			strlcat(state->pp_buf, tmp, PAGE_SIZE);
131 			snprintf(tmp, sizeof(tmp), "(res %d spb %d)",
132 				be32_to_cpu(pb->pb_Environment[6]),
133 				be32_to_cpu(pb->pb_Environment[4]));
134 			strlcat(state->pp_buf, tmp, PAGE_SIZE);
135 		}
136 		res = 1;
137 	}
138 	strlcat(state->pp_buf, "\n", PAGE_SIZE);
139 
140 rdb_done:
141 	return res;
142 }
143