1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <common.h>
33#include <command.h>
34#include <ide.h>
35#include "part_mac.h"
36
37#if defined(CONFIG_CMD_IDE) || \
38 defined(CONFIG_CMD_MG_DISK) || \
39 defined(CONFIG_CMD_SCSI) || \
40 defined(CONFIG_CMD_SATA) || \
41 defined(CONFIG_CMD_USB) || \
42 defined(CONFIG_MMC) || \
43 defined(CONFIG_SYSTEMACE)
44
45
46#ifndef __ldiv_t_defined
47typedef struct {
48 long int quot;
49 long int rem;
50} ldiv_t;
51extern ldiv_t ldiv (long int __numer, long int __denom);
52# define __ldiv_t_defined 1
53#endif
54
55
56static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
57static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
58
59
60
61
62int test_part_mac (block_dev_desc_t *dev_desc)
63{
64 mac_driver_desc_t ddesc;
65 mac_partition_t mpart;
66 ulong i, n;
67
68 if (part_mac_read_ddb (dev_desc, &ddesc)) {
69
70 return (-1);
71 }
72
73 n = 1;
74 for (i=1; i<=n; ++i) {
75 if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)&mpart) != 1) ||
76 (mpart.signature != MAC_PARTITION_MAGIC) ) {
77 return (-1);
78 }
79
80 n = mpart.map_count;
81 }
82 return (0);
83}
84
85
86void print_part_mac (block_dev_desc_t *dev_desc)
87{
88 ulong i, n;
89 mac_driver_desc_t ddesc;
90 mac_partition_t mpart;
91 ldiv_t mb, gb;
92
93 if (part_mac_read_ddb (dev_desc, &ddesc)) {
94
95 return;
96 }
97
98 n = ddesc.blk_count;
99
100 mb = ldiv(n, ((1024 * 1024) / ddesc.blk_size));
101
102 mb.rem *= 10 * ddesc.blk_size;
103 mb.rem += 512 * 1024;
104 mb.rem /= 1024 * 1024;
105
106 gb = ldiv(10 * mb.quot + mb.rem, 10240);
107 gb.rem += 512;
108 gb.rem /= 1024;
109
110
111 printf ("Block Size=%d, Number of Blocks=%d, "
112 "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
113 "DeviceType=0x%x, DeviceId=0x%x\n\n"
114 " #: type name"
115 " length base (size)\n",
116 ddesc.blk_size,
117 ddesc.blk_count,
118 mb.quot, mb.rem, gb.quot, gb.rem,
119 ddesc.dev_type, ddesc.dev_id
120 );
121
122 n = 1;
123 for (i=1; i<=n; ++i) {
124 ulong bytes;
125 char c;
126
127 printf ("%4ld: ", i);
128 if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)&mpart) != 1) {
129 printf ("** Can't read Partition Map on %d:%ld **\n",
130 dev_desc->dev, i);
131 return;
132 }
133
134 if (mpart.signature != MAC_PARTITION_MAGIC) {
135 printf ("** Bad Signature on %d:%ld - "
136 "expected 0x%04x, got 0x%04x\n",
137 dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart.signature);
138 return;
139 }
140
141
142 n = mpart.map_count;
143
144 c = 'k';
145 bytes = mpart.block_count;
146 bytes /= (1024 / ddesc.blk_size);
147 if (bytes >= 1024) {
148 bytes >>= 10;
149 c = 'M';
150 }
151 if (bytes >= 1024) {
152 bytes >>= 10;
153 c = 'G';
154 }
155
156 printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
157 mpart.type,
158 mpart.name,
159 mpart.block_count,
160 mpart.start_block,
161 bytes, c
162 );
163 }
164
165 return;
166}
167
168
169
170
171
172static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
173{
174 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
175 printf ("** Can't read Driver Desriptor Block **\n");
176 return (-1);
177 }
178
179 if (ddb_p->signature != MAC_DRIVER_MAGIC) {
180#if 0
181 printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
182 MAC_DRIVER_MAGIC, ddb_p->signature);
183#endif
184 return (-1);
185 }
186 return (0);
187}
188
189
190
191
192static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
193{
194 int n = 1;
195
196 for (;;) {
197
198
199
200
201
202 if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
203 printf ("** Can't read Partition Map on %d:%d **\n",
204 dev_desc->dev, n);
205 return (-1);
206 }
207
208 if (pdb_p->signature != MAC_PARTITION_MAGIC) {
209 printf ("** Bad Signature on %d:%d: "
210 "expected 0x%04x, got 0x%04x\n",
211 dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
212 return (-1);
213 }
214
215 if (n == part)
216 return (0);
217
218 if ((part < 1) || (part > pdb_p->map_count)) {
219 printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
220 dev_desc->dev, part,
221 dev_desc->dev,
222 dev_desc->dev, pdb_p->map_count);
223 return (-1);
224 }
225
226
227 n = part;
228 }
229
230
231}
232
233int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
234{
235 mac_driver_desc_t ddesc;
236 mac_partition_t mpart;
237
238 if (part_mac_read_ddb (dev_desc, &ddesc)) {
239 return (-1);
240 }
241
242 info->blksz = ddesc.blk_size;
243
244 if (part_mac_read_pdb (dev_desc, part, &mpart)) {
245 return (-1);
246 }
247
248 info->start = mpart.start_block;
249 info->size = mpart.block_count;
250 memcpy (info->type, mpart.type, sizeof(info->type));
251 memcpy (info->name, mpart.name, sizeof(info->name));
252
253 return (0);
254}
255
256#endif
257