part_mac.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  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. #include <common.h>
  15. #include <command.h>
  16. #include <memalign.h>
  17. #include <ide.h>
  18. #include "part_mac.h"
  19. #ifdef HAVE_BLOCK_DEVICE
  20. /* stdlib.h causes some compatibility problems; should fixe these! -- wd */
  21. #ifndef __ldiv_t_defined
  22. typedef struct {
  23. long int quot; /* Quotient */
  24. long int rem; /* Remainder */
  25. } ldiv_t;
  26. extern ldiv_t ldiv (long int __numer, long int __denom);
  27. # define __ldiv_t_defined 1
  28. #endif
  29. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p);
  30. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p);
  31. /*
  32. * Test for a valid MAC partition
  33. */
  34. int test_part_mac (block_dev_desc_t *dev_desc)
  35. {
  36. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  37. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  38. ulong i, n;
  39. if (part_mac_read_ddb (dev_desc, ddesc)) {
  40. /* error reading Driver Desriptor Block, or no valid Signature */
  41. return (-1);
  42. }
  43. n = 1; /* assuming at least one partition */
  44. for (i=1; i<=n; ++i) {
  45. if ((dev_desc->block_read(dev_desc, i, 1,
  46. (ulong *)mpart) != 1) ||
  47. (mpart->signature != MAC_PARTITION_MAGIC) ) {
  48. return (-1);
  49. }
  50. /* update partition count */
  51. n = mpart->map_count;
  52. }
  53. return (0);
  54. }
  55. void print_part_mac (block_dev_desc_t *dev_desc)
  56. {
  57. ulong i, n;
  58. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  59. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  60. ldiv_t mb, gb;
  61. if (part_mac_read_ddb (dev_desc, ddesc)) {
  62. /* error reading Driver Desriptor Block, or no valid Signature */
  63. return;
  64. }
  65. n = ddesc->blk_count;
  66. mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
  67. /* round to 1 digit */
  68. mb.rem *= 10 * ddesc->blk_size;
  69. mb.rem += 512 * 1024;
  70. mb.rem /= 1024 * 1024;
  71. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  72. gb.rem += 512;
  73. gb.rem /= 1024;
  74. printf ("Block Size=%d, Number of Blocks=%d, "
  75. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  76. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  77. " #: type name"
  78. " length base (size)\n",
  79. ddesc->blk_size,
  80. ddesc->blk_count,
  81. mb.quot, mb.rem, gb.quot, gb.rem,
  82. ddesc->dev_type, ddesc->dev_id
  83. );
  84. n = 1; /* assuming at least one partition */
  85. for (i=1; i<=n; ++i) {
  86. ulong bytes;
  87. char c;
  88. printf ("%4ld: ", i);
  89. if (dev_desc->block_read(dev_desc, i, 1, (ulong *)mpart) != 1) {
  90. printf ("** Can't read Partition Map on %d:%ld **\n",
  91. dev_desc->dev, i);
  92. return;
  93. }
  94. if (mpart->signature != MAC_PARTITION_MAGIC) {
  95. printf ("** Bad Signature on %d:%ld - "
  96. "expected 0x%04x, got 0x%04x\n",
  97. dev_desc->dev, i, MAC_PARTITION_MAGIC, mpart->signature);
  98. return;
  99. }
  100. /* update partition count */
  101. n = mpart->map_count;
  102. c = 'k';
  103. bytes = mpart->block_count;
  104. bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
  105. if (bytes >= 1024) {
  106. bytes >>= 10;
  107. c = 'M';
  108. }
  109. if (bytes >= 1024) {
  110. bytes >>= 10;
  111. c = 'G';
  112. }
  113. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  114. mpart->type,
  115. mpart->name,
  116. mpart->block_count,
  117. mpart->start_block,
  118. bytes, c
  119. );
  120. }
  121. return;
  122. }
  123. /*
  124. * Read Device Descriptor Block
  125. */
  126. static int part_mac_read_ddb (block_dev_desc_t *dev_desc, mac_driver_desc_t *ddb_p)
  127. {
  128. if (dev_desc->block_read(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
  129. printf ("** Can't read Driver Desriptor Block **\n");
  130. return (-1);
  131. }
  132. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  133. #if 0
  134. printf ("** Bad Signature: expected 0x%04x, got 0x%04x\n",
  135. MAC_DRIVER_MAGIC, ddb_p->signature);
  136. #endif
  137. return (-1);
  138. }
  139. return (0);
  140. }
  141. /*
  142. * Read Partition Descriptor Block
  143. */
  144. static int part_mac_read_pdb (block_dev_desc_t *dev_desc, int part, mac_partition_t *pdb_p)
  145. {
  146. int n = 1;
  147. for (;;) {
  148. /*
  149. * We must always read the descritpor block for
  150. * partition 1 first since this is the only way to
  151. * know how many partitions we have.
  152. */
  153. if (dev_desc->block_read(dev_desc, n, 1, (ulong *)pdb_p) != 1) {
  154. printf ("** Can't read Partition Map on %d:%d **\n",
  155. dev_desc->dev, n);
  156. return (-1);
  157. }
  158. if (pdb_p->signature != MAC_PARTITION_MAGIC) {
  159. printf ("** Bad Signature on %d:%d: "
  160. "expected 0x%04x, got 0x%04x\n",
  161. dev_desc->dev, n, MAC_PARTITION_MAGIC, pdb_p->signature);
  162. return (-1);
  163. }
  164. if (n == part)
  165. return (0);
  166. if ((part < 1) || (part > pdb_p->map_count)) {
  167. printf ("** Invalid partition %d:%d [%d:1...%d:%d only]\n",
  168. dev_desc->dev, part,
  169. dev_desc->dev,
  170. dev_desc->dev, pdb_p->map_count);
  171. return (-1);
  172. }
  173. /* update partition count */
  174. n = part;
  175. }
  176. /* NOTREACHED */
  177. }
  178. int get_partition_info_mac (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
  179. {
  180. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  181. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  182. if (part_mac_read_ddb (dev_desc, ddesc)) {
  183. return (-1);
  184. }
  185. info->blksz = ddesc->blk_size;
  186. if (part_mac_read_pdb (dev_desc, part, mpart)) {
  187. return (-1);
  188. }
  189. info->start = mpart->start_block;
  190. info->size = mpart->block_count;
  191. memcpy (info->type, mpart->type, sizeof(info->type));
  192. memcpy (info->name, mpart->name, sizeof(info->name));
  193. return (0);
  194. }
  195. #endif