part_mac.c 5.6 KB

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