part_mac.c 5.4 KB

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