1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * Support for harddisk partitions.
9  *
10  * To be compatible with LinuxPPC and Apple we use the standard Apple
11  * SCSI disk partitioning scheme. For more information see:
12  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
13  */
14 
15 #include <command.h>
16 #include <log.h>
17 #include <memalign.h>
18 #include "part_mac.h"
19 #include <part.h>
20 
21 /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
22 #ifndef __ldiv_t_defined
23 typedef struct {
24 	long int quot;		/* Quotient	*/
25 	long int rem;		/* Remainder	*/
26 } ldiv_t;
27 extern ldiv_t ldiv (long int __numer, long int __denom);
28 # define __ldiv_t_defined	1
29 #endif
30 
31 static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p);
32 static int part_mac_read_pdb(struct blk_desc *desc, int part,
33 			     mac_partition_t *pdb_p);
34 
35 /*
36  * Test for a valid MAC partition
37  */
part_test_mac(struct blk_desc * desc)38 static int part_test_mac(struct blk_desc *desc)
39 {
40 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
41 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
42 	ulong i, n;
43 
44 	if (part_mac_read_ddb(desc, ddesc)) {
45 		/*
46 		 * error reading Driver Descriptor Block,
47 		 * or no valid Signature
48 		 */
49 		return (-1);
50 	}
51 
52 	n = 1;	/* assuming at least one partition */
53 	for (i=1; i<=n; ++i) {
54 		if ((blk_dread(desc, i, 1, (ulong *)mpart) != 1) ||
55 		    mpart->signature != MAC_PARTITION_MAGIC) {
56 			return (-1);
57 		}
58 		/* update partition count */
59 		n = mpart->map_count;
60 	}
61 	return (0);
62 }
63 
part_print_mac(struct blk_desc * desc)64 static void part_print_mac(struct blk_desc *desc)
65 {
66 	ulong i, n;
67 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
68 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
69 	ldiv_t mb, gb;
70 
71 	if (part_mac_read_ddb(desc, ddesc)) {
72 		/*
73 		 * error reading Driver Descriptor Block,
74 		 * or no valid Signature
75 		 */
76 		return;
77 	}
78 
79 	n  = ddesc->blk_count;
80 
81 	mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
82 	/* round to 1 digit */
83 	mb.rem *= 10 * ddesc->blk_size;
84 	mb.rem += 512 * 1024;
85 	mb.rem /= 1024 * 1024;
86 
87 	gb = ldiv(10 * mb.quot + mb.rem, 10240);
88 	gb.rem += 512;
89 	gb.rem /= 1024;
90 
91 	printf ("Block Size=%d, Number of Blocks=%d, "
92 		"Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
93 		"DeviceType=0x%x, DeviceId=0x%x\n\n"
94 		"   #:                 type name"
95 		"                   length   base       (size)\n",
96 		ddesc->blk_size,
97 		ddesc->blk_count,
98 		mb.quot, mb.rem, gb.quot, gb.rem,
99 		ddesc->dev_type, ddesc->dev_id
100 		);
101 
102 	n = 1;	/* assuming at least one partition */
103 	for (i=1; i<=n; ++i) {
104 		ulong bytes;
105 		char c;
106 
107 		printf ("%4ld: ", i);
108 		if (blk_dread(desc, i, 1, (ulong *)mpart) != 1) {
109 			printf ("** Can't read Partition Map on %d:%ld **\n",
110 				desc->devnum, i);
111 			return;
112 		}
113 
114 		if (mpart->signature != MAC_PARTITION_MAGIC) {
115 			printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
116 			       desc->devnum, i, MAC_PARTITION_MAGIC,
117 			       mpart->signature);
118 			return;
119 		}
120 
121 		/* update partition count */
122 		n = mpart->map_count;
123 
124 		c      = 'k';
125 		bytes  = mpart->block_count;
126 		bytes /= (1024 / ddesc->blk_size);  /* kB; assumes blk_size == 512 */
127 		if (bytes >= 1024) {
128 			bytes >>= 10;
129 			c = 'M';
130 		}
131 		if (bytes >= 1024) {
132 			bytes >>= 10;
133 			c = 'G';
134 		}
135 
136 		printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
137 			mpart->type,
138 			mpart->name,
139 			mpart->block_count,
140 			mpart->start_block,
141 			bytes, c
142 			);
143 	}
144 
145 	return;
146 }
147 
148 /*
149  * Read Device Descriptor Block
150  */
part_mac_read_ddb(struct blk_desc * desc,mac_driver_desc_t * ddb_p)151 static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p)
152 {
153 	if (blk_dread(desc, 0, 1, (ulong *)ddb_p) != 1) {
154 		debug("** Can't read Driver Descriptor Block **\n");
155 		return (-1);
156 	}
157 
158 	if (ddb_p->signature != MAC_DRIVER_MAGIC) {
159 		return (-1);
160 	}
161 	return (0);
162 }
163 
164 /*
165  * Read Partition Descriptor Block
166  */
part_mac_read_pdb(struct blk_desc * desc,int part,mac_partition_t * pdb_p)167 static int part_mac_read_pdb(struct blk_desc *desc, int part,
168 			     mac_partition_t *pdb_p)
169 {
170 	int n = 1;
171 
172 	for (;;) {
173 		/*
174 		 * We must always read the descritpor block for
175 		 * partition 1 first since this is the only way to
176 		 * know how many partitions we have.
177 		 */
178 		if (blk_dread(desc, n, 1, (ulong *)pdb_p) != 1) {
179 			printf("** Can't read Partition Map on %d:%d **\n",
180 			       desc->devnum, n);
181 			return (-1);
182 		}
183 
184 		if (pdb_p->signature != MAC_PARTITION_MAGIC) {
185 			printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n",
186 			       desc->devnum, n, MAC_PARTITION_MAGIC,
187 			       pdb_p->signature);
188 			return (-1);
189 		}
190 
191 		if (n == part)
192 			return (0);
193 
194 		if ((part < 1) || (part > pdb_p->map_count)) {
195 			printf("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
196 			       desc->devnum, part, desc->devnum, desc->devnum,
197 			       pdb_p->map_count);
198 			return (-1);
199 		}
200 
201 		/* update partition count */
202 		n = part;
203 	}
204 
205 	/* NOTREACHED */
206 }
207 
part_get_info_mac(struct blk_desc * desc,int part,struct disk_partition * info)208 static int part_get_info_mac(struct blk_desc *desc, int part,
209 			     struct disk_partition *info)
210 {
211 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
212 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
213 
214 	if (part_mac_read_ddb(desc, ddesc))
215 		return -1;
216 
217 	info->blksz = ddesc->blk_size;
218 
219 	if (part_mac_read_pdb(desc, part, mpart))
220 		return -1;
221 
222 	info->start = mpart->start_block;
223 	info->size  = mpart->block_count;
224 	memcpy (info->type, mpart->type, sizeof(info->type));
225 	memcpy (info->name, mpart->name, sizeof(info->name));
226 
227 	return (0);
228 }
229 
230 U_BOOT_PART_TYPE(mac) = {
231 	.name		= "MAC",
232 	.part_type	= PART_TYPE_MAC,
233 	.max_entries	= MAC_ENTRY_NUMBERS,
234 	.get_info	= part_get_info_mac,
235 	.print		= part_print_mac,
236 	.test		= part_test_mac,
237 };
238