1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2fe8c2806Swdenk /*
3fe8c2806Swdenk * (C) Copyright 2000
4fe8c2806Swdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5fe8c2806Swdenk */
6fe8c2806Swdenk
7fe8c2806Swdenk /*
8fe8c2806Swdenk * Support for harddisk partitions.
9fe8c2806Swdenk *
10fe8c2806Swdenk * To be compatible with LinuxPPC and Apple we use the standard Apple
11fe8c2806Swdenk * SCSI disk partitioning scheme. For more information see:
12fe8c2806Swdenk * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
13fe8c2806Swdenk */
14fe8c2806Swdenk
15fe8c2806Swdenk #include <common.h>
16fe8c2806Swdenk #include <command.h>
17cf92e05cSSimon Glass #include <memalign.h>
18fe8c2806Swdenk #include <ide.h>
19fe8c2806Swdenk #include "part_mac.h"
20fe8c2806Swdenk
211811a928SAdam Ford #ifdef CONFIG_HAVE_BLOCK_DEVICE
22fe8c2806Swdenk
23fe8c2806Swdenk /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
24fe8c2806Swdenk #ifndef __ldiv_t_defined
25fe8c2806Swdenk typedef struct {
26fe8c2806Swdenk long int quot; /* Quotient */
27fe8c2806Swdenk long int rem; /* Remainder */
28fe8c2806Swdenk } ldiv_t;
29fe8c2806Swdenk extern ldiv_t ldiv (long int __numer, long int __denom);
30fe8c2806Swdenk # define __ldiv_t_defined 1
31fe8c2806Swdenk #endif
32fe8c2806Swdenk
33fe8c2806Swdenk
344101f687SSimon Glass static int part_mac_read_ddb(struct blk_desc *dev_desc,
354101f687SSimon Glass mac_driver_desc_t *ddb_p);
364101f687SSimon Glass static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
374101f687SSimon Glass mac_partition_t *pdb_p);
38fe8c2806Swdenk
39fe8c2806Swdenk /*
40fe8c2806Swdenk * Test for a valid MAC partition
41fe8c2806Swdenk */
part_test_mac(struct blk_desc * dev_desc)42084bf4c2SSimon Glass static int part_test_mac(struct blk_desc *dev_desc)
43fe8c2806Swdenk {
4464a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
4564a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
46fe8c2806Swdenk ulong i, n;
47fe8c2806Swdenk
4864a08a9fSBenoît Thébaudeau if (part_mac_read_ddb (dev_desc, ddesc)) {
495eae466eSHeinrich Schuchardt /*
505eae466eSHeinrich Schuchardt * error reading Driver Descriptor Block,
515eae466eSHeinrich Schuchardt * or no valid Signature
525eae466eSHeinrich Schuchardt */
53fe8c2806Swdenk return (-1);
54fe8c2806Swdenk }
55fe8c2806Swdenk
56fe8c2806Swdenk n = 1; /* assuming at least one partition */
57fe8c2806Swdenk for (i=1; i<=n; ++i) {
582a981dc2SSimon Glass if ((blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) ||
5964a08a9fSBenoît Thébaudeau (mpart->signature != MAC_PARTITION_MAGIC) ) {
60fe8c2806Swdenk return (-1);
61fe8c2806Swdenk }
62fe8c2806Swdenk /* update partition count */
6364a08a9fSBenoît Thébaudeau n = mpart->map_count;
64fe8c2806Swdenk }
65fe8c2806Swdenk return (0);
66fe8c2806Swdenk }
67fe8c2806Swdenk
part_print_mac(struct blk_desc * dev_desc)68084bf4c2SSimon Glass static void part_print_mac(struct blk_desc *dev_desc)
69fe8c2806Swdenk {
70fe8c2806Swdenk ulong i, n;
7164a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
7264a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
73fe8c2806Swdenk ldiv_t mb, gb;
74fe8c2806Swdenk
7564a08a9fSBenoît Thébaudeau if (part_mac_read_ddb (dev_desc, ddesc)) {
765eae466eSHeinrich Schuchardt /*
775eae466eSHeinrich Schuchardt * error reading Driver Descriptor Block,
785eae466eSHeinrich Schuchardt * or no valid Signature
795eae466eSHeinrich Schuchardt */
80fe8c2806Swdenk return;
81fe8c2806Swdenk }
82fe8c2806Swdenk
8364a08a9fSBenoît Thébaudeau n = ddesc->blk_count;
84fe8c2806Swdenk
8564a08a9fSBenoît Thébaudeau mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
86fe8c2806Swdenk /* round to 1 digit */
8764a08a9fSBenoît Thébaudeau mb.rem *= 10 * ddesc->blk_size;
88fe8c2806Swdenk mb.rem += 512 * 1024;
89fe8c2806Swdenk mb.rem /= 1024 * 1024;
90fe8c2806Swdenk
91fe8c2806Swdenk gb = ldiv(10 * mb.quot + mb.rem, 10240);
92fe8c2806Swdenk gb.rem += 512;
93fe8c2806Swdenk gb.rem /= 1024;
94fe8c2806Swdenk
95fe8c2806Swdenk
96fe8c2806Swdenk printf ("Block Size=%d, Number of Blocks=%d, "
97fe8c2806Swdenk "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
98fe8c2806Swdenk "DeviceType=0x%x, DeviceId=0x%x\n\n"
99fe8c2806Swdenk " #: type name"
100fe8c2806Swdenk " length base (size)\n",
10164a08a9fSBenoît Thébaudeau ddesc->blk_size,
10264a08a9fSBenoît Thébaudeau ddesc->blk_count,
103fe8c2806Swdenk mb.quot, mb.rem, gb.quot, gb.rem,
10464a08a9fSBenoît Thébaudeau ddesc->dev_type, ddesc->dev_id
105fe8c2806Swdenk );
106fe8c2806Swdenk
107fe8c2806Swdenk n = 1; /* assuming at least one partition */
108fe8c2806Swdenk for (i=1; i<=n; ++i) {
109fe8c2806Swdenk ulong bytes;
110fe8c2806Swdenk char c;
111fe8c2806Swdenk
112fe8c2806Swdenk printf ("%4ld: ", i);
1132a981dc2SSimon Glass if (blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) {
114fe8c2806Swdenk printf ("** Can't read Partition Map on %d:%ld **\n",
115bcce53d0SSimon Glass dev_desc->devnum, i);
116fe8c2806Swdenk return;
117fe8c2806Swdenk }
118fe8c2806Swdenk
11964a08a9fSBenoît Thébaudeau if (mpart->signature != MAC_PARTITION_MAGIC) {
120bcce53d0SSimon Glass printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
121bcce53d0SSimon Glass dev_desc->devnum, i, MAC_PARTITION_MAGIC,
122bcce53d0SSimon Glass mpart->signature);
123fe8c2806Swdenk return;
124fe8c2806Swdenk }
125fe8c2806Swdenk
126fe8c2806Swdenk /* update partition count */
12764a08a9fSBenoît Thébaudeau n = mpart->map_count;
128fe8c2806Swdenk
129fe8c2806Swdenk c = 'k';
13064a08a9fSBenoît Thébaudeau bytes = mpart->block_count;
13164a08a9fSBenoît Thébaudeau bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
132fe8c2806Swdenk if (bytes >= 1024) {
133fe8c2806Swdenk bytes >>= 10;
134fe8c2806Swdenk c = 'M';
135fe8c2806Swdenk }
136fe8c2806Swdenk if (bytes >= 1024) {
137fe8c2806Swdenk bytes >>= 10;
138fe8c2806Swdenk c = 'G';
139fe8c2806Swdenk }
140fe8c2806Swdenk
141fe8c2806Swdenk printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
14264a08a9fSBenoît Thébaudeau mpart->type,
14364a08a9fSBenoît Thébaudeau mpart->name,
14464a08a9fSBenoît Thébaudeau mpart->block_count,
14564a08a9fSBenoît Thébaudeau mpart->start_block,
146fe8c2806Swdenk bytes, c
147fe8c2806Swdenk );
148fe8c2806Swdenk }
149fe8c2806Swdenk
150fe8c2806Swdenk return;
151fe8c2806Swdenk }
152fe8c2806Swdenk
153fe8c2806Swdenk
154fe8c2806Swdenk /*
155fe8c2806Swdenk * Read Device Descriptor Block
156fe8c2806Swdenk */
part_mac_read_ddb(struct blk_desc * dev_desc,mac_driver_desc_t * ddb_p)1574101f687SSimon Glass static int part_mac_read_ddb(struct blk_desc *dev_desc,
1584101f687SSimon Glass mac_driver_desc_t *ddb_p)
159fe8c2806Swdenk {
1602a981dc2SSimon Glass if (blk_dread(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
161337e3b89SBin Meng debug("** Can't read Driver Descriptor Block **\n");
162fe8c2806Swdenk return (-1);
163fe8c2806Swdenk }
164fe8c2806Swdenk
165fe8c2806Swdenk if (ddb_p->signature != MAC_DRIVER_MAGIC) {
166fe8c2806Swdenk return (-1);
167fe8c2806Swdenk }
168fe8c2806Swdenk return (0);
169fe8c2806Swdenk }
170fe8c2806Swdenk
171fe8c2806Swdenk /*
172fe8c2806Swdenk * Read Partition Descriptor Block
173fe8c2806Swdenk */
part_mac_read_pdb(struct blk_desc * dev_desc,int part,mac_partition_t * pdb_p)1744101f687SSimon Glass static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
1754101f687SSimon Glass mac_partition_t *pdb_p)
176fe8c2806Swdenk {
177fe8c2806Swdenk int n = 1;
178fe8c2806Swdenk
179fe8c2806Swdenk for (;;) {
180fe8c2806Swdenk /*
181fe8c2806Swdenk * We must always read the descritpor block for
182fe8c2806Swdenk * partition 1 first since this is the only way to
183fe8c2806Swdenk * know how many partitions we have.
184fe8c2806Swdenk */
1852a981dc2SSimon Glass if (blk_dread(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
186fe8c2806Swdenk printf ("** Can't read Partition Map on %d:%d **\n",
187bcce53d0SSimon Glass dev_desc->devnum, n);
188fe8c2806Swdenk return (-1);
189fe8c2806Swdenk }
190fe8c2806Swdenk
191fe8c2806Swdenk if (pdb_p->signature != MAC_PARTITION_MAGIC) {
192bcce53d0SSimon Glass printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n",
193bcce53d0SSimon Glass dev_desc->devnum, n, MAC_PARTITION_MAGIC,
194bcce53d0SSimon Glass pdb_p->signature);
195fe8c2806Swdenk return (-1);
196fe8c2806Swdenk }
197fe8c2806Swdenk
198fe8c2806Swdenk if (n == part)
199fe8c2806Swdenk return (0);
200fe8c2806Swdenk
201fe8c2806Swdenk if ((part < 1) || (part > pdb_p->map_count)) {
202fe8c2806Swdenk printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
203bcce53d0SSimon Glass dev_desc->devnum, part,
204bcce53d0SSimon Glass dev_desc->devnum,
205bcce53d0SSimon Glass dev_desc->devnum, pdb_p->map_count);
206fe8c2806Swdenk return (-1);
207fe8c2806Swdenk }
208fe8c2806Swdenk
209fe8c2806Swdenk /* update partition count */
210fe8c2806Swdenk n = part;
211fe8c2806Swdenk }
212fe8c2806Swdenk
213fe8c2806Swdenk /* NOTREACHED */
214fe8c2806Swdenk }
215fe8c2806Swdenk
part_get_info_mac(struct blk_desc * dev_desc,int part,disk_partition_t * info)2163e8bd469SSimon Glass static int part_get_info_mac(struct blk_desc *dev_desc, int part,
2174101f687SSimon Glass disk_partition_t *info)
218fe8c2806Swdenk {
21964a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
22064a08a9fSBenoît Thébaudeau ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
221fe8c2806Swdenk
22264a08a9fSBenoît Thébaudeau if (part_mac_read_ddb (dev_desc, ddesc)) {
223fe8c2806Swdenk return (-1);
224fe8c2806Swdenk }
225fe8c2806Swdenk
22664a08a9fSBenoît Thébaudeau info->blksz = ddesc->blk_size;
227fe8c2806Swdenk
22864a08a9fSBenoît Thébaudeau if (part_mac_read_pdb (dev_desc, part, mpart)) {
229fe8c2806Swdenk return (-1);
230fe8c2806Swdenk }
231fe8c2806Swdenk
23264a08a9fSBenoît Thébaudeau info->start = mpart->start_block;
23364a08a9fSBenoît Thébaudeau info->size = mpart->block_count;
23464a08a9fSBenoît Thébaudeau memcpy (info->type, mpart->type, sizeof(info->type));
23564a08a9fSBenoît Thébaudeau memcpy (info->name, mpart->name, sizeof(info->name));
236fe8c2806Swdenk
237fe8c2806Swdenk return (0);
238fe8c2806Swdenk }
239fe8c2806Swdenk
24096e5b03cSSimon Glass U_BOOT_PART_TYPE(mac) = {
24196e5b03cSSimon Glass .name = "MAC",
24296e5b03cSSimon Glass .part_type = PART_TYPE_MAC,
24387b8530fSPetr Kulhavy .max_entries = MAC_ENTRY_NUMBERS,
2443e8bd469SSimon Glass .get_info = part_get_info_mac,
245084bf4c2SSimon Glass .print = part_print_mac,
246084bf4c2SSimon Glass .test = part_test_mac,
24796e5b03cSSimon Glass };
248cde5c64dSJon Loeliger #endif
249