61927234fb8e231fd2a44e3e54906fe579e8e1a0
[oweals/u-boot.git] / disk / part_mac.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
9  * Support for harddisk partitions.
10  *
11  * To be compatible with LinuxPPC and Apple we use the standard Apple
12  * SCSI disk partitioning scheme. For more information see:
13  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
14  */
15
16 #include <common.h>
17 #include <command.h>
18 #include <memalign.h>
19 #include <ide.h>
20 #include "part_mac.h"
21
22 #ifdef HAVE_BLOCK_DEVICE
23
24 /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
25 #ifndef __ldiv_t_defined
26 typedef struct {
27         long int quot;          /* Quotient     */
28         long int rem;           /* Remainder    */
29 } ldiv_t;
30 extern ldiv_t ldiv (long int __numer, long int __denom);
31 # define __ldiv_t_defined       1
32 #endif
33
34
35 static int part_mac_read_ddb(struct blk_desc *dev_desc,
36                              mac_driver_desc_t *ddb_p);
37 static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
38                              mac_partition_t *pdb_p);
39
40 /*
41  * Test for a valid MAC partition
42  */
43 static int test_part_mac(struct blk_desc *dev_desc)
44 {
45         ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
46         ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
47         ulong i, n;
48
49         if (part_mac_read_ddb (dev_desc, ddesc)) {
50                 /* error reading Driver Desriptor Block, or no valid Signature */
51                 return (-1);
52         }
53
54         n = 1;  /* assuming at least one partition */
55         for (i=1; i<=n; ++i) {
56                 if ((dev_desc->block_read(dev_desc, i, 1,
57                                           (ulong *)mpart) != 1) ||
58                     (mpart->signature != MAC_PARTITION_MAGIC) ) {
59                         return (-1);
60                 }
61                 /* update partition count */
62                 n = mpart->map_count;
63         }
64         return (0);
65 }
66
67 static void print_part_mac(struct blk_desc *dev_desc)
68 {
69         ulong i, n;
70         ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
71         ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
72         ldiv_t mb, gb;
73
74         if (part_mac_read_ddb (dev_desc, ddesc)) {
75                 /* error reading Driver Desriptor Block, or no valid Signature */
76                 return;
77         }
78
79         n  = ddesc->blk_count;
80
81         mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
82         /* round to 1 digit */
83         mb.rem *= 10 * ddesc->blk_size;
84         mb.rem += 512 * 1024;
85         mb.rem /= 1024 * 1024;
86
87         gb = ldiv(10 * mb.quot + mb.rem, 10240);
88         gb.rem += 512;
89         gb.rem /= 1024;
90
91
92         printf ("Block Size=%d, Number of Blocks=%d, "
93                 "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
94                 "DeviceType=0x%x, DeviceId=0x%x\n\n"
95                 "   #:                 type name"
96                 "                   length   base       (size)\n",
97                 ddesc->blk_size,
98                 ddesc->blk_count,
99                 mb.quot, mb.rem, gb.quot, gb.rem,
100                 ddesc->dev_type, ddesc->dev_id
101                 );
102
103         n = 1;  /* assuming at least one partition */
104         for (i=1; i<=n; ++i) {
105                 ulong bytes;
106                 char c;
107
108                 printf ("%4ld: ", i);
109                 if (dev_desc->block_read(dev_desc, i, 1, (ulong *)mpart) != 1) {
110                         printf ("** Can't read Partition Map on %d:%ld **\n",
111                                 dev_desc->dev, i);
112                         return;
113                 }
114
115                 if (mpart->signature != MAC_PARTITION_MAGIC) {
116                         printf ("** Bad Signature on %d:%ld - "
117                                 "expected 0x%04x, got 0x%04x\n",
118                                 dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart->signature);
119                         return;
120                 }
121
122                 /* update partition count */
123                 n = mpart->map_count;
124
125                 c      = 'k';
126                 bytes  = mpart->block_count;
127                 bytes /= (1024 / ddesc->blk_size);  /* kB; assumes blk_size == 512 */
128                 if (bytes >= 1024) {
129                         bytes >>= 10;
130                         c = 'M';
131                 }
132                 if (bytes >= 1024) {
133                         bytes >>= 10;
134                         c = 'G';
135                 }
136
137                 printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
138                         mpart->type,
139                         mpart->name,
140                         mpart->block_count,
141                         mpart->start_block,
142                         bytes, c
143                         );
144         }
145
146         return;
147 }
148
149
150 /*
151  * Read Device Descriptor Block
152  */
153 static int part_mac_read_ddb(struct blk_desc *dev_desc,
154                              mac_driver_desc_t *ddb_p)
155 {
156         if (dev_desc->block_read(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
157                 printf ("** Can't read Driver Desriptor Block **\n");
158                 return (-1);
159         }
160
161         if (ddb_p->signature != MAC_DRIVER_MAGIC) {
162 #if 0
163                 printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
164                         MAC_DRIVER_MAGIC, ddb_p->signature);
165 #endif
166                 return (-1);
167         }
168         return (0);
169 }
170
171 /*
172  * Read Partition Descriptor Block
173  */
174 static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
175                              mac_partition_t *pdb_p)
176 {
177         int n = 1;
178
179         for (;;) {
180                 /*
181                  * We must always read the descritpor block for
182                  * partition 1 first since this is the only way to
183                  * know how many partitions we have.
184                  */
185                 if (dev_desc->block_read(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
186                         printf ("** Can't read Partition Map on %d:%d **\n",
187                                 dev_desc->dev, n);
188                         return (-1);
189                 }
190
191                 if (pdb_p->signature != MAC_PARTITION_MAGIC) {
192                         printf ("** Bad Signature on %d:%d: "
193                                 "expected 0x%04x, got 0x%04x\n",
194                                 dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
195                         return (-1);
196                 }
197
198                 if (n == part)
199                         return (0);
200
201                 if ((part < 1) || (part > pdb_p->map_count)) {
202                         printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
203                                 dev_desc->dev, part,
204                                 dev_desc->dev,
205                                 dev_desc->dev, pdb_p->map_count);
206                         return (-1);
207                 }
208
209                 /* update partition count */
210                 n = part;
211         }
212
213         /* NOTREACHED */
214 }
215
216 static int part_get_info_mac(struct blk_desc *dev_desc, int part,
217                                   disk_partition_t *info)
218 {
219         ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
220         ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
221
222         if (part_mac_read_ddb (dev_desc, ddesc)) {
223                 return (-1);
224         }
225
226         info->blksz = ddesc->blk_size;
227
228         if (part_mac_read_pdb (dev_desc, part, mpart)) {
229                 return (-1);
230         }
231
232         info->start = mpart->start_block;
233         info->size  = mpart->block_count;
234         memcpy (info->type, mpart->type, sizeof(info->type));
235         memcpy (info->name, mpart->name, sizeof(info->name));
236
237         return (0);
238 }
239
240 U_BOOT_PART_TYPE(mac) = {
241         .name           = "MAC",
242         .part_type      = PART_TYPE_MAC,
243         .get_info       = part_get_info_mac,
244         .print          = print_part_mac,
245         .test           = test_part_mac,
246 };
247 #endif