Blame view

disk/part_mac.c 5.52 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
fe8c2806c   wdenk   Initial revision
2
3
4
  /*
   * (C) Copyright 2000
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
fe8c2806c   wdenk   Initial revision
5
6
7
8
9
10
11
12
13
14
15
16
   */
  
  /*
   * Support for harddisk partitions.
   *
   * To be compatible with LinuxPPC and Apple we use the standard Apple
   * SCSI disk partitioning scheme. For more information see:
   * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
   */
  
  #include <common.h>
  #include <command.h>
cf92e05c0   Simon Glass   Move ALLOC_CACHE_...
17
  #include <memalign.h>
fe8c2806c   wdenk   Initial revision
18
  #include <ide.h>
fe8c2806c   wdenk   Initial revision
19
  #include "part_mac.h"
1811a928c   Adam Ford   Move most CONFIG_...
20
  #ifdef CONFIG_HAVE_BLOCK_DEVICE
fe8c2806c   wdenk   Initial revision
21
22
23
24
25
26
27
28
29
30
  
  /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  #ifndef __ldiv_t_defined
  typedef struct {
  	long int quot;		/* Quotient	*/
  	long int rem;		/* Remainder	*/
  } ldiv_t;
  extern ldiv_t ldiv (long int __numer, long int __denom);
  # define __ldiv_t_defined	1
  #endif
4101f6879   Simon Glass   dm: Drop the bloc...
31
32
33
34
  static int part_mac_read_ddb(struct blk_desc *dev_desc,
  			     mac_driver_desc_t *ddb_p);
  static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
  			     mac_partition_t *pdb_p);
fe8c2806c   wdenk   Initial revision
35
36
37
38
  
  /*
   * Test for a valid MAC partition
   */
084bf4c24   Simon Glass   part: Rename test...
39
  static int part_test_mac(struct blk_desc *dev_desc)
fe8c2806c   wdenk   Initial revision
40
  {
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
41
42
  	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
fe8c2806c   wdenk   Initial revision
43
  	ulong i, n;
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
44
  	if (part_mac_read_ddb (dev_desc, ddesc)) {
5eae466ea   Heinrich Schuchardt   disk: part: fix typo
45
46
47
48
  		/*
  		 * error reading Driver Descriptor Block,
  		 * or no valid Signature
  		 */
fe8c2806c   wdenk   Initial revision
49
50
51
52
53
  		return (-1);
  	}
  
  	n = 1;	/* assuming at least one partition */
  	for (i=1; i<=n; ++i) {
2a981dc2c   Simon Glass   dm: block: Adjust...
54
  		if ((blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) ||
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
55
  		    (mpart->signature != MAC_PARTITION_MAGIC) ) {
fe8c2806c   wdenk   Initial revision
56
57
58
  			return (-1);
  		}
  		/* update partition count */
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
59
  		n = mpart->map_count;
fe8c2806c   wdenk   Initial revision
60
61
62
  	}
  	return (0);
  }
084bf4c24   Simon Glass   part: Rename test...
63
  static void part_print_mac(struct blk_desc *dev_desc)
fe8c2806c   wdenk   Initial revision
64
65
  {
  	ulong i, n;
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
66
67
  	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
fe8c2806c   wdenk   Initial revision
68
  	ldiv_t mb, gb;
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
69
  	if (part_mac_read_ddb (dev_desc, ddesc)) {
5eae466ea   Heinrich Schuchardt   disk: part: fix typo
70
71
72
73
  		/*
  		 * error reading Driver Descriptor Block,
  		 * or no valid Signature
  		 */
fe8c2806c   wdenk   Initial revision
74
75
  		return;
  	}
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
76
  	n  = ddesc->blk_count;
fe8c2806c   wdenk   Initial revision
77

64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
78
  	mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
fe8c2806c   wdenk   Initial revision
79
  	/* round to 1 digit */
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
80
  	mb.rem *= 10 * ddesc->blk_size;
fe8c2806c   wdenk   Initial revision
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  	mb.rem += 512 * 1024;
  	mb.rem /= 1024 * 1024;
  
  	gb = ldiv(10 * mb.quot + mb.rem, 10240);
  	gb.rem += 512;
  	gb.rem /= 1024;
  
  
  	printf ("Block Size=%d, Number of Blocks=%d, "
  		"Total Capacity: %ld.%ld MB = %ld.%ld GB
  "
  		"DeviceType=0x%x, DeviceId=0x%x
  
  "
  		"   #:                 type name"
  		"                   length   base       (size)
  ",
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
98
99
  		ddesc->blk_size,
  		ddesc->blk_count,
fe8c2806c   wdenk   Initial revision
100
  		mb.quot, mb.rem, gb.quot, gb.rem,
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
101
  		ddesc->dev_type, ddesc->dev_id
fe8c2806c   wdenk   Initial revision
102
103
104
105
106
107
108
109
  		);
  
  	n = 1;	/* assuming at least one partition */
  	for (i=1; i<=n; ++i) {
  		ulong bytes;
  		char c;
  
  		printf ("%4ld: ", i);
2a981dc2c   Simon Glass   dm: block: Adjust...
110
  		if (blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) {
fe8c2806c   wdenk   Initial revision
111
112
  			printf ("** Can't read Partition Map on %d:%ld **
  ",
bcce53d04   Simon Glass   dm: block: Rename...
113
  				dev_desc->devnum, i);
fe8c2806c   wdenk   Initial revision
114
115
  			return;
  		}
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
116
  		if (mpart->signature != MAC_PARTITION_MAGIC) {
bcce53d04   Simon Glass   dm: block: Rename...
117
118
119
120
  			printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x
  ",
  			       dev_desc->devnum, i, MAC_PARTITION_MAGIC,
  			       mpart->signature);
fe8c2806c   wdenk   Initial revision
121
122
123
124
  			return;
  		}
  
  		/* update partition count */
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
125
  		n = mpart->map_count;
fe8c2806c   wdenk   Initial revision
126
127
  
  		c      = 'k';
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
128
129
  		bytes  = mpart->block_count;
  		bytes /= (1024 / ddesc->blk_size);  /* kB; assumes blk_size == 512 */
fe8c2806c   wdenk   Initial revision
130
131
132
133
134
135
136
137
138
139
140
  		if (bytes >= 1024) {
  			bytes >>= 10;
  			c = 'M';
  		}
  		if (bytes >= 1024) {
  			bytes >>= 10;
  			c = 'G';
  		}
  
  		printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)
  ",
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
141
142
143
144
  			mpart->type,
  			mpart->name,
  			mpart->block_count,
  			mpart->start_block,
fe8c2806c   wdenk   Initial revision
145
146
147
148
149
150
151
152
153
154
155
  			bytes, c
  			);
  	}
  
  	return;
  }
  
  
  /*
   * Read Device Descriptor Block
   */
4101f6879   Simon Glass   dm: Drop the bloc...
156
157
  static int part_mac_read_ddb(struct blk_desc *dev_desc,
  			     mac_driver_desc_t *ddb_p)
fe8c2806c   wdenk   Initial revision
158
  {
2a981dc2c   Simon Glass   dm: block: Adjust...
159
  	if (blk_dread(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
337e3b897   Bin Meng   part: mac: Suppre...
160
161
  		debug("** Can't read Driver Descriptor Block **
  ");
fe8c2806c   wdenk   Initial revision
162
163
164
165
  		return (-1);
  	}
  
  	if (ddb_p->signature != MAC_DRIVER_MAGIC) {
fe8c2806c   wdenk   Initial revision
166
167
168
169
170
171
172
173
  		return (-1);
  	}
  	return (0);
  }
  
  /*
   * Read Partition Descriptor Block
   */
4101f6879   Simon Glass   dm: Drop the bloc...
174
175
  static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
  			     mac_partition_t *pdb_p)
fe8c2806c   wdenk   Initial revision
176
177
178
179
180
  {
  	int n = 1;
  
  	for (;;) {
  		/*
8bde7f776   wdenk   * Code cleanup:
181
182
183
  		 * We must always read the descritpor block for
  		 * partition 1 first since this is the only way to
  		 * know how many partitions we have.
fe8c2806c   wdenk   Initial revision
184
  		 */
2a981dc2c   Simon Glass   dm: block: Adjust...
185
  		if (blk_dread(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
fe8c2806c   wdenk   Initial revision
186
187
  			printf ("** Can't read Partition Map on %d:%d **
  ",
bcce53d04   Simon Glass   dm: block: Rename...
188
  				dev_desc->devnum, n);
fe8c2806c   wdenk   Initial revision
189
190
191
192
  			return (-1);
  		}
  
  		if (pdb_p->signature != MAC_PARTITION_MAGIC) {
bcce53d04   Simon Glass   dm: block: Rename...
193
194
195
196
  			printf("** Bad Signature on %d:%d: expected 0x%04x, got 0x%04x
  ",
  			       dev_desc->devnum, n, MAC_PARTITION_MAGIC,
  			       pdb_p->signature);
fe8c2806c   wdenk   Initial revision
197
198
199
200
201
202
203
204
205
  			return (-1);
  		}
  
  		if (n == part)
  			return (0);
  
  		if ((part < 1) || (part > pdb_p->map_count)) {
  			printf ("** Invalid partition %d:%d [%d:1...%d:%d only]
  ",
bcce53d04   Simon Glass   dm: block: Rename...
206
207
208
  				dev_desc->devnum, part,
  				dev_desc->devnum,
  				dev_desc->devnum, pdb_p->map_count);
fe8c2806c   wdenk   Initial revision
209
210
211
212
213
214
215
216
217
  			return (-1);
  		}
  
  		/* update partition count */
  		n = part;
  	}
  
  	/* NOTREACHED */
  }
3e8bd4695   Simon Glass   dm: part: Rename ...
218
  static int part_get_info_mac(struct blk_desc *dev_desc, int part,
96e5b03c8   Simon Glass   dm: part: Convert...
219
  				  disk_partition_t *info)
fe8c2806c   wdenk   Initial revision
220
  {
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
221
222
  	ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  	ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
fe8c2806c   wdenk   Initial revision
223

64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
224
  	if (part_mac_read_ddb (dev_desc, ddesc)) {
fe8c2806c   wdenk   Initial revision
225
226
  		return (-1);
  	}
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
227
  	info->blksz = ddesc->blk_size;
fe8c2806c   wdenk   Initial revision
228

64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
229
  	if (part_mac_read_pdb (dev_desc, part, mpart)) {
fe8c2806c   wdenk   Initial revision
230
231
  		return (-1);
  	}
64a08a9ff   Benoît Thébaudeau   part_mac: dcache:...
232
233
234
235
  	info->start = mpart->start_block;
  	info->size  = mpart->block_count;
  	memcpy (info->type, mpart->type, sizeof(info->type));
  	memcpy (info->name, mpart->name, sizeof(info->name));
fe8c2806c   wdenk   Initial revision
236
237
238
  
  	return (0);
  }
96e5b03c8   Simon Glass   dm: part: Convert...
239
240
241
  U_BOOT_PART_TYPE(mac) = {
  	.name		= "MAC",
  	.part_type	= PART_TYPE_MAC,
87b8530fe   Petr Kulhavy   disk: part: imple...
242
  	.max_entries	= MAC_ENTRY_NUMBERS,
3e8bd4695   Simon Glass   dm: part: Rename ...
243
  	.get_info	= part_get_info_mac,
084bf4c24   Simon Glass   part: Rename test...
244
245
  	.print		= part_print_mac,
  	.test		= part_test_mac,
96e5b03c8   Simon Glass   dm: part: Convert...
246
  };
cde5c64d1   Jon Loeliger   disk/: Remove obs...
247
  #endif