blob: 74dc12fe58ac13f81027488993d507d86101969c [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
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>
wdenkfe8c2802002-11-03 00:38:21 +000035#include "part_mac.h"
36
Stephen Warren2c1af9d2013-02-28 15:03:46 +000037#ifdef HAVE_BLOCK_DEVICE
wdenkfe8c2802002-11-03 00:38:21 +000038
39/* stdlib.h causes some compatibility problems; should fixe these! -- wd */
40#ifndef __ldiv_t_defined
41typedef struct {
42 long int quot; /* Quotient */
43 long int rem; /* Remainder */
44} ldiv_t;
45extern ldiv_t ldiv (long int __numer, long int __denom);
46# define __ldiv_t_defined 1
47#endif
48
49
50static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
51static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
52
53/*
54 * Test for a valid MAC partition
55 */
56int test_part_mac (block_dev_desc_t *dev_desc)
57{
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020058 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
59 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +000060 ulong i, n;
61
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020062 if (part_mac_read_ddb (dev_desc, ddesc)) {
wdenkfe8c2802002-11-03 00:38:21 +000063 /* error reading Driver Desriptor Block, or no valid Signature */
64 return (-1);
65 }
66
67 n = 1; /* assuming at least one partition */
68 for (i=1; i<=n; ++i) {
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020069 if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)mpart) != 1) ||
70 (mpart->signature != MAC_PARTITION_MAGIC) ) {
wdenkfe8c2802002-11-03 00:38:21 +000071 return (-1);
72 }
73 /* update partition count */
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020074 n = mpart->map_count;
wdenkfe8c2802002-11-03 00:38:21 +000075 }
76 return (0);
77}
78
79
80void print_part_mac (block_dev_desc_t *dev_desc)
81{
82 ulong i, n;
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020083 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
84 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +000085 ldiv_t mb, gb;
86
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020087 if (part_mac_read_ddb (dev_desc, ddesc)) {
wdenkfe8c2802002-11-03 00:38:21 +000088 /* error reading Driver Desriptor Block, or no valid Signature */
89 return;
90 }
91
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020092 n = ddesc->blk_count;
wdenkfe8c2802002-11-03 00:38:21 +000093
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020094 mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
wdenkfe8c2802002-11-03 00:38:21 +000095 /* round to 1 digit */
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +020096 mb.rem *= 10 * ddesc->blk_size;
wdenkfe8c2802002-11-03 00:38:21 +000097 mb.rem += 512 * 1024;
98 mb.rem /= 1024 * 1024;
99
100 gb = ldiv(10 * mb.quot + mb.rem, 10240);
101 gb.rem += 512;
102 gb.rem /= 1024;
103
104
105 printf ("Block Size=%d, Number of Blocks=%d, "
106 "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
107 "DeviceType=0x%x, DeviceId=0x%x\n\n"
108 " #: type name"
109 " length base (size)\n",
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200110 ddesc->blk_size,
111 ddesc->blk_count,
wdenkfe8c2802002-11-03 00:38:21 +0000112 mb.quot, mb.rem, gb.quot, gb.rem,
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200113 ddesc->dev_type, ddesc->dev_id
wdenkfe8c2802002-11-03 00:38:21 +0000114 );
115
116 n = 1; /* assuming at least one partition */
117 for (i=1; i<=n; ++i) {
118 ulong bytes;
119 char c;
120
121 printf ("%4ld: ", i);
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200122 if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)mpart) != 1) {
wdenkfe8c2802002-11-03 00:38:21 +0000123 printf ("** Can't read Partition Map on %d:%ld **\n",
124 dev_desc->dev, i);
125 return;
126 }
127
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200128 if (mpart->signature != MAC_PARTITION_MAGIC) {
wdenkfe8c2802002-11-03 00:38:21 +0000129 printf ("** Bad Signature on %d:%ld - "
130 "expected 0x%04x, got 0x%04x\n",
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200131 dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart->signature);
wdenkfe8c2802002-11-03 00:38:21 +0000132 return;
133 }
134
135 /* update partition count */
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200136 n = mpart->map_count;
wdenkfe8c2802002-11-03 00:38:21 +0000137
138 c = 'k';
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200139 bytes = mpart->block_count;
140 bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
wdenkfe8c2802002-11-03 00:38:21 +0000141 if (bytes >= 1024) {
142 bytes >>= 10;
143 c = 'M';
144 }
145 if (bytes >= 1024) {
146 bytes >>= 10;
147 c = 'G';
148 }
149
150 printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200151 mpart->type,
152 mpart->name,
153 mpart->block_count,
154 mpart->start_block,
wdenkfe8c2802002-11-03 00:38:21 +0000155 bytes, c
156 );
157 }
158
159 return;
160}
161
162
163/*
164 * Read Device Descriptor Block
165 */
166static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
167{
168 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
169 printf ("** Can't read Driver Desriptor Block **\n");
170 return (-1);
171 }
172
173 if (ddb_p->signature != MAC_DRIVER_MAGIC) {
174#if 0
175 printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
176 MAC_DRIVER_MAGIC, ddb_p->signature);
177#endif
178 return (-1);
179 }
180 return (0);
181}
182
183/*
184 * Read Partition Descriptor Block
185 */
186static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
187{
188 int n = 1;
189
190 for (;;) {
191 /*
wdenk8bde7f72003-06-27 21:31:46 +0000192 * We must always read the descritpor block for
193 * partition 1 first since this is the only way to
194 * know how many partitions we have.
wdenkfe8c2802002-11-03 00:38:21 +0000195 */
196 if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
197 printf ("** Can't read Partition Map on %d:%d **\n",
198 dev_desc->dev, n);
199 return (-1);
200 }
201
202 if (pdb_p->signature != MAC_PARTITION_MAGIC) {
203 printf ("** Bad Signature on %d:%d: "
204 "expected 0x%04x, got 0x%04x\n",
205 dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
206 return (-1);
207 }
208
209 if (n == part)
210 return (0);
211
212 if ((part < 1) || (part > pdb_p->map_count)) {
213 printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
214 dev_desc->dev, part,
215 dev_desc->dev,
216 dev_desc->dev, pdb_p->map_count);
217 return (-1);
218 }
219
220 /* update partition count */
221 n = part;
222 }
223
224 /* NOTREACHED */
225}
226
227int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
228{
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200229 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
230 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +0000231
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200232 if (part_mac_read_ddb (dev_desc, ddesc)) {
wdenkfe8c2802002-11-03 00:38:21 +0000233 return (-1);
234 }
235
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200236 info->blksz = ddesc->blk_size;
wdenkfe8c2802002-11-03 00:38:21 +0000237
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200238 if (part_mac_read_pdb (dev_desc, part, mpart)) {
wdenkfe8c2802002-11-03 00:38:21 +0000239 return (-1);
240 }
241
Benoît Thébaudeau64a08a92012-07-13 21:31:03 +0200242 info->start = mpart->start_block;
243 info->size = mpart->block_count;
244 memcpy (info->type, mpart->type, sizeof(info->type));
245 memcpy (info->name, mpart->name, sizeof(info->name));
wdenkfe8c2802002-11-03 00:38:21 +0000246
247 return (0);
248}
249
Jon Loeligercde5c642007-07-09 17:22:37 -0500250#endif