part_mac.c 5.4 KB

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