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