xref: /openbmc/u-boot/disk/part_mac.c (revision 1811a928c6c7604d6d05a84b4d552a7c31b4994e)
1fe8c2806Swdenk /*
2fe8c2806Swdenk  * (C) Copyright 2000
3fe8c2806Swdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4fe8c2806Swdenk  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6fe8c2806Swdenk  */
7fe8c2806Swdenk 
8fe8c2806Swdenk /*
9fe8c2806Swdenk  * Support for harddisk partitions.
10fe8c2806Swdenk  *
11fe8c2806Swdenk  * To be compatible with LinuxPPC and Apple we use the standard Apple
12fe8c2806Swdenk  * SCSI disk partitioning scheme. For more information see:
13fe8c2806Swdenk  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
14fe8c2806Swdenk  */
15fe8c2806Swdenk 
16fe8c2806Swdenk #include <common.h>
17fe8c2806Swdenk #include <command.h>
18cf92e05cSSimon Glass #include <memalign.h>
19fe8c2806Swdenk #include <ide.h>
20fe8c2806Swdenk #include "part_mac.h"
21fe8c2806Swdenk 
22*1811a928SAdam Ford #ifdef CONFIG_HAVE_BLOCK_DEVICE
23fe8c2806Swdenk 
24fe8c2806Swdenk /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
25fe8c2806Swdenk #ifndef __ldiv_t_defined
26fe8c2806Swdenk typedef struct {
27fe8c2806Swdenk 	long int quot;		/* Quotient	*/
28fe8c2806Swdenk 	long int rem;		/* Remainder	*/
29fe8c2806Swdenk } ldiv_t;
30fe8c2806Swdenk extern ldiv_t ldiv (long int __numer, long int __denom);
31fe8c2806Swdenk # define __ldiv_t_defined	1
32fe8c2806Swdenk #endif
33fe8c2806Swdenk 
34fe8c2806Swdenk 
354101f687SSimon Glass static int part_mac_read_ddb(struct blk_desc *dev_desc,
364101f687SSimon Glass 			     mac_driver_desc_t *ddb_p);
374101f687SSimon Glass static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
384101f687SSimon Glass 			     mac_partition_t *pdb_p);
39fe8c2806Swdenk 
40fe8c2806Swdenk /*
41fe8c2806Swdenk  * Test for a valid MAC partition
42fe8c2806Swdenk  */
43084bf4c2SSimon Glass static int part_test_mac(struct blk_desc *dev_desc)
44fe8c2806Swdenk {
4564a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
4664a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
47fe8c2806Swdenk 	ulong i, n;
48fe8c2806Swdenk 
4964a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
505eae466eSHeinrich Schuchardt 		/*
515eae466eSHeinrich Schuchardt 		 * error reading Driver Descriptor Block,
525eae466eSHeinrich Schuchardt 		 * or no valid Signature
535eae466eSHeinrich Schuchardt 		 */
54fe8c2806Swdenk 		return (-1);
55fe8c2806Swdenk 	}
56fe8c2806Swdenk 
57fe8c2806Swdenk 	n = 1;	/* assuming at least one partition */
58fe8c2806Swdenk 	for (i=1; i<=n; ++i) {
592a981dc2SSimon Glass 		if ((blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) ||
6064a08a9fSBenoît Thébaudeau 		    (mpart->signature != MAC_PARTITION_MAGIC) ) {
61fe8c2806Swdenk 			return (-1);
62fe8c2806Swdenk 		}
63fe8c2806Swdenk 		/* update partition count */
6464a08a9fSBenoît Thébaudeau 		n = mpart->map_count;
65fe8c2806Swdenk 	}
66fe8c2806Swdenk 	return (0);
67fe8c2806Swdenk }
68fe8c2806Swdenk 
69084bf4c2SSimon Glass static void part_print_mac(struct blk_desc *dev_desc)
70fe8c2806Swdenk {
71fe8c2806Swdenk 	ulong i, n;
7264a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
7364a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
74fe8c2806Swdenk 	ldiv_t mb, gb;
75fe8c2806Swdenk 
7664a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
775eae466eSHeinrich Schuchardt 		/*
785eae466eSHeinrich Schuchardt 		 * error reading Driver Descriptor Block,
795eae466eSHeinrich Schuchardt 		 * or no valid Signature
805eae466eSHeinrich Schuchardt 		 */
81fe8c2806Swdenk 		return;
82fe8c2806Swdenk 	}
83fe8c2806Swdenk 
8464a08a9fSBenoît Thébaudeau 	n  = ddesc->blk_count;
85fe8c2806Swdenk 
8664a08a9fSBenoît Thébaudeau 	mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
87fe8c2806Swdenk 	/* round to 1 digit */
8864a08a9fSBenoît Thébaudeau 	mb.rem *= 10 * ddesc->blk_size;
89fe8c2806Swdenk 	mb.rem += 512 * 1024;
90fe8c2806Swdenk 	mb.rem /= 1024 * 1024;
91fe8c2806Swdenk 
92fe8c2806Swdenk 	gb = ldiv(10 * mb.quot + mb.rem, 10240);
93fe8c2806Swdenk 	gb.rem += 512;
94fe8c2806Swdenk 	gb.rem /= 1024;
95fe8c2806Swdenk 
96fe8c2806Swdenk 
97fe8c2806Swdenk 	printf ("Block Size=%d, Number of Blocks=%d, "
98fe8c2806Swdenk 		"Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
99fe8c2806Swdenk 		"DeviceType=0x%x, DeviceId=0x%x\n\n"
100fe8c2806Swdenk 		"   #:                 type name"
101fe8c2806Swdenk 		"                   length   base       (size)\n",
10264a08a9fSBenoît Thébaudeau 		ddesc->blk_size,
10364a08a9fSBenoît Thébaudeau 		ddesc->blk_count,
104fe8c2806Swdenk 		mb.quot, mb.rem, gb.quot, gb.rem,
10564a08a9fSBenoît Thébaudeau 		ddesc->dev_type, ddesc->dev_id
106fe8c2806Swdenk 		);
107fe8c2806Swdenk 
108fe8c2806Swdenk 	n = 1;	/* assuming at least one partition */
109fe8c2806Swdenk 	for (i=1; i<=n; ++i) {
110fe8c2806Swdenk 		ulong bytes;
111fe8c2806Swdenk 		char c;
112fe8c2806Swdenk 
113fe8c2806Swdenk 		printf ("%4ld: ", i);
1142a981dc2SSimon Glass 		if (blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) {
115fe8c2806Swdenk 			printf ("** Can't read Partition Map on %d:%ld **\n",
116bcce53d0SSimon Glass 				dev_desc->devnum, i);
117fe8c2806Swdenk 			return;
118fe8c2806Swdenk 		}
119fe8c2806Swdenk 
12064a08a9fSBenoît Thébaudeau 		if (mpart->signature != MAC_PARTITION_MAGIC) {
121bcce53d0SSimon Glass 			printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
122bcce53d0SSimon Glass 			       dev_desc->devnum, i, MAC_PARTITION_MAGIC,
123bcce53d0SSimon Glass 			       mpart->signature);
124fe8c2806Swdenk 			return;
125fe8c2806Swdenk 		}
126fe8c2806Swdenk 
127fe8c2806Swdenk 		/* update partition count */
12864a08a9fSBenoît Thébaudeau 		n = mpart->map_count;
129fe8c2806Swdenk 
130fe8c2806Swdenk 		c      = 'k';
13164a08a9fSBenoît Thébaudeau 		bytes  = mpart->block_count;
13264a08a9fSBenoît Thébaudeau 		bytes /= (1024 / ddesc->blk_size);  /* kB; assumes blk_size == 512 */
133fe8c2806Swdenk 		if (bytes >= 1024) {
134fe8c2806Swdenk 			bytes >>= 10;
135fe8c2806Swdenk 			c = 'M';
136fe8c2806Swdenk 		}
137fe8c2806Swdenk 		if (bytes >= 1024) {
138fe8c2806Swdenk 			bytes >>= 10;
139fe8c2806Swdenk 			c = 'G';
140fe8c2806Swdenk 		}
141fe8c2806Swdenk 
142fe8c2806Swdenk 		printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
14364a08a9fSBenoît Thébaudeau 			mpart->type,
14464a08a9fSBenoît Thébaudeau 			mpart->name,
14564a08a9fSBenoît Thébaudeau 			mpart->block_count,
14664a08a9fSBenoît Thébaudeau 			mpart->start_block,
147fe8c2806Swdenk 			bytes, c
148fe8c2806Swdenk 			);
149fe8c2806Swdenk 	}
150fe8c2806Swdenk 
151fe8c2806Swdenk 	return;
152fe8c2806Swdenk }
153fe8c2806Swdenk 
154fe8c2806Swdenk 
155fe8c2806Swdenk /*
156fe8c2806Swdenk  * Read Device Descriptor Block
157fe8c2806Swdenk  */
1584101f687SSimon Glass static int part_mac_read_ddb(struct blk_desc *dev_desc,
1594101f687SSimon Glass 			     mac_driver_desc_t *ddb_p)
160fe8c2806Swdenk {
1612a981dc2SSimon Glass 	if (blk_dread(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
162337e3b89SBin Meng 		debug("** Can't read Driver Descriptor Block **\n");
163fe8c2806Swdenk 		return (-1);
164fe8c2806Swdenk 	}
165fe8c2806Swdenk 
166fe8c2806Swdenk 	if (ddb_p->signature != MAC_DRIVER_MAGIC) {
167fe8c2806Swdenk 		return (-1);
168fe8c2806Swdenk 	}
169fe8c2806Swdenk 	return (0);
170fe8c2806Swdenk }
171fe8c2806Swdenk 
172fe8c2806Swdenk /*
173fe8c2806Swdenk  * Read Partition Descriptor Block
174fe8c2806Swdenk  */
1754101f687SSimon Glass static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
1764101f687SSimon Glass 			     mac_partition_t *pdb_p)
177fe8c2806Swdenk {
178fe8c2806Swdenk 	int n = 1;
179fe8c2806Swdenk 
180fe8c2806Swdenk 	for (;;) {
181fe8c2806Swdenk 		/*
182fe8c2806Swdenk 		 * We must always read the descritpor block for
183fe8c2806Swdenk 		 * partition 1 first since this is the only way to
184fe8c2806Swdenk 		 * know how many partitions we have.
185fe8c2806Swdenk 		 */
1862a981dc2SSimon Glass 		if (blk_dread(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
187fe8c2806Swdenk 			printf ("** Can't read Partition Map on %d:%d **\n",
188bcce53d0SSimon Glass 				dev_desc->devnum, n);
189fe8c2806Swdenk 			return (-1);
190fe8c2806Swdenk 		}
191fe8c2806Swdenk 
192fe8c2806Swdenk 		if (pdb_p->signature != MAC_PARTITION_MAGIC) {
193bcce53d0SSimon Glass 			printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n",
194bcce53d0SSimon Glass 			       dev_desc->devnum, n, MAC_PARTITION_MAGIC,
195bcce53d0SSimon Glass 			       pdb_p->signature);
196fe8c2806Swdenk 			return (-1);
197fe8c2806Swdenk 		}
198fe8c2806Swdenk 
199fe8c2806Swdenk 		if (n == part)
200fe8c2806Swdenk 			return (0);
201fe8c2806Swdenk 
202fe8c2806Swdenk 		if ((part < 1) || (part > pdb_p->map_count)) {
203fe8c2806Swdenk 			printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
204bcce53d0SSimon Glass 				dev_desc->devnum, part,
205bcce53d0SSimon Glass 				dev_desc->devnum,
206bcce53d0SSimon Glass 				dev_desc->devnum, pdb_p->map_count);
207fe8c2806Swdenk 			return (-1);
208fe8c2806Swdenk 		}
209fe8c2806Swdenk 
210fe8c2806Swdenk 		/* update partition count */
211fe8c2806Swdenk 		n = part;
212fe8c2806Swdenk 	}
213fe8c2806Swdenk 
214fe8c2806Swdenk 	/* NOTREACHED */
215fe8c2806Swdenk }
216fe8c2806Swdenk 
2173e8bd469SSimon Glass static int part_get_info_mac(struct blk_desc *dev_desc, int part,
2184101f687SSimon Glass 				  disk_partition_t *info)
219fe8c2806Swdenk {
22064a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
22164a08a9fSBenoît Thébaudeau 	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
222fe8c2806Swdenk 
22364a08a9fSBenoît Thébaudeau 	if (part_mac_read_ddb (dev_desc, ddesc)) {
224fe8c2806Swdenk 		return (-1);
225fe8c2806Swdenk 	}
226fe8c2806Swdenk 
22764a08a9fSBenoît Thébaudeau 	info->blksz = ddesc->blk_size;
228fe8c2806Swdenk 
22964a08a9fSBenoît Thébaudeau 	if (part_mac_read_pdb (dev_desc, part, mpart)) {
230fe8c2806Swdenk 		return (-1);
231fe8c2806Swdenk 	}
232fe8c2806Swdenk 
23364a08a9fSBenoît Thébaudeau 	info->start = mpart->start_block;
23464a08a9fSBenoît Thébaudeau 	info->size  = mpart->block_count;
23564a08a9fSBenoît Thébaudeau 	memcpy (info->type, mpart->type, sizeof(info->type));
23664a08a9fSBenoît Thébaudeau 	memcpy (info->name, mpart->name, sizeof(info->name));
237fe8c2806Swdenk 
238fe8c2806Swdenk 	return (0);
239fe8c2806Swdenk }
240fe8c2806Swdenk 
24196e5b03cSSimon Glass U_BOOT_PART_TYPE(mac) = {
24296e5b03cSSimon Glass 	.name		= "MAC",
24396e5b03cSSimon Glass 	.part_type	= PART_TYPE_MAC,
24487b8530fSPetr Kulhavy 	.max_entries	= MAC_ENTRY_NUMBERS,
2453e8bd469SSimon Glass 	.get_info	= part_get_info_mac,
246084bf4c2SSimon Glass 	.print		= part_print_mac,
247084bf4c2SSimon Glass 	.test		= part_test_mac,
24896e5b03cSSimon Glass };
249cde5c64dSJon Loeliger #endif
250