xref: /openbmc/u-boot/disk/part_mac.c (revision 1e52fea3)
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 /*
25  * Support for harddisk partitions.
26  *
27  * To be compatible with LinuxPPC and Apple we use the standard Apple
28  * SCSI disk partitioning scheme. For more information see:
29  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
30  */
31 
32 #include <common.h>
33 #include <command.h>
34 #include <ide.h>
35 #include "part_mac.h"
36 
37 #if defined(CONFIG_CMD_IDE) || \
38     defined(CONFIG_CMD_MG_DISK) || \
39     defined(CONFIG_CMD_SCSI) || \
40     defined(CONFIG_CMD_SATA) || \
41     defined(CONFIG_CMD_USB) || \
42     defined(CONFIG_MMC) || \
43     defined(CONFIG_SYSTEMACE)
44 
45 /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
46 #ifndef __ldiv_t_defined
47 typedef struct {
48 	long int quot;		/* Quotient	*/
49 	long int rem;		/* Remainder	*/
50 } ldiv_t;
51 extern ldiv_t ldiv (long int __numer, long int __denom);
52 # define __ldiv_t_defined	1
53 #endif
54 
55 
56 static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
57 static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
58 
59 /*
60  * Test for a valid MAC partition
61  */
62 int test_part_mac (block_dev_desc_t *dev_desc)
63 {
64 	mac_driver_desc_t	ddesc;
65 	mac_partition_t		mpart;
66 	ulong i, n;
67 
68 	if (part_mac_read_ddb (dev_desc, &ddesc)) {
69 		/* error reading Driver Desriptor Block, or no valid Signature */
70 		return (-1);
71 	}
72 
73 	n = 1;	/* assuming at least one partition */
74 	for (i=1; i<=n; ++i) {
75 		if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)&mpart) != 1) ||
76 		    (mpart.signature != MAC_PARTITION_MAGIC) ) {
77 			return (-1);
78 		}
79 		/* update partition count */
80 		n = mpart.map_count;
81 	}
82 	return (0);
83 }
84 
85 
86 void print_part_mac (block_dev_desc_t *dev_desc)
87 {
88 	ulong i, n;
89 	mac_driver_desc_t	ddesc;
90 	mac_partition_t		mpart;
91 	ldiv_t mb, gb;
92 
93 	if (part_mac_read_ddb (dev_desc, &ddesc)) {
94 		/* error reading Driver Desriptor Block, or no valid Signature */
95 		return;
96 	}
97 
98 	n  = ddesc.blk_count;
99 
100 	mb = ldiv(n, ((1024 * 1024) / ddesc.blk_size)); /* MB */
101 	/* round to 1 digit */
102 	mb.rem *= 10 * ddesc.blk_size;
103 	mb.rem += 512 * 1024;
104 	mb.rem /= 1024 * 1024;
105 
106 	gb = ldiv(10 * mb.quot + mb.rem, 10240);
107 	gb.rem += 512;
108 	gb.rem /= 1024;
109 
110 
111 	printf ("Block Size=%d, Number of Blocks=%d, "
112 		"Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
113 		"DeviceType=0x%x, DeviceId=0x%x\n\n"
114 		"   #:                 type name"
115 		"                   length   base       (size)\n",
116 		ddesc.blk_size,
117 		ddesc.blk_count,
118 		mb.quot, mb.rem, gb.quot, gb.rem,
119 		ddesc.dev_type, ddesc.dev_id
120 		);
121 
122 	n = 1;	/* assuming at least one partition */
123 	for (i=1; i<=n; ++i) {
124 		ulong bytes;
125 		char c;
126 
127 		printf ("%4ld: ", i);
128 		if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)&mpart) != 1) {
129 			printf ("** Can't read Partition Map on %d:%ld **\n",
130 				dev_desc->dev, i);
131 			return;
132 		}
133 
134 		if (mpart.signature != MAC_PARTITION_MAGIC) {
135 			printf ("** Bad Signature on %d:%ld - "
136 				"expected 0x%04x, got 0x%04x\n",
137 				dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart.signature);
138 			return;
139 		}
140 
141 		/* update partition count */
142 		n = mpart.map_count;
143 
144 		c      = 'k';
145 		bytes  = mpart.block_count;
146 		bytes /= (1024 / ddesc.blk_size);  /* kB; assumes blk_size == 512 */
147 		if (bytes >= 1024) {
148 			bytes >>= 10;
149 			c = 'M';
150 		}
151 		if (bytes >= 1024) {
152 			bytes >>= 10;
153 			c = 'G';
154 		}
155 
156 		printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
157 			mpart.type,
158 			mpart.name,
159 			mpart.block_count,
160 			mpart.start_block,
161 			bytes, c
162 			);
163 	}
164 
165 	return;
166 }
167 
168 
169 /*
170  * Read Device Descriptor Block
171  */
172 static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
173 {
174 	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
175 		printf ("** Can't read Driver Desriptor Block **\n");
176 		return (-1);
177 	}
178 
179 	if (ddb_p->signature != MAC_DRIVER_MAGIC) {
180 #if 0
181 		printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
182 			MAC_DRIVER_MAGIC, ddb_p->signature);
183 #endif
184 		return (-1);
185 	}
186 	return (0);
187 }
188 
189 /*
190  * Read Partition Descriptor Block
191  */
192 static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
193 {
194 	int n = 1;
195 
196 	for (;;) {
197 		/*
198 		 * We must always read the descritpor block for
199 		 * partition 1 first since this is the only way to
200 		 * know how many partitions we have.
201 		 */
202 		if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
203 			printf ("** Can't read Partition Map on %d:%d **\n",
204 				dev_desc->dev, n);
205 			return (-1);
206 		}
207 
208 		if (pdb_p->signature != MAC_PARTITION_MAGIC) {
209 			printf ("** Bad Signature on %d:%d: "
210 				"expected 0x%04x, got 0x%04x\n",
211 				dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
212 			return (-1);
213 		}
214 
215 		if (n == part)
216 			return (0);
217 
218 		if ((part < 1) || (part > pdb_p->map_count)) {
219 			printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
220 				dev_desc->dev, part,
221 				dev_desc->dev,
222 				dev_desc->dev, pdb_p->map_count);
223 			return (-1);
224 		}
225 
226 		/* update partition count */
227 		n = part;
228 	}
229 
230 	/* NOTREACHED */
231 }
232 
233 int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
234 {
235 	mac_driver_desc_t	ddesc;
236 	mac_partition_t		mpart;
237 
238 	if (part_mac_read_ddb (dev_desc, &ddesc)) {
239 		return (-1);
240 	}
241 
242 	info->blksz = ddesc.blk_size;
243 
244 	if (part_mac_read_pdb (dev_desc, part, &mpart)) {
245 		return (-1);
246 	}
247 
248 	info->start = mpart.start_block;
249 	info->size  = mpart.block_count;
250 	memcpy (info->type, mpart.type, sizeof(info->type));
251 	memcpy (info->name, mpart.name, sizeof(info->name));
252 
253 	return (0);
254 }
255 
256 #endif
257