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