part_mac.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Support for harddisk partitions.
  8. *
  9. * To be compatible with LinuxPPC and Apple we use the standard Apple
  10. * SCSI disk partitioning scheme. For more information see:
  11. * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <memalign.h>
  16. #include <ide.h>
  17. #include "part_mac.h"
  18. #ifdef CONFIG_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(struct blk_desc *dev_desc,
  29. mac_driver_desc_t *ddb_p);
  30. static int part_mac_read_pdb(struct blk_desc *dev_desc, int part,
  31. mac_partition_t *pdb_p);
  32. /*
  33. * Test for a valid MAC partition
  34. */
  35. static int part_test_mac(struct blk_desc *dev_desc)
  36. {
  37. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  38. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  39. ulong i, n;
  40. if (part_mac_read_ddb (dev_desc, ddesc)) {
  41. /*
  42. * error reading Driver Descriptor Block,
  43. * or no valid Signature
  44. */
  45. return (-1);
  46. }
  47. n = 1; /* assuming at least one partition */
  48. for (i=1; i<=n; ++i) {
  49. if ((blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) ||
  50. (mpart->signature != MAC_PARTITION_MAGIC) ) {
  51. return (-1);
  52. }
  53. /* update partition count */
  54. n = mpart->map_count;
  55. }
  56. return (0);
  57. }
  58. static void part_print_mac(struct blk_desc *dev_desc)
  59. {
  60. ulong i, n;
  61. ALLOC_CACHE_ALIGN_BUFFER(mac_driver_desc_t, ddesc, 1);
  62. ALLOC_CACHE_ALIGN_BUFFER(mac_partition_t, mpart, 1);
  63. ldiv_t mb, gb;
  64. if (part_mac_read_ddb (dev_desc, ddesc)) {
  65. /*
  66. * error reading Driver Descriptor Block,
  67. * or no valid Signature
  68. */
  69. return;
  70. }
  71. n = ddesc->blk_count;
  72. mb = ldiv(n, ((1024 * 1024) / ddesc->blk_size)); /* MB */
  73. /* round to 1 digit */
  74. mb.rem *= 10 * ddesc->blk_size;
  75. mb.rem += 512 * 1024;
  76. mb.rem /= 1024 * 1024;
  77. gb = ldiv(10 * mb.quot + mb.rem, 10240);
  78. gb.rem += 512;
  79. gb.rem /= 1024;
  80. printf ("Block Size=%d, Number of Blocks=%d, "
  81. "Total Capacity: %ld.%ld MB = %ld.%ld GB\n"
  82. "DeviceType=0x%x, DeviceId=0x%x\n\n"
  83. " #: type name"
  84. " length base (size)\n",
  85. ddesc->blk_size,
  86. ddesc->blk_count,
  87. mb.quot, mb.rem, gb.quot, gb.rem,
  88. ddesc->dev_type, ddesc->dev_id
  89. );
  90. n = 1; /* assuming at least one partition */
  91. for (i=1; i<=n; ++i) {
  92. ulong bytes;
  93. char c;
  94. printf ("%4ld: ", i);
  95. if (blk_dread(dev_desc, i, 1, (ulong *)mpart) != 1) {
  96. printf ("** Can't read Partition Map on %d:%ld **\n",
  97. dev_desc->devnum, i);
  98. return;
  99. }
  100. if (mpart->signature != MAC_PARTITION_MAGIC) {
  101. printf("** Bad Signature on %d:%ld - expected 0x%04x, got 0x%04x\n",
  102. dev_desc->devnum, i, MAC_PARTITION_MAGIC,
  103. mpart->signature);
  104. return;
  105. }
  106. /* update partition count */
  107. n = mpart->map_count;
  108. c = 'k';
  109. bytes = mpart->block_count;
  110. bytes /= (1024 / ddesc->blk_size); /* kB; assumes blk_size == 512 */
  111. if (bytes >= 1024) {
  112. bytes >>= 10;
  113. c = 'M';
  114. }
  115. if (bytes >= 1024) {
  116. bytes >>= 10;
  117. c = 'G';
  118. }
  119. printf ("%20.32s %-18.32s %10u @ %-10u (%3ld%c)\n",
  120. mpart->type,
  121. mpart->name,
  122. mpart->block_count,
  123. mpart->start_block,
  124. bytes, c
  125. );
  126. }
  127. return;
  128. }
  129. /*
  130. * Read Device Descriptor Block
  131. */
  132. static int part_mac_read_ddb(struct blk_desc *dev_desc,
  133. mac_driver_desc_t *ddb_p)
  134. {
  135. if (blk_dread(dev_desc, 0, 1, (ulong *)ddb_p) != 1) {
  136. debug("** Can't read Driver Descriptor Block **\n");
  137. return (-1);
  138. }
  139. if (ddb_p->signature != MAC_DRIVER_MAGIC) {
  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 (blk_dread(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. .max_entries = MAC_ENTRY_NUMBERS,
  204. .get_info = part_get_info_mac,
  205. .print = part_print_mac,
  206. .test = part_test_mac,
  207. };
  208. #endif