Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 5 | */ |
| 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 15 | #include <command.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 17 | #include <memalign.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 18 | #include <ide.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 19 | #include "part_mac.h" |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 20 | #include <part.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 21 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 22 | /* stdlib.h causes some compatibility problems; should fixe these! -- wd */ |
| 23 | #ifndef __ldiv_t_defined |
| 24 | typedef struct { |
| 25 | long int quot; /* Quotient */ |
| 26 | long int rem; /* Remainder */ |
| 27 | } ldiv_t; |
| 28 | extern ldiv_t ldiv (long int __numer, long int __denom); |
| 29 | # define __ldiv_t_defined 1 |
| 30 | #endif |
| 31 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 32 | static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p); |
| 33 | static int part_mac_read_pdb(struct blk_desc *desc, int part, |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 34 | mac_partition_t *pdb_p); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Test for a valid MAC partition |
| 38 | */ |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 39 | static int part_test_mac(struct blk_desc *desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 40 | { |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 41 | ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1); |
| 42 | ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 43 | ulong i, n; |
| 44 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 45 | if (part_mac_read_ddb(desc, ddesc)) { |
Heinrich Schuchardt | 5eae466 | 2017-08-29 18:36:59 +0200 | [diff] [blame] | 46 | /* |
| 47 | * error reading Driver Descriptor Block, |
| 48 | * or no valid Signature |
| 49 | */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 50 | return (-1); |
| 51 | } |
| 52 | |
| 53 | n = 1; /* assuming at least one partition */ |
| 54 | for (i=1; i<=n; ++i) { |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 55 | if ((blk_dread(desc, i, 1, (ulong *)mpart) != 1) || |
| 56 | mpart->signature != MAC_PARTITION_MAGIC) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 57 | return (-1); |
| 58 | } |
| 59 | /* update partition count */ |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 60 | n = mpart->map_count; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 61 | } |
| 62 | return (0); |
| 63 | } |
| 64 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 65 | static void part_print_mac(struct blk_desc *desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 66 | { |
| 67 | ulong i, n; |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 68 | ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1); |
| 69 | ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 70 | ldiv_t mb, gb; |
| 71 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 72 | if (part_mac_read_ddb(desc, ddesc)) { |
Heinrich Schuchardt | 5eae466 | 2017-08-29 18:36:59 +0200 | [diff] [blame] | 73 | /* |
| 74 | * error reading Driver Descriptor Block, |
| 75 | * or no valid Signature |
| 76 | */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 80 | n = ddesc->blk_count; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 81 | |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 82 | mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 83 | /* round to 1 digit */ |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 84 | mb.rem *= 10 * ddesc->blk_size; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 85 | mb.rem += 512 * 1024; |
| 86 | mb.rem /= 1024 * 1024; |
| 87 | |
| 88 | gb = ldiv(10 * mb.quot + mb.rem, 10240); |
| 89 | gb.rem += 512; |
| 90 | gb.rem /= 1024; |
| 91 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 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", |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 97 | ddesc->blk_size, |
| 98 | ddesc->blk_count, |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 99 | mb.quot, mb.rem, gb.quot, gb.rem, |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 100 | ddesc->dev_type, ddesc->dev_id |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 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); |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 109 | if (blk_dread(desc, i, 1, (ulong *)mpart) != 1) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 110 | printf ("** Can't read Partition Map on %d:%ld **\n", |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 111 | desc->devnum, i); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 115 | if (mpart->signature != MAC_PARTITION_MAGIC) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 116 | printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n", |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 117 | desc->devnum, i, MAC_PARTITION_MAGIC, |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 118 | mpart->signature); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | /* update partition count */ |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 123 | n = mpart->map_count; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 124 | |
| 125 | c = 'k'; |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 126 | bytes = mpart->block_count; |
| 127 | bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 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", |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 138 | mpart->type, |
| 139 | mpart->name, |
| 140 | mpart->block_count, |
| 141 | mpart->start_block, |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 142 | bytes, c |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | return; |
| 147 | } |
| 148 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Read Device Descriptor Block |
| 151 | */ |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 152 | static int part_mac_read_ddb(struct blk_desc *desc, mac_driver_desc_t *ddb_p) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 153 | { |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 154 | if (blk_dread(desc, 0, 1, (ulong *)ddb_p) != 1) { |
Bin Meng | 337e3b8 | 2017-09-03 18:44:23 -0700 | [diff] [blame] | 155 | debug("** Can't read Driver Descriptor Block **\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 156 | return (-1); |
| 157 | } |
| 158 | |
| 159 | if (ddb_p->signature != MAC_DRIVER_MAGIC) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 160 | return (-1); |
| 161 | } |
| 162 | return (0); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Read Partition Descriptor Block |
| 167 | */ |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 168 | static int part_mac_read_pdb(struct blk_desc *desc, int part, |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 169 | mac_partition_t *pdb_p) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 170 | { |
| 171 | int n = 1; |
| 172 | |
| 173 | for (;;) { |
| 174 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | * We must always read the descritpor block for |
| 176 | * partition 1 first since this is the only way to |
| 177 | * know how many partitions we have. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 178 | */ |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 179 | if (blk_dread(desc, n, 1, (ulong *)pdb_p) != 1) { |
| 180 | printf("** Can't read Partition Map on %d:%d **\n", |
| 181 | desc->devnum, n); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 182 | return (-1); |
| 183 | } |
| 184 | |
| 185 | if (pdb_p->signature != MAC_PARTITION_MAGIC) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 186 | printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x\n", |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 187 | desc->devnum, n, MAC_PARTITION_MAGIC, |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 188 | pdb_p->signature); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 189 | return (-1); |
| 190 | } |
| 191 | |
| 192 | if (n == part) |
| 193 | return (0); |
| 194 | |
| 195 | if ((part < 1) || (part > pdb_p->map_count)) { |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 196 | printf("** Invalid partition %d:%d [%d:1...%d:%d only]\n", |
| 197 | desc->devnum, part, desc->devnum, desc->devnum, |
| 198 | pdb_p->map_count); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 199 | return (-1); |
| 200 | } |
| 201 | |
| 202 | /* update partition count */ |
| 203 | n = part; |
| 204 | } |
| 205 | |
| 206 | /* NOTREACHED */ |
| 207 | } |
| 208 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 209 | static int part_get_info_mac(struct blk_desc *desc, int part, |
| 210 | struct disk_partition *info) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 211 | { |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 212 | ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1); |
| 213 | ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 214 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 215 | if (part_mac_read_ddb(desc, ddesc)) |
| 216 | return -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 217 | |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 218 | info->blksz = ddesc->blk_size; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 219 | |
Simon Glass | 060148a | 2023-08-24 13:55:29 -0600 | [diff] [blame] | 220 | if (part_mac_read_pdb(desc, part, mpart)) |
| 221 | return -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 222 | |
Benoît Thébaudeau | 64a08a9 | 2012-07-13 21:31:03 +0200 | [diff] [blame] | 223 | info->start = mpart->start_block; |
| 224 | info->size = mpart->block_count; |
| 225 | memcpy (info->type, mpart->type, sizeof(info->type)); |
| 226 | memcpy (info->name, mpart->name, sizeof(info->name)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 227 | |
| 228 | return (0); |
| 229 | } |
| 230 | |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 231 | U_BOOT_PART_TYPE(mac) = { |
| 232 | .name = "MAC", |
| 233 | .part_type = PART_TYPE_MAC, |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 234 | .max_entries = MAC_ENTRY_NUMBERS, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 235 | .get_info = part_get_info_mac, |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 236 | .print = part_print_mac, |
| 237 | .test = part_test_mac, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 238 | }; |