xref: /openbmc/u-boot/disk/part_mac.c (revision 64a08a9ffc0ff8c9e56b204842a5650c19b48db4)
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 
377bd2722eSMike Frysinger #if defined(CONFIG_CMD_IDE) || \
38cde5c64dSJon Loeliger     defined(CONFIG_CMD_SCSI) || \
39c7057b52SDave Liu     defined(CONFIG_CMD_SATA) || \
40cde5c64dSJon Loeliger     defined(CONFIG_CMD_USB) || \
411968e615Swdenk     defined(CONFIG_MMC) || \
427bd2722eSMike Frysinger     defined(CONFIG_SYSTEMACE)
43fe8c2806Swdenk 
44fe8c2806Swdenk /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
45fe8c2806Swdenk #ifndef __ldiv_t_defined
46fe8c2806Swdenk typedef struct {
47fe8c2806Swdenk 	long int quot;		/* Quotient	*/
48fe8c2806Swdenk 	long int rem;		/* Remainder	*/
49fe8c2806Swdenk } ldiv_t;
50fe8c2806Swdenk extern ldiv_t ldiv (long int __numer, long int __denom);
51fe8c2806Swdenk # define __ldiv_t_defined	1
52fe8c2806Swdenk #endif
53fe8c2806Swdenk 
54fe8c2806Swdenk 
55fe8c2806Swdenk static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
56fe8c2806Swdenk static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
57fe8c2806Swdenk 
58fe8c2806Swdenk /*
59fe8c2806Swdenk  * Test for a valid MAC partition
60fe8c2806Swdenk  */
61fe8c2806Swdenk int test_part_mac (block_dev_desc_t *dev_desc)
62fe8c2806Swdenk {
63*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
64*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
65fe8c2806Swdenk 	ulong i, n;
66fe8c2806Swdenk 
67*64a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
68fe8c2806Swdenk 		/* error reading Driver Desriptor Block, or no valid Signature */
69fe8c2806Swdenk 		return (-1);
70fe8c2806Swdenk 	}
71fe8c2806Swdenk 
72fe8c2806Swdenk 	n = 1;	/* assuming at least one partition */
73fe8c2806Swdenk 	for (i=1; i<=n; ++i) {
74*64a08a9fSBenoît Thébaudeau 		if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)mpart) != 1) ||
75*64a08a9fSBenoît Thébaudeau 		    (mpart->signature != MAC_PARTITION_MAGIC) ) {
76fe8c2806Swdenk 			return (-1);
77fe8c2806Swdenk 		}
78fe8c2806Swdenk 		/* update partition count */
79*64a08a9fSBenoît Thébaudeau 		n = mpart->map_count;
80fe8c2806Swdenk 	}
81fe8c2806Swdenk 	return (0);
82fe8c2806Swdenk }
83fe8c2806Swdenk 
84fe8c2806Swdenk 
85fe8c2806Swdenk void print_part_mac (block_dev_desc_t *dev_desc)
86fe8c2806Swdenk {
87fe8c2806Swdenk 	ulong i, n;
88*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
89*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
90fe8c2806Swdenk 	ldiv_t mb, gb;
91fe8c2806Swdenk 
92*64a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
93fe8c2806Swdenk 		/* error reading Driver Desriptor Block, or no valid Signature */
94fe8c2806Swdenk 		return;
95fe8c2806Swdenk 	}
96fe8c2806Swdenk 
97*64a08a9fSBenoît Thébaudeau 	n  = ddesc->blk_count;
98fe8c2806Swdenk 
99*64a08a9fSBenoît Thébaudeau 	mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
100fe8c2806Swdenk 	/* round to 1 digit */
101*64a08a9fSBenoît Thébaudeau 	mb.rem *= 10 * ddesc->blk_size;
102fe8c2806Swdenk 	mb.rem += 512 * 1024;
103fe8c2806Swdenk 	mb.rem /= 1024 * 1024;
104fe8c2806Swdenk 
105fe8c2806Swdenk 	gb = ldiv(10 * mb.quot + mb.rem, 10240);
106fe8c2806Swdenk 	gb.rem += 512;
107fe8c2806Swdenk 	gb.rem /= 1024;
108fe8c2806Swdenk 
109fe8c2806Swdenk 
110fe8c2806Swdenk 	printf ("Block Size=%d, Number of Blocks=%d, "
111fe8c2806Swdenk 		"Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
112fe8c2806Swdenk 		"DeviceType=0x%x, DeviceId=0x%x\n\n"
113fe8c2806Swdenk 		"   #:                 type name"
114fe8c2806Swdenk 		"                   length   base       (size)\n",
115*64a08a9fSBenoît Thébaudeau 		ddesc->blk_size,
116*64a08a9fSBenoît Thébaudeau 		ddesc->blk_count,
117fe8c2806Swdenk 		mb.quot, mb.rem, gb.quot, gb.rem,
118*64a08a9fSBenoît Thébaudeau 		ddesc->dev_type, ddesc->dev_id
119fe8c2806Swdenk 		);
120fe8c2806Swdenk 
121fe8c2806Swdenk 	n = 1;	/* assuming at least one partition */
122fe8c2806Swdenk 	for (i=1; i<=n; ++i) {
123fe8c2806Swdenk 		ulong bytes;
124fe8c2806Swdenk 		char c;
125fe8c2806Swdenk 
126fe8c2806Swdenk 		printf ("%4ld: ", i);
127*64a08a9fSBenoît Thébaudeau 		if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)mpart) != 1) {
128fe8c2806Swdenk 			printf ("** Can't read Partition Map on %d:%ld **\n",
129fe8c2806Swdenk 				dev_desc->dev, i);
130fe8c2806Swdenk 			return;
131fe8c2806Swdenk 		}
132fe8c2806Swdenk 
133*64a08a9fSBenoît Thébaudeau 		if (mpart->signature != MAC_PARTITION_MAGIC) {
134fe8c2806Swdenk 			printf ("** Bad Signature on %d:%ld - "
135fe8c2806Swdenk 				"expected 0x%04x, got 0x%04x\n",
136*64a08a9fSBenoît Thébaudeau 				dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart->signature);
137fe8c2806Swdenk 			return;
138fe8c2806Swdenk 		}
139fe8c2806Swdenk 
140fe8c2806Swdenk 		/* update partition count */
141*64a08a9fSBenoît Thébaudeau 		n = mpart->map_count;
142fe8c2806Swdenk 
143fe8c2806Swdenk 		c      = 'k';
144*64a08a9fSBenoît Thébaudeau 		bytes  = mpart->block_count;
145*64a08a9fSBenoît Thébaudeau 		bytes /= (1024 / ddesc->blk_size);  /* kB; assumes blk_size == 512 */
146fe8c2806Swdenk 		if (bytes >= 1024) {
147fe8c2806Swdenk 			bytes >>= 10;
148fe8c2806Swdenk 			c = 'M';
149fe8c2806Swdenk 		}
150fe8c2806Swdenk 		if (bytes >= 1024) {
151fe8c2806Swdenk 			bytes >>= 10;
152fe8c2806Swdenk 			c = 'G';
153fe8c2806Swdenk 		}
154fe8c2806Swdenk 
155fe8c2806Swdenk 		printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
156*64a08a9fSBenoît Thébaudeau 			mpart->type,
157*64a08a9fSBenoît Thébaudeau 			mpart->name,
158*64a08a9fSBenoît Thébaudeau 			mpart->block_count,
159*64a08a9fSBenoît Thébaudeau 			mpart->start_block,
160fe8c2806Swdenk 			bytes, c
161fe8c2806Swdenk 			);
162fe8c2806Swdenk 	}
163fe8c2806Swdenk 
164fe8c2806Swdenk 	return;
165fe8c2806Swdenk }
166fe8c2806Swdenk 
167fe8c2806Swdenk 
168fe8c2806Swdenk /*
169fe8c2806Swdenk  * Read Device Descriptor Block
170fe8c2806Swdenk  */
171fe8c2806Swdenk static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
172fe8c2806Swdenk {
173fe8c2806Swdenk 	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
174fe8c2806Swdenk 		printf ("** Can't read Driver Desriptor Block **\n");
175fe8c2806Swdenk 		return (-1);
176fe8c2806Swdenk 	}
177fe8c2806Swdenk 
178fe8c2806Swdenk 	if (ddb_p->signature != MAC_DRIVER_MAGIC) {
179fe8c2806Swdenk #if 0
180fe8c2806Swdenk 		printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
181fe8c2806Swdenk 			MAC_DRIVER_MAGIC, ddb_p->signature);
182fe8c2806Swdenk #endif
183fe8c2806Swdenk 		return (-1);
184fe8c2806Swdenk 	}
185fe8c2806Swdenk 	return (0);
186fe8c2806Swdenk }
187fe8c2806Swdenk 
188fe8c2806Swdenk /*
189fe8c2806Swdenk  * Read Partition Descriptor Block
190fe8c2806Swdenk  */
191fe8c2806Swdenk static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
192fe8c2806Swdenk {
193fe8c2806Swdenk 	int n = 1;
194fe8c2806Swdenk 
195fe8c2806Swdenk 	for (;;) {
196fe8c2806Swdenk 		/*
197fe8c2806Swdenk 		 * We must always read the descritpor block for
198fe8c2806Swdenk 		 * partition 1 first since this is the only way to
199fe8c2806Swdenk 		 * know how many partitions we have.
200fe8c2806Swdenk 		 */
201fe8c2806Swdenk 		if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
202fe8c2806Swdenk 			printf ("** Can't read Partition Map on %d:%d **\n",
203fe8c2806Swdenk 				dev_desc->dev, n);
204fe8c2806Swdenk 			return (-1);
205fe8c2806Swdenk 		}
206fe8c2806Swdenk 
207fe8c2806Swdenk 		if (pdb_p->signature != MAC_PARTITION_MAGIC) {
208fe8c2806Swdenk 			printf ("** Bad Signature on %d:%d: "
209fe8c2806Swdenk 				"expected 0x%04x, got 0x%04x\n",
210fe8c2806Swdenk 				dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
211fe8c2806Swdenk 			return (-1);
212fe8c2806Swdenk 		}
213fe8c2806Swdenk 
214fe8c2806Swdenk 		if (n == part)
215fe8c2806Swdenk 			return (0);
216fe8c2806Swdenk 
217fe8c2806Swdenk 		if ((part < 1) || (part > pdb_p->map_count)) {
218fe8c2806Swdenk 			printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
219fe8c2806Swdenk 				dev_desc->dev, part,
220fe8c2806Swdenk 				dev_desc->dev,
221fe8c2806Swdenk 				dev_desc->dev, pdb_p->map_count);
222fe8c2806Swdenk 			return (-1);
223fe8c2806Swdenk 		}
224fe8c2806Swdenk 
225fe8c2806Swdenk 		/* update partition count */
226fe8c2806Swdenk 		n = part;
227fe8c2806Swdenk 	}
228fe8c2806Swdenk 
229fe8c2806Swdenk 	/* NOTREACHED */
230fe8c2806Swdenk }
231fe8c2806Swdenk 
232fe8c2806Swdenk int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
233fe8c2806Swdenk {
234*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
235*64a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
236fe8c2806Swdenk 
237*64a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
238fe8c2806Swdenk 		return (-1);
239fe8c2806Swdenk 	}
240fe8c2806Swdenk 
241*64a08a9fSBenoît Thébaudeau 	info->blksz = ddesc->blk_size;
242fe8c2806Swdenk 
243*64a08a9fSBenoît Thébaudeau 	if (part_mac_read_pdb (dev_desc, part, mpart)) {
244fe8c2806Swdenk 		return (-1);
245fe8c2806Swdenk 	}
246fe8c2806Swdenk 
247*64a08a9fSBenoît Thébaudeau 	info->start = mpart->start_block;
248*64a08a9fSBenoît Thébaudeau 	info->size  = mpart->block_count;
249*64a08a9fSBenoît Thébaudeau 	memcpy (info->type, mpart->type, sizeof(info->type));
250*64a08a9fSBenoît Thébaudeau 	memcpy (info->name, mpart->name, sizeof(info->name));
251fe8c2806Swdenk 
252fe8c2806Swdenk 	return (0);
253fe8c2806Swdenk }
254fe8c2806Swdenk 
255cde5c64dSJon Loeliger #endif
256