blob: dd3ce0be832b8bb22f5c7a16da92166a186e94a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkfe8c2802002-11-03 00:38:21 +00005 */
6
7/*
8 * Support for harddisk partitions.
9 *
10 * To be compatible with LinuxPPC and Apple we use the standard Apple
11 * SCSI disk partitioning scheme. For more information see:
12 * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
13 */
14
wdenkfe8c2802002-11-03 00:38:21 +000015#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060017#include <memalign.h>
wdenkfe8c2802002-11-03 00:38:21 +000018#include "part_mac.h"
Simon Glass655306c2020-05-10 11:39:58 -060019#include <part.h>
wdenkfe8c2802002-11-03 00:38:21 +000020
wdenkfe8c2802002-11-03 00:38:21 +000021/* stdlib.h causes some compatibility problems; should fixe these! -- wd */
22#ifndef __ldiv_t_defined
23typedef struct {
24 long int quot; /* Quotient */
25 long int rem; /* Remainder */
26} ldiv_t;
27extern ldiv_t ldiv (long int __numer, long int __denom);
28# define __ldiv_t_defined 1
29#endif
30
Simon Glasse7762912023-08-24 13:55:29 -060031static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p);
32static int part_mac_read_pdb(struct blk_desc *desc, int part,
Simon Glasse3394752016-02-29 15:25:34 -070033 mac_partition_t *pdb_p);
wdenkfe8c2802002-11-03 00:38:21 +000034
35/*
36 * Test for a valid MAC partition
37 */
Simon Glasse7762912023-08-24 13:55:29 -060038static int part_test_mac(struct blk_desc *desc)
wdenkfe8c2802002-11-03 00:38:21 +000039{
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020040 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
41 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +000042 ulong i, n;
43
Simon Glasse7762912023-08-24 13:55:29 -060044 if (part_mac_read_ddb(desc, ddesc)) {
Heinrich Schuchardtf6237c92017-08-29 18:36:59 +020045 /*
46 * error reading Driver Descriptor Block,
47 * or no valid Signature
48 */
wdenkfe8c2802002-11-03 00:38:21 +000049 return (-1);
50 }
51
52 n = 1; /* assuming at least one partition */
53 for (i=1; i<=n; ++i) {
Simon Glasse7762912023-08-24 13:55:29 -060054 if ((blk_dread(desc, i, 1, (ulong *)mpart) != 1) ||
55 mpart->signature != MAC_PARTITION_MAGIC) {
wdenkfe8c2802002-11-03 00:38:21 +000056 return (-1);
57 }
58 /* update partition count */
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020059 n = mpart->map_count;
wdenkfe8c2802002-11-03 00:38:21 +000060 }
61 return (0);
62}
63
Simon Glasse7762912023-08-24 13:55:29 -060064static void part_print_mac(struct blk_desc *desc)
wdenkfe8c2802002-11-03 00:38:21 +000065{
66 ulong i, n;
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020067 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
68 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +000069 ldiv_t mb, gb;
70
Simon Glasse7762912023-08-24 13:55:29 -060071 if (part_mac_read_ddb(desc, ddesc)) {
Heinrich Schuchardtf6237c92017-08-29 18:36:59 +020072 /*
73 * error reading Driver Descriptor Block,
74 * or no valid Signature
75 */
wdenkfe8c2802002-11-03 00:38:21 +000076 return;
77 }
78
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020079 n = ddesc->blk_count;
wdenkfe8c2802002-11-03 00:38:21 +000080
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020081 mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
wdenkfe8c2802002-11-03 00:38:21 +000082 /* round to 1 digit */
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020083 mb.rem *= 10 * ddesc->blk_size;
wdenkfe8c2802002-11-03 00:38:21 +000084 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
wdenkfe8c2802002-11-03 00:38:21 +000091 printf ("Block Size=%d, Number of Blocks=%d, "
92 "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
93 "DeviceType=0x%x, DeviceId=0x%x\n\n"
94 " #: type name"
95 " length base (size)\n",
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020096 ddesc->blk_size,
97 ddesc->blk_count,
wdenkfe8c2802002-11-03 00:38:21 +000098 mb.quot, mb.rem, gb.quot, gb.rem,
Benoît Thébaudeaubca93832012-07-13 21:31:03 +020099 ddesc->dev_type, ddesc->dev_id
wdenkfe8c2802002-11-03 00:38:21 +0000100 );
101
102 n = 1; /* assuming at least one partition */
103 for (i=1; i<=n; ++i) {
104 ulong bytes;
105 char c;
106
107 printf ("%4ld: ", i);
Simon Glasse7762912023-08-24 13:55:29 -0600108 if (blk_dread(desc, i, 1, (ulong *)mpart) != 1) {
wdenkfe8c2802002-11-03 00:38:21 +0000109 printf ("** Can't read Partition Map on %d:%ld **\n",
Simon Glasse7762912023-08-24 13:55:29 -0600110 desc->devnum, i);
wdenkfe8c2802002-11-03 00:38:21 +0000111 return;
112 }
113
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200114 if (mpart->signature != MAC_PARTITION_MAGIC) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700115 printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
Simon Glasse7762912023-08-24 13:55:29 -0600116 desc->devnum, i, MAC_PARTITION_MAGIC,
Simon Glass2f26fff2016-02-29 15:25:51 -0700117 mpart->signature);
wdenkfe8c2802002-11-03 00:38:21 +0000118 return;
119 }
120
121 /* update partition count */
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200122 n = mpart->map_count;
wdenkfe8c2802002-11-03 00:38:21 +0000123
124 c = 'k';
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200125 bytes = mpart->block_count;
126 bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
wdenkfe8c2802002-11-03 00:38:21 +0000127 if (bytes >= 1024) {
128 bytes >>= 10;
129 c = 'M';
130 }
131 if (bytes >= 1024) {
132 bytes >>= 10;
133 c = 'G';
134 }
135
136 printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200137 mpart->type,
138 mpart->name,
139 mpart->block_count,
140 mpart->start_block,
wdenkfe8c2802002-11-03 00:38:21 +0000141 bytes, c
142 );
143 }
144
145 return;
146}
147
wdenkfe8c2802002-11-03 00:38:21 +0000148/*
149 * Read Device Descriptor Block
150 */
Simon Glasse7762912023-08-24 13:55:29 -0600151static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p)
wdenkfe8c2802002-11-03 00:38:21 +0000152{
Simon Glasse7762912023-08-24 13:55:29 -0600153 if (blk_dread(desc, 0, 1, (ulong *)ddb_p) != 1) {
Bin Meng78ac30a2017-09-03 18:44:23 -0700154 debug("** Can't read Driver Descriptor Block **\n");
wdenkfe8c2802002-11-03 00:38:21 +0000155 return (-1);
156 }
157
158 if (ddb_p->signature != MAC_DRIVER_MAGIC) {
wdenkfe8c2802002-11-03 00:38:21 +0000159 return (-1);
160 }
161 return (0);
162}
163
164/*
165 * Read Partition Descriptor Block
166 */
Simon Glasse7762912023-08-24 13:55:29 -0600167static int part_mac_read_pdb(struct blk_desc *desc, int part,
Simon Glasse3394752016-02-29 15:25:34 -0700168 mac_partition_t *pdb_p)
wdenkfe8c2802002-11-03 00:38:21 +0000169{
170 int n = 1;
171
172 for (;;) {
173 /*
wdenk57b2d802003-06-27 21:31:46 +0000174 * We must always read the descritpor block for
175 * partition 1 first since this is the only way to
176 * know how many partitions we have.
wdenkfe8c2802002-11-03 00:38:21 +0000177 */
Simon Glasse7762912023-08-24 13:55:29 -0600178 if (blk_dread(desc, n, 1, (ulong *)pdb_p) != 1) {
179 printf("** Can't read Partition Map on %d:%d **\n",
180 desc->devnum, n);
wdenkfe8c2802002-11-03 00:38:21 +0000181 return (-1);
182 }
183
184 if (pdb_p->signature != MAC_PARTITION_MAGIC) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700185 printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n",
Simon Glasse7762912023-08-24 13:55:29 -0600186 desc->devnum, n, MAC_PARTITION_MAGIC,
Simon Glass2f26fff2016-02-29 15:25:51 -0700187 pdb_p->signature);
wdenkfe8c2802002-11-03 00:38:21 +0000188 return (-1);
189 }
190
191 if (n == part)
192 return (0);
193
194 if ((part < 1) || (part > pdb_p->map_count)) {
Simon Glasse7762912023-08-24 13:55:29 -0600195 printf("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
196 desc->devnum, part, desc->devnum, desc->devnum,
197 pdb_p->map_count);
wdenkfe8c2802002-11-03 00:38:21 +0000198 return (-1);
199 }
200
201 /* update partition count */
202 n = part;
203 }
204
205 /* NOTREACHED */
206}
207
Simon Glasse7762912023-08-24 13:55:29 -0600208static int part_get_info_mac(struct blk_desc *desc, int part,
209 struct disk_partition *info)
wdenkfe8c2802002-11-03 00:38:21 +0000210{
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200211 ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
212 ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
wdenkfe8c2802002-11-03 00:38:21 +0000213
Simon Glasse7762912023-08-24 13:55:29 -0600214 if (part_mac_read_ddb(desc, ddesc))
215 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000216
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200217 info->blksz = ddesc->blk_size;
wdenkfe8c2802002-11-03 00:38:21 +0000218
Simon Glasse7762912023-08-24 13:55:29 -0600219 if (part_mac_read_pdb(desc, part, mpart))
220 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000221
Benoît Thébaudeaubca93832012-07-13 21:31:03 +0200222 info->start = mpart->start_block;
223 info->size = mpart->block_count;
224 memcpy (info->type, mpart->type, sizeof(info->type));
225 memcpy (info->name, mpart->name, sizeof(info->name));
wdenkfe8c2802002-11-03 00:38:21 +0000226
227 return (0);
228}
229
Simon Glass83ce5632016-02-29 15:25:47 -0700230U_BOOT_PART_TYPE(mac) = {
231 .name = "MAC",
232 .part_type = PART_TYPE_MAC,
Petr Kulhavy712257e2016-09-09 10:27:15 +0200233 .max_entries = MAC_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -0700234 .get_info = part_get_info_mac,
Simon Glass0f1c9522016-02-29 15:26:04 -0700235 .print = part_print_mac,
236 .test = part_test_mac,
Simon Glass83ce5632016-02-29 15:25:47 -0700237};