part_mac.c 5.6 KB

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