part_mac.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Support for harddisk partitions.
  25. *
  26. * To be compatible with LinuxPPC and Apple we use the standard Apple
  27. * SCSI disk partitioning scheme. For more information see:
  28. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  29. */
  30. #include <common.h>
  31. #include <command.h>
  32. #include <ide.h>
  33. #include "part_mac.h"
  34. #ifdef HAVE_BLOCK_DEVICE
  35. /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  36. #ifndef __ldiv_t_defined
  37. typedef struct {
  38. long int quot; /* Quotient */
  39. long int rem; /* Remainder */
  40. } ldiv_t;
  41. extern ldiv_t ldiv (long int __numer, long int __denom);
  42. # define __ldiv_t_defined 1
  43. #endif
  44. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
  45. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
  46. /*
  47. * Test for a valid MAC partition
  48. */
  49. int test_part_mac (block_dev_desc_t *dev_desc)
  50. {
  51. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  52. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  53. ulong i, n;
  54. if (part_mac_read_ddb (dev_desc, ddesc)) {
  55. /* error reading Driver Desriptor Block, or no valid Signature */
  56. return (-1);
  57. }
  58. n = 1; /* assuming at least one partition */
  59. for (i=1; i<=n; ++i) {
  60. if ((dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)mpart) != 1) ||
  61. (mpart->signature != MAC_PARTITION_MAGIC) ) {
  62. return (-1);
  63. }
  64. /* update partition count */
  65. n = mpart->map_count;
  66. }
  67. return (0);
  68. }
  69. void print_part_mac (block_dev_desc_t *dev_desc)
  70. {
  71. ulong i, n;
  72. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  73. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  74. ldiv_t mb, gb;
  75. if (part_mac_read_ddb (dev_desc, ddesc)) {
  76. /* error reading Driver Desriptor Block, or no valid Signature */
  77. return;
  78. }
  79. n = ddesc->blk_count;
  80. mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
  81. /* round to 1 digit */
  82. mb.rem *= 10 * ddesc->blk_size;
  83. mb.rem += 512 * 1024;
  84. mb.rem /= 1024 * 1024;
  85. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  86. gb.rem += 512;
  87. gb.rem /= 1024;
  88. printf ("Block Size=%d, Number of Blocks=%d, "
  89. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  90. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  91. " #: type name"
  92. " length base (size)\n",
  93. ddesc->blk_size,
  94. ddesc->blk_count,
  95. mb.quot, mb.rem, gb.quot, gb.rem,
  96. ddesc->dev_type, ddesc->dev_id
  97. );
  98. n = 1; /* assuming at least one partition */
  99. for (i=1; i<=n; ++i) {
  100. ulong bytes;
  101. char c;
  102. printf ("%4ld: ", i);
  103. if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *)mpart) != 1) {
  104. printf ("** Can't read Partition Map on %d:%ld **\n",
  105. dev_desc->dev, i);
  106. return;
  107. }
  108. if (mpart->signature != MAC_PARTITION_MAGIC) {
  109. printf ("** Bad Signature on %d:%ld - "
  110. "expected 0x%04x, got 0x%04x\n",
  111. dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart->signature);
  112. return;
  113. }
  114. /* update partition count */
  115. n = mpart->map_count;
  116. c = 'k';
  117. bytes = mpart->block_count;
  118. bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
  119. if (bytes >= 1024) {
  120. bytes >>= 10;
  121. c = 'M';
  122. }
  123. if (bytes >= 1024) {
  124. bytes >>= 10;
  125. c = 'G';
  126. }
  127. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  128. mpart->type,
  129. mpart->name,
  130. mpart->block_count,
  131. mpart->start_block,
  132. bytes, c
  133. );
  134. }
  135. return;
  136. }
  137. /*
  138. * Read Device Descriptor Block
  139. */
  140. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
  141. {
  142. if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)ddb_p) != 1) {
  143. printf ("** Can't read Driver Desriptor Block **\n");
  144. return (-1);
  145. }
  146. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  147. #if 0
  148. printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
  149. MAC_DRIVER_MAGIC, ddb_p->signature);
  150. #endif
  151. return (-1);
  152. }
  153. return (0);
  154. }
  155. /*
  156. * Read Partition Descriptor Block
  157. */
  158. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
  159. {
  160. int n = 1;
  161. for (;;) {
  162. /*
  163. * We must always read the descritpor block for
  164. * partition 1 first since this is the only way to
  165. * know how many partitions we have.
  166. */
  167. if (dev_desc->block_read (dev_desc->dev, n, 1, (ulong *)pdb_p) != 1) {
  168. printf ("** Can't read Partition Map on %d:%d **\n",
  169. dev_desc->dev, n);
  170. return (-1);
  171. }
  172. if (pdb_p->signature != MAC_PARTITION_MAGIC) {
  173. printf ("** Bad Signature on %d:%d: "
  174. "expected 0x%04x, got 0x%04x\n",
  175. dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
  176. return (-1);
  177. }
  178. if (n == part)
  179. return (0);
  180. if ((part < 1) || (part > pdb_p->map_count)) {
  181. printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
  182. dev_desc->dev, part,
  183. dev_desc->dev,
  184. dev_desc->dev, pdb_p->map_count);
  185. return (-1);
  186. }
  187. /* update partition count */
  188. n = part;
  189. }
  190. /* NOTREACHED */
  191. }
  192. int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
  193. {
  194. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  195. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  196. if (part_mac_read_ddb (dev_desc, ddesc)) {
  197. return (-1);
  198. }
  199. info->blksz = ddesc->blk_size;
  200. if (part_mac_read_pdb (dev_desc, part, mpart)) {
  201. return (-1);
  202. }
  203. info->start = mpart->start_block;
  204. info->size = mpart->block_count;
  205. memcpy (info->type, mpart->type, sizeof(info->type));
  206. memcpy (info->name, mpart->name, sizeof(info->name));
  207. return (0);
  208. }
  209. #endif