part_mac.c 5.6 KB

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