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